1# @ohos.distributedDeviceManager (Device Management) (System API) 2 3The **distributedDeviceManager** module provides APIs for distributed device management. 4 5Applications can call the APIs to: 6 7- Subscribe to or unsubscribe from device state changes. 8- Discover devices nearby. 9- Authenticate or deauthenticate a device. 10- Query the trusted device list. 11- Query local device information, including the device name, type, and ID. 12 13> **NOTE** 14> 15> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. This topic describes only the system APIs provided by the module. For details about its public APIs, see [@ohos.distributedDeviceManager](js-apis-distributedDeviceManager.md). 16 17## Modules to Import 18 19```ts 20import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 21``` 22 23## StrategyForHeartbeat<sup>15+</sup> 24 25Defines the heartbeat broadcast policy. 26 27**System capability**: SystemCapability.DistributedHardware.DeviceManager 28 29**System API**: This is a system API. 30 31| Name | Value | Description | 32| ----------- | ---- | --------------- | 33| TEMP_STOP_HEARTBEAT | 100 | Stops the heartbeat broadcast temporarily, and resumes it upon timeout expiration. | 34| START_HEARTBEAT | 101 | Starts heartbeat broadcast. | 35 36## DeviceManager 37 38Provides APIs to obtain information about trusted devices and local devices. Before calling any API in **DeviceManager**, you must use **createDeviceManager** to create a **DeviceManager** instance, for example, **dmInstance**. 39 40### replyUiAction 41 42replyUiAction(action: number, actionResult: string): void; 43 44Replies to the user's UI operation. This API can be used only by the PIN HAP of the **deviceManager**. 45 46**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 47 48**System capability**: SystemCapability.DistributedHardware.DeviceManager 49 50**System API**: This is a system API. 51 52**Parameters** 53 54 | Name | Type | Mandatory | Description | 55 | ------------- | --------------- | ---- | ------------------- | 56 | action | number | Yes | User operation.<br>- 0: Grant authorization.<br>- 1. Cancel authorization.<br>- 2: Wait until the authorization dialog times out.<br>- 3: Cancel the display of the PIN box.<br>- 4: Cancel the display of the PIN input box.<br>- 5: Confirm the input in the PIN input box. | 57 | actionResult | string | Yes | User operation result. The value is a string of 1 to 255 characters.| 58 59**Error codes** 60 61For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 62 63| ID| Error Message | 64| -------- | --------------------------------------------------------------- | 65| 201 | Permission verification failed. The application does not have the permission required to call the API. | 66| 202 | Permission verification failed. A non-system application calls a system API. | 67| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter type; 3. Parameter verification failed; 4. The size of specified actionResult is greater than 255. | 68 69**Example** 70 71For details about how to initialize `dmInstance` in the example, see [Creating a DeviceManager Instance](js-apis-distributedDeviceManager.md#distributeddevicemanagercreatedevicemanager). 72<!--code_no_check--> 73 ```ts 74 import { BusinessError } from '@kit.BasicServicesKit'; 75 76 try { 77 /* 78 action = 0 - Grant the permission. 79 action = 1 - Revoke the permission. 80 action = 2 - The user operation in the permission request dialog box times out. 81 action = 3 - Cancel the display of the PIN box. 82 action = 4 - Cancel the display of the PIN input box. 83 action = 5 - Confirm the input in the PIN input box. 84 */ 85 let operation = 0; 86 dmInstance.replyUiAction(operation, 'extra'); 87 } catch (err) { 88 let e: BusinessError = err as BusinessError; 89 console.error('replyUiAction errCode:' + e.code + ',errMessage:' + e.message); 90 } 91 ``` 92 93### on('replyResult') 94 95on(type: 'replyResult', callback: Callback<{ param: string;}>): void; 96 97Subscribes to the reply to the UI operation result. 98 99**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 100 101**System capability**: SystemCapability.DistributedHardware.DeviceManager 102 103**System API**: This is a system API. 104 105**Parameters** 106 107 | Name | Type | Mandatory| Description | 108 | -------- | ------------------------------------ | ---- | ------------------------------ | 109 | type | string | Yes | Event type, which has a fixed value of **replyResult**.| 110 | callback | Callback<{ param: string;}> | Yes | Callback invoked to return the UI status change. | 111 112**Error codes** 113 114For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 115 116| ID| Error Message | 117| -------- | --------------------------------------------------------------- | 118| 202 | Permission verification failed. A non-system application calls a system API. | 119| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter type; 3. Parameter verification failed; 4. The size of specified type is greater than 255. | 120 121**Example** 122 123For details about how to initialize `dmInstance` in the example, see [Creating a DeviceManager Instance](js-apis-distributedDeviceManager.md#distributeddevicemanagercreatedevicemanager). 124<!--code_no_check--> 125 ```ts 126 import { BusinessError } from '@kit.BasicServicesKit'; 127 128 class Data { 129 param: string = ''; 130 } 131 132 interface TmpStr { 133 verifyFailed: boolean; 134 } 135 136 try { 137 dmInstance.on('replyResult', (data: Data) => { 138 console.log('replyResult executed, dialog closed' + JSON.stringify(data)); 139 let tmpStr: TmpStr = JSON.parse(data.param); 140 let isShow = tmpStr.verifyFailed; 141 console.log('replyResult executed, dialog closed' + isShow); 142 }); 143 } catch (err) { 144 let e: BusinessError = err as BusinessError; 145 console.error('replyResult errCode:' + e.code + ',errMessage:' + e.message); 146 } 147 ``` 148 149### off('replyResult') 150 151off(type: 'replyResult', callback?: Callback<{ param: string;}>): void; 152 153Unsubscribes from the reply to the UI operation result. 154 155**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 156 157**System capability**: SystemCapability.DistributedHardware.DeviceManager 158 159**System API**: This is a system API. 160 161**Parameters** 162 163 | Name | Type | Mandatory| Description | 164 | -------- | ------------------------------------- | ---- | ------------------------------ | 165 | type | string | Yes | Event type, which has a fixed value of **replyResult**.| 166 | callback | Callback<{ param: string;}> | No | Callback to unregister.| 167 168**Error codes** 169 170For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 171 172| ID| Error Message | 173| -------- | --------------------------------------------------------------- | 174| 202 | Permission verification failed. A non-system application calls a system API. | 175| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter type; 3. Parameter verification failed; 4. The size of specified type is greater than 255. | 176 177**Example** 178 179For details about how to initialize `dmInstance` in the example, see [Creating a DeviceManager Instance](js-apis-distributedDeviceManager.md#distributeddevicemanagercreatedevicemanager). 180<!--code_no_check--> 181 ```ts 182 import { BusinessError } from '@kit.BasicServicesKit'; 183 184 try { 185 dmInstance.off('replyResult'); 186 } catch (err) { 187 let e: BusinessError = err as BusinessError; 188 console.error('replyResult errCode:' + e.code + ',errMessage:' + e.message); 189 } 190 ``` 191 192### setHeartbeatPolicy<sup>15+</sup> 193 194setHeartbeatPolicy(policy: StrategyForHeartbeat, delayTime: number): void; 195 196Sets the heartbeat broadcast policy. 197 198**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 199 200**System capability**: SystemCapability.DistributedHardware.DeviceManager 201 202**System API**: This is a system API. 203 204**Parameters** 205 206 | Name | Type | Mandatory | Description | 207 | ------------- | --------------- | ---- | ------------------- | 208 | policy | [StrategyForHeartbeat](#strategyforheartbeat15) | Yes | Heartbeat broadcast policy. | 209 | delayTime | number | Yes | Duration for temporarily disabling heartbeat broadcast. The value ranges from 1000 to 15000, in milliseconds. | 210 211**Error codes** 212 213For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Device Management Error Codes](errorcode-device-manager.md). 214 215| ID| Error Message | 216| -------- | --------------------------------------------------------------- | 217| 201 | Permission verification failed. The application does not have the permission required to call the API. | 218| 202 | Permission verification failed. A non-system application calls a system API. | 219| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 220| 11600102 | Failed to obtain service. | 221 222**Example** 223 224For details about how to initialize `dmInstance` in the example, see [Creating a DeviceManager Instance](js-apis-distributedDeviceManager.md#distributeddevicemanagercreatedevicemanager). 225<!--code_no_check--> 226 ```ts 227 import { BusinessError } from '@kit.BasicServicesKit'; 228 229 try { 230 let policy = distributedDeviceManager.StrategyForHeartbeat.TEMP_STOP_HEARTBEAT; 231 let delayTime = 1000; 232 dmInstance.setHeartbeatPolicy(policy, delayTime); 233 } catch (err) { 234 let e: BusinessError = err as BusinessError; 235 console.error('setHeartbeatPolicy errCode:' + e.code + ',errMessage:' + e.message); 236 } 237 ``` 238