1# Distributed Device Management Development 2 3## Introduction 4 5The distributed service allows multiple physically separated devices to be integrated into a virtual Super Device, allowing one device to control others and sharing data among devices with distributed communication capabilities. 6 7As the entry of the distributed service, distributed device management implements unified management of trusted and untrusted devices nearby. 8 9Distributed device management provides the following functionalities: 10 11- **Discovering devices**<br> 12 Discover and report the devices that are connected to the same LAN or have Bluetooth enabled. You can filter devices based on the device type, distance, and whether the device is trusted. 13 14- **Binding a device**<br> 15 Bind an untrusted device discovered to establish a trust relationship with each other. The device authentication framework provides a variety of authentication modes, such as PIN-based pairing, tap-to-pair, and scan-to-pair, and supports diversified authentication APIs. 16 17- **Querying device information**<br> 18 Obtain local device information, brief information about all the trusted devices, and detailed information about a trusted device. 19 20- **Listening for devices**<br> 21 Listen for online/offline status of nearby devices. If a device goes online, the device is trusted and distributed operations can be performed. If a device goes offline, the distributed service is unavailable. 22 23### Working Principles 24 25When discovering a device, the application initiates a request for binding the device to establish a trust relationship. When the service ends, the service determines whether to unbind the device. 26 27### Constraints 28 29The distributed service is available only for the devices connected to the same LAN or have Bluetooth enabled. 30 31Device information is sensitive user data. Even if the devices are connected to the same LAN or have Bluetooth enabled, the application must request the ohos.permission.DISTRIBUTED_DATASYNC permission from the user before obtaining the device location. The system provides the device management capabilities for the application only after the user has granted the permission. 32 33## Requesting Permissions 34 35### Scenario 36 37To use the distributed device management capabilities, your application must have the ohos.permission.DISTRIBUTED_DATASYNC permission, which allows application data to be exchanged between devices. This permission is a user_grant permission, which means the application must apply for user authorization. 38 39Before using the distributed device management capabilities, the application must be checked for the required permission. 40 41### How to Develop 42 43The APIs used in this section are based on the stage model. 44 451. Declare the ohos.permission.DISTRIBUTED_DATASYNC permission in the **module.json5** file. 46 47 ```ts 48 { 49 "module" : { 50 "requestPermissions":[ 51 { 52 "name" : "ohos.permission.DISTRIBUTED_DATASYNC", 53 "reason": "$string:distributed_permission", 54 "usedScene": { 55 "abilities": [ 56 "MainAbility" 57 ], 58 "when": "inuse" 59 } 60 } 61 ] 62 } 63 } 64 ``` 652. Import the **common** and **abilityAccessCtrl** modules. 66 67 ```ts 68 import { common, abilityAccessCtrl } from '@kit.AbilityKit'; 69 ``` 70 713. Use **requestPermissionsFromUser** to request user authorization for the ohos.permission.DISTRIBUTED_DATASYNC permission. 72 73 ```ts 74 let context = getContext(this) as common.UIAbilityContext; 75 let atManager = abilityAccessCtrl.createAtManager(); 76 try { 77 atManager.requestPermissionsFromUser(context, ['ohos.permission.DISTRIBUTED_DATASYNC']).then((data) => { 78 console.log('data: ' + JSON.stringify(data)); 79 }).catch((err: object) => { 80 console.log('err: ' + JSON.stringify(err)); 81 }) 82 } catch (err) { 83 console.log('catch err->' + JSON.stringify(err)); 84 } 85 ``` 86 87## Discovering Devices 88 89### Scenario 90 91Discover nearby devices. 92 93### Available APIs 94 95startDiscovering(discoverParam: {[key: string]: Object;} , filterOptions?: {[key: string]: Object;} ): void; 96 97Starts to discover the devices that are in the same LAN or have Bluetooth enabled. For details, see [startDiscovering](../reference/apis-distributedservice-kit/js-apis-distributedDeviceManager.md#startdiscovering). 98 99 100### How to Develop 101 1021. Request the ohos.permission.DISTRIBUTED_DATASYNC permission for your application. 103 1042. Import the **distributedDeviceManager** module, which provides APIs for device management. 105 106 ```ts 107 import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 108 ``` 109 1103. Import the **BusinessError** module, which provides the error codes thrown by the APIs of the **distributedDeviceManager** module. 111 112 ```ts 113 import { BusinessError } from '@kit.BasicServicesKit'; 114 ``` 115 1164. Create a **DeviceManager** instance, which is the entry for calling distributed device management APIs and registering the callback for discovering devices. 117 118 ```ts 119 try { 120 let dmInstance = distributedDeviceManager.createDeviceManager('ohos.samples.jsHelloWorld'); 121 dmInstance.on('discoverSuccess', data => console.log('discoverSuccess on:' + JSON.stringify(data))); 122 dmInstance.on('discoverFailure', data => console.log('discoverFailure on:' + JSON.stringify(data))); 123 } catch(err) { 124 let e: BusinessError = err as BusinessError; 125 console.error('createDeviceManager errCode:' + e.code + ',errMessage:' + e.message); 126 } 127 ``` 128 1295. Start to discover devices. The discovery process lasts 2 minutes, and a maximum of 99 devices can be discovered. 130 131 ```ts 132 interface DiscoverParam { 133 discoverTargetType: number; 134 } 135 interface FilterOptions { 136 availableStatus: number; 137 discoverDistance: number; 138 authenticationStatus: number; 139 authorizationType: number; 140 } 141 let discoverParam: Record<string, number> = { 142 'discoverTargetType': 1 143 }; 144 let filterOptions: Record<string, number> = { 145 'availableStatus': 0 146 }; 147 try { 148 dmInstance.startDiscovering(discoverParam, filterOptions); 149 } catch (err) { 150 let e: BusinessError = err as BusinessError; 151 console.error('startDiscovering errCode:' + e.code + ',errMessage:' + e.message); 152 } 153 ``` 154 155## Binding a Device 156 157### Scenario 158 159Bind an untrusted device discovered to establish a trust relationship. 160 161### Available APIs 162 163bindTarget(deviceId: string, bindParam: {[key: string]: Object;} , callback: AsyncCallback<{deviceId: string;}>): void; 164 165Binds a device. For details, see [bindTarget](../reference/apis-distributedservice-kit/js-apis-distributedDeviceManager.md#bindtarget). 166 167### How to Develop 168 1691. Request the ohos.permission.DISTRIBUTED_DATASYNC permission for your application. 170 1712. Discover devices nearby. 172 1733. Bind an untrusted device. 174 175 ```ts 176 class Data { 177 deviceId: string = ''; 178 } 179 let deviceId = 'XXXXXXXX'; 180 let bindParam: Record<string, string | number> = { 181 'bindType': 1, 182 'targetPkgName': 'xxxx', 183 'appName': 'xxxx', 184 'appOperation': 'xxxx', 185 'customDescription': 'xxxx' 186 }; 187 try { 188 dmInstance.bindTarget(deviceId, bindParam, (err: BusinessError, data: Data) => { 189 if (err) { 190 console.error('bindTarget errCode:' + err.code + ',errMessage:' + err.message); 191 return; 192 } 193 console.info('bindTarget result:' + JSON.stringify(data)); 194 }); 195 } catch (err) { 196 let e: BusinessError = err as BusinessError; 197 console.error('bindTarget errCode:' + e.code + ',errMessage:' + e.message); 198 } 199 ``` 200 201## Querying Device Information 202 203### Scenario 204 205Obtain information about all the online and trusted devices. 206 207### Available APIs 208 209getAvailableDeviceListSync(): Array<DeviceBasicInfo>; 210 211Obtains information about all the available devices. For details, see [getAvailableDeviceListSync](../reference/apis-distributedservice-kit/js-apis-distributedDeviceManager.md#getavailabledevicelistsync). 212 213### How to Develop 214 2151. Request the ohos.permission.DISTRIBUTED_DATASYNC permission for your application. 216 2172. Discover devices nearby. 218 2193. Bind an untrust device to establish a trust relationship. 220 2214. Obtain information about all the online and trusted devices. 222 223 ```ts 224 try { 225 let deviceInfoList: Array<distributedDeviceManager.DeviceBasicInfo> = dmInstance.getAvailableDeviceListSync(); 226 } catch (err) { 227 let e: BusinessError = err as BusinessError; 228 console.error('getAvailableDeviceListSync errCode:' + e.code + ',errMessage:' + e.message); 229 } 230 ``` 231 232## Listening for Device Online/Offline Status 233 234### Scenario 235 236You can listen for the device online/offline status. The service will be notified when a device goes offline or online. 237 238### Available APIs 239 240on(type: 'deviceStateChange', callback: Callback<{ action: DeviceStateChange; device: DeviceBasicInfo; }>): void; 241 242Listens for device online/offline status. For details, see [on('deviceStateChange')](../reference/apis-distributedservice-kit/js-apis-distributedDeviceManager.md#ondevicestatechange). 243 244### How to Develop 245 2461. Request the ohos.permission.DISTRIBUTED_DATASYNC permission for your application. 247 2482. Import the **distributedDeviceManager** module, which provides APIs for device management. 249 250 ```ts 251 import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 252 ``` 253 2543. Import the **BusinessError** module, which provides the error codes thrown by the APIs of the **distributedDeviceManager** module. 255 256 ```ts 257 import { BusinessError } from '@kit.BasicServicesKit'; 258 ``` 259 2604. Create a **DeviceManager** instance. 261 262 ```ts 263 try { 264 let dmInstance = distributedDeviceManager.createDeviceManager('ohos.samples.jsHelloWorld'); 265 dmInstance.on('deviceStateChange', data => console.log('deviceStateChange on:' + JSON.stringify(data))); 266 } catch(err) { 267 let e: BusinessError = err as BusinessError; 268 console.error('createDeviceManager errCode:' + e.code + ',errMessage:' + e.message); 269 } 270 ``` 271