js-apis-device-manager.md 42.2 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

@
@shi-xiaoxiao-iris 已提交
77
| 名称                     | 类型                        | 必填   | 说明       |
H
HelloCrease 已提交
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

@
@shi-xiaoxiao-iris 已提交
91
| 名称           | 值  | 说明   |
H
HelloCrease 已提交
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

@
@shi-xiaoxiao-iris 已提交
108
| 名称      | 值  | 说明              |
H
HelloCrease 已提交
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

@
@shi-xiaoxiao-iris 已提交
121
| 名称            | 类型                                | 必填   | 说明                |
H
HelloCrease 已提交
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

@
@shi-xiaoxiao-iris 已提交
138
| 名称                    | 值  | 说明    |
H
HelloCrease 已提交
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

@
@shi-xiaoxiao-iris 已提交
150
| 名称   | 值  | 说明        |
H
HelloCrease 已提交
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

@
@shi-xiaoxiao-iris 已提交
163
| 名称         | 值  | 说明    |
H
HelloCrease 已提交
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

@
@shi-xiaoxiao-iris 已提交
177
| 名称                        | 值  | 说明             |
H
HelloCrease 已提交
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

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

## AuthInfo

认证信息。

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

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

@
@shi-xiaoxiao-iris 已提交
206
## PublishInfo<sup>9+</sup>
S
summer8999 已提交
207 208 209 210 211

发布设备参数

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

@
@shi-xiaoxiao-iris 已提交
212
| 名称          | 类型                              | 必填   | 说明                |
S
summer8999 已提交
213 214 215 216 217
| ------------- | --------------------------------- | ---- | ----------------- |
| 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
**系统能力**:SystemCapability.DistributedHardware.DeviceManager

@
@shi-xiaoxiao-iris 已提交
257
**返回值:**
@
@shi-xiaoxiao-iris 已提交
258

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

@
@shi-xiaoxiao-iris 已提交
263
**错误码:**
@
@shi-xiaoxiao-iris 已提交
264

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

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

@
@shi-xiaoxiao-iris 已提交
271
**示例:**
@
@shi-xiaoxiao-iris 已提交
272

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

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

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

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

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

@
@shi-xiaoxiao-iris 已提交
289
**参数:**
@
@shi-xiaoxiao-iris 已提交
290

@
@shi-xiaoxiao-iris 已提交
291
  | 参数名       | 类型                                     | 必填   | 说明                    |
H
HelloCrease 已提交
292 293
  | -------- | ---------------------------------------- | ---- | --------------------- |
  | callback | AsyncCallback&lt;Array&lt;[DeviceInfo](#deviceinfo)&gt;&gt; | 是    | 获取所有可信设备列表的回调,返回设备信息。 |
294

@
@shi-xiaoxiao-iris 已提交
295
**错误码:**
@
@shi-xiaoxiao-iris 已提交
296

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

@
@shi-xiaoxiao-iris 已提交
299
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
300 301
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
@
@shi-xiaoxiao-iris 已提交
302

@
@shi-xiaoxiao-iris 已提交
303
**示例:**
@
@shi-xiaoxiao-iris 已提交
304

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

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

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

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

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

@
@shi-xiaoxiao-iris 已提交
327
**返回值:**
@
@shi-xiaoxiao-iris 已提交
328

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

@
@shi-xiaoxiao-iris 已提交
333
**错误码:**
@
@shi-xiaoxiao-iris 已提交
334

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

@
@shi-xiaoxiao-iris 已提交
337
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
338 339
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
@
@shi-xiaoxiao-iris 已提交
340

@
@shi-xiaoxiao-iris 已提交
341
**示例:**
@
@shi-xiaoxiao-iris 已提交
342

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

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

getLocalDeviceInfoSync(): [DeviceInfo](#deviceinfo)

同步获取本地设备信息。

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

@
@shi-xiaoxiao-iris 已提交
359
**返回值:**
@
@shi-xiaoxiao-iris 已提交
360

361 362 363
  | 名称                      | 说明              |
  | ------------------------- | ---------------- |
  | [DeviceInfo](#deviceinfo) | 返回本地设备列表。 |
364

@
@shi-xiaoxiao-iris 已提交
365
**错误码:**
@
@shi-xiaoxiao-iris 已提交
366

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

@
@shi-xiaoxiao-iris 已提交
369
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
370 371
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
@
@shi-xiaoxiao-iris 已提交
372

@
@shi-xiaoxiao-iris 已提交
373
**示例:**
@
@shi-xiaoxiao-iris 已提交
374

375
  ```js
376 377 378
  try {
    var deviceInfo = dmInstance.getLocalDeviceInfoSync();
  } catch (err) {
379
    console.error("getLocalDeviceInfoSync errCode:" + err.code + ",errMessage:" + err.message);
380
  }
381 382 383 384 385 386 387 388 389 390
  ```

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

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

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

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

@
@shi-xiaoxiao-iris 已提交
391
**参数:**
@
@shi-xiaoxiao-iris 已提交
392

@
@shi-xiaoxiao-iris 已提交
393
  | 参数名       | 类型                                     | 必填   | 说明        |
H
HelloCrease 已提交
394 395
  | -------- | ---------------------------------------- | ---- | --------- |
  | callback | AsyncCallback&lt;[DeviceInfo](#deviceinfo)&gt; | 是    | 获取本地设备信息。 |
396

@
@shi-xiaoxiao-iris 已提交
397
**错误码:**
@
@shi-xiaoxiao-iris 已提交
398

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

@
@shi-xiaoxiao-iris 已提交
401
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
402 403
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
@
@shi-xiaoxiao-iris 已提交
404

@
@shi-xiaoxiao-iris 已提交
405
**示例:**
@
@shi-xiaoxiao-iris 已提交
406

407
  ```js
408
  try {
409
    dmInstance.getLocalDeviceInfo((err, data) => {
410
    if (err) {
411
      console.error("getLocalDeviceInfo errCode:" + err.code + ",errMessage:" + err.message);
412
      return;
413
    }
414 415 416
      console.log('get local device info: ' + JSON.stringify(data));
    });
  } catch (err) {
417
    console.error("getLocalDeviceInfo errCode:" + err.code + ",errMessage:" + err.message);
418
  }
419 420 421 422 423 424 425 426 427 428
  ```

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

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

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

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

@
@shi-xiaoxiao-iris 已提交
429 430
**返回值:**

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

@
@shi-xiaoxiao-iris 已提交
435
**错误码:**
@
@shi-xiaoxiao-iris 已提交
436

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

@
@shi-xiaoxiao-iris 已提交
439
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
440 441 442
| ------- | --------------------------------------------------------------- |
| 11600101| Failed to execute the function.                                 |

@
@shi-xiaoxiao-iris 已提交
443
**示例:**
@
@shi-xiaoxiao-iris 已提交
444

445
  ```js
446 447 448
  dmInstance.getLocalDeviceInfo().then((data) => {
    console.log('get local device info: ' + JSON.stringify(data));
  }).catch((err) => {
449
    console.error("getLocalDeviceInfo errCode:" + err.code + ",errMessage:" + err.message);
450
  });
451 452
  ```

@
@shi-xiaoxiao-iris 已提交
453
### startDeviceDiscovery<sup>8+</sup>
454 455 456 457 458 459 460

startDeviceDiscovery(subscribeInfo: SubscribeInfo): void

发现周边设备。

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

@
@shi-xiaoxiao-iris 已提交
461
**参数:**
@
@shi-xiaoxiao-iris 已提交
462

@
@shi-xiaoxiao-iris 已提交
463
  | 参数名            | 类型                       | 必填 | 说明    |
H
HelloCrease 已提交
464
  | ------------- | ------------------------------- | ---- | ----- |
465
  | subscribeInfo | [SubscribeInfo](#subscribeinfo) | 是   | 发现信息。|
466

@
@shi-xiaoxiao-iris 已提交
467
**错误码:**
@
@shi-xiaoxiao-iris 已提交
468

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

@
@shi-xiaoxiao-iris 已提交
471
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
472 473 474 475
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
| 11600104 | Discovery invalid.                                              |

@
@shi-xiaoxiao-iris 已提交
476
**示例:**
@
@shi-xiaoxiao-iris 已提交
477

478
  ```js
479
  // 生成发现标识,随机数确保每次调用发现接口的标识不一致
480 481 482
  var subscribeId = Math.floor(Math.random() * 10000 + 1000);
  var subscribeInfo = {
      "subscribeId": subscribeId,
483 484 485
      "mode": 0xAA, // 主动模式
      "medium": 0,  // 自动发现类型,同时支持多种发现类型
      "freq": 2,    // 高频率
486 487 488 489
      "isSameAccount": false,
      "isWakeRemote": false,
      "capability": 1
  };
490 491 492
  try {
    dmInstance.startDeviceDiscovery(subscribeInfo); // 当有设备发现时,通过deviceFound回调通知给应用程序
  } catch (err) {
493
    console.error("startDeviceDiscovery errCode:" + err.code + ",errMessage:" + err.message);
494
  }
495 496
  ```

@
@shi-xiaoxiao-iris 已提交
497
### startDeviceDiscovery<sup>9+</sup>
S
summer8999 已提交
498

S
summer8999 已提交
499
startDeviceDiscovery(subscribeInfo: SubscribeInfo, filterOptions?: string): void
S
summer8999 已提交
500 501 502 503 504

发现周边设备。

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

@
@shi-xiaoxiao-iris 已提交
505
**参数:**
@
@shi-xiaoxiao-iris 已提交
506

@
@shi-xiaoxiao-iris 已提交
507
  | 参数名            | 类型                       | 必填   | 说明    |
508
  | ------------- | ------------------------------- | ---- | -----  |
S
summer8999 已提交
509
  | subscribeInfo | [SubscribeInfo](#subscribeinfo) | 是   | 发现信息。 |
510
  | filterOptions | string                          | 否   | 发现设备过滤信息。|
S
summer8999 已提交
511

@
@shi-xiaoxiao-iris 已提交
512
**错误码:**
@
@shi-xiaoxiao-iris 已提交
513

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

@
@shi-xiaoxiao-iris 已提交
516
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
517 518 519 520
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
| 11600104 | Discovery invalid.                                              |

@
@shi-xiaoxiao-iris 已提交
521
**示例:**
@
@shi-xiaoxiao-iris 已提交
522

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

551 552 553 554 555 556 557 558
### stopDeviceDiscovery

stopDeviceDiscovery(subscribeId: number): void

停止发现周边设备。

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

@
@shi-xiaoxiao-iris 已提交
559
**参数:**
@
@shi-xiaoxiao-iris 已提交
560

@
@shi-xiaoxiao-iris 已提交
561
  | 参数名          | 类型   | 必填   | 说明    |
H
HelloCrease 已提交
562 563
  | ----------- | ------ | ---- | ----- |
  | subscribeId | number | 是    | 发现标识。 |
564

@
@shi-xiaoxiao-iris 已提交
565
**错误码:**
@
@shi-xiaoxiao-iris 已提交
566

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

@
@shi-xiaoxiao-iris 已提交
569
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
570 571
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
@
@shi-xiaoxiao-iris 已提交
572

@
@shi-xiaoxiao-iris 已提交
573
**示例:**
@
@shi-xiaoxiao-iris 已提交
574

575
  ```js
576
  // 入参需要和startDeviceDiscovery接口传入的subscribeId配对使用
577 578 579
  try {
    dmInstance.stopDeviceDiscovery(subscribeId);
  } catch (err) {
580
    console.error("stopDeviceDiscovery errCode:" + err.code + ",errMessage:" + err.message);
581
  }
582 583
  ```

@
@shi-xiaoxiao-iris 已提交
584
### publishDeviceDiscovery<sup>9+</sup>
S
summer8999 已提交
585

S
summer8999 已提交
586
publishDeviceDiscovery(publishInfo: PublishInfo): void
S
summer8999 已提交
587

S
summer8999 已提交
588
发布设备发现。
S
summer8999 已提交
589 590 591

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

@
@shi-xiaoxiao-iris 已提交
592
**参数:**
@
@shi-xiaoxiao-iris 已提交
593

@
@shi-xiaoxiao-iris 已提交
594
  | 参数名          | 类型                        | 必填 | 说明    |
S
summer8999 已提交
595 596 597
  | ------------- | ------------------------------- | ---- | ----- |
  | publishInfo   | [PublishInfo](#publishinfo)     | 是   | 发布设备发现信息。 |

@
@shi-xiaoxiao-iris 已提交
598
**错误码:**
@
@shi-xiaoxiao-iris 已提交
599

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

@
@shi-xiaoxiao-iris 已提交
602
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
603 604 605
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
| 11600105 | Publish invalid.                                                |
@
@shi-xiaoxiao-iris 已提交
606

@
@shi-xiaoxiao-iris 已提交
607
**示例:**
@
@shi-xiaoxiao-iris 已提交
608

S
summer8999 已提交
609
  ```js
610
  // 生成发布标识,随机数确保每次调用发布接口的标识不一致
S
summer8999 已提交
611
  var publishId = Math.floor(Math.random() * 10000 + 1000);
S
summer8999 已提交
612
  var publishInfo = {
S
summer8999 已提交
613
      "publishId": publishId,
614 615 616
      "mode": 0xAA, // 主动模式
      "freq": 2,    // 高频率
      "ranging": 1  // 支持发现时测距
S
summer8999 已提交
617
  };
618 619 620
  try {
    dmInstance.publishDeviceDiscovery(publishInfo); // 当有发布结果时,通过回调通知给应用程序
  } catch (err) {
621
    console.error("publishDeviceDiscovery errCode:" + err.code + ",errMessage:" + err.message);
622
  }
S
summer8999 已提交
623 624
  ```
  
@
@shi-xiaoxiao-iris 已提交
625
### unPublishDeviceDiscovery<sup>9+</sup>
S
summer8999 已提交
626

S
summer8999 已提交
627
unPublishDeviceDiscovery(publishId: number): void
S
summer8999 已提交
628 629 630 631 632

停止发布设备发现。

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

@
@shi-xiaoxiao-iris 已提交
633
**参数:**
@
@shi-xiaoxiao-iris 已提交
634

@
@shi-xiaoxiao-iris 已提交
635
  | 参数名        | 类型 | 必填 | 说明  |
S
summer8999 已提交
636 637
  | ----------- | -------- | ---- | ----- |
  | publishId   | number   | 是   | 发布标识。 |
S
summer8999 已提交
638

@
@shi-xiaoxiao-iris 已提交
639
**错误码:**
@
@shi-xiaoxiao-iris 已提交
640

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

@
@shi-xiaoxiao-iris 已提交
643
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
644 645
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
@
@shi-xiaoxiao-iris 已提交
646

@
@shi-xiaoxiao-iris 已提交
647
**示例:**
@
@shi-xiaoxiao-iris 已提交
648

S
summer8999 已提交
649
  ```js
650
  // 入参需要和publishDeviceDiscovery接口传入的publishId配对使用
651
  try {
652
    dmInstance.unPublishDeviceDiscovery(publishId);
653
  } catch (err) {
654
    console.error("unPublishDeviceDiscovery errCode:" + err.code + ",errMessage:" + err.message);
655
  }
S
summer8999 已提交
656 657
  ```

658 659
### authenticateDevice

660
authenticateDevice(deviceInfo: DeviceInfo, authParam: AuthParam, callback: AsyncCallback&lt;{&nbsp;deviceId: string, pinToken ?: number&nbsp;}&gt;): void
661 662 663 664 665

认证设备。

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

@
@shi-xiaoxiao-iris 已提交
666
**参数:**
@
@shi-xiaoxiao-iris 已提交
667

@
@shi-xiaoxiao-iris 已提交
668
  | 参数名         | 类型                                     | 必填   | 说明      |
H
HelloCrease 已提交
669 670 671
  | ---------- | ---------------------------------------- | ---- | ------- |
  | deviceInfo | [DeviceInfo](#deviceinfo)                | 是    | 设备信息。   |
  | authParam  | [AuthParam](#authparam)                  | 是    | 认证参数。   |
672
  | callback   | AsyncCallback&lt;{&nbsp;deviceId: string, pinToken ?: number&nbsp;}&gt; | 是    | 认证结果回调。 |
673

@
@shi-xiaoxiao-iris 已提交
674
**错误码:**
@
@shi-xiaoxiao-iris 已提交
675

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

@
@shi-xiaoxiao-iris 已提交
678
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
679 680 681
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
| 11600103 | Authentication invalid.                                         |
@
@shi-xiaoxiao-iris 已提交
682

@
@shi-xiaoxiao-iris 已提交
683
**示例:**
@
@shi-xiaoxiao-iris 已提交
684

685
  ```js
686
  // 认证的设备信息,可以从发现的结果中获取
687 688 689 690 691 692
  var deviceInfo ={
      "deviceId": "XXXXXXXX",
      "deviceName": "",
      deviceType: 0x0E
  };
  let authParam = {
693
      "authType": 1, // 认证类型: 1 - 无账号PIN码认证
694 695
      "extraInfo": {} 
  }
696 697
  try {
    dmInstance.authenticateDevice(deviceInfo, authParam, (err, data) => {
698
      if (err) {
699
          console.error("authenticateDevice errCode:" + err.code + ",errMessage:" + err.message);
700 701
          return;
      }
@
@shi-xiaoxiao-iris 已提交
702 703
      console.info("authenticateDevice result:" + JSON.stringify(data));
      let token = data.pinToken;
704 705
    });
  } catch (err) {
706
    console.error("authenticateDevice errCode:" + err.code + ",errMessage:" + err.message);
707
  }
708 709 710 711 712 713 714 715 716 717
  ```

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

unAuthenticateDevice(deviceInfo: DeviceInfo): void

解除认证设备。

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

@
@shi-xiaoxiao-iris 已提交
718
**参数:**
@
@shi-xiaoxiao-iris 已提交
719

@
@shi-xiaoxiao-iris 已提交
720
  | 参数名         | 类型                      | 必填   | 说明    |
H
HelloCrease 已提交
721 722
  | ---------- | ------------------------- | ---- | ----- |
  | deviceInfo | [DeviceInfo](#deviceinfo) | 是    | 设备信息。 |
723

@
@shi-xiaoxiao-iris 已提交
724
**错误码:**
@
@shi-xiaoxiao-iris 已提交
725

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

@
@shi-xiaoxiao-iris 已提交
728
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
729 730
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
@
@shi-xiaoxiao-iris 已提交
731

@
@shi-xiaoxiao-iris 已提交
732
**示例:**
@
@shi-xiaoxiao-iris 已提交
733

734
  ```js
735 736 737
  try {
    dmInstance.unAuthenticateDevice(deviceInfo);
  } catch (err) {
738
    console.error("unAuthenticateDevice errCode:" + err.code + ",errMessage:" + err.message);
739
  }
740 741 742 743
  ```

### verifyAuthInfo

744
verifyAuthInfo(authInfo: AuthInfo, callback: AsyncCallback&lt;{&nbsp;deviceId: string, level: number&nbsp;}&gt;): void
745 746 747 748 749

验证认证信息。

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

@
@shi-xiaoxiao-iris 已提交
750
**参数:**
@
@shi-xiaoxiao-iris 已提交
751

@
@shi-xiaoxiao-iris 已提交
752
  | 参数名       | 类型                                     | 必填   | 说明      |
H
HelloCrease 已提交
753 754
  | -------- | ---------------------------------------- | ---- | ------- |
  | authInfo | [AuthInfo](#authinfo)                    | 是    | 认证信息。   |
755
  | callback | AsyncCallback&lt;{&nbsp;deviceId: string, level: number&nbsp;}&gt; | 是    | 验证结果回调。 |
756

@
@shi-xiaoxiao-iris 已提交
757
**错误码:**
@
@shi-xiaoxiao-iris 已提交
758

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

@
@shi-xiaoxiao-iris 已提交
761
| 错误码ID | 错误信息                                                        |
@
@shi-xiaoxiao-iris 已提交
762 763
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
@
@shi-xiaoxiao-iris 已提交
764

@
@shi-xiaoxiao-iris 已提交
765
**示例:**
@
@shi-xiaoxiao-iris 已提交
766

767 768 769 770 771 772
  ```js
  let authInfo = {
    "authType": 1,
    "token": xxxxxx,
    "extraInfo": {}
  }
773 774
  try {
    dmInstance.verifyAuthInfo(authInfo, (err, data) => {
775
    if (err) {
776
        console.error("verifyAuthInfo errCode:" + err.code + ",errMessage:" + err.message);
777 778
        return;
    }
@
@shi-xiaoxiao-iris 已提交
779
    console.info("verifyAuthInfo result:" + JSON.stringify(data));
780 781
    });
  } catch (err) {
782
    console.error("verifyAuthInfo errCode:" + err.code + ",errMessage:" + err.message);
783
  }
784 785
  ```

@
@shi-xiaoxiao-iris 已提交
786
### setUserOperation<sup>9+</sup>
@
@shi-xiaoxiao-iris 已提交
787 788 789 790 791 792 793

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

设置用户ui操作行为。

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

@
@shi-xiaoxiao-iris 已提交
794
**参数:**
@
@shi-xiaoxiao-iris 已提交
795

@
@shi-xiaoxiao-iris 已提交
796
  | 参数名       | 类型            | 必填  | 说明                |
797 798 799
  | ------------- | --------------- | ---- | ------------------- |
  | operateAction | number          | 是    | 用户操作动作。       |
  | params        | string          | 是    | 表示用户的输入参数。 |
@
@shi-xiaoxiao-iris 已提交
800

@
@shi-xiaoxiao-iris 已提交
801
**示例:**
@
@shi-xiaoxiao-iris 已提交
802 803

  ```js
@
@shi-xiaoxiao-iris 已提交
804 805 806 807 808 809 810 811 812 813 814 815 816
 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 已提交
817 818 819
  }
  ```

@
@shi-xiaoxiao-iris 已提交
820
### on('uiStateChange')<sup>9+</sup>
@
@shi-xiaoxiao-iris 已提交
821

822
on(type: 'uiStateChange', callback: Callback&lt;{&nbsp; param: string&nbsp;}&gt;): void;
@
@shi-xiaoxiao-iris 已提交
823 824 825 826 827

ui状态变更回调。

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

@
@shi-xiaoxiao-iris 已提交
828
**参数:**
@
@shi-xiaoxiao-iris 已提交
829

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

@
@shi-xiaoxiao-iris 已提交
835
**示例:**
@
@shi-xiaoxiao-iris 已提交
836 837 838

  ```js
  try {
@
@shi-xiaoxiao-iris 已提交
839
    dmInstance.on('uiStateChange', (data) => {
@
@shi-xiaoxiao-iris 已提交
840 841 842 843 844 845 846 847 848 849 850 851 852
    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 已提交
853
### off('uiStateChange')<sup>9+</sup>
@
@shi-xiaoxiao-iris 已提交
854

855
off(type: 'uiStateChange', callback?: Callback&lt;{&nbsp; param: string&nbsp;}&gt;): void;
@
@shi-xiaoxiao-iris 已提交
856 857 858 859 860

取消ui状态变更回调

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

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

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

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

  ```js
  try {
@
@shi-xiaoxiao-iris 已提交
872
    dmInstance.off('uiStateChange');
@
@shi-xiaoxiao-iris 已提交
873 874 875 876 877
  } catch (err) {
    console.error("uiStateChange errCode:" + err.code + ",errMessage:" + err.message);
  }
  ```

Z
zengyawen 已提交
878
### on('deviceStateChange')
Z
zengyawen 已提交
879

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

Z
zengyawen 已提交
882 883
注册设备状态回调。

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

@
@shi-xiaoxiao-iris 已提交
886
**参数:**
@
@shi-xiaoxiao-iris 已提交
887

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

@
@shi-xiaoxiao-iris 已提交
893
**示例:**
@
@shi-xiaoxiao-iris 已提交
894

895
  ```js
896 897 898 899 900
  try {
    dmInstance.on('deviceStateChange', (data) => {
      console.info("deviceStateChange on:" + JSON.stringify(data));
    });
  } catch (err) {
901
    console.error("deviceStateChange errCode:" + err.code + ",errMessage:" + err.message);
902
  }
Z
zengyawen 已提交
903
  ```
Z
zengyawen 已提交
904

Z
zengyawen 已提交
905
### off('deviceStateChange')
Z
zengyawen 已提交
906

907
off(type: 'deviceStateChange', callback?: Callback&lt;{&nbsp; action: DeviceStateChangeAction, device: DeviceInfo &nbsp;}&gt;): void
Z
zengyawen 已提交
908 909 910

取消注册设备状态回调。

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

@
@shi-xiaoxiao-iris 已提交
913
**参数:**
@
@shi-xiaoxiao-iris 已提交
914

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

@
@shi-xiaoxiao-iris 已提交
920
**示例:**
@
@shi-xiaoxiao-iris 已提交
921

922
  ```js
923 924 925 926 927
  try {
    dmInstance.off('deviceStateChange', (data) => {
      console.info('deviceStateChange' + JSON.stringify(data));
    });
  } catch (err) {
928
    console.error("deviceStateChange errCode:" + err.code + ",errMessage:" + err.message);
929
  }
Z
zengyawen 已提交
930 931
  ```

932 933
### on('deviceFound')

934
on(type: 'deviceFound', callback: Callback&lt;{&nbsp; subscribeId: number, device: DeviceInfo &nbsp;}&gt;): void
935 936 937 938 939

注册发现设备回调监听。

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

@
@shi-xiaoxiao-iris 已提交
940
**参数:**
@
@shi-xiaoxiao-iris 已提交
941

@
@shi-xiaoxiao-iris 已提交
942
  | 参数名       | 类型                                     | 必填   | 说明                         |
H
HelloCrease 已提交
943 944
  | -------- | ---------------------------------------- | ---- | -------------------------- |
  | type     | string                                   | 是    | 注册设备发现回调,以便在发现周边设备时通知应用程序。 |
945
  | callback | Callback&lt;{&nbsp; subscribeId: number, device: [DeviceInfo](#deviceinfo) &nbsp;}&gt; | 是    | 注册设备发现的回调方法。               |
946

@
@shi-xiaoxiao-iris 已提交
947
**示例:**
@
@shi-xiaoxiao-iris 已提交
948

949
  ```js
950 951 952 953 954
  try {
    dmInstance.on('deviceFound', (data) => {
      console.info("deviceFound:" + JSON.stringify(data));
    });
  } catch (err) {
955
    console.error("deviceFound errCode:" + err.code + ",errMessage:" + err.message);
956
  }
957 958 959 960
  ```

### off('deviceFound')

961
off(type: 'deviceFound', callback?: Callback&lt;{&nbsp; subscribeId: number, device: DeviceInfo &nbsp;}&gt;): void
962 963 964 965 966

取消注册设备发现回调。

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

@
@shi-xiaoxiao-iris 已提交
967
**参数:**
@
@shi-xiaoxiao-iris 已提交
968

@
@shi-xiaoxiao-iris 已提交
969
  | 参数名       | 类型                                     | 必填   | 说明                          |
H
HelloCrease 已提交
970 971
  | -------- | ---------------------------------------- | ---- | --------------------------- |
  | type     | string                                   | 是    | 取消注册设备发现回调。                 |
972
  | callback | Callback&lt;{&nbsp; subscribeId: number, device: [DeviceInfo](#deviceinfo) &nbsp;}&gt; | 否    | 指示要取消注册的设备发现回调,返回设备状态和设备信息。 |
973

@
@shi-xiaoxiao-iris 已提交
974
**示例:**
@
@shi-xiaoxiao-iris 已提交
975

976
  ```js
977 978 979 980 981
  try {
    dmInstance.off('deviceFound', (data) => {
      console.info('deviceFound' + JSON.stringify(data));
    });
  } catch (err) {
982
    console.error("deviceFound errCode:" + err.code + ",errMessage:" + err.message);
983
  }
984 985 986 987
  ```

### on('discoverFail')

988
on(type: 'discoverFail', callback: Callback&lt;{&nbsp; subscribeId: number, reason: number &nbsp;}&gt;): void
989 990 991 992 993

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

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

@
@shi-xiaoxiao-iris 已提交
994
**参数:**
@
@shi-xiaoxiao-iris 已提交
995

@
@shi-xiaoxiao-iris 已提交
996
  | 参数名       | 类型                                     | 必填   | 说明                             |
H
HelloCrease 已提交
997 998
  | -------- | ---------------------------------------- | ---- | ------------------------------ |
  | type     | string                                   | 是    | 注册设备发现失败回调,以便在发现周边设备失败时通知应用程序。 |
999
  | callback | Callback&lt;{&nbsp; subscribeId: number, reason: number &nbsp;}&gt; | 是    | 注册设备发现失败的回调方法。                 |
1000

@
@shi-xiaoxiao-iris 已提交
1001
**示例:**
@
@shi-xiaoxiao-iris 已提交
1002

1003
  ```js
1004 1005
  try {
    dmInstance.on('discoverFail', (data) => {
1006
        console.info("discoverFail on:" + JSON.stringify(data));
1007
    });
1008
  } catch (err) {
1009
    console.error("discoverFail errCode:" + err.code + ",errMessage:" + err.message);
1010
  }
1011 1012 1013 1014
  ```

### off('discoverFail')

1015
off(type: 'discoverFail', callback?: Callback&lt;{&nbsp; subscribeId: number, reason: number &nbsp;}&gt;): void
1016 1017 1018 1019 1020

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

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

@
@shi-xiaoxiao-iris 已提交
1021
**参数:**
@
@shi-xiaoxiao-iris 已提交
1022

@
@shi-xiaoxiao-iris 已提交
1023
  | 参数名       | 类型                                     | 必填   | 说明                |
H
HelloCrease 已提交
1024 1025
  | -------- | ---------------------------------------- | ---- | ----------------- |
  | type     | string                                   | 是    | 取消注册设备发现失败回调。     |
1026
  | callback | Callback&lt;{&nbsp; subscribeId: number, reason: number &nbsp;}&gt; | 否    | 指示要取消注册的设备发现失败回调。 |
1027

@
@shi-xiaoxiao-iris 已提交
1028
**示例:**
@
@shi-xiaoxiao-iris 已提交
1029

1030
  ```js
1031 1032 1033 1034 1035
  try {
    dmInstance.off('discoverFail', (data) => {
      console.info('discoverFail' + JSON.stringify(data));
    });
  } catch (err) {
1036
    console.error("discoverFail errCode:" + err.code + ",errMessage:" + err.message);
1037
  }
1038 1039
  ```

@
@shi-xiaoxiao-iris 已提交
1040
### on('publishSuccess')<sup>9+</sup>
S
summer8999 已提交
1041

1042
on(type: 'publishSuccess', callback: Callback&lt;{&nbsp; publishId: number &nbsp;}&gt;): void
S
summer8999 已提交
1043 1044 1045 1046 1047

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

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

@
@shi-xiaoxiao-iris 已提交
1048
**参数:**
@
@shi-xiaoxiao-iris 已提交
1049

@
@shi-xiaoxiao-iris 已提交
1050
  | 参数名     | 类型                                 | 必填 | 说明                       |
S
summer8999 已提交
1051
  | -------- | ---------------------------------------- | ---- | -------------------------- |
S
summer8999 已提交
1052
  | type     | string                                   | 是   | 注册发布设备成功回调,以便将发布成功时通知应用程序。 |
1053
  | callback | Callback&lt;{&nbsp; publishId: number &nbsp;}&gt;    | 是   | 注册设备发布成功的回调方法。               |
S
summer8999 已提交
1054

@
@shi-xiaoxiao-iris 已提交
1055

@
@shi-xiaoxiao-iris 已提交
1056
**示例:**
@
@shi-xiaoxiao-iris 已提交
1057

S
summer8999 已提交
1058
  ```js
1059 1060 1061 1062 1063
  try {
    dmInstance.on('publishSuccess', (data) => {
      console.info("publishSuccess:" + JSON.stringify(data));
    });
  } catch (err) {
1064
    console.error("publishSuccess errCode:" + err.code + ",errMessage:" + err.message);
1065
  }
S
summer8999 已提交
1066 1067
  ```

@
@shi-xiaoxiao-iris 已提交
1068
### off('publishSuccess')<sup>9+</sup>
S
summer8999 已提交
1069

1070
off(type: 'publishSuccess', callback?: Callback&lt;{&nbsp; publishId: number &nbsp;}&gt;): void
S
summer8999 已提交
1071

S
summer8999 已提交
1072
取消注册设备发布成功回调。
S
summer8999 已提交
1073 1074 1075

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

@
@shi-xiaoxiao-iris 已提交
1076
**参数:**
@
@shi-xiaoxiao-iris 已提交
1077

@
@shi-xiaoxiao-iris 已提交
1078
  | 参数名     | 类型                                 | 必填 | 说明                          |
S
summer8999 已提交
1079
  | -------- | ---------------------------------------- | ---- | --------------------------- |
S
summer8999 已提交
1080
  | type     | string                                   | 是   | 取消注册设备发布成功回调。                 |
1081
  | callback | Callback&lt;{&nbsp; publishId: number &nbsp;}&gt;    | 否   | 指示要取消注册的设备发布成功回调。 |
S
summer8999 已提交
1082

@
@shi-xiaoxiao-iris 已提交
1083
**示例:**
@
@shi-xiaoxiao-iris 已提交
1084

S
summer8999 已提交
1085
  ```js
1086 1087 1088 1089 1090
  try {
    dmInstance.off('publishSuccess', (data) => {
      console.info('publishSuccess' + JSON.stringify(data));
    });
  } catch (err) {
1091
    console.error("publishSuccess errCode:" + err.code + ",errMessage:" + err.message);
1092
  }
S
summer8999 已提交
1093 1094
  ```

@
@shi-xiaoxiao-iris 已提交
1095
### on('publishFail')<sup>9+</sup>
S
summer8999 已提交
1096

1097
on(type: 'publishFail', callback: Callback&lt;{&nbsp; publishId: number, reason: number &nbsp;}&gt;): void
S
summer8999 已提交
1098 1099 1100 1101 1102

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

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

@
@shi-xiaoxiao-iris 已提交
1103
**参数:**
@
@shi-xiaoxiao-iris 已提交
1104

@
@shi-xiaoxiao-iris 已提交
1105
  | 参数名     | 类型                                              | 必填 | 说明                             |
S
summer8999 已提交
1106 1107
  | -------- | ----------------------------------------------------- | ---- | ------------------------------ |
  | type     | string                                                | 是   | 注册设备发布失败回调,以便在发布设备失败时通知应用程序。 |
1108
  | callback | Callback&lt;{&nbsp; publishId: number, reason: number &nbsp;}&gt; | 是   | 注册设备发布失败的回调方法。                 |
S
summer8999 已提交
1109

@
@shi-xiaoxiao-iris 已提交
1110
**示例:**
@
@shi-xiaoxiao-iris 已提交
1111

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

@
@shi-xiaoxiao-iris 已提交
1122
### off('publishFail')<sup>9+</sup>
S
summer8999 已提交
1123

1124
off(type: 'publishFail', callback?: Callback&lt;{&nbsp; publishId: number, reason: number &nbsp;}&gt;): void
S
summer8999 已提交
1125

S
summer8999 已提交
1126
取消注册设备发布失败回调。
S
summer8999 已提交
1127 1128 1129

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

@
@shi-xiaoxiao-iris 已提交
1130
**参数:**
@
@shi-xiaoxiao-iris 已提交
1131

@
@shi-xiaoxiao-iris 已提交
1132
  | 参数名     | 类型                                              | 必填 | 说明                |
S
summer8999 已提交
1133 1134
  | -------- | ----------------------------------------------------- | ---- | ----------------- |
  | type     | string                                                | 是   | 取消注册设备发布失败回调。     |
1135
  | callback | Callback&lt;{&nbsp; publishId: number, reason: number &nbsp;}&gt; | 否   | 指示要取消注册设备发布失败回调。 |
S
summer8999 已提交
1136

@
@shi-xiaoxiao-iris 已提交
1137
**示例:**
@
@shi-xiaoxiao-iris 已提交
1138

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

Z
zengyawen 已提交
1149 1150 1151
### on('serviceDie')

on(type: 'serviceDie', callback: () =&gt; void): void
Z
zengyawen 已提交
1152 1153 1154

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

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

@
@shi-xiaoxiao-iris 已提交
1157
**参数:**
@
@shi-xiaoxiao-iris 已提交
1158

@
@shi-xiaoxiao-iris 已提交
1159
  | 参数名       | 类型                    | 必填   | 说明                                       |
H
HelloCrease 已提交
1160 1161 1162
  | -------- | ----------------------- | ---- | ---------------------------------------- |
  | type     | string                  | 是    | 注册serviceDie回调,以便在devicemanager服务异常终止时通知应用程序。 |
  | callback | ()&nbsp;=&gt;&nbsp;void | 是    | 注册serviceDie的回调方法。                       |
Z
zengyawen 已提交
1163

@
@shi-xiaoxiao-iris 已提交
1164
**示例:**
@
@shi-xiaoxiao-iris 已提交
1165

1166
  ```js
1167 1168 1169 1170 1171
  try {
    dmInstance.on("serviceDie", () => {
      console.info("serviceDie on");
    });
  } catch (err) {
1172
    console.error("serviceDie errCode:" + err.code + ",errMessage:" + err.message);
1173
  }
Z
zengyawen 已提交
1174
  ```
Z
zengyawen 已提交
1175

Z
zengyawen 已提交
1176 1177 1178 1179 1180
### off('serviceDie')

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

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

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

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

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

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

1193
  ```js
1194 1195 1196 1197 1198
  try {
    dmInstance.off("serviceDie", () => {
      console.info("serviceDie off");
    });
  } catch (err) {
1199
    console.error("serviceDie errCode:" + err.code + ",errMessage:" + err.message);
1200
  }
1201
  ```