js-apis-device-manager.md 43.8 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 39
**参数**

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

@
@shi-xiaoxiao-iris 已提交
45 46 47 48 49 50 51 52 53 54 55
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型     | 说明                                                           |          
| ------- | --------------------------------------------------------------- |
| 401     | Input parameter error.                                          |
| 11600101| Failed to execute the function.                                 |

**示例**

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 223

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


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

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

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

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

@
@shi-xiaoxiao-iris 已提交
232 233 234 235 236 237 238 239 240 241
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型     | 说明                                                           |          
| ------- | --------------------------------------------------------------- |
| 11600101| Failed to execute the function.                                 |

**示例**

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


Z
zengyawen 已提交
251
### getTrustedDeviceListSync
Z
zengyawen 已提交
252

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

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

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

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

- 示例:
265
  ```js
266 267 268
  try {
    var deviceInfoList = dmInstance.getTrustedDeviceListSync();
  } catch (err) {
269
    console.error("getTrustedDeviceListSync errCode:" + err.code + ",errMessage:" + err.message);
270
  }
Z
zengyawen 已提交
271
  ```
Z
zengyawen 已提交
272 273


274 275 276 277 278 279 280 281
### getTrustedDeviceList<sup>8+</sup>

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

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

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

@
@shi-xiaoxiao-iris 已提交
282 283
**参数**

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

@
@shi-xiaoxiao-iris 已提交
288 289 290 291 292 293 294 295 296 297 298
  **错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型     | 说明                                                           |          
| ------- | --------------------------------------------------------------- |
| 401     | Input parameter error.                                          |
| 11600101| Failed to execute the function.                                 |

**示例**

299
  ```js
300 301 302
  try {
    dmInstance.getTrustedDeviceList((err, data) => {
      if (err) {
303
        console.error("getTrustedDeviceList errCode:" + err.code + ",errMessage:" + err.message);
304 305
        return;
      }
306
      console.log('get trusted device info: ' + JSON.stringify(data));
307
    });
308
  } catch (err) {
309
    console.error("getTrustedDeviceList errCode:" + err.code + ",errMessage:" + err.message);
310
  }
311 312 313 314 315 316 317 318 319 320 321
  ```

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

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

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

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

- 返回值:
H
HelloCrease 已提交
322 323
  | 类型                                       | 说明                    |
  | ---------------------------------------- | --------------------- |
324 325 326 327
  | Promise&lt;Array&lt;[DeviceInfo](#deviceinfo)&gt;&gt; | Promise实例,用于获取异步返回结果。 |

- 示例:
  ```js
328 329 330
  dmInstance.getTrustedDeviceList().then((data) => {
    console.log('get trusted device info: ' + JSON.stringify(data));
    }).catch((err) => {
331
      console.error("getTrustedDeviceList errCode:" + err.code + ",errMessage:" + err.message);
332
  });
333 334 335 336 337 338 339 340 341 342 343
  ```

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

getLocalDeviceInfoSync(): [DeviceInfo](#deviceinfo)

同步获取本地设备信息。

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

- 返回值:
H
HelloCrease 已提交
344 345
  | 名称                                     | 说明        |
  | -------------------------------------- | --------- |
346
  | Array&lt;[DeviceInfo](#deviceinfo)&gt; | 返回本地设备列表。 |
347 348 349

- 示例:
  ```js
350 351 352
  try {
    var deviceInfo = dmInstance.getLocalDeviceInfoSync();
  } catch (err) {
353
    console.error("getLocalDeviceInfoSync errCode:" + err.code + ",errMessage:" + err.message);
354
  }
355 356 357 358 359 360 361 362 363 364 365
  ```


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

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

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

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

@
@shi-xiaoxiao-iris 已提交
366 367
**参数**

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

@
@shi-xiaoxiao-iris 已提交
372 373 374 375 376 377 378 379 380 381 382
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型     | 说明                                                           |          
| ------- | --------------------------------------------------------------- |
| 401     | Input parameter error.                                          |
| 11600101| Failed to execute the function.                                 |

**示例**

383
  ```js
384
  try {
385
    dmInstance.getLocalDeviceInfo((err, data) => {
386
    if (err) {
387
      console.error("getLocalDeviceInfo errCode:" + err.code + ",errMessage:" + err.message);
388
      return;
389
    }
390 391 392
      console.log('get local device info: ' + JSON.stringify(data));
    });
  } catch (err) {
393
    console.error("getLocalDeviceInfo errCode:" + err.code + ",errMessage:" + err.message);
394
  }
395 396 397 398 399 400 401 402 403 404 405
  ```

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

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

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

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

- 返回值:
H
HelloCrease 已提交
406 407
  | 类型                                       | 说明                    |
  | ---------------------------------------- | --------------------- |
408 409 410 411
  | Promise&lt;[DeviceInfo](#deviceinfo)&gt; | Promise实例,用于获取异步返回结果。 |

- 示例:
  ```js
412 413 414
  dmInstance.getLocalDeviceInfo().then((data) => {
    console.log('get local device info: ' + JSON.stringify(data));
  }).catch((err) => {
415
    console.error("getLocalDeviceInfo errCode:" + err.code + ",errMessage:" + err.message);
416
  });
417 418
  ```

S
summer8999 已提交
419
### startDeviceDiscovery8+</sup>
420 421 422 423 424 425 426

startDeviceDiscovery(subscribeInfo: SubscribeInfo): void

发现周边设备。

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

@
@shi-xiaoxiao-iris 已提交
427 428
**参数**

H
HelloCrease 已提交
429 430 431
  | 名称            | 参数类型                            | 必填   | 说明    |
  | ------------- | ------------------------------- | ---- | ----- |
  | subscribeInfo | [SubscribeInfo](#subscribeinfo) | 是    | 发现信息。 |
432

@
@shi-xiaoxiao-iris 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型      | 说明                                                           |          
| -------- | --------------------------------------------------------------- |
| 401      | Input parameter error.                                          |
| 201      | Permission verify failed.                                       |
| 11600101 | Failed to execute the function.                                 |
| 11600104 | Discovery invalid.                                              |

**示例**

446
  ```js
447
  // 生成发现标识,随机数确保每次调用发现接口的标识不一致
448 449 450
  var subscribeId = Math.floor(Math.random() * 10000 + 1000);
  var subscribeInfo = {
      "subscribeId": subscribeId,
451 452 453
      "mode": 0xAA, // 主动模式
      "medium": 0,  // 自动发现类型,同时支持多种发现类型
      "freq": 2,    // 高频率
454 455 456 457
      "isSameAccount": false,
      "isWakeRemote": false,
      "capability": 1
  };
458 459 460
  try {
    dmInstance.startDeviceDiscovery(subscribeInfo); // 当有设备发现时,通过deviceFound回调通知给应用程序
  } catch (err) {
461
    console.error("startDeviceDiscovery errCode:" + err.code + ",errMessage:" + err.message);
462
  }
463 464
  ```

S
summer8999 已提交
465 466
### startDeviceDiscovery9+</sup>

S
summer8999 已提交
467
startDeviceDiscovery(subscribeInfo: SubscribeInfo, filterOptions?: string): void
S
summer8999 已提交
468 469 470 471 472

发现周边设备。

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

@
@shi-xiaoxiao-iris 已提交
473 474
**参数**

S
summer8999 已提交
475 476 477
  | 名称            | 参数类型                            | 必填   | 说明    |
  | ------------- | ------------------------------- | ---- | ----- |
  | subscribeInfo | [SubscribeInfo](#subscribeinfo) | 是   | 发现信息。 |
S
summer8999 已提交
478
  | filterOptions | string        | 否   | 发现设备过滤信息。|
S
summer8999 已提交
479

@
@shi-xiaoxiao-iris 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型      | 说明                                                           |          
| -------- | --------------------------------------------------------------- |
| 401      | Input parameter error.                                          |
| 201      | Permission verify failed.                                       |
| 11600101 | Failed to execute the function.                                 |
| 11600104 | Discovery invalid.                                              |

**示例**

S
summer8999 已提交
493
  ```js
494
  // 生成发现标识,随机数确保每次调用发现接口的标识不一致
S
summer8999 已提交
495 496 497
  var subscribeId = Math.floor(Math.random() * 10000 + 1000);
  var subscribeInfo = {
      "subscribeId": subscribeId,
498 499 500
      "mode": 0xAA, // 主动模式
      "medium": 0,  // 自动发现类型,同时支持多种发现类型
      "freq": 2,    // 高频率
S
summer8999 已提交
501 502 503 504 505
      "isSameAccount": false,
      "isWakeRemote": false,
      "capability": 1
  };
  var filterOptions = {
S
summer8999 已提交
506
    "filter_op": "OR", // 可选, 默认"OR"
S
summer8999 已提交
507 508 509
    "filters": [
        {
            "type": "range",
S
summer8999 已提交
510
            "value": 50 // 需过滤发现设备的距离,单位(cm)
S
summer8999 已提交
511 512
        }
    ]
S
summer8999 已提交
513
  };
514 515 516
  try {
    dmInstance.startDeviceDiscovery(subscribeInfo, JSON.stringify(filterOptions)); // 当有设备发现时,通过deviceFound回调通知给应用程序
  } catch (err) {
517
    console.error("startDeviceDiscovery errCode:" + err.code + ",errMessage:" + err.message);
518
  }
S
summer8999 已提交
519 520
  ```
  
521 522 523 524 525 526 527 528
### stopDeviceDiscovery

stopDeviceDiscovery(subscribeId: number): void

停止发现周边设备。

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

@
@shi-xiaoxiao-iris 已提交
529 530
**参数**

H
HelloCrease 已提交
531 532 533
  | 名称          | 参数类型   | 必填   | 说明    |
  | ----------- | ------ | ---- | ----- |
  | subscribeId | number | 是    | 发现标识。 |
534

@
@shi-xiaoxiao-iris 已提交
535 536 537 538 539 540 541 542 543 544 545
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型     | 说明                                                           |          
| ------- | --------------------------------------------------------------- |
| 401     | Input parameter error.                                          |
| 11600101| Failed to execute the function.                                 |

**示例**

546
  ```js
547
  // 入参需要和startDeviceDiscovery接口传入的subscribeId配对使用
548 549 550
  try {
    dmInstance.stopDeviceDiscovery(subscribeId);
  } catch (err) {
551
    console.error("stopDeviceDiscovery errCode:" + err.code + ",errMessage:" + err.message);
552
  }
553 554
  ```

S
summer8999 已提交
555 556
### publishDeviceDiscovery9+</sup>

S
summer8999 已提交
557
publishDeviceDiscovery(publishInfo: PublishInfo): void
S
summer8999 已提交
558

S
summer8999 已提交
559
发布设备发现。
S
summer8999 已提交
560 561 562

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

@
@shi-xiaoxiao-iris 已提交
563 564
**参数**

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

@
@shi-xiaoxiao-iris 已提交
569 570 571 572 573 574 575 576 577 578 579
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型     | 说明                                                           |          
| ------- | --------------------------------------------------------------- |
| 401     | Input parameter error.                                          |
| 11600101| Failed to execute the function.                                 |

**示例**

S
summer8999 已提交
580
  ```js
581
  // 生成发布标识,随机数确保每次调用发布接口的标识不一致
S
summer8999 已提交
582
  var publishId = Math.floor(Math.random() * 10000 + 1000);
S
summer8999 已提交
583
  var publishInfo = {
S
summer8999 已提交
584
      "publishId": publishId,
585 586 587
      "mode": 0xAA, // 主动模式
      "freq": 2,    // 高频率
      "ranging": 1  // 支持发现时测距
S
summer8999 已提交
588
  };
589 590 591
  try {
    dmInstance.publishDeviceDiscovery(publishInfo); // 当有发布结果时,通过回调通知给应用程序
  } catch (err) {
592
    console.error("publishDeviceDiscovery errCode:" + err.code + ",errMessage:" + err.message);
593
  }
S
summer8999 已提交
594 595
  ```
  
S
summer8999 已提交
596
### unPublishDeviceDiscovery9+</sup>
S
summer8999 已提交
597

S
summer8999 已提交
598
unPublishDeviceDiscovery(publishId: number): void
S
summer8999 已提交
599 600 601 602 603

停止发布设备发现。

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

@
@shi-xiaoxiao-iris 已提交
604 605
**参数**

S
summer8999 已提交
606 607 608
  | 名称        | 参数类型 | 必填 | 说明  |
  | ----------- | -------- | ---- | ----- |
  | publishId   | number   | 是   | 发布标识。 |
S
summer8999 已提交
609

@
@shi-xiaoxiao-iris 已提交
610 611 612 613 614 615 616 617 618 619 620
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型     | 说明                                                           |          
| ------- | --------------------------------------------------------------- |
| 401     | Input parameter error.                                          |
| 11600101| Failed to execute the function.                                 |

**示例**

S
summer8999 已提交
621
  ```js
622
  // 入参需要和publishDeviceDiscovery接口传入的publishId配对使用
623
  try {
624
    dmInstance.unPublishDeviceDiscovery(publishId);
625
  } catch (err) {
626
    console.error("unPublishDeviceDiscovery errCode:" + err.code + ",errMessage:" + err.message);
627
  }
S
summer8999 已提交
628 629
  ```

630 631 632 633 634 635 636 637
### authenticateDevice

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

认证设备。

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

@
@shi-xiaoxiao-iris 已提交
638 639
**参数**

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

@
@shi-xiaoxiao-iris 已提交
646 647 648 649 650 651 652 653 654 655 656
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型     | 说明                                                           |          
| ------- | --------------------------------------------------------------- |
| 401     | Input parameter error.                                          |
| 11600101| Failed to execute the function.                                 |

**示例**

657
  ```js
658
  // 认证的设备信息,可以从发现的结果中获取
659 660 661 662 663 664
  var deviceInfo ={
      "deviceId": "XXXXXXXX",
      "deviceName": "",
      deviceType: 0x0E
  };
  let authParam = {
665
      "authType": 1, // 认证类型: 1 - 无账号PIN码认证
666 667
      "extraInfo": {} 
  }
668 669
  try {
    dmInstance.authenticateDevice(deviceInfo, authParam, (err, data) => {
670
      if (err) {
671
          console.error("authenticateDevice errCode:" + err.code + ",errMessage:" + err.message);
672 673 674 675
          return;
      }
      console.info(TAG + "authenticateDevice result:" + JSON.stringify(data));
      token = data.pinToken;
676 677
    });
  } catch (err) {
678
    console.error("authenticateDevice errCode:" + err.code + ",errMessage:" + err.message);
679
  }
680 681 682 683 684 685 686 687 688 689
  ```

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

unAuthenticateDevice(deviceInfo: DeviceInfo): void

解除认证设备。

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

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

H
HelloCrease 已提交
692 693 694
  | 名称         | 参数类型                      | 必填   | 说明    |
  | ---------- | ------------------------- | ---- | ----- |
  | deviceInfo | [DeviceInfo](#deviceinfo) | 是    | 设备信息。 |
695

@
@shi-xiaoxiao-iris 已提交
696 697 698 699 700 701 702 703 704 705 706
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型     | 说明                                                           |          
| ------- | --------------------------------------------------------------- |
| 401     | Input parameter error.                                          |
| 11600101| Failed to execute the function.                                 |

**示例**

707
  ```js
708 709 710
  try {
    dmInstance.unAuthenticateDevice(deviceInfo);
  } catch (err) {
711
    console.error("unAuthenticateDevice errCode:" + err.code + ",errMessage:" + err.message);
712
  }
713 714 715 716 717 718 719 720 721 722 723
  ```


### verifyAuthInfo

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

验证认证信息。

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

@
@shi-xiaoxiao-iris 已提交
724 725
**参数**

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

@
@shi-xiaoxiao-iris 已提交
731 732 733 734 735 736 737 738 739 740 741
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型     | 说明                                                           |          
| ------- | --------------------------------------------------------------- |
| 401     | Input parameter error.                                          |
| 11600101| Failed to execute the function.                                 |

**示例**

742 743 744 745 746 747
  ```js
  let authInfo = {
    "authType": 1,
    "token": xxxxxx,
    "extraInfo": {}
  }
748 749
  try {
    dmInstance.verifyAuthInfo(authInfo, (err, data) => {
750
    if (err) {
751
        console.error("verifyAuthInfo errCode:" + err.code + ",errMessage:" + err.message);
752 753 754
        return;
    }
    console.info(TAG + "verifyAuthInfo result:" + JSON.stringify(data));
755 756
    });
  } catch (err) {
757
    console.error("verifyAuthInfo errCode:" + err.code + ",errMessage:" + err.message);
758
  }
759 760 761
  ```


Z
zengyawen 已提交
762
### on('deviceStateChange')
Z
zengyawen 已提交
763

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

Z
zengyawen 已提交
766 767
注册设备状态回调。

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

@
@shi-xiaoxiao-iris 已提交
770 771
**参数**

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

@
@shi-xiaoxiao-iris 已提交
777 778 779 780 781 782 783 784 785 786 787
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型     | 说明                                                           |          
| ------- | --------------------------------------------------------------- |
| 401     | Input parameter error.                                          |
| 11600101| Failed to execute the function.                                 |

**示例**

788
  ```js
789 790 791 792 793
  try {
    dmInstance.on('deviceStateChange', (data) => {
      console.info("deviceStateChange on:" + JSON.stringify(data));
    });
  } catch (err) {
794
    console.error("deviceStateChange errCode:" + err.code + ",errMessage:" + err.message);
795
  }
Z
zengyawen 已提交
796
  ```
Z
zengyawen 已提交
797 798


Z
zengyawen 已提交
799
### off('deviceStateChange')
Z
zengyawen 已提交
800

801
off(type: 'deviceStateChange', callback?: Callback&lt;{ action: DeviceStateChangeAction, device: DeviceInfo }&gt;): void
Z
zengyawen 已提交
802 803 804

取消注册设备状态回调。

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

@
@shi-xiaoxiao-iris 已提交
807 808
**参数**

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

@
@shi-xiaoxiao-iris 已提交
814 815 816 817 818 819 820 821 822 823 824
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型     | 说明                                                           |          
| ------- | --------------------------------------------------------------- |
| 401     | Input parameter error.                                          |
| 11600101| Failed to execute the function.                                 |

**示例**

825
  ```js
826 827 828 829 830
  try {
    dmInstance.off('deviceStateChange', (data) => {
      console.info('deviceStateChange' + JSON.stringify(data));
    });
  } catch (err) {
831
    console.error("deviceStateChange errCode:" + err.code + ",errMessage:" + err.message);
832
  }
Z
zengyawen 已提交
833 834 835
  ```


836 837 838 839 840 841 842 843
### on('deviceFound')

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

注册发现设备回调监听。

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

@
@shi-xiaoxiao-iris 已提交
844 845
**参数**

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

@
@shi-xiaoxiao-iris 已提交
851 852 853 854 855 856 857 858 859 860 861
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型     | 说明                                                           |          
| ------- | --------------------------------------------------------------- |
| 401     | Input parameter error.                                          |
| 11600101| Failed to execute the function.                                 |

**示例**

862
  ```js
863 864 865 866 867
  try {
    dmInstance.on('deviceFound', (data) => {
      console.info("deviceFound:" + JSON.stringify(data));
    });
  } catch (err) {
868
    console.error("deviceFound errCode:" + err.code + ",errMessage:" + err.message);
869
  }
870 871 872 873 874 875 876 877 878 879
  ```

### off('deviceFound')

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

取消注册设备发现回调。

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

@
@shi-xiaoxiao-iris 已提交
880 881
**参数**

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

@
@shi-xiaoxiao-iris 已提交
887 888 889 890 891 892 893 894 895 896 897
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型     | 说明                                                           |          
| ------- | --------------------------------------------------------------- |
| 401     | Input parameter error.                                          |
| 11600101| Failed to execute the function.                                 |

**示例**

898
  ```js
899 900 901 902 903
  try {
    dmInstance.off('deviceFound', (data) => {
      console.info('deviceFound' + JSON.stringify(data));
    });
  } catch (err) {
904
    console.error("deviceFound errCode:" + err.code + ",errMessage:" + err.message);
905
  }
906 907 908 909 910 911 912 913 914 915
  ```

### on('discoverFail')

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

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

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

@
@shi-xiaoxiao-iris 已提交
916 917
**参数**

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

@
@shi-xiaoxiao-iris 已提交
923 924 925 926 927 928 929 930 931 932 933
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型     | 说明                                                           |          
| ------- | --------------------------------------------------------------- |
| 401     | Input parameter error.                                          |
| 11600101| Failed to execute the function.                                 |

**示例**

934
  ```js
935 936
  try {
    dmInstance.on('discoverFail', (data) => {
937
        console.info("discoverFail on:" + JSON.stringify(data));
938
    });
939
  } catch (err) {
940
    console.error("discoverFail errCode:" + err.code + ",errMessage:" + err.message);
941
  }
942 943 944 945 946 947 948 949 950 951
  ```

### off('discoverFail')

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

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

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

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

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

@
@shi-xiaoxiao-iris 已提交
959 960 961 962 963 964 965 966 967 968 969
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型     | 说明                                                           |          
| ------- | --------------------------------------------------------------- |
| 401     | Input parameter error.                                          |
| 11600101| Failed to execute the function.                                 |

**示例**

970
  ```js
971 972 973 974 975
  try {
    dmInstance.off('discoverFail', (data) => {
      console.info('discoverFail' + JSON.stringify(data));
    });
  } catch (err) {
976
    console.error("discoverFail errCode:" + err.code + ",errMessage:" + err.message);
977
  }
978 979
  ```

S
summer8999 已提交
980
### on('publishSuccess')9+</sup>
S
summer8999 已提交
981 982 983 984 985 986 987

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

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

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

@
@shi-xiaoxiao-iris 已提交
988 989
**参数**

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

@
@shi-xiaoxiao-iris 已提交
995 996 997 998 999 1000 1001 1002 1003 1004 1005
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型     | 说明                                                           |          
| ------- | --------------------------------------------------------------- |
| 401     | Input parameter error.                                          |
| 11600101| Failed to execute the function.                                 |

**示例**

S
summer8999 已提交
1006
  ```js
1007 1008 1009 1010 1011
  try {
    dmInstance.on('publishSuccess', (data) => {
      console.info("publishSuccess:" + JSON.stringify(data));
    });
  } catch (err) {
1012
    console.error("publishSuccess errCode:" + err.code + ",errMessage:" + err.message);
1013
  }
S
summer8999 已提交
1014 1015
  ```

S
summer8999 已提交
1016
### off('publishSuccess')9+</sup>
S
summer8999 已提交
1017 1018 1019

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

S
summer8999 已提交
1020
取消注册设备发布成功回调。
S
summer8999 已提交
1021 1022 1023

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

@
@shi-xiaoxiao-iris 已提交
1024 1025
**参数**

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

@
@shi-xiaoxiao-iris 已提交
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型     | 说明                                                           |          
| ------- | --------------------------------------------------------------- |
| 401     | Input parameter error.                                          |
| 11600101| Failed to execute the function.                                 |

**示例**

S
summer8999 已提交
1042
  ```js
1043 1044 1045 1046 1047
  try {
    dmInstance.off('publishSuccess', (data) => {
      console.info('publishSuccess' + JSON.stringify(data));
    });
  } catch (err) {
1048
    console.error("publishSuccess errCode:" + err.code + ",errMessage:" + err.message);
1049
  }
S
summer8999 已提交
1050 1051
  ```

S
summer8999 已提交
1052
### on('publishFail')9+</sup>
S
summer8999 已提交
1053 1054 1055 1056 1057 1058 1059

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

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

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

@
@shi-xiaoxiao-iris 已提交
1060 1061
**参数**

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

@
@shi-xiaoxiao-iris 已提交
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型     | 说明                                                           |          
| ------- | --------------------------------------------------------------- |
| 401     | Input parameter error.                                          |
| 11600101| Failed to execute the function.                                 |

**示例**

S
summer8999 已提交
1078
  ```js
1079 1080
  try {
    dmInstance.on('publishFail', (data) => {
1081
      console.info("publishFail on:" + JSON.stringify(data));
1082 1083
    });
  } catch (err) {
1084
    console.error("publishFail errCode:" + err.code + ",errMessage:" + err.message);
1085
  }
S
summer8999 已提交
1086 1087
  ```

S
summer8999 已提交
1088
### off('publishFail')9+</sup>
S
summer8999 已提交
1089 1090 1091

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

S
summer8999 已提交
1092
取消注册设备发布失败回调。
S
summer8999 已提交
1093 1094 1095

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

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

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

@
@shi-xiaoxiao-iris 已提交
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型     | 说明                                                           |          
| ------- | --------------------------------------------------------------- |
| 401     | Input parameter error.                                          |
| 11600101| Failed to execute the function.                                 |

**示例**

S
summer8999 已提交
1114
  ```js
1115 1116 1117 1118 1119
  try {
    dmInstance.off('publishFail', (data) => {
      console.info('publishFail' + JSON.stringify(data));
    });
  } catch (err) {
1120
    console.error("publishFail errCode:" + err.code + ",errMessage:" + err.message);
1121
  }
S
summer8999 已提交
1122
  ```
1123

Z
zengyawen 已提交
1124 1125 1126
### on('serviceDie')

on(type: 'serviceDie', callback: () =&gt; void): void
Z
zengyawen 已提交
1127 1128 1129

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

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

@
@shi-xiaoxiao-iris 已提交
1132 1133
**参数**

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

@
@shi-xiaoxiao-iris 已提交
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型     | 说明                                                           |          
| ------- | --------------------------------------------------------------- |
| 401     | Input parameter error.                                          |
| 11600101| Failed to execute the function.                                 |

**示例**

1150
  ```js
1151 1152 1153 1154 1155
  try {
    dmInstance.on("serviceDie", () => {
      console.info("serviceDie on");
    });
  } catch (err) {
1156
    console.error("serviceDie errCode:" + err.code + ",errMessage:" + err.message);
1157
  }
Z
zengyawen 已提交
1158
  ```
Z
zengyawen 已提交
1159 1160


Z
zengyawen 已提交
1161 1162 1163 1164 1165
### off('serviceDie')

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

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

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

@
@shi-xiaoxiao-iris 已提交
1169 1170
**参数**

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

@
@shi-xiaoxiao-iris 已提交
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
**错误码**

以下的错误码的详细介绍请参见[分布式硬件子系统错误码](../errorcodes/errorcode-device-manager.md)

| 类型     | 说明                                                           |          
| ------- | --------------------------------------------------------------- |
| 401     | Input parameter error.                                          |
| 11600101| Failed to execute the function.                                 |

**示例**

1187
  ```js
1188 1189 1190 1191 1192
  try {
    dmInstance.off("serviceDie", () => {
      console.info("serviceDie off");
    });
  } catch (err) {
1193
    console.error("serviceDie errCode:" + err.code + ",errMessage:" + err.message);
1194
  }
1195
  ```