js-apis-device-manager.md 44.3 KB
Newer Older
S
summer8999 已提交
1 2 3



Z
zengyawen 已提交
4
# 设备管理
Z
zengyawen 已提交
5

6 7 8 9 10 11 12 13 14
本模块提供分布式设备管理能力。

系统应用可调用接口实现如下功能:

- 注册和解除注册设备上下线变化监听
- 发现周边不可信设备
- 认证和取消认证设备
- 查询可信设备列表
- 查询本地设备信息,包括设备名称,设备类型和设备标识
S
summer8999 已提交
15
- 发布设备发现
S
summer8999 已提交
16

17 18
> **说明:**
>
19 20
> - 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
> - 本模块接口为系统接口,三方应用不支持调用。
Z
zengyawen 已提交
21

Z
zengyawen 已提交
22 23

## 导入模块
Z
zengyawen 已提交
24

25
```js
Z
zengyawen 已提交
26 27 28 29
import deviceManager from '@ohos.distributedHardware.deviceManager';
```


Z
zengyawen 已提交
30 31 32
## deviceManager.createDeviceManager

createDeviceManager(bundleName: string, callback: AsyncCallback<DeviceManager>): void
Z
zengyawen 已提交
33 34

创建一个设备管理器实例。
35 36 37

**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
38
**参数:**
@
@shi-xiaoxiao-iris 已提交
39

H
HelloCrease 已提交
40 41 42 43
  | 参数名        | 类型                                       | 必填   | 说明                                   |
  | ---------- | ---------------------------------------- | ---- | ------------------------------------ |
  | bundleName | string                                   | 是    | 指示应用程序的包名。                           |
  | callback   | AsyncCallback<[DeviceManager](#devicemanager)> | 是    | DeviceManager实例创建时调用的回调,返回设备管理器对象实例。 |
Z
zengyawen 已提交
44

@
@shi-xiaoxiao-iris 已提交
45
**错误码:**
@
@shi-xiaoxiao-iris 已提交
46

@
@shi-xiaoxiao-iris 已提交
47
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
@
@shi-xiaoxiao-iris 已提交
48

@
@shi-xiaoxiao-iris 已提交
49
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
50 51 52
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
| 11600102 | Failed to obtain the service.                                   |
@
@shi-xiaoxiao-iris 已提交
53

@
@shi-xiaoxiao-iris 已提交
54
**示例:**
@
@shi-xiaoxiao-iris 已提交
55

56 57 58
  ```js
  try {
    deviceManager.createDeviceManager("ohos.samples.jshelloworld", (err, data) => {
Z
zengyawen 已提交
59
      if (err) { 
60
        console.error("createDeviceManager errCode:" + err.code + ",errMessage:" + err.message);
61
        return;
Z
zengyawen 已提交
62 63
      }
      console.info("createDeviceManager success");
H
HelloCrease 已提交
64
      let dmInstance = data;
65 66
    });
  } catch(err) {
67
    console.error("createDeviceManager errCode:" + err.code + ",errMessage:" + err.message);
68
  }
Z
zengyawen 已提交
69 70
  ```

71
## DeviceInfo
Z
zengyawen 已提交
72

73
设备信息。
Z
zengyawen 已提交
74

75 76
**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager

H
HelloCrease 已提交
77 78 79 80 81 82
| 名称                     | 类型                        | 必填   | 描述       |
| ---------------------- | ------------------------- | ---- | -------- |
| deviceId               | string                    | 是    | 设备的唯一标识。 |
| deviceName             | string                    | 是    | 设备名称。    |
| deviceType             | [DeviceType](#devicetype) | 是    | 设备类型。    |
| networkId<sup>8+</sup> | string                    | 是    | 设备网络标识。  |
S
summer8999 已提交
83
| range<sup>9+</sup>     | number                    | 是    | 发现设备的距离。  |
Z
zengyawen 已提交
84 85

## DeviceType
Z
zengyawen 已提交
86 87 88

表示设备类型的枚举类。

89 90
**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager

H
HelloCrease 已提交
91 92 93 94 95 96 97 98 99
| 名称           | 默认值  | 说明   |
| ------------ | ---- | ---- |
| SPEAKER      | 0x0A | 智能音箱 |
| PHONE        | 0x0E | 手机   |
| TABLET       | 0x11 | 平板   |
| WEARABLE     | 0x6D | 智能穿戴 |
| TV           | 0x9C | 智慧屏  |
| CAR          | 0x83 | 车    |
| UNKNOWN_TYPE | 0    | 未知设备 |
100

Z
zengyawen 已提交
101

102
## DeviceStateChangeAction
Z
zengyawen 已提交
103

104 105 106 107
表示设备状态变化的枚举。

**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager

H
HelloCrease 已提交
108 109
| 名称      | 默认值  | 说明              |
| ------- | ---- | --------------- |
S
summer8999 已提交
110 111 112
| ONLINE  | 0    | 设备物理上线状态。           |
| READY   | 1    | 设备可用状态,表示设备间信息已在分布式数据中同步完成, 可以运行分布式业务。 |
| OFFLINE | 2    | 设备物理下线状态。           |
H
HelloCrease 已提交
113
| CHANGE  | 3    | 设备信息更改。         |
114 115 116 117

## SubscribeInfo

发现信息。
Z
zengyawen 已提交
118

119 120
**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager

H
HelloCrease 已提交
121 122 123 124 125 126 127 128 129
| 名称            | 类型                                | 必填   | 描述                |
| ------------- | --------------------------------- | ---- | ----------------- |
| subscribeId   | number                            | 是    | 发现标识,用于标识不同的发现周期。 |
| mode          | [DiscoverMode ](#discovermode)    | 否    | 发现模式。             |
| medium        | [ExchangeMedium](#exchangemedium) | 否    | 发现类型。             |
| freq          | [ExchangeFreq](#exchangefreq)     | 否    | 发现频率。             |
| isSameAccount | boolean                           | 否    | 是否同账号。            |
| isWakeRemote  | boolean                           | 否    | 是否唤醒设备。           |
| capability    | [SubscribeCap](#subscribecap)     | 否    | 发现能力。             |
130 131 132 133 134 135 136 137


## DiscoverMode 

表示发现模式的枚举。

**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager

H
HelloCrease 已提交
138 139
| 名称                    | 默认值  | 说明    |
| --------------------- | ---- | ----- |
140
| DISCOVER_MODE_PASSIVE | 0x55 | 被动模式。 |
H
HelloCrease 已提交
141
| DISCOVER_MODE_ACTIVE  | 0xAA | 主动模式。 |
142 143 144 145 146 147 148 149


## ExchangeMedium 

表示发现类型的枚举。

**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager

H
HelloCrease 已提交
150 151 152 153 154 155
| 名称   | 默认值  | 说明        |
| ---- | ---- | --------- |
| AUTO | 0    | 自动发现类型。   |
| BLE  | 1    | 蓝牙发现类型。   |
| COAP | 2    | WiFi发现类型。 |
| USB  | 3    | USB发现类型。  |
156 157 158 159 160 161 162

## ExchangeFreq 

表示发现频率的枚举。

**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager

H
HelloCrease 已提交
163 164 165 166 167 168
| 名称         | 默认值  | 说明    |
| ---------- | ---- | ----- |
| LOW        | 0    | 低频率。  |
| MID        | 1    | 中频率。  |
| HIGH       | 2    | 高频率。  |
| SUPER_HIGH | 3    | 超高频率。 |
169 170 171 172 173 174 175 176


## SubscribeCap 

表示发现能力的枚举。

**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager

H
HelloCrease 已提交
177 178 179 180
| 名称                        | 默认值  | 说明             |
| ------------------------- | ---- | -------------- |
| SUBSCRIBE_CAPABILITY_DDMP | 0    | DDMP能力,后续会被废弃。 |
| SUBSCRIBE_CAPABILITY_OSD  | 1    | OSD能力。         |
181 182 183 184 185 186 187 188


## AuthParam

认证参数。

**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager

H
HelloCrease 已提交
189 190 191 192
| 名称        | 类型                   | 必填   | 描述         |
| --------- | -------------------- | ---- | ---------- |
| authType  | number               | 是    | 认证类型。      |
| extraInfo | {[key:string] : any} | 否    | 认证参数可扩展字段。 |
193 194 195 196 197 198 199

## AuthInfo

认证信息。

**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager

H
HelloCrease 已提交
200 201 202 203 204
| 名称        | 类型                   | 必填   | 描述         |
| --------- | -------------------- | ---- | ---------- |
| authType  | number               | 是    | 认证类型。      |
| token     | number               | 是    | 认证Token。   |
| extraInfo | {[key:string] : any} | 否    | 认证信息可扩展字段。 |
Z
zengyawen 已提交
205

S
summer8999 已提交
206
## PublishInfo9+</sup>
S
summer8999 已提交
207 208 209 210 211 212 213 214 215 216 217

发布设备参数

**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager

| 名称          | 类型                              | 必填   | 描述                |
| ------------- | --------------------------------- | ---- | ----------------- |
| publishId     | number                            | 是    | 发布设备标识,用于标识不同的发布周期。 |
| mode          | [DiscoverMode ](#discovermode)    | 是    | 发现模式。             |
| freq          | [ExchangeFreq](#exchangefreq)     | 是    | 发现频率。             |
| ranging       | boolean                           | 是    | 发布的设备是否支持测距能力。             |
Z
zengyawen 已提交
218 219

## DeviceManager
Z
zengyawen 已提交
220 221 222

设备管理实例,用于获取可信设备和本地设备的相关信息。在调用DeviceManager的方法前,需要先通过createDeviceManager构建一个DeviceManager实例dmInstance。

Z
zengyawen 已提交
223
### release
Z
zengyawen 已提交
224

Z
zengyawen 已提交
225
release(): void
Z
zengyawen 已提交
226

Z
zengyawen 已提交
227
设备管理实例不再使用后,通过该方法释放DeviceManager实例。
Z
zengyawen 已提交
228

229 230
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
231
**错误码:**
@
@shi-xiaoxiao-iris 已提交
232

@
@shi-xiaoxiao-iris 已提交
233
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
@
@shi-xiaoxiao-iris 已提交
234

@
@shi-xiaoxiao-iris 已提交
235
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
236 237
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
@
@shi-xiaoxiao-iris 已提交
238

@
@shi-xiaoxiao-iris 已提交
239
**示例:**
@
@shi-xiaoxiao-iris 已提交
240

241
  ```js
242 243 244
  try {
    dmInstance.release();
  } catch (err) {
245
    console.error("release errCode:" + err.code + ",errMessage:" + err.message);
246
  }
Z
zengyawen 已提交
247
  ```
Z
zengyawen 已提交
248

Z
zengyawen 已提交
249
### getTrustedDeviceListSync
Z
zengyawen 已提交
250

Z
zengyawen 已提交
251
getTrustedDeviceListSync(): Array&lt;DeviceInfo&gt;
Z
zengyawen 已提交
252 253 254

同步获取所有可信设备列表。

255 256
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

257 258
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
259
**返回值:**
@
@shi-xiaoxiao-iris 已提交
260

H
HelloCrease 已提交
261 262
  | 名称                                     | 说明        |
  | -------------------------------------- | --------- |
Z
zengyawen 已提交
263 264
  | Array&lt;[DeviceInfo](#deviceinfo)&gt; | 返回可信设备列表。 |

@
@shi-xiaoxiao-iris 已提交
265
**错误码:**
@
@shi-xiaoxiao-iris 已提交
266

@
@shi-xiaoxiao-iris 已提交
267
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
@
@shi-xiaoxiao-iris 已提交
268

@
@shi-xiaoxiao-iris 已提交
269
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
270 271
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
@
@shi-xiaoxiao-iris 已提交
272

@
@shi-xiaoxiao-iris 已提交
273
**示例:**
@
@shi-xiaoxiao-iris 已提交
274

275
  ```js
276 277 278
  try {
    var deviceInfoList = dmInstance.getTrustedDeviceListSync();
  } catch (err) {
279
    console.error("getTrustedDeviceListSync errCode:" + err.code + ",errMessage:" + err.message);
280
  }
Z
zengyawen 已提交
281
  ```
Z
zengyawen 已提交
282

283 284 285 286 287 288
### getTrustedDeviceList<sup>8+</sup>

getTrustedDeviceList(callback:AsyncCallback&lt;Array&lt;DeviceInfo&gt;&gt;): void

获取所有可信设备列表。使用callback异步回调。

289 290
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

291 292
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
293
**参数:**
@
@shi-xiaoxiao-iris 已提交
294

H
HelloCrease 已提交
295 296 297
  | 名称       | 参数类型                                     | 必填   | 说明                    |
  | -------- | ---------------------------------------- | ---- | --------------------- |
  | callback | AsyncCallback&lt;Array&lt;[DeviceInfo](#deviceinfo)&gt;&gt; | 是    | 获取所有可信设备列表的回调,返回设备信息。 |
298

@
@shi-xiaoxiao-iris 已提交
299
**错误码:**
@
@shi-xiaoxiao-iris 已提交
300

@
@shi-xiaoxiao-iris 已提交
301
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
@
@shi-xiaoxiao-iris 已提交
302

@
@shi-xiaoxiao-iris 已提交
303
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
304 305
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
@
@shi-xiaoxiao-iris 已提交
306

@
@shi-xiaoxiao-iris 已提交
307
**示例:**
@
@shi-xiaoxiao-iris 已提交
308

309
  ```js
310 311 312
  try {
    dmInstance.getTrustedDeviceList((err, data) => {
      if (err) {
313
        console.error("getTrustedDeviceList errCode:" + err.code + ",errMessage:" + err.message);
314 315
        return;
      }
316
      console.log('get trusted device info: ' + JSON.stringify(data));
317
    });
318
  } catch (err) {
319
    console.error("getTrustedDeviceList errCode:" + err.code + ",errMessage:" + err.message);
320
  }
321 322 323 324 325 326 327 328
  ```

### getTrustedDeviceList<sup>8+</sup>

getTrustedDeviceList(): Promise&lt;Array&lt;DeviceInfo&gt;&gt;

获取所有可信设备列表。使用Promise异步回调。

329 330
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

331 332
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
333
**返回值:**
@
@shi-xiaoxiao-iris 已提交
334

H
HelloCrease 已提交
335 336
  | 类型                                       | 说明                    |
  | ---------------------------------------- | --------------------- |
337 338
  | Promise&lt;Array&lt;[DeviceInfo](#deviceinfo)&gt;&gt; | Promise实例,用于获取异步返回结果。 |

@
@shi-xiaoxiao-iris 已提交
339
**错误码:**
@
@shi-xiaoxiao-iris 已提交
340

@
@shi-xiaoxiao-iris 已提交
341
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
@
@shi-xiaoxiao-iris 已提交
342

@
@shi-xiaoxiao-iris 已提交
343
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
344 345
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
@
@shi-xiaoxiao-iris 已提交
346

@
@shi-xiaoxiao-iris 已提交
347
**示例:**
@
@shi-xiaoxiao-iris 已提交
348

349
  ```js
350 351 352
  dmInstance.getTrustedDeviceList().then((data) => {
    console.log('get trusted device info: ' + JSON.stringify(data));
    }).catch((err) => {
353
      console.error("getTrustedDeviceList errCode:" + err.code + ",errMessage:" + err.message);
354
  });
355 356 357 358 359 360 361 362
  ```

### getLocalDeviceInfoSync<sup>8+</sup>

getLocalDeviceInfoSync(): [DeviceInfo](#deviceinfo)

同步获取本地设备信息。

363 364
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

365 366
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
367
**返回值:**
@
@shi-xiaoxiao-iris 已提交
368

369 370 371
  | 名称                      | 说明              |
  | ------------------------- | ---------------- |
  | [DeviceInfo](#deviceinfo) | 返回本地设备列表。 |
372

@
@shi-xiaoxiao-iris 已提交
373
**错误码:**
@
@shi-xiaoxiao-iris 已提交
374

@
@shi-xiaoxiao-iris 已提交
375
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
@
@shi-xiaoxiao-iris 已提交
376

@
@shi-xiaoxiao-iris 已提交
377
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
378 379
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
@
@shi-xiaoxiao-iris 已提交
380

@
@shi-xiaoxiao-iris 已提交
381
**示例:**
@
@shi-xiaoxiao-iris 已提交
382

383
  ```js
384 385 386
  try {
    var deviceInfo = dmInstance.getLocalDeviceInfoSync();
  } catch (err) {
387
    console.error("getLocalDeviceInfoSync errCode:" + err.code + ",errMessage:" + err.message);
388
  }
389 390 391 392 393 394 395 396
  ```

### getLocalDeviceInfo<sup>8+</sup>

getLocalDeviceInfo(callback:AsyncCallback&lt;DeviceInfo&gt;): void

获取本地设备信息。使用callback异步回调。

397 398
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

399 400
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
401
**参数:**
@
@shi-xiaoxiao-iris 已提交
402

H
HelloCrease 已提交
403 404 405
  | 名称       | 参数类型                                     | 必填   | 说明        |
  | -------- | ---------------------------------------- | ---- | --------- |
  | callback | AsyncCallback&lt;[DeviceInfo](#deviceinfo)&gt; | 是    | 获取本地设备信息。 |
406

@
@shi-xiaoxiao-iris 已提交
407
**错误码:**
@
@shi-xiaoxiao-iris 已提交
408

@
@shi-xiaoxiao-iris 已提交
409
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
@
@shi-xiaoxiao-iris 已提交
410

@
@shi-xiaoxiao-iris 已提交
411
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
412 413
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
@
@shi-xiaoxiao-iris 已提交
414

@
@shi-xiaoxiao-iris 已提交
415
**示例:**
@
@shi-xiaoxiao-iris 已提交
416

417
  ```js
418
  try {
419
    dmInstance.getLocalDeviceInfo((err, data) => {
420
    if (err) {
421
      console.error("getLocalDeviceInfo errCode:" + err.code + ",errMessage:" + err.message);
422
      return;
423
    }
424 425 426
      console.log('get local device info: ' + JSON.stringify(data));
    });
  } catch (err) {
427
    console.error("getLocalDeviceInfo errCode:" + err.code + ",errMessage:" + err.message);
428
  }
429 430 431 432 433 434 435 436
  ```

### getLocalDeviceInfo<sup>8+</sup>

getLocalDeviceInfo(): Promise&lt;DeviceInfo&gt;

获取本地设备信息。使用Promise异步回调。

437 438
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

439 440
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
441 442
**返回值:**

H
HelloCrease 已提交
443 444
  | 类型                                       | 说明                    |
  | ---------------------------------------- | --------------------- |
445 446
  | Promise&lt;[DeviceInfo](#deviceinfo)&gt; | Promise实例,用于获取异步返回结果。 |

@
@shi-xiaoxiao-iris 已提交
447
**错误码:**
@
@shi-xiaoxiao-iris 已提交
448

@
@shi-xiaoxiao-iris 已提交
449
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
@
@shi-xiaoxiao-iris 已提交
450

@
@shi-xiaoxiao-iris 已提交
451
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
452 453 454
| ------- | --------------------------------------------------------------- |
| 11600101| Failed to execute the function.                                 |

@
@shi-xiaoxiao-iris 已提交
455
**示例:**
@
@shi-xiaoxiao-iris 已提交
456

457
  ```js
458 459 460
  dmInstance.getLocalDeviceInfo().then((data) => {
    console.log('get local device info: ' + JSON.stringify(data));
  }).catch((err) => {
461
    console.error("getLocalDeviceInfo errCode:" + err.code + ",errMessage:" + err.message);
462
  });
463 464
  ```

S
summer8999 已提交
465
### startDeviceDiscovery8+</sup>
466 467 468 469 470

startDeviceDiscovery(subscribeInfo: SubscribeInfo): void

发现周边设备。

471 472
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

473 474
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
475
**参数:**
@
@shi-xiaoxiao-iris 已提交
476

477
  | 名称            | 参数类型                       | 必填 | 说明    |
H
HelloCrease 已提交
478
  | ------------- | ------------------------------- | ---- | ----- |
479
  | subscribeInfo | [SubscribeInfo](#subscribeinfo) | 是   | 发现信息。|
480

@
@shi-xiaoxiao-iris 已提交
481
**错误码:**
@
@shi-xiaoxiao-iris 已提交
482

@
@shi-xiaoxiao-iris 已提交
483
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
@
@shi-xiaoxiao-iris 已提交
484

@
@shi-xiaoxiao-iris 已提交
485
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
486 487 488 489
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
| 11600104 | Discovery invalid.                                              |

@
@shi-xiaoxiao-iris 已提交
490
**示例:**
@
@shi-xiaoxiao-iris 已提交
491

492
  ```js
493
  // 生成发现标识,随机数确保每次调用发现接口的标识不一致
494 495 496
  var subscribeId = Math.floor(Math.random() * 10000 + 1000);
  var subscribeInfo = {
      "subscribeId": subscribeId,
497 498 499
      "mode": 0xAA, // 主动模式
      "medium": 0,  // 自动发现类型,同时支持多种发现类型
      "freq": 2,    // 高频率
500 501 502 503
      "isSameAccount": false,
      "isWakeRemote": false,
      "capability": 1
  };
504 505 506
  try {
    dmInstance.startDeviceDiscovery(subscribeInfo); // 当有设备发现时,通过deviceFound回调通知给应用程序
  } catch (err) {
507
    console.error("startDeviceDiscovery errCode:" + err.code + ",errMessage:" + err.message);
508
  }
509 510
  ```

S
summer8999 已提交
511 512
### startDeviceDiscovery9+</sup>

S
summer8999 已提交
513
startDeviceDiscovery(subscribeInfo: SubscribeInfo, filterOptions?: string): void
S
summer8999 已提交
514 515 516

发现周边设备。

517 518
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

S
summer8999 已提交
519 520
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
521
**参数:**
@
@shi-xiaoxiao-iris 已提交
522

523 524
  | 名称            | 参数类型                       | 必填   | 说明    |
  | ------------- | ------------------------------- | ---- | -----  |
S
summer8999 已提交
525
  | subscribeInfo | [SubscribeInfo](#subscribeinfo) | 是   | 发现信息。 |
526
  | filterOptions | string                          | 否   | 发现设备过滤信息。|
S
summer8999 已提交
527

@
@shi-xiaoxiao-iris 已提交
528
**错误码:**
@
@shi-xiaoxiao-iris 已提交
529

@
@shi-xiaoxiao-iris 已提交
530
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
@
@shi-xiaoxiao-iris 已提交
531

@
@shi-xiaoxiao-iris 已提交
532
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
533 534 535 536
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
| 11600104 | Discovery invalid.                                              |

@
@shi-xiaoxiao-iris 已提交
537
**示例:**
@
@shi-xiaoxiao-iris 已提交
538

S
summer8999 已提交
539
  ```js
540
  // 生成发现标识,随机数确保每次调用发现接口的标识不一致
S
summer8999 已提交
541 542 543
  var subscribeId = Math.floor(Math.random() * 10000 + 1000);
  var subscribeInfo = {
      "subscribeId": subscribeId,
544 545 546
      "mode": 0xAA, // 主动模式
      "medium": 0,  // 自动发现类型,同时支持多种发现类型
      "freq": 2,    // 高频率
S
summer8999 已提交
547 548 549 550 551
      "isSameAccount": false,
      "isWakeRemote": false,
      "capability": 1
  };
  var filterOptions = {
S
summer8999 已提交
552
    "filter_op": "OR", // 可选, 默认"OR"
S
summer8999 已提交
553 554 555
    "filters": [
        {
            "type": "range",
S
summer8999 已提交
556
            "value": 50 // 需过滤发现设备的距离,单位(cm)
S
summer8999 已提交
557 558
        }
    ]
S
summer8999 已提交
559
  };
560 561 562
  try {
    dmInstance.startDeviceDiscovery(subscribeInfo, JSON.stringify(filterOptions)); // 当有设备发现时,通过deviceFound回调通知给应用程序
  } catch (err) {
563
    console.error("startDeviceDiscovery errCode:" + err.code + ",errMessage:" + err.message);
564
  }
S
summer8999 已提交
565
  ```
@
@shi-xiaoxiao-iris 已提交
566

567 568 569 570 571 572
### stopDeviceDiscovery

stopDeviceDiscovery(subscribeId: number): void

停止发现周边设备。

573 574
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

575 576
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
577
**参数:**
@
@shi-xiaoxiao-iris 已提交
578

H
HelloCrease 已提交
579 580 581
  | 名称          | 参数类型   | 必填   | 说明    |
  | ----------- | ------ | ---- | ----- |
  | subscribeId | number | 是    | 发现标识。 |
582

@
@shi-xiaoxiao-iris 已提交
583
**错误码:**
@
@shi-xiaoxiao-iris 已提交
584

@
@shi-xiaoxiao-iris 已提交
585
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
@
@shi-xiaoxiao-iris 已提交
586

@
@shi-xiaoxiao-iris 已提交
587
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
588 589
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
@
@shi-xiaoxiao-iris 已提交
590

@
@shi-xiaoxiao-iris 已提交
591
**示例:**
@
@shi-xiaoxiao-iris 已提交
592

593
  ```js
594
  // 入参需要和startDeviceDiscovery接口传入的subscribeId配对使用
595 596 597
  try {
    dmInstance.stopDeviceDiscovery(subscribeId);
  } catch (err) {
598
    console.error("stopDeviceDiscovery errCode:" + err.code + ",errMessage:" + err.message);
599
  }
600 601
  ```

S
summer8999 已提交
602 603
### publishDeviceDiscovery9+</sup>

S
summer8999 已提交
604
publishDeviceDiscovery(publishInfo: PublishInfo): void
S
summer8999 已提交
605

S
summer8999 已提交
606
发布设备发现。
S
summer8999 已提交
607

608 609
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

S
summer8999 已提交
610 611
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
612
**参数:**
@
@shi-xiaoxiao-iris 已提交
613

S
summer8999 已提交
614
  | 名称          | 参数类型                        | 必填 | 说明    |
S
summer8999 已提交
615 616 617
  | ------------- | ------------------------------- | ---- | ----- |
  | publishInfo   | [PublishInfo](#publishinfo)     | 是   | 发布设备发现信息。 |

@
@shi-xiaoxiao-iris 已提交
618
**错误码:**
@
@shi-xiaoxiao-iris 已提交
619

@
@shi-xiaoxiao-iris 已提交
620
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
@
@shi-xiaoxiao-iris 已提交
621

@
@shi-xiaoxiao-iris 已提交
622
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
623 624 625
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
| 11600105 | Publish invalid.                                                |
@
@shi-xiaoxiao-iris 已提交
626

@
@shi-xiaoxiao-iris 已提交
627
**示例:**
@
@shi-xiaoxiao-iris 已提交
628

S
summer8999 已提交
629
  ```js
630
  // 生成发布标识,随机数确保每次调用发布接口的标识不一致
S
summer8999 已提交
631
  var publishId = Math.floor(Math.random() * 10000 + 1000);
S
summer8999 已提交
632
  var publishInfo = {
S
summer8999 已提交
633
      "publishId": publishId,
634 635 636
      "mode": 0xAA, // 主动模式
      "freq": 2,    // 高频率
      "ranging": 1  // 支持发现时测距
S
summer8999 已提交
637
  };
638 639 640
  try {
    dmInstance.publishDeviceDiscovery(publishInfo); // 当有发布结果时,通过回调通知给应用程序
  } catch (err) {
641
    console.error("publishDeviceDiscovery errCode:" + err.code + ",errMessage:" + err.message);
642
  }
S
summer8999 已提交
643 644
  ```
  
S
summer8999 已提交
645
### unPublishDeviceDiscovery9+</sup>
S
summer8999 已提交
646

S
summer8999 已提交
647
unPublishDeviceDiscovery(publishId: number): void
S
summer8999 已提交
648 649 650

停止发布设备发现。

651 652
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

S
summer8999 已提交
653 654
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
655
**参数:**
@
@shi-xiaoxiao-iris 已提交
656

S
summer8999 已提交
657 658 659
  | 名称        | 参数类型 | 必填 | 说明  |
  | ----------- | -------- | ---- | ----- |
  | publishId   | number   | 是   | 发布标识。 |
S
summer8999 已提交
660

@
@shi-xiaoxiao-iris 已提交
661
**错误码:**
@
@shi-xiaoxiao-iris 已提交
662

@
@shi-xiaoxiao-iris 已提交
663
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
@
@shi-xiaoxiao-iris 已提交
664

@
@shi-xiaoxiao-iris 已提交
665
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
666 667
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
@
@shi-xiaoxiao-iris 已提交
668

@
@shi-xiaoxiao-iris 已提交
669
**示例:**
@
@shi-xiaoxiao-iris 已提交
670

S
summer8999 已提交
671
  ```js
672
  // 入参需要和publishDeviceDiscovery接口传入的publishId配对使用
673
  try {
674
    dmInstance.unPublishDeviceDiscovery(publishId);
675
  } catch (err) {
676
    console.error("unPublishDeviceDiscovery errCode:" + err.code + ",errMessage:" + err.message);
677
  }
S
summer8999 已提交
678 679
  ```

680 681 682 683 684 685
### authenticateDevice

authenticateDevice(deviceInfo: DeviceInfo, authParam: AuthParam, callback: AsyncCallback<{deviceId: string, pinToken ?: number}>): void

认证设备。

686 687
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

688 689
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
690
**参数:**
@
@shi-xiaoxiao-iris 已提交
691

H
HelloCrease 已提交
692 693 694 695 696
  | 名称         | 参数类型                                     | 必填   | 说明      |
  | ---------- | ---------------------------------------- | ---- | ------- |
  | deviceInfo | [DeviceInfo](#deviceinfo)                | 是    | 设备信息。   |
  | authParam  | [AuthParam](#authparam)                  | 是    | 认证参数。   |
  | callback   | AsyncCallback<{ deviceId: string, pinToken ?: number }> | 是    | 认证结果回调。 |
697

@
@shi-xiaoxiao-iris 已提交
698
**错误码:**
@
@shi-xiaoxiao-iris 已提交
699

@
@shi-xiaoxiao-iris 已提交
700
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
@
@shi-xiaoxiao-iris 已提交
701

@
@shi-xiaoxiao-iris 已提交
702
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
703 704 705
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
| 11600103 | Authentication invalid.                                         |
@
@shi-xiaoxiao-iris 已提交
706

@
@shi-xiaoxiao-iris 已提交
707
**示例:**
@
@shi-xiaoxiao-iris 已提交
708

709
  ```js
710
  // 认证的设备信息,可以从发现的结果中获取
711 712 713 714 715 716
  var deviceInfo ={
      "deviceId": "XXXXXXXX",
      "deviceName": "",
      deviceType: 0x0E
  };
  let authParam = {
717
      "authType": 1, // 认证类型: 1 - 无账号PIN码认证
718 719
      "extraInfo": {} 
  }
720 721
  try {
    dmInstance.authenticateDevice(deviceInfo, authParam, (err, data) => {
722
      if (err) {
723
          console.error("authenticateDevice errCode:" + err.code + ",errMessage:" + err.message);
724 725
          return;
      }
@
@shi-xiaoxiao-iris 已提交
726 727
      console.info("authenticateDevice result:" + JSON.stringify(data));
      let token = data.pinToken;
728 729
    });
  } catch (err) {
730
    console.error("authenticateDevice errCode:" + err.code + ",errMessage:" + err.message);
731
  }
732 733 734 735 736 737 738 739
  ```

### unAuthenticateDevice<sup>8+</sup>

unAuthenticateDevice(deviceInfo: DeviceInfo): void

解除认证设备。

740 741
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

742 743
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
744
**参数:**
@
@shi-xiaoxiao-iris 已提交
745

H
HelloCrease 已提交
746 747 748
  | 名称         | 参数类型                      | 必填   | 说明    |
  | ---------- | ------------------------- | ---- | ----- |
  | deviceInfo | [DeviceInfo](#deviceinfo) | 是    | 设备信息。 |
749

@
@shi-xiaoxiao-iris 已提交
750
**错误码:**
@
@shi-xiaoxiao-iris 已提交
751

@
@shi-xiaoxiao-iris 已提交
752
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
@
@shi-xiaoxiao-iris 已提交
753

@
@shi-xiaoxiao-iris 已提交
754
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
755 756
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
@
@shi-xiaoxiao-iris 已提交
757

@
@shi-xiaoxiao-iris 已提交
758
**示例:**
@
@shi-xiaoxiao-iris 已提交
759

760
  ```js
761 762 763
  try {
    dmInstance.unAuthenticateDevice(deviceInfo);
  } catch (err) {
764
    console.error("unAuthenticateDevice errCode:" + err.code + ",errMessage:" + err.message);
765
  }
766 767 768 769 770 771 772 773
  ```

### verifyAuthInfo

verifyAuthInfo(authInfo: AuthInfo, callback: AsyncCallback<{deviceId: string, level: number}>): void

验证认证信息。

774 775
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

776 777
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
778
**参数:**
@
@shi-xiaoxiao-iris 已提交
779

H
HelloCrease 已提交
780 781 782
  | 名称       | 参数类型                                     | 必填   | 说明      |
  | -------- | ---------------------------------------- | ---- | ------- |
  | authInfo | [AuthInfo](#authinfo)                    | 是    | 认证信息。   |
783
  | callback | AsyncCallback<{ deviceId: string, level: number }> | 是    | 验证结果回调。 |
784

@
@shi-xiaoxiao-iris 已提交
785
**错误码:**
@
@shi-xiaoxiao-iris 已提交
786

@
@shi-xiaoxiao-iris 已提交
787
以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
@
@shi-xiaoxiao-iris 已提交
788

@
@shi-xiaoxiao-iris 已提交
789
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
790 791
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
@
@shi-xiaoxiao-iris 已提交
792

@
@shi-xiaoxiao-iris 已提交
793
**示例:**
@
@shi-xiaoxiao-iris 已提交
794

795 796 797 798 799 800
  ```js
  let authInfo = {
    "authType": 1,
    "token": xxxxxx,
    "extraInfo": {}
  }
801 802
  try {
    dmInstance.verifyAuthInfo(authInfo, (err, data) => {
803
    if (err) {
804
        console.error("verifyAuthInfo errCode:" + err.code + ",errMessage:" + err.message);
805 806
        return;
    }
@
@shi-xiaoxiao-iris 已提交
807
    console.info("verifyAuthInfo result:" + JSON.stringify(data));
808 809
    });
  } catch (err) {
810
    console.error("verifyAuthInfo errCode:" + err.code + ",errMessage:" + err.message);
811
  }
812 813
  ```

@
@shi-xiaoxiao-iris 已提交
814
### setUserOperation9+</sup>
@
@shi-xiaoxiao-iris 已提交
815 816 817 818 819

setUserOperation(operateAction: number, params: string): void;

设置用户ui操作行为。

820 821
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

@
@shi-xiaoxiao-iris 已提交
822 823
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
824
**参数:**
@
@shi-xiaoxiao-iris 已提交
825

826 827 828 829
  | 名称       | 参数类型            | 必填  | 说明                |
  | ------------- | --------------- | ---- | ------------------- |
  | operateAction | number          | 是    | 用户操作动作。       |
  | params        | string          | 是    | 表示用户的输入参数。 |
@
@shi-xiaoxiao-iris 已提交
830

@
@shi-xiaoxiao-iris 已提交
831
**示例:**
@
@shi-xiaoxiao-iris 已提交
832 833

  ```js
@
@shi-xiaoxiao-iris 已提交
834 835 836 837 838 839 840 841 842 843 844 845 846
 try {
    /*
      operateAction = 0 - 允许授权
      operateAction = 1 - 取消授权
      operateAction = 2 - 授权框用户操作超时
      operateAction = 3 - 取消pin码框展示
      operateAction = 4 - 取消pin码输入框展示
      operateAction = 5 - pin码输入框确定操作
    */
    let operation = 0;
    this.dmInstance.setUserOperation(operation, "extra")
    } catch (err) {
      console.error("setUserOperation errCode:" + err.code + ",errMessage:" + err.message);
@
@shi-xiaoxiao-iris 已提交
847 848 849
  }
  ```

@
@shi-xiaoxiao-iris 已提交
850
### on('uiStateChange')9+</sup>
@
@shi-xiaoxiao-iris 已提交
851 852 853 854 855

on(type: 'uiStateChange', callback: Callback<{ param: string}>): void;

ui状态变更回调。

856 857
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

@
@shi-xiaoxiao-iris 已提交
858 859
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
860
**参数:**
@
@shi-xiaoxiao-iris 已提交
861

@
@shi-xiaoxiao-iris 已提交
862 863 864 865
  | 名称      | 参数类型                             | 必填 | 说明                            |
  | -------- | ------------------------------------ | ---- | ------------------------------ |
  | type     | string                                | 是  | 注册的设备管理器 ui 状态回调,以便在状态改变时通知应用。 |
  | callback | Callback&lt;{&nbsp;param: string}&gt; | 是  | 指示要注册的设备管理器 ui 状态回调,返回ui状态。        |
@
@shi-xiaoxiao-iris 已提交
866

@
@shi-xiaoxiao-iris 已提交
867
**示例:**
@
@shi-xiaoxiao-iris 已提交
868 869 870

  ```js
  try {
@
@shi-xiaoxiao-iris 已提交
871
    dmInstance.on('uiStateChange', (data) => {
@
@shi-xiaoxiao-iris 已提交
872 873 874 875 876 877 878 879 880 881 882 883 884
    console.log("uiStateChange executed, dialog closed" + JSON.stringify(data))
    var tmpStr = JSON.parse(data.param)
    this.isShow = tmpStr.verifyFailed
    console.log("uiStateChange executed, dialog closed" + this.isShow)
    if (!this.isShow) {
        this.destruction()
    }
  });
  } catch (err) {
    console.error("uiStateChange errCode:" + err.code + ",errMessage:" + err.message);
  }
  ```

@
@shi-xiaoxiao-iris 已提交
885
### off('uiStateChange')9+</sup>
@
@shi-xiaoxiao-iris 已提交
886 887 888

off(type: 'uiStateChange', callback?: Callback<{ param: string}>): void;

889 890 891
取消ui状态变更回调。

**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。
@
@shi-xiaoxiao-iris 已提交
892 893 894

**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
895
**参数:**
@
@shi-xiaoxiao-iris 已提交
896

@
@shi-xiaoxiao-iris 已提交
897
  | 名称      | 参数类型                              | 必填 | 说明                            |
@
@shi-xiaoxiao-iris 已提交
898 899 900 901
  | -------- | ------------------------------------- | ---- | ------------------------------ |
  | type     | string                                | 是   | 取消注册的设备管理器 ui 状态回调。 |
  | callback | Callback&lt;{&nbsp;param: string}&gt; | 是   | 指示要取消注册的设备管理器 ui 状态,返回UI状态。 |

@
@shi-xiaoxiao-iris 已提交
902
**示例:**
@
@shi-xiaoxiao-iris 已提交
903 904 905

  ```js
  try {
@
@shi-xiaoxiao-iris 已提交
906
    dmInstance.off('uiStateChange');
@
@shi-xiaoxiao-iris 已提交
907 908 909 910 911
  } catch (err) {
    console.error("uiStateChange errCode:" + err.code + ",errMessage:" + err.message);
  }
  ```

Z
zengyawen 已提交
912
### on('deviceStateChange')
Z
zengyawen 已提交
913

Z
zengyawen 已提交
914
on(type: 'deviceStateChange',  callback: Callback&lt;{ action: DeviceStateChangeAction, device: DeviceInfo }&gt;): void
Z
zengyawen 已提交
915

Z
zengyawen 已提交
916 917
注册设备状态回调。

918 919
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

920 921
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
922
**参数:**
@
@shi-xiaoxiao-iris 已提交
923

H
HelloCrease 已提交
924 925 926 927
  | 名称       | 参数类型                                     | 必填   | 说明                             |
  | -------- | ---------------------------------------- | ---- | ------------------------------ |
  | type     | string                                   | 是    | 注册设备状态回调,固定为deviceStateChange。 |
  | callback | Callback&lt;{&nbsp;action:&nbsp;[DeviceStateChangeAction](#devicestatechangeaction),&nbsp;device:&nbsp;[DeviceInfo](#deviceinfo)&nbsp;}&gt; | 是    | 指示要注册的设备状态回调,返回设备状态和设备信息。      |
Z
zengyawen 已提交
928

@
@shi-xiaoxiao-iris 已提交
929
**示例:**
@
@shi-xiaoxiao-iris 已提交
930

931
  ```js
932 933 934 935 936
  try {
    dmInstance.on('deviceStateChange', (data) => {
      console.info("deviceStateChange on:" + JSON.stringify(data));
    });
  } catch (err) {
937
    console.error("deviceStateChange errCode:" + err.code + ",errMessage:" + err.message);
938
  }
Z
zengyawen 已提交
939
  ```
Z
zengyawen 已提交
940

Z
zengyawen 已提交
941
### off('deviceStateChange')
Z
zengyawen 已提交
942

943
off(type: 'deviceStateChange', callback?: Callback&lt;{ action: DeviceStateChangeAction, device: DeviceInfo }&gt;): void
Z
zengyawen 已提交
944 945 946

取消注册设备状态回调。

947 948
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

949 950
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
951
**参数:**
@
@shi-xiaoxiao-iris 已提交
952

H
HelloCrease 已提交
953 954 955 956
  | 名称       | 参数类型                                     | 必填   | 说明                          |
  | -------- | ---------------------------------------- | ---- | --------------------------- |
  | type     | string                                   | 是    | 根据应用程序的包名取消注册设备状态回调。        |
  | callback | Callback&lt;{&nbsp;action:&nbsp;[DeviceStateChangeAction](#devicestatechangeaction),&nbsp;device:&nbsp;[DeviceInfo](#deviceinfo)&nbsp;&nbsp;}&gt; | 是    | 指示要取消注册的设备状态回调,返回设备状态和设备信息。 |
Z
zengyawen 已提交
957

@
@shi-xiaoxiao-iris 已提交
958
**示例:**
@
@shi-xiaoxiao-iris 已提交
959

960
  ```js
961 962 963 964 965
  try {
    dmInstance.off('deviceStateChange', (data) => {
      console.info('deviceStateChange' + JSON.stringify(data));
    });
  } catch (err) {
966
    console.error("deviceStateChange errCode:" + err.code + ",errMessage:" + err.message);
967
  }
Z
zengyawen 已提交
968 969
  ```

970 971 972 973 974 975
### on('deviceFound')

on(type: 'deviceFound', callback: Callback&lt;{ subscribeId: number, device: DeviceInfo }&gt;): void

注册发现设备回调监听。

976 977
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

978 979
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
980
**参数:**
@
@shi-xiaoxiao-iris 已提交
981

H
HelloCrease 已提交
982 983 984 985
  | 名称       | 参数类型                                     | 必填   | 说明                         |
  | -------- | ---------------------------------------- | ---- | -------------------------- |
  | type     | string                                   | 是    | 注册设备发现回调,以便在发现周边设备时通知应用程序。 |
  | callback | Callback&lt;{&nbsp;subscribeId: number, device: DeviceInfo&nbsp;}&gt; | 是    | 注册设备发现的回调方法。               |
986

@
@shi-xiaoxiao-iris 已提交
987
**示例:**
@
@shi-xiaoxiao-iris 已提交
988

989
  ```js
990 991 992 993 994
  try {
    dmInstance.on('deviceFound', (data) => {
      console.info("deviceFound:" + JSON.stringify(data));
    });
  } catch (err) {
995
    console.error("deviceFound errCode:" + err.code + ",errMessage:" + err.message);
996
  }
997 998 999 1000 1001 1002 1003 1004
  ```

### off('deviceFound')

off(type: 'deviceFound', callback?: Callback&lt;{ subscribeId: number, device: DeviceInfo }&gt;): void

取消注册设备发现回调。

1005 1006
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

1007 1008
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
1009
**参数:**
@
@shi-xiaoxiao-iris 已提交
1010

H
HelloCrease 已提交
1011 1012 1013 1014
  | 名称       | 参数类型                                     | 必填   | 说明                          |
  | -------- | ---------------------------------------- | ---- | --------------------------- |
  | type     | string                                   | 是    | 取消注册设备发现回调。                 |
  | callback | Callback&lt;{&nbsp;subscribeId: number, device: [DeviceInfo](#deviceinfo)&nbsp;}&gt; | 是    | 指示要取消注册的设备发现回调,返回设备状态和设备信息。 |
1015

@
@shi-xiaoxiao-iris 已提交
1016
**示例:**
@
@shi-xiaoxiao-iris 已提交
1017

1018
  ```js
1019 1020 1021 1022 1023
  try {
    dmInstance.off('deviceFound', (data) => {
      console.info('deviceFound' + JSON.stringify(data));
    });
  } catch (err) {
1024
    console.error("deviceFound errCode:" + err.code + ",errMessage:" + err.message);
1025
  }
1026 1027 1028 1029 1030 1031 1032 1033
  ```

### on('discoverFail')

on(type: 'discoverFail', callback: Callback&lt;{ subscribeId: number, reason: number }&gt;): void

注册设备发现失败回调监听。

1034 1035
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

1036 1037
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
1038
**参数:**
@
@shi-xiaoxiao-iris 已提交
1039

H
HelloCrease 已提交
1040 1041 1042 1043
  | 名称       | 参数类型                                     | 必填   | 说明                             |
  | -------- | ---------------------------------------- | ---- | ------------------------------ |
  | type     | string                                   | 是    | 注册设备发现失败回调,以便在发现周边设备失败时通知应用程序。 |
  | callback | Callback&lt;{&nbsp;subscribeId: number, reason: number&nbsp;}&gt; | 是    | 注册设备发现失败的回调方法。                 |
1044

@
@shi-xiaoxiao-iris 已提交
1045
**示例:**
@
@shi-xiaoxiao-iris 已提交
1046

1047
  ```js
1048 1049
  try {
    dmInstance.on('discoverFail', (data) => {
1050
        console.info("discoverFail on:" + JSON.stringify(data));
1051
    });
1052
  } catch (err) {
1053
    console.error("discoverFail errCode:" + err.code + ",errMessage:" + err.message);
1054
  }
1055 1056 1057 1058 1059 1060 1061 1062
  ```

### off('discoverFail')

off(type: 'discoverFail', callback?: Callback&lt;{ subscribeId: number, reason: number }&gt;): void

取消注册设备发现失败回调。

1063 1064
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

1065 1066
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
1067
**参数:**
@
@shi-xiaoxiao-iris 已提交
1068

H
HelloCrease 已提交
1069 1070 1071 1072
  | 名称       | 参数类型                                     | 必填   | 说明                |
  | -------- | ---------------------------------------- | ---- | ----------------- |
  | type     | string                                   | 是    | 取消注册设备发现失败回调。     |
  | callback | Callback&lt;{&nbsp;subscribeId: number, reason: number&nbsp;}&gt; | 是    | 指示要取消注册的设备发现失败回调。 |
1073

@
@shi-xiaoxiao-iris 已提交
1074
**示例:**
@
@shi-xiaoxiao-iris 已提交
1075

1076
  ```js
1077 1078 1079 1080 1081
  try {
    dmInstance.off('discoverFail', (data) => {
      console.info('discoverFail' + JSON.stringify(data));
    });
  } catch (err) {
1082
    console.error("discoverFail errCode:" + err.code + ",errMessage:" + err.message);
1083
  }
1084 1085
  ```

S
summer8999 已提交
1086
### on('publishSuccess')9+</sup>
S
summer8999 已提交
1087 1088 1089 1090 1091

on(type: 'publishSuccess', callback: Callback&lt;{ publishId: number }&gt;): void

注册发布设备发现回调监听。

1092 1093
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

S
summer8999 已提交
1094 1095
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
1096
**参数:**
@
@shi-xiaoxiao-iris 已提交
1097

S
summer8999 已提交
1098
  | 名称     | 参数类型                                 | 必填 | 说明                       |
S
summer8999 已提交
1099
  | -------- | ---------------------------------------- | ---- | -------------------------- |
S
summer8999 已提交
1100 1101
  | type     | string                                   | 是   | 注册发布设备成功回调,以便将发布成功时通知应用程序。 |
  | callback | Callback&lt;{ publishId: number }&gt;    | 是   | 注册设备发布成功的回调方法。               |
S
summer8999 已提交
1102

@
@shi-xiaoxiao-iris 已提交
1103

@
@shi-xiaoxiao-iris 已提交
1104
**示例:**
@
@shi-xiaoxiao-iris 已提交
1105

S
summer8999 已提交
1106
  ```js
1107 1108 1109 1110 1111
  try {
    dmInstance.on('publishSuccess', (data) => {
      console.info("publishSuccess:" + JSON.stringify(data));
    });
  } catch (err) {
1112
    console.error("publishSuccess errCode:" + err.code + ",errMessage:" + err.message);
1113
  }
S
summer8999 已提交
1114 1115
  ```

S
summer8999 已提交
1116
### off('publishSuccess')9+</sup>
S
summer8999 已提交
1117 1118 1119

off(type: 'publishSuccess', callback?: Callback&lt;{ publishId: number }&gt;): void

S
summer8999 已提交
1120
取消注册设备发布成功回调。
S
summer8999 已提交
1121

1122 1123
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

S
summer8999 已提交
1124 1125
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
1126
**参数:**
@
@shi-xiaoxiao-iris 已提交
1127

S
summer8999 已提交
1128
  | 名称     | 参数类型                                 | 必填 | 说明                          |
S
summer8999 已提交
1129
  | -------- | ---------------------------------------- | ---- | --------------------------- |
S
summer8999 已提交
1130 1131
  | type     | string                                   | 是   | 取消注册设备发布成功回调。                 |
  | callback | Callback&lt;{ publishId: number }&gt;    | 是   | 指示要取消注册的设备发布成功回调。 |
S
summer8999 已提交
1132

@
@shi-xiaoxiao-iris 已提交
1133
**示例:**
@
@shi-xiaoxiao-iris 已提交
1134

S
summer8999 已提交
1135
  ```js
1136 1137 1138 1139 1140
  try {
    dmInstance.off('publishSuccess', (data) => {
      console.info('publishSuccess' + JSON.stringify(data));
    });
  } catch (err) {
1141
    console.error("publishSuccess errCode:" + err.code + ",errMessage:" + err.message);
1142
  }
S
summer8999 已提交
1143 1144
  ```

S
summer8999 已提交
1145
### on('publishFail')9+</sup>
S
summer8999 已提交
1146 1147 1148 1149 1150

on(type: 'publishFail', callback: Callback&lt;{ publishId: number, reason: number }&gt;): void

注册设备发布失败回调监听。

1151 1152
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

S
summer8999 已提交
1153 1154
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
1155
**参数:**
@
@shi-xiaoxiao-iris 已提交
1156

S
summer8999 已提交
1157 1158 1159 1160
  | 名称     | 参数类型                                              | 必填 | 说明                             |
  | -------- | ----------------------------------------------------- | ---- | ------------------------------ |
  | type     | string                                                | 是   | 注册设备发布失败回调,以便在发布设备失败时通知应用程序。 |
  | callback | Callback&lt;{ publishId: number, reason: number }&gt; | 是   | 注册设备发布失败的回调方法。                 |
S
summer8999 已提交
1161

@
@shi-xiaoxiao-iris 已提交
1162
**示例:**
@
@shi-xiaoxiao-iris 已提交
1163

S
summer8999 已提交
1164
  ```js
1165 1166
  try {
    dmInstance.on('publishFail', (data) => {
1167
      console.info("publishFail on:" + JSON.stringify(data));
1168 1169
    });
  } catch (err) {
1170
    console.error("publishFail errCode:" + err.code + ",errMessage:" + err.message);
1171
  }
S
summer8999 已提交
1172 1173
  ```

S
summer8999 已提交
1174
### off('publishFail')9+</sup>
S
summer8999 已提交
1175 1176 1177

off(type: 'publishFail', callback?: Callback&lt;{ publishId: number, reason: number }&gt;): void

S
summer8999 已提交
1178
取消注册设备发布失败回调。
S
summer8999 已提交
1179

1180 1181
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

S
summer8999 已提交
1182 1183
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
1184
**参数:**
@
@shi-xiaoxiao-iris 已提交
1185

S
summer8999 已提交
1186 1187 1188
  | 名称     | 参数类型                                              | 必填 | 说明                |
  | -------- | ----------------------------------------------------- | ---- | ----------------- |
  | type     | string                                                | 是   | 取消注册设备发布失败回调。     |
S
summer8999 已提交
1189
  | callback | Callback&lt;{ publishId: number, reason: number }&gt; | 是   | 指示要取消注册设备发布失败回调。 |
S
summer8999 已提交
1190

@
@shi-xiaoxiao-iris 已提交
1191
**示例:**
@
@shi-xiaoxiao-iris 已提交
1192

S
summer8999 已提交
1193
  ```js
1194 1195 1196 1197 1198
  try {
    dmInstance.off('publishFail', (data) => {
      console.info('publishFail' + JSON.stringify(data));
    });
  } catch (err) {
1199
    console.error("publishFail errCode:" + err.code + ",errMessage:" + err.message);
1200
  }
S
summer8999 已提交
1201
  ```
1202

Z
zengyawen 已提交
1203 1204 1205
### on('serviceDie')

on(type: 'serviceDie', callback: () =&gt; void): void
Z
zengyawen 已提交
1206 1207 1208

注册设备管理服务死亡监听。

1209 1210
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

1211 1212
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
1213
**参数:**
@
@shi-xiaoxiao-iris 已提交
1214

H
HelloCrease 已提交
1215 1216 1217 1218
  | 名称       | 参数类型                    | 必填   | 说明                                       |
  | -------- | ----------------------- | ---- | ---------------------------------------- |
  | type     | string                  | 是    | 注册serviceDie回调,以便在devicemanager服务异常终止时通知应用程序。 |
  | callback | ()&nbsp;=&gt;&nbsp;void | 是    | 注册serviceDie的回调方法。                       |
Z
zengyawen 已提交
1219

@
@shi-xiaoxiao-iris 已提交
1220
**示例:**
@
@shi-xiaoxiao-iris 已提交
1221

1222
  ```js
1223 1224 1225 1226 1227
  try {
    dmInstance.on("serviceDie", () => {
      console.info("serviceDie on");
    });
  } catch (err) {
1228
    console.error("serviceDie errCode:" + err.code + ",errMessage:" + err.message);
1229
  }
Z
zengyawen 已提交
1230
  ```
Z
zengyawen 已提交
1231

Z
zengyawen 已提交
1232 1233 1234 1235 1236
### off('serviceDie')

off(type: 'serviceDie', callback?: () =&gt; void): void

取消注册设备管理服务死亡监听。
Z
zengyawen 已提交
1237

1238 1239
**需要权限**:ohos.permission.ACCESS_SERVICE_DM,仅系统应用可用。

1240 1241
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
1242
**参数:**
@
@shi-xiaoxiao-iris 已提交
1243

H
HelloCrease 已提交
1244 1245 1246 1247
  | 名称       | 参数类型                    | 必填   | 说明                                       |
  | -------- | ----------------------- | ---- | ---------------------------------------- |
  | type     | string                  | 是    | 取消注册serviceDie回调,以便在devicemanager服务异常终止时通知应用程序。 |
  | callback | ()&nbsp;=&gt;&nbsp;void | 否    | 取消注册serviceDie的回调方法。                     |
Z
zengyawen 已提交
1248

@
@shi-xiaoxiao-iris 已提交
1249
**示例:**
@
@shi-xiaoxiao-iris 已提交
1250

1251
  ```js
1252 1253 1254 1255 1256
  try {
    dmInstance.off("serviceDie", () => {
      console.info("serviceDie off");
    });
  } catch (err) {
1257
    console.error("serviceDie errCode:" + err.code + ",errMessage:" + err.message);
1258
  }
1259
  ```