1# @ohos.distributedDeviceManager (设备管理)(系统接口) 2 3本模块提供分布式设备管理能力。 4 5应用可调用接口实现如下功能: 6 7- 注册和解除注册设备上下线变化监听。 8- 发现周边不可信设备。 9- 认证和取消认证设备。 10- 查询可信设备列表。 11- 查询本地设备信息,包括设备名称,设备类型和设备标识等。 12 13> **说明:** 14> 15> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.distributedDeviceManager (设备管理)](js-apis-distributedDeviceManager.md)。 16 17## 导入模块 18 19```ts 20import { distributedDeviceManager } from '@kit.DistributedServiceKit'; 21``` 22 23## StrategyForHeartbeat<sup>15+</sup> 24 25表示心跳广播策略。 26 27**系统能力**:SystemCapability.DistributedHardware.DeviceManager 28 29**系统API**: 此接口为系统接口。 30 31| 名称 | 值 | 说明 | 32| ----------- | ---- | --------------- | 33| TEMP_STOP_HEARTBEAT | 100 | 临时停止心跳广播,超时后自动恢复。 | 34| START_HEARTBEAT | 101 | 开始心跳广播。 | 35 36## DeviceManager 37 38设备管理实例,用于获取可信设备和本地设备的相关信息。在调用DeviceManager的方法前,需要先通过createDeviceManager构建一个DeviceManager实例dmInstance。 39 40### replyUiAction 41 42replyUiAction(action: number, actionResult: string): void; 43 44回复用户UI操作行为。此接口只能被devicemanager的PIN码hap使用。 45 46**需要权限**:ohos.permission.ACCESS_SERVICE_DM 47 48**系统能力**:SystemCapability.DistributedHardware.DeviceManager 49 50**系统API**: 此接口为系统接口。 51 52**参数:** 53 54 | 参数名 | 类型 | 必填 | 说明 | 55 | ------------- | --------------- | ---- | ------------------- | 56 | action | number | 是 | 用户操作动作。<br />-0:允许授权。<br />-1:取消授权。<br />-2:授权框用户操作超时。<br />-3:取消pin码框展示。<br />-4:取消pin码输入框展示。<br />-5:pin码输入框确定操作。 | 57 | actionResult | string | 是 | 表示用户操作结果,长度范围1~255字符。 | 58 59**错误码:** 60 61以下的错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 62 63| 错误码ID | 错误信息 | 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**示例:** 70 71示例中`dmInstance`的初始化请参见[创建一个设备管理实例](js-apis-distributedDeviceManager.md#distributeddevicemanagercreatedevicemanager)。 72<!--code_no_check--> 73 ```ts 74 import { BusinessError } from '@kit.BasicServicesKit'; 75 76 try { 77 /* 78 action = 0 - 允许授权 79 action = 1 - 取消授权 80 action = 2 - 授权框用户操作超时 81 action = 3 - 取消pin码框展示 82 action = 4 - 取消pin码输入框展示 83 action = 5 - pin码输入框确定操作 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 97回复UI操作结果回调。 98 99**需要权限**:ohos.permission.ACCESS_SERVICE_DM 100 101**系统能力**:SystemCapability.DistributedHardware.DeviceManager 102 103**系统API**: 此接口为系统接口。 104 105**参数:** 106 107 | 参数名 | 类型 | 必填 | 说明 | 108 | -------- | ------------------------------------ | ---- | ------------------------------ | 109 | type | string | 是 | 注册的设备管理器 UI 状态回调,以便在状态改变时通知应用,固定为replyResult。 | 110 | callback | Callback<{ param: string;}> | 是 | 指示要注册的设备管理器 UI 状态回调,返回UI状态。 | 111 112**错误码:** 113 114以下的错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 115 116| 错误码ID | 错误信息 | 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**示例:** 122 123示例中`dmInstance`的初始化请参见[创建一个设备管理实例](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 153取消回复UI操作结果回调。 154 155**需要权限**:ohos.permission.ACCESS_SERVICE_DM 156 157**系统能力**:SystemCapability.DistributedHardware.DeviceManager 158 159**系统API**: 此接口为系统接口。 160 161**参数:** 162 163 | 参数名 | 类型 | 必填 | 说明 | 164 | -------- | ------------------------------------- | ---- | ------------------------------ | 165 | type | string | 是 | 取消注册的设备管理器 UI 状态回调,固定为replyResult。 | 166 | callback | Callback<{ param: string;}> | 否 | 指示要取消注册的设备管理器 UI 状态,返回UI状态。 | 167 168**错误码:** 169 170以下的错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 171 172| 错误码ID | 错误信息 | 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**示例:** 178 179示例中`dmInstance`的初始化请参见[创建一个设备管理实例](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 196设置心跳广播策略。 197 198**需要权限**:ohos.permission.ACCESS_SERVICE_DM 199 200**系统能力**:SystemCapability.DistributedHardware.DeviceManager 201 202**系统API**: 此接口为系统接口。 203 204**参数:** 205 206 | 参数名 | 类型 | 必填 | 说明 | 207 | ------------- | --------------- | ---- | ------------------- | 208 | policy | [StrategyForHeartbeat](#strategyforheartbeat15) | 是 | 心跳广播策略。 | 209 | delayTime | number | 是 | 临时关闭心跳广播的时长,单位为:ms,取值范围1000ms到15000ms。 | 210 211**错误码:** 212 213以下的错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[设备管理错误码](errorcode-device-manager.md)。 214 215| 错误码ID | 错误信息 | 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**示例:** 223 224示例中`dmInstance`的初始化请参见[创建一个设备管理实例](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