• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.distributedHardware.deviceManager (设备管理)
2
3本模块能力已更新至新模块。建议使用新模块的接口进行开发,参见[@ohos.distributedDeviceManager](js-apis-distributedDeviceManager.md)。
4
5本模块提供分布式设备管理能力。
6
7系统应用可调用接口实现如下功能:
8
9- 注册和解除注册设备上下线变化监听
10- 发现周边不可信设备
11- 认证和取消认证设备
12- 查询可信设备列表
13- 查询本地设备信息,包括设备名称,设备类型和设备标识
14- 发布设备发现
15
16> **说明:**
17>
18> - 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
19> - 本模块接口为系统接口,三方应用不支持调用。
20
21
22## 导入模块
23
24```ts
25import deviceManager from '@ohos.distributedHardware.deviceManager';
26```
27
28
29## deviceManager.createDeviceManager
30
31createDeviceManager(bundleName: string, callback: AsyncCallback<DeviceManager>): void
32
33创建一个设备管理器实例。
34
35**系统能力**:SystemCapability.DistributedHardware.DeviceManager
36
37**参数:**
38
39| 参数名     | 类型                                                 | 必填 | 说明                                                        |
40| ---------- | ---------------------------------------------------- | ---- | ----------------------------------------------------------- |
41| bundleName | string                                               | 是   | 指示应用程序的Bundle名称。                                  |
42| callback   | AsyncCallback<[DeviceManager](#devicemanager)> | 是   | DeviceManager实例创建时调用的回调,返回设备管理器对象实例。 |
43
44**示例:**
45
46  ```ts
47  import deviceManager from '@ohos.distributedHardware.deviceManager';
48  import { BusinessError } from '@ohos.base'
49
50  let dmInstance: deviceManager.DeviceManager | null = null;
51  try {
52    deviceManager.createDeviceManager("ohos.samples.jshelloworld", (err: BusinessError, data: deviceManager.DeviceManager) => {
53      if (err) {
54        console.error("createDeviceManager errCode:" + err.code + ",errMessage:" + err.message);
55        return;
56      }
57      console.info("createDeviceManager success");
58      dmInstance = data;
59    });
60  } catch(err) {
61    let e: BusinessError = err as BusinessError;
62    console.error("createDeviceManager errCode:" + e.code + ",errMessage:" + e.message);
63  }
64  ```
65
66## DeviceInfo
67
68设备信息。
69
70**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
71
72| 名称                     | 类型                        | 必填   | 说明       |
73| ---------------------- | ------------------------- | ---- | -------- |
74| deviceId               | string                    | 是    | 设备的唯一标识。 |
75| deviceName             | string                    | 是    | 设备名称。    |
76| deviceType             | [DeviceType](#devicetype) | 是    | 设备类型。    |
77| networkId<sup>8+</sup> | string                    | 是    | 设备网络标识。  |
78| range<sup>9+</sup>     | number                    | 是    | 发现设备的距离。  |
79| authForm<sup>10+</sup> | [AuthForm](#authform10)     | 是    | 设备认证类型。  |
80
81## DeviceType
82
83表示设备类型的枚举类。
84
85**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
86
87| 名称           | 值  | 说明   |
88| ------------ | ---- | ---- |
89| SPEAKER      | 0x0A | 智能音箱。 |
90| PHONE        | 0x0E | 手机。   |
91| TABLET       | 0x11 | 平板。   |
92| WEARABLE     | 0x6D | 智能穿戴。 |
93| TV           | 0x9C | 智慧屏。  |
94| CAR          | 0x83 | 车。    |
95| UNKNOWN_TYPE | 0    | 未知设备。 |
96
97## AuthForm<sup>10+</sup>
98
99表示设备认证类型的枚举类。
100
101**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
102
103| 名称                 | 值  | 说明             |
104| ------------------- | ---- | --------------- |
105| INVALID_TYPE        | -1   | 设备没有认证。 |
106| PEER_TO_PEER        | 0    | 无账号设备点对点认证。   |
107| IDENTICAL_ACCOUNT   | 1    | 设备同账号认证。   |
108| ACROSS_ACCOUNT      | 2    | 设备跨账号认证。 |
109
110## DeviceStateChangeAction
111
112表示设备状态变化的枚举。
113
114**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
115
116| 名称      | 值  | 说明              |
117| ------- | ---- | --------------- |
118| ONLINE  | 0    | 设备物理上线状态。           |
119| READY   | 1    | 设备可用状态,表示设备间信息已在分布式数据中同步完成, 可以运行分布式业务。 |
120| OFFLINE | 2    | 设备物理下线状态。           |
121| CHANGE  | 3    | 设备信息更改。         |
122
123## SubscribeInfo
124
125发现信息。
126
127**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
128
129| 名称            | 类型                                | 必填   | 说明                |
130| ------------- | --------------------------------- | ---- | ----------------- |
131| subscribeId   | number                            | 是    | 发现标识,用于标识不同的发现周期。 |
132| mode          | [DiscoverMode ](#discovermode)    | 是    | 发现模式。             |
133| medium        | [ExchangeMedium](#exchangemedium) | 是    | 发现类型。             |
134| freq          | [ExchangeFreq](#exchangefreq)     | 是    | 发现频率。             |
135| isSameAccount | boolean                           | 否    | 是否同帐号。            |
136| isWakeRemote  | boolean                           | 否    | 是否唤醒设备。           |
137| capability    | [SubscribeCap](#subscribecap)     | 是    | 发现能力。             |
138
139
140## DiscoverMode
141
142表示发现模式的枚举。
143
144**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
145
146| 名称                    | 值  | 说明    |
147| --------------------- | ---- | ----- |
148| DISCOVER_MODE_PASSIVE | 0x55 | 被动模式。 |
149| DISCOVER_MODE_ACTIVE  | 0xAA | 主动模式。 |
150
151
152## ExchangeMedium
153
154表示发现类型的枚举。
155
156**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
157
158| 名称   | 值  | 说明        |
159| ---- | ---- | --------- |
160| AUTO | 0    | 自动发现类型。   |
161| BLE  | 1    | 蓝牙发现类型。   |
162| COAP | 2    | WiFi发现类型。 |
163| USB  | 3    | USB发现类型。  |
164
165## ExchangeFreq
166
167表示发现频率的枚举。
168
169**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
170
171| 名称         | 值  | 说明    |
172| ---------- | ---- | ----- |
173| LOW        | 0    | 低频率。  |
174| MID        | 1    | 中频率。  |
175| HIGH       | 2    | 高频率。  |
176| SUPER_HIGH | 3    | 超高频率。 |
177
178
179## SubscribeCap
180
181表示发现能力的枚举。
182
183**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
184
185| 名称                        | 值  | 说明             |
186| ------------------------- | ---- | -------------- |
187| SUBSCRIBE_CAPABILITY_DDMP | 0    | DDMP能力,后续会被废弃。 |
188| SUBSCRIBE_CAPABILITY_OSD  | 1    | OSD能力。         |
189
190
191## AuthParam
192
193认证参数。
194
195**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
196
197| 名称        | 类型                   | 必填   | 说明         |
198| --------- | -------------------- | ---- | ---------- |
199| authType  | number               | 是    | 认证类型。      |
200| extraInfo | {[key:string]&nbsp;:&nbsp;any} | 否    | 认证参数可扩展字段。可选,默认为undefined。 |
201
202## AuthInfo
203
204认证信息。
205
206**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
207
208| 名称        | 类型                   | 必填   | 说明         |
209| --------- | -------------------- | ---- | ---------- |
210| authType  | number               | 是    | 认证类型。      |
211| token     | number               | 是    | 认证Token。   |
212| extraInfo | {[key:string]&nbsp;:&nbsp;any} | 否    | 认证信息可扩展字段。可选,默认为undefined。 |
213
214## PublishInfo<sup>9+</sup>
215
216发布设备参数
217
218**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedHardware.DeviceManager
219
220| 名称          | 类型                              | 必填   | 说明                |
221| ------------- | --------------------------------- | ---- | ----------------- |
222| publishId     | number                            | 是    | 发布设备标识,用于标识不同的发布周期。 |
223| mode          | [DiscoverMode ](#discovermode)    | 是    | 发现模式。             |
224| freq          | [ExchangeFreq](#exchangefreq)     | 是    | 发现频率。             |
225| ranging       | boolean                           | 是    | 发布的设备是否支持测距能力。             |
226
227## DeviceManager
228
229设备管理实例,用于获取可信设备和本地设备的相关信息。在调用DeviceManager的方法前,需要先通过createDeviceManager构建一个DeviceManager实例dmInstance。
230
231### release
232
233release(): void
234
235设备管理实例不再使用后,通过该方法释放DeviceManager实例。
236
237**需要权限**:ohos.permission.ACCESS_SERVICE_DM
238
239**系统能力**:SystemCapability.DistributedHardware.DeviceManager
240
241**错误码:**
242
243以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
244
245| 错误码ID | 错误信息                                                        |
246| -------- | --------------------------------------------------------------- |
247| 11600101 | Failed to execute the function.                                 |
248
249**示例:**
250
251  ```ts
252  import { BusinessError } from '@ohos.base'
253
254  try {
255    dmInstance.release();
256  } catch (err) {
257    let e: BusinessError = err as BusinessError;
258    console.error("release errCode:" + e.code + ",errMessage:" + e.message);
259  }
260  ```
261
262### getTrustedDeviceListSync
263
264getTrustedDeviceListSync(): Array&lt;DeviceInfo&gt;
265
266同步获取所有可信设备列表。
267
268**需要权限**:ohos.permission.ACCESS_SERVICE_DM
269
270**系统能力**:SystemCapability.DistributedHardware.DeviceManager
271
272**返回值:**
273
274  | 名称                                     | 说明        |
275  | -------------------------------------- | --------- |
276  | Array&lt;[DeviceInfo](#deviceinfo)&gt; | 返回可信设备列表。 |
277
278**错误码:**
279
280以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
281
282| 错误码ID | 错误信息                                                        |
283| -------- | --------------------------------------------------------------- |
284| 11600101 | Failed to execute the function.                                 |
285
286**示例:**
287
288  ```ts
289  import deviceManager from '@ohos.distributedHardware.deviceManager';
290  import { BusinessError } from '@ohos.base'
291
292  try {
293    let deviceInfoList: Array<deviceManager.DeviceInfo> = dmInstance.getTrustedDeviceListSync();
294  } catch (err) {
295    let e: BusinessError = err as BusinessError;
296    console.error("getTrustedDeviceListSync errCode:" + e.code + ",errMessage:" + e.message);
297  }
298  ```
299
300### getTrustedDeviceListSync<sup>10+</sup>
301
302getTrustedDeviceListSync(isRefresh: boolean): Array&lt;DeviceInfo&gt;
303
304打开软总线系统端的心跳模式,让周围处于下线状态的可信设备快速上线,同时刷新已上线的可信设备列表。
305
306**需要权限**:ohos.permission.ACCESS_SERVICE_DM
307
308**系统能力**:SystemCapability.DistributedHardware.DeviceManager
309
310**参数:**
311
312| 参数名        | 类型                               | 必填 | 说明                                |
313| ------------- | --------------------------------- | ---- | ---------------------------------- |
314|   isRefresh   | boolean                           | 是   | 是否打开心跳模式,刷新可信列表。      |
315
316**返回值:**
317
318| 名称                                     | 说明            |
319| -------------------------------------- | ---------------- |
320| Array&lt;[DeviceInfo](#deviceinfo)&gt; | 返回可信设备列表。 |
321
322**错误码:**
323
324以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
325
326| 错误码ID | 错误信息                                                         |
327| -------- | --------------------------------------------------------------- |
328| 11600101 | Failed to execute the function.                                 |
329
330**示例:**
331
332  ```ts
333  import deviceManager from '@ohos.distributedHardware.deviceManager';
334  import { BusinessError } from '@ohos.base'
335
336  try {
337    let deviceInfoList: Array<deviceManager.DeviceInfo> = dmInstance.getTrustedDeviceListSync(true);
338  } catch (err) {
339    let e: BusinessError = err as BusinessError;
340    console.error("getTrustedDeviceListSync errCode:" + e.code + ",errMessage:" + e.message);
341  }
342  ```
343
344### getTrustedDeviceList<sup>8+</sup>
345
346getTrustedDeviceList(callback:AsyncCallback&lt;Array&lt;DeviceInfo&gt;&gt;): void
347
348获取所有可信设备列表。使用callback异步回调。
349
350**需要权限**:ohos.permission.ACCESS_SERVICE_DM
351
352**系统能力**:SystemCapability.DistributedHardware.DeviceManager
353
354**参数:**
355
356  | 参数名       | 类型                                     | 必填   | 说明                    |
357  | -------- | ---------------------------------------- | ---- | --------------------- |
358  | callback | AsyncCallback&lt;Array&lt;[DeviceInfo](#deviceinfo)&gt;&gt; | 是    | 获取所有可信设备列表的回调,返回设备信息。 |
359
360**示例:**
361
362  ```ts
363  import deviceManager from '@ohos.distributedHardware.deviceManager';
364  import { BusinessError } from '@ohos.base'
365  try {
366    dmInstance.getTrustedDeviceList((err: BusinessError, data: Array<deviceManager.DeviceInfo>) => {
367      if (err) {
368        console.error("getTrustedDeviceList errCode:" + err.code + ",errMessage:" + err.message);
369        return;
370      }
371      console.log('get trusted device info: ' + JSON.stringify(data));
372    });
373  } catch (err) {
374    let e: BusinessError = err as BusinessError;
375    console.error("getTrustedDeviceList errCode:" + e.code + ",errMessage:" + e.message);
376  }
377  ```
378
379### getTrustedDeviceList<sup>8+</sup>
380
381getTrustedDeviceList(): Promise&lt;Array&lt;DeviceInfo&gt;&gt;
382
383获取所有可信设备列表。使用Promise异步回调。
384
385**需要权限**:ohos.permission.ACCESS_SERVICE_DM
386
387**系统能力**:SystemCapability.DistributedHardware.DeviceManager
388
389**返回值:**
390
391  | 类型                                       | 说明                    |
392  | ---------------------------------------- | --------------------- |
393  | Promise&lt;Array&lt;[DeviceInfo](#deviceinfo)&gt;&gt; | Promise实例,用于获取异步返回结果。 |
394
395**示例:**
396
397  ```ts
398  import deviceManager from '@ohos.distributedHardware.deviceManager';
399  import { BusinessError } from '@ohos.base'
400
401  dmInstance.getTrustedDeviceList().then((data: Array<deviceManager.DeviceInfo>) => {
402    console.log('get trusted device info: ' + JSON.stringify(data));
403    }).catch((err: BusinessError) => {
404      console.error("getTrustedDeviceList errCode:" + err.code + ",errMessage:" + err.message);
405  });
406  ```
407
408### getLocalDeviceInfoSync<sup>8+</sup>
409
410getLocalDeviceInfoSync(): [DeviceInfo](#deviceinfo)
411
412同步获取本地设备信息。
413
414**需要权限**:ohos.permission.ACCESS_SERVICE_DM
415
416**系统能力**:SystemCapability.DistributedHardware.DeviceManager
417
418**返回值:**
419
420  | 名称                      | 说明              |
421  | ------------------------- | ---------------- |
422  | [DeviceInfo](#deviceinfo) | 返回本地设备列表。 |
423
424**错误码:**
425
426以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
427
428| 错误码ID | 错误信息                                                        |
429| -------- | --------------------------------------------------------------- |
430| 11600101 | Failed to execute the function.                                 |
431
432**示例:**
433
434  ```ts
435  import deviceManager from '@ohos.distributedHardware.deviceManager';
436  import { BusinessError } from '@ohos.base'
437
438  try {
439    let deviceInfo: deviceManager.DeviceInfo = dmInstance.getLocalDeviceInfoSync();
440  } catch (err) {
441    let e: BusinessError = err as BusinessError;
442    console.error("getLocalDeviceInfoSync errCode:" + e.code + ",errMessage:" + e.message);
443  }
444  ```
445
446### getLocalDeviceInfo<sup>8+</sup>
447
448getLocalDeviceInfo(callback:AsyncCallback&lt;DeviceInfo&gt;): void
449
450获取本地设备信息。使用callback异步回调。
451
452**需要权限**:ohos.permission.ACCESS_SERVICE_DM
453
454**系统能力**:SystemCapability.DistributedHardware.DeviceManager
455
456**参数:**
457
458  | 参数名       | 类型                                     | 必填   | 说明        |
459  | -------- | ---------------------------------------- | ---- | --------- |
460  | callback | AsyncCallback&lt;[DeviceInfo](#deviceinfo)&gt; | 是    | 获取本地设备信息。 |
461
462**示例:**
463
464  ```ts
465  import deviceManager from '@ohos.distributedHardware.deviceManager';
466  import { BusinessError } from '@ohos.base'
467
468
469  try {
470    dmInstance.getLocalDeviceInfo((err: BusinessError, data: deviceManager.DeviceInfo) => {
471    if (err) {
472      console.error("getLocalDeviceInfo errCode:" + err.code + ",errMessage:" + err.message);
473      return;
474    }
475      console.log('get local device info: ' + JSON.stringify(data));
476    });
477  } catch (err) {
478    let e: BusinessError = err as BusinessError;
479    console.error("getLocalDeviceInfo errCode:" + e.code + ",errMessage:" + e.message);
480  }
481  ```
482
483### getLocalDeviceInfo<sup>8+</sup>
484
485getLocalDeviceInfo(): Promise&lt;DeviceInfo&gt;
486
487获取本地设备信息。使用Promise异步回调。
488
489**需要权限**:ohos.permission.ACCESS_SERVICE_DM
490
491**系统能力**:SystemCapability.DistributedHardware.DeviceManager
492
493**返回值:**
494
495  | 类型                                       | 说明                    |
496  | ---------------------------------------- | --------------------- |
497  | Promise&lt;[DeviceInfo](#deviceinfo)&gt; | Promise实例,用于获取异步返回结果。 |
498
499**示例:**
500
501  ```ts
502  import deviceManager from '@ohos.distributedHardware.deviceManager';
503  import { BusinessError } from '@ohos.base'
504
505  dmInstance.getLocalDeviceInfo().then((data: deviceManager.DeviceInfo) => {
506    console.log('get local device info: ' + JSON.stringify(data));
507  }).catch((err: BusinessError) => {
508    console.error("getLocalDeviceInfo errCode:" + err.code + ",errMessage:" + err.message);
509  });
510  ```
511
512### getDeviceInfo<sup>10+</sup>
513
514getDeviceInfo(networkId: string, callback:AsyncCallback&lt;DeviceInfo&gt;): void
515
516通过指定设备的网络标识获取该设备的信息。使用callback异步回调。
517
518**需要权限**:ohos.permission.ACCESS_SERVICE_DM
519
520**系统能力**:SystemCapability.DistributedHardware.DeviceManager
521
522**参数:**
523
524  | 参数名       | 类型                                     | 必填   | 说明        |
525  | -------- | ---------------------------------------- | ---- | --------- |
526  | networkId| string                                   | 是   | 设备的网络标识。 |
527  | callback | AsyncCallback&lt;[DeviceInfo](#deviceinfo)&gt; | 是    | 获取指定设备信息。 |
528
529**示例:**
530
531  ```ts
532  import deviceManager from '@ohos.distributedHardware.deviceManager';
533  import { BusinessError } from '@ohos.base'
534
535  try {
536    // 设备网络标识,可以从可信设备列表中获取
537    let networkId = "xxxxxxx"
538    dmInstance.getDeviceInfo(networkId, (err: BusinessError, data: deviceManager.DeviceInfo) => {
539    if (err) {
540      console.error("getDeviceInfo errCode:" + err.code + ",errMessage:" + err.message);
541      return;
542    }
543      console.log('get device info: ' + JSON.stringify(data));
544    });
545  } catch (err) {
546    let e: BusinessError = err as BusinessError;
547    console.error("getDeviceInfo errCode:" + e.code + ",errMessage:" + e.message);
548  }
549  ```
550
551### getDeviceInfo<sup>10+</sup>
552
553getDeviceInfo(networkId: string): Promise&lt;DeviceInfo&gt;
554
555通过指定设备的网络标识获取该设备的信息。使用Promise异步回调。
556
557**需要权限**:ohos.permission.ACCESS_SERVICE_DM
558
559**系统能力**:SystemCapability.DistributedHardware.DeviceManager
560
561**参数:**
562
563  | 参数名   | 类型                                     | 必填 | 说明        |
564  | -------- | ---------------------------------------- | ---- | --------- |
565  | networkId| string                                   | 是   | 设备的网络标识。 |
566
567**返回值:**
568
569  | 类型                                       | 说明                    |
570  | ---------------------------------------- | --------------------- |
571  | Promise&lt;[DeviceInfo](#deviceinfo)&gt; | Promise实例,用于获取异步返回结果。 |
572
573**示例:**
574
575  ```ts
576  import deviceManager from '@ohos.distributedHardware.deviceManager';
577  import { BusinessError } from '@ohos.base'
578
579  // 设备网络标识,可以从可信设备列表中获取
580  let networkId = "xxxxxxx"
581  dmInstance.getDeviceInfo(networkId).then((data: deviceManager.DeviceInfo) => {
582    console.log('get device info: ' + JSON.stringify(data));
583  }).catch((err: BusinessError) => {
584    console.error("getDeviceInfo errCode:" + err.code + ",errMessage:" + err.message);
585  });
586  ```
587
588### startDeviceDiscovery<sup>8+</sup>
589
590startDeviceDiscovery(subscribeInfo: SubscribeInfo): void
591
592发现周边设备。发现状态持续两分钟,超过两分钟,会停止发现,最大发现数量99个。
593
594**需要权限**:ohos.permission.ACCESS_SERVICE_DM
595
596**系统能力**:SystemCapability.DistributedHardware.DeviceManager
597
598**参数:**
599
600  | 参数名            | 类型                       | 必填 | 说明    |
601  | ------------- | ------------------------------- | ---- | ----- |
602  | subscribeInfo | [SubscribeInfo](#subscribeinfo) | 是   | 发现信息。|
603
604**错误码:**
605
606以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
607
608| 错误码ID | 错误信息                                                        |
609| -------- | --------------------------------------------------------------- |
610| 11600101 | Failed to execute the function.                                 |
611| 11600104 | Discovery invalid.                                              |
612
613**示例:**
614
615  ```ts
616  import { BusinessError } from '@ohos.base'
617
618  interface SubscribeInfo {
619    subscribeId: number,
620    mode: number, // 主动模式
621    medium: number,  // 自动发现类型,同时支持多种发现类型
622    freq: number,    // 高频率
623    isSameAccount: boolean,
624    isWakeRemote: boolean,
625    capability: number
626  };
627
628  // 生成发现标识,随机数确保每次调用发现接口的标识不一致
629  let subscribeId = Math.floor(Math.random() * 10000 + 1000);
630  let subscribeInfo: SubscribeInfo = {
631    subscribeId: subscribeId,
632    mode: 0xAA, // 主动模式
633    medium: 0,  // 自动发现类型,同时支持多种发现类型
634    freq: 2,    // 高频率
635    isSameAccount: false,
636    isWakeRemote: false,
637    capability: 1
638  };
639  try {
640    dmInstance.startDeviceDiscovery(subscribeInfo); // 当有设备发现时,通过deviceFound回调通知给应用程序
641  } catch (err) {
642    let e: BusinessError = err as BusinessError;
643    console.error("startDeviceDiscovery errCode:" + e.code + ",errMessage:" + e.message);
644  }
645  ```
646
647### startDeviceDiscovery<sup>9+</sup>
648
649startDeviceDiscovery(subscribeInfo: SubscribeInfo, filterOptions?: string): void
650
651发现周边设备。发现状态持续两分钟,超过两分钟,会停止发现,最大发现数量99个。
652
653**需要权限**:ohos.permission.ACCESS_SERVICE_DM
654
655**系统能力**:SystemCapability.DistributedHardware.DeviceManager
656
657**参数:**
658
659  | 参数名            | 类型                       | 必填   | 说明    |
660  | ------------- | ------------------------------- | ---- | -----  |
661  | subscribeInfo | [SubscribeInfo](#subscribeinfo) | 是   | 发现信息。 |
662  | filterOptions | string                          | 否   | 发现设备过滤信息。可选,默认为undefined,发现未上线设备。|
663
664**错误码:**
665
666以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
667
668| 错误码ID | 错误信息                                                        |
669| -------- | --------------------------------------------------------------- |
670| 11600101 | Failed to execute the function.                                 |
671| 11600104 | Discovery invalid.                                              |
672
673**示例:**
674
675  ```ts
676  import { BusinessError } from '@ohos.base'
677
678  interface Filters {
679    type: string,
680    value: number
681  }
682
683  interface FilterOptions {
684    filter_op: string, // 可选, 默认"OR"
685    filters: Filters[]
686    }
687
688  interface SubscribeInfo {
689    subscribeId: number,
690    mode: number, // 主动模式
691    medium: number,  // 自动发现类型,同时支持多种发现类型
692    freq: number,    // 高频率
693    isSameAccount: boolean,
694    isWakeRemote: boolean,
695    capability: number
696  }
697
698  // 生成发现标识,随机数确保每次调用发现接口的标识不一致
699  let subscribeId = Math.floor(Math.random() * 10000 + 1000);
700  let subscribeInfo: SubscribeInfo = {
701    subscribeId: subscribeId,
702    mode: 0xAA, // 主动模式
703    medium: 0,  // 自动发现类型,同时支持多种发现类型
704    freq: 2,    // 高频率
705    isSameAccount: false,
706    isWakeRemote: false,
707    capability: 1
708  };
709
710  let filters: Filters[] = [
711    {
712        type: "range",
713        value: 50 // 需过滤发现设备的距离,单位(cm)
714    }
715  ]
716
717  let filterOptions: FilterOptions = {
718    filter_op: "OR", // 可选, 默认"OR"
719    filters: filters
720  };
721  try {
722    dmInstance.startDeviceDiscovery(subscribeInfo, JSON.stringify(filterOptions)); // 当有设备发现时,通过deviceFound回调通知给应用程序
723  } catch (err) {
724    let e: BusinessError = err as BusinessError;
725    console.error("startDeviceDiscovery errCode:" + e.code + ",errMessage:" + e.message);
726  }
727  ```
728
729### stopDeviceDiscovery
730
731stopDeviceDiscovery(subscribeId: number): void
732
733停止发现周边设备。
734
735**需要权限**:ohos.permission.ACCESS_SERVICE_DM
736
737**系统能力**:SystemCapability.DistributedHardware.DeviceManager
738
739**参数:**
740
741  | 参数名          | 类型   | 必填   | 说明    |
742  | ----------- | ------ | ---- | ----- |
743  | subscribeId | number | 是    | 发现标识。 |
744
745**错误码:**
746
747以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
748
749| 错误码ID | 错误信息                                                        |
750| -------- | --------------------------------------------------------------- |
751| 11600101 | Failed to execute the function.                                 |
752
753**示例:**
754
755  ```ts
756  import { BusinessError } from '@ohos.base'
757
758  try {
759    // stopDeviceDiscovery和startDeviceDiscovery需配对使用,入参需要和startDeviceDiscovery接口传入的subscribeId值相等
760    let subscribeId = 12345;
761    dmInstance.stopDeviceDiscovery(subscribeId);
762  } catch (err) {
763    let e: BusinessError = err as BusinessError;
764    console.error("stopDeviceDiscovery errCode:" + e.code + ",errMessage:" + e.message);
765  }
766  ```
767
768### publishDeviceDiscovery<sup>9+</sup>
769
770publishDeviceDiscovery(publishInfo: PublishInfo): void
771
772发布设备发现。发布状态持续两分钟,超过两分钟会停止发布。
773
774**需要权限**:ohos.permission.ACCESS_SERVICE_DM
775
776**系统能力**:SystemCapability.DistributedHardware.DeviceManager
777
778**参数:**
779
780  | 参数名          | 类型                        | 必填 | 说明    |
781  | ------------- | ------------------------------- | ---- | ----- |
782  | publishInfo   | [PublishInfo](#publishinfo9)     | 是   | 发布设备发现信息。 |
783
784**错误码:**
785
786以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
787
788| 错误码ID | 错误信息                                                        |
789| -------- | --------------------------------------------------------------- |
790| 11600101 | Failed to execute the function.                                 |
791| 11600105 | Publish invalid.                                                |
792
793**示例:**
794
795  ```ts
796  import { BusinessError } from '@ohos.base'
797
798  interface PublishInfo {
799    publishId: number,
800    mode: number, // 主动模式
801    freq: number,    // 高频率
802    ranging: boolean  // 支持发现时测距
803  };
804
805  // 生成发布标识,随机数确保每次调用发布接口的标识不一致
806  let publishId = Math.floor(Math.random() * 10000 + 1000);
807  let publishInfo: PublishInfo = {
808    publishId: publishId,
809    mode: 0xAA, // 主动模式
810    freq: 2,    // 高频率
811    ranging: true  // 支持发现时测距
812  };
813
814  try {
815    dmInstance.publishDeviceDiscovery(publishInfo); // 当有发布结果时,通过回调通知给应用程序
816  } catch (err) {
817    let e: BusinessError = err as BusinessError;
818    console.error("publishDeviceDiscovery errCode:" + e.code + ",errMessage:" + e.message);
819  }
820  ```
821
822### unPublishDeviceDiscovery<sup>9+</sup>
823
824unPublishDeviceDiscovery(publishId: number): void
825
826停止发布设备发现。
827
828**需要权限**:ohos.permission.ACCESS_SERVICE_DM
829
830**系统能力**:SystemCapability.DistributedHardware.DeviceManager
831
832**参数:**
833
834  | 参数名        | 类型 | 必填 | 说明  |
835  | ----------- | -------- | ---- | ----- |
836  | publishId   | number   | 是   | 发布标识。 |
837
838**错误码:**
839
840以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
841
842| 错误码ID | 错误信息                                                        |
843| -------- | --------------------------------------------------------------- |
844| 11600101 | Failed to execute the function.                                 |
845
846**示例:**
847
848  ```ts
849  import { BusinessError } from '@ohos.base'
850
851  try {
852    // unPublishDeviceDiscovery和publishDeviceDiscovery配对使用,入参需要和publishDeviceDiscovery接口传入的publishId值相等
853    let publishId = 12345;
854    dmInstance.unPublishDeviceDiscovery(publishId);
855  } catch (err) {
856    let e: BusinessError = err as BusinessError;
857    console.error("unPublishDeviceDiscovery errCode:" + e.code + ",errMessage:" + e.message);
858  }
859  ```
860
861### authenticateDevice
862
863authenticateDevice(deviceInfo: DeviceInfo, authParam: AuthParam, callback: AsyncCallback&lt;{deviceId: string, pinToken ?: number}&gt;): void
864
865认证设备。
866
867**需要权限**:ohos.permission.ACCESS_SERVICE_DM
868
869**系统能力**:SystemCapability.DistributedHardware.DeviceManager
870
871**参数:**
872
873  | 参数名         | 类型                                     | 必填   | 说明      |
874  | ---------- | ---------------------------------------- | ---- | ------- |
875  | deviceInfo | [DeviceInfo](#deviceinfo)                | 是    | 设备信息。   |
876  | authParam  | [AuthParam](#authparam)                  | 是    | 认证参数。   |
877  | callback   | AsyncCallback&lt;{deviceId:&nbsp;string,&nbsp;pinToken&nbsp;?:&nbsp;number}&gt; | 是    | 认证结果回调。 |
878
879**示例:**
880
881  ```ts
882  import { BusinessError } from '@ohos.base'
883
884  class Data {
885    deviceId: string = ""
886    pinToken?: number = 0
887  }
888
889  interface DeviceInfo {
890    deviceId: string,
891    deviceName: string
892    deviceType: number,
893    networkId: string,
894    range: number
895  };
896
897  interface ExtraInfo {
898    targetPkgName: string,
899    appName: string,
900    appDescription: string,
901    business: string
902  }
903
904  interface AuthParam {
905    authType: number,// 认证类型: 1 - 无帐号PIN码认证
906    extraInfo: ExtraInfo
907  }
908
909  // 认证的设备信息,可以从发现的结果中获取
910  let deviceInfo: deviceManager.DeviceInfo = {
911    deviceId: "XXXXXXXX",
912    deviceName: "",
913    deviceType: 0x0E,
914    networkId: "xxxxxxx",
915    range: 0,
916    authForm: 0
917  };
918  let extraInfo: ExtraInfo = {
919    targetPkgName: 'ohos.samples.xxx',
920    appName: 'xxx',
921    appDescription: 'xxx',
922    business: '0'
923  }
924  let authParam: AuthParam = {
925    authType: 1,// 认证类型: 1 - 无帐号PIN码认证
926    extraInfo: extraInfo
927  }
928
929  try {
930    dmInstance.authenticateDevice(deviceInfo, authParam, (err: BusinessError, data: Data) => {
931      if (err) {
932          console.error("authenticateDevice errCode:" + err.code + ",errMessage:" + err.message);
933          return;
934      }
935      console.info("authenticateDevice result:" + JSON.stringify(data));
936      let token = data.pinToken;
937    });
938  } catch (err) {
939    let e: BusinessError = err as BusinessError;
940    console.error("authenticateDevice errCode:" + e.code + ",errMessage:" + e.message);
941  }
942  ```
943
944### unAuthenticateDevice<sup>8+</sup>
945
946unAuthenticateDevice(deviceInfo: DeviceInfo): void
947
948解除认证设备。
949
950**需要权限**:ohos.permission.ACCESS_SERVICE_DM
951
952**系统能力**:SystemCapability.DistributedHardware.DeviceManager
953
954**参数:**
955
956  | 参数名         | 类型                      | 必填   | 说明    |
957  | ---------- | ------------------------- | ---- | ----- |
958  | deviceInfo | [DeviceInfo](#deviceinfo) | 是    | 设备信息。 |
959
960**错误码:**
961
962以下的错误码的详细介绍请参见[设备管理错误码](../errorcodes/errorcode-device-manager.md)
963
964| 错误码ID | 错误信息                                                        |
965| -------- | --------------------------------------------------------------- |
966| 11600101 | Failed to execute the function.                                 |
967
968**示例:**
969
970  ```ts
971  import { BusinessError } from '@ohos.base'
972
973  interface DeviceInfo {
974    deviceId: string,
975    deviceName: string,
976    deviceType: number,
977    networkId: string,
978    range: number
979  }
980
981  try {
982    let deviceInfo: deviceManager.DeviceInfo = {
983      deviceId: "XXXXXXXX",
984      deviceName: "",
985      deviceType: 0x0E,
986      networkId: "xxxxxxx",
987      range: 0,
988      authForm: 0
989    };
990    dmInstance.unAuthenticateDevice(deviceInfo);
991  } catch (err) {
992    let e: BusinessError = err as BusinessError;
993    console.error("unAuthenticateDevice errCode:" + e.code + ",errMessage:" + e.message);
994  }
995  ```
996
997### verifyAuthInfo
998
999verifyAuthInfo(authInfo: AuthInfo, callback: AsyncCallback&lt;{deviceId: string, level: number}&gt;): void
1000
1001验证认证信息。
1002
1003**需要权限**:ohos.permission.ACCESS_SERVICE_DM
1004
1005**系统能力**:SystemCapability.DistributedHardware.DeviceManager
1006
1007**参数:**
1008
1009  | 参数名       | 类型                                     | 必填   | 说明      |
1010  | -------- | ---------------------------------------- | ---- | ------- |
1011  | authInfo | [AuthInfo](#authinfo)                    | 是    | 认证信息。   |
1012  | callback | AsyncCallback&lt;{deviceId:&nbsp;string,&nbsp;level:&nbsp;number}&gt; | 是    | 验证结果回调。 |
1013
1014**示例:**
1015
1016  ```ts
1017  import { BusinessError } from '@ohos.base'
1018
1019  interface ExtraInfo {
1020    authType: number,
1021    token: number
1022  }
1023
1024  interface AuthInfo {
1025    authType: number,
1026    token: number,
1027    extraInfo: ExtraInfo
1028  }
1029
1030  class Data {
1031    deviceId: string = ""
1032    level: number = 0
1033  }
1034
1035  let extraInfo: ExtraInfo = {
1036    authType: 0,
1037    token: 0
1038  }
1039
1040  let authInfo: AuthInfo = {
1041    authType: 1,
1042    token: 123456,
1043    extraInfo: extraInfo
1044  }
1045  try {
1046    dmInstance.verifyAuthInfo(authInfo, (err: BusinessError, data: Data) => {
1047      if (err) {
1048          console.error("verifyAuthInfo errCode:" + err.code + ",errMessage:" + err.message);
1049          return;
1050    }
1051    console.info("verifyAuthInfo result:" + JSON.stringify(data));
1052    });
1053  } catch (err) {
1054    let e: BusinessError = err as BusinessError;
1055    console.error("verifyAuthInfo errCode:" + e.code + ",errMessage:" + e.message);
1056  }
1057  ```
1058
1059### setUserOperation<sup>9+</sup>
1060
1061setUserOperation(operateAction: number, params: string): void;
1062
1063设置用户ui操作行为。
1064
1065**需要权限**:ohos.permission.ACCESS_SERVICE_DM
1066
1067**系统能力**:SystemCapability.DistributedHardware.DeviceManager
1068
1069**参数:**
1070
1071  | 参数名       | 类型            | 必填  | 说明                |
1072  | ------------- | --------------- | ---- | ------------------- |
1073  | operateAction | number          | 是    | 用户操作动作。       |
1074  | params        | string          | 是    | 表示用户的输入参数。 |
1075
1076**示例:**
1077
1078  ```ts
1079  import { BusinessError } from '@ohos.base'
1080
1081 try {
1082    /*
1083      operateAction = 0 - 允许授权
1084      operateAction = 1 - 取消授权
1085      operateAction = 2 - 授权框用户操作超时
1086      operateAction = 3 - 取消pin码框展示
1087      operateAction = 4 - 取消pin码输入框展示
1088      operateAction = 5 - pin码输入框确定操作
1089    */
1090    let operation = 0;
1091    dmInstance.setUserOperation(operation, "extra")
1092    } catch (err) {
1093      let e: BusinessError = err as BusinessError;
1094      console.error("setUserOperation errCode:" + e.code + ",errMessage:" + e.message);
1095  }
1096  ```
1097
1098### requestCredentialRegisterInfo<sup>10+</sup>
1099
1100requestCredentialRegisterInfo(requestInfo: string, callback: AsyncCallback<{registerInfo: string}>): void;
1101
1102获取凭据的注册信息。
1103
1104**需要权限**:ohos.permission.ACCESS_SERVICE_DM
1105
1106**系统能力**:SystemCapability.DistributedHardware.DeviceManager
1107
1108**参数:**
1109
1110  | 参数名       | 类型            | 必填  | 说明                |
1111  | ------------- | --------------- | ---- | ------------------- |
1112  | requestInfo   | string          | 是    | 请求凭据信息。       |
1113  | callback      | AsyncCallback<{registerInfo: string}>         | 是    | 凭据的注册信息回调。 |
1114
1115**示例:**
1116
1117  ```ts
1118  import { BusinessError } from '@ohos.base'
1119
1120  interface CredentialInfo {
1121    version: string,
1122    userId: string
1123  }
1124
1125  class Data {
1126    registerInfo: string = ""
1127  }
1128
1129  let credentialInfo: CredentialInfo = {
1130    version: "1.2.3",
1131    userId: "123"
1132  }
1133  try {
1134    let jsonCredentialInfo = JSON.stringify(credentialInfo);
1135    dmInstance.requestCredentialRegisterInfo(jsonCredentialInfo, (err: BusinessError, data: Data) => {
1136      if (data) {
1137          console.info("requestCredentialRegisterInfo result:" + JSON.stringify(data));
1138      } else {
1139          console.info("requestCredentialRegisterInfo result: data is null");
1140      }
1141    });
1142  } catch (err) {
1143    let e: BusinessError = err as BusinessError;
1144    console.error("requestCredentialRegisterInfo err:" + e.code + "," + e.message);
1145  }
1146  ```
1147
1148### importCredential<sup>10+</sup>
1149
1150importCredential(credentialInfo: string, callback: AsyncCallback<{resultInfo: string}>): void;
1151
1152导入凭据信息。
1153
1154**需要权限**:ohos.permission.ACCESS_SERVICE_DM
1155
1156**系统能力**:SystemCapability.DistributedHardware.DeviceManager
1157
1158**参数:**
1159
1160  | 参数名       | 类型            | 必填  | 说明                |
1161  | ------------- | --------------- | ---- | ------------------- |
1162  | credentialInfo| string          | 是    | 导入凭据信息。       |
1163  | callback      | AsyncCallback<{resultInfo: string}>           | 是    | 导入凭据结果回调。 |
1164
1165**示例:**
1166
1167  ```ts
1168  import { BusinessError } from '@ohos.base'
1169
1170  class Data {
1171    resultInfo: string = ""
1172  }
1173
1174  interface CredentialData {
1175    credentialType: number,
1176    credentialId: string,
1177    serverPk: string,
1178    pkInfoSignature : string,
1179    pkInfo: string,
1180    authCode: string,
1181    peerDeviceId: string
1182  }
1183
1184  interface CredentialInfo {
1185    processType: number,
1186    authType: number,
1187    userId: string,
1188    deviceId: string,
1189    version: string,
1190    devicePk : string,
1191    credentialData : CredentialData
1192  }
1193
1194  let credentialData: CredentialData = {
1195    credentialType: 2,
1196    credentialId: "102",
1197    serverPk: "3059301306072A8648CE3D020106082A8648CE3D03",
1198    pkInfoSignature : "30440220490BCB4F822004C9A76AB8D97F80041FC0E",
1199    pkInfo: "",
1200    authCode: "",
1201    peerDeviceId: ""
1202  }
1203
1204  let credentialInfo: CredentialInfo = {
1205    processType: 1,
1206    authType: 1,
1207    userId: "123",
1208    deviceId: "aaa",
1209    version: "1.2.3",
1210    devicePk : "0000",
1211    credentialData : credentialData
1212  }
1213
1214  try {
1215    let jsonCredentialInfo = JSON.stringify(credentialInfo);
1216    dmInstance.importCredential(jsonCredentialInfo, (err: BusinessError, data: Data) => {
1217      if (data) {
1218          console.info("importCredential result:" + JSON.stringify(data));
1219      } else {
1220          console.info("importCredential result: data is null");
1221      }
1222    });
1223  } catch (err) {
1224    let e: BusinessError = err as BusinessError;
1225    console.error("importCredential err:" + e.code + "," + e.message);
1226  }
1227  ```
1228
1229### deleteCredential<sup>10+</sup>
1230
1231deleteCredential(queryInfo: string, callback: AsyncCallback<{resultInfo: string}>): void;
1232
1233删除凭据信息。
1234
1235**需要权限**:ohos.permission.ACCESS_SERVICE_DM
1236
1237**系统能力**:SystemCapability.DistributedHardware.DeviceManager
1238
1239**参数:**
1240
1241  | 参数名       | 类型            | 必填  | 说明                |
1242  | ------------- | --------------- | ---- | ------------------- |
1243  | queryInfo     | string          | 是    | 删除凭据信息。       |
1244  | callback      | AsyncCallback<{resultInfo: string}>           | 是    | 删除凭据结果回调。 |
1245
1246**示例:**
1247
1248  ```ts
1249  import { BusinessError } from '@ohos.base'
1250
1251  class Data {
1252    resultInfo: string = ""
1253  }
1254
1255  interface QueryInfo {
1256    processType: number,
1257    authType: number,
1258    userId: string
1259  }
1260
1261  let queryInfo: QueryInfo = {
1262    processType: 1,
1263    authType: 1,
1264    userId: "123"
1265  }
1266
1267  try {
1268    let jsonQueryInfo = JSON.stringify(queryInfo);
1269    dmInstance.deleteCredential(jsonQueryInfo, (err: BusinessError, data: Data) => {
1270      if (data) {
1271          console.info("deleteCredential result:" + JSON.stringify(data));
1272      } else {
1273          console.info("deleteCredential result: data is null");
1274      }
1275    });
1276  } catch (err) {
1277    let e: BusinessError = err as BusinessError;
1278    console.error("deleteCredential err:" + e.code + "," + e.message);
1279  }
1280  ```
1281
1282### on('uiStateChange')<sup>9+</sup>
1283
1284on(type: 'uiStateChange', callback: Callback&lt;{ param: string}&gt;): void;
1285
1286ui状态变更回调。
1287
1288**需要权限**:ohos.permission.ACCESS_SERVICE_DM
1289
1290**系统能力**:SystemCapability.DistributedHardware.DeviceManager
1291
1292**参数:**
1293
1294  | 参数名      | 类型                             | 必填 | 说明                            |
1295  | -------- | ------------------------------------ | ---- | ------------------------------ |
1296  | type     | string                                | 是  | 注册的设备管理器 ui 状态回调,以便在状态改变时通知应用。 |
1297  | callback | Callback&lt;{&nbsp;param:&nbsp;string}&gt; | 是  | 指示要注册的设备管理器 ui 状态回调,返回ui状态。        |
1298
1299**示例:**
1300
1301  ```ts
1302  import { BusinessError } from '@ohos.base'
1303
1304  class Data {
1305    param: string = ""
1306  }
1307
1308  interface TmpStr {
1309    verifyFailed: boolean
1310  }
1311
1312  try {
1313    dmInstance.on('uiStateChange', (data: Data) => {
1314    console.log("uiStateChange executed, dialog closed" + JSON.stringify(data))
1315    let tmpStr: TmpStr = JSON.parse(data.param)
1316    let isShow = tmpStr.verifyFailed
1317    console.log("uiStateChange executed, dialog closed" + isShow)
1318  });
1319  } catch (err) {
1320    let e: BusinessError = err as BusinessError;
1321    console.error("uiStateChange errCode:" + e.code + ",errMessage:" + e.message);
1322  }
1323  ```
1324
1325### off('uiStateChange')<sup>9+</sup>
1326
1327off(type: 'uiStateChange', callback?: Callback&lt;{ param: string}&gt;): void;
1328
1329取消ui状态变更回调。
1330
1331**需要权限**:ohos.permission.ACCESS_SERVICE_DM
1332
1333**系统能力**:SystemCapability.DistributedHardware.DeviceManager
1334
1335**参数:**
1336
1337  | 参数名      | 类型                              | 必填 | 说明                            |
1338  | -------- | ------------------------------------- | ---- | ------------------------------ |
1339  | type     | string                                | 是   | 取消注册的设备管理器 ui 状态回调。 |
1340  | callback | Callback&lt;{&nbsp;param:&nbsp;string}&gt; | 否   | 指示要取消注册的设备管理器 ui 状态,返回UI状态。 |
1341
1342**示例:**
1343
1344  ```ts
1345  import { BusinessError } from '@ohos.base'
1346
1347  try {
1348    dmInstance.off('uiStateChange');
1349  } catch (err) {
1350    let e: BusinessError = err as BusinessError;
1351    console.error("uiStateChange errCode:" + e.code + ",errMessage:" + e.message);
1352  }
1353  ```
1354
1355### on('deviceStateChange')
1356
1357on(type: 'deviceStateChange',  callback: Callback&lt;{ action: DeviceStateChangeAction, device: DeviceInfo }&gt;): void
1358
1359注册设备状态回调。
1360
1361**需要权限**:ohos.permission.ACCESS_SERVICE_DM
1362
1363**系统能力**:SystemCapability.DistributedHardware.DeviceManager
1364
1365**参数:**
1366
1367  | 参数名       | 类型                                     | 必填   | 说明                             |
1368  | -------- | ---------------------------------------- | ---- | ------------------------------ |
1369  | type     | string                                   | 是    | 注册设备状态回调,固定为deviceStateChange。 |
1370  | callback | Callback&lt;{&nbsp;action:&nbsp;[DeviceStateChangeAction](#devicestatechangeaction),&nbsp;device:&nbsp;[DeviceInfo](#deviceinfo)&nbsp;}&gt; | 是    | 指示要注册的设备状态回调,返回设备状态和设备信息。      |
1371
1372**示例:**
1373
1374  ```ts
1375  import deviceManager from '@ohos.distributedHardware.deviceManager';
1376  import { BusinessError } from '@ohos.base'
1377
1378  class Data {
1379    action: deviceManager.DeviceStateChangeAction = 0
1380    device: deviceManager.DeviceInfo = {
1381      deviceId: "",
1382      deviceName: "",
1383      deviceType: 0,
1384      networkId: "",
1385      range: 0,
1386      authForm:0,
1387    }
1388  }
1389
1390  try {
1391    dmInstance.on('deviceStateChange', (data: Data) => {
1392      console.info("deviceStateChange on:" + JSON.stringify(data));
1393    });
1394  } catch (err) {
1395    let e: BusinessError = err as BusinessError;
1396    console.error("deviceStateChange errCode:" + e.code + ",errMessage:" + e.message);
1397  }
1398  ```
1399
1400### off('deviceStateChange')
1401
1402off(type: 'deviceStateChange', callback?: Callback&lt;{ action: DeviceStateChangeAction, device: DeviceInfo }&gt;): void
1403
1404取消注册设备状态回调。
1405
1406**需要权限**:ohos.permission.ACCESS_SERVICE_DM
1407
1408**系统能力**:SystemCapability.DistributedHardware.DeviceManager
1409
1410**参数:**
1411
1412  | 参数名       | 类型                                     | 必填   | 说明                          |
1413  | -------- | ---------------------------------------- | ---- | --------------------------- |
1414  | type     | string                                   | 是    | 根据应用程序的包名取消注册设备状态回调。        |
1415  | callback | Callback&lt;{&nbsp;action:&nbsp;[DeviceStateChangeAction](#devicestatechangeaction),&nbsp;device:&nbsp;[DeviceInfo](#deviceinfo)&nbsp;}&gt; | 否    | 指示要取消注册的设备状态回调,返回设备状态和设备信息。 |
1416
1417**示例:**
1418
1419  ```ts
1420  import deviceManager from '@ohos.distributedHardware.deviceManager';
1421  import { BusinessError } from '@ohos.base'
1422
1423  class Data {
1424    action: deviceManager.DeviceStateChangeAction = 0
1425    device: deviceManager.DeviceInfo = {
1426      deviceId: "",
1427      deviceName: "",
1428      deviceType: 0,
1429      networkId: "",
1430      range: 0,
1431      authForm:0,
1432    }
1433  }
1434
1435  try {
1436    dmInstance.off('deviceStateChange', (data: Data) => {
1437      console.info('deviceStateChange' + JSON.stringify(data));
1438    });
1439  } catch (err) {
1440    let e: BusinessError = err as BusinessError;
1441    console.error("deviceStateChange errCode:" + e.code + ",errMessage:" + e.message);
1442  }
1443  ```
1444
1445### on('deviceFound')
1446
1447on(type: 'deviceFound', callback: Callback&lt;{ subscribeId: number, device: DeviceInfo }&gt;): void
1448
1449注册发现设备回调监听。
1450
1451**需要权限**:ohos.permission.ACCESS_SERVICE_DM
1452
1453**系统能力**:SystemCapability.DistributedHardware.DeviceManager
1454
1455**参数:**
1456
1457  | 参数名       | 类型                                     | 必填   | 说明                         |
1458  | -------- | ---------------------------------------- | ---- | -------------------------- |
1459  | type     | string                                   | 是    | 注册设备发现回调,以便在发现周边设备时通知应用程序。 |
1460  | callback | Callback&lt;{&nbsp;subscribeId:&nbsp;number,&nbsp;device:&nbsp;[DeviceInfo](#deviceinfo)&nbsp;}&gt; | 是    | 注册设备发现的回调方法。               |
1461
1462**示例:**
1463
1464  ```ts
1465  import deviceManager from '@ohos.distributedHardware.deviceManager';
1466  import { BusinessError } from '@ohos.base'
1467
1468  class Data {
1469    subscribeId: number = 0
1470    device: deviceManager.DeviceInfo = {
1471      deviceId: "",
1472      deviceName: "",
1473      deviceType: 0,
1474      networkId: "",
1475      range: 0,
1476      authForm:0,
1477    }
1478  }
1479
1480  try {
1481    dmInstance.on('deviceFound', (data: Data) => {
1482      console.info("deviceFound:" + JSON.stringify(data));
1483    });
1484  } catch (err) {
1485    let e: BusinessError = err as BusinessError;
1486    console.error("deviceFound errCode:" + e.code + ",errMessage:" + e.message);
1487  }
1488  ```
1489
1490### off('deviceFound')
1491
1492off(type: 'deviceFound', callback?: Callback&lt;{ subscribeId: number, device: DeviceInfo }&gt;): void
1493
1494取消注册设备发现回调。
1495
1496**需要权限**:ohos.permission.ACCESS_SERVICE_DM
1497
1498**系统能力**:SystemCapability.DistributedHardware.DeviceManager
1499
1500**参数:**
1501
1502  | 参数名       | 类型                                     | 必填   | 说明                          |
1503  | -------- | ---------------------------------------- | ---- | --------------------------- |
1504  | type     | string                                   | 是    | 取消注册设备发现回调。                 |
1505  | callback | Callback&lt;{&nbsp;subscribeId:&nbsp;number,&nbsp;device:&nbsp;[DeviceInfo](#deviceinfo)&nbsp;}&gt; | 否    | 指示要取消注册的设备发现回调,返回设备状态和设备信息。 |
1506
1507**示例:**
1508
1509  ```ts
1510  import deviceManager from '@ohos.distributedHardware.deviceManager';
1511  import { BusinessError } from '@ohos.base'
1512
1513  class Data {
1514    subscribeId: number = 0
1515    device: deviceManager.DeviceInfo = {
1516      deviceId: "",
1517      deviceName: "",
1518      deviceType: 0,
1519      networkId: "",
1520      range: 0,
1521      authForm:0,
1522    }
1523  }
1524
1525  try {
1526    dmInstance.off('deviceFound', (data: Data) => {
1527      console.info('deviceFound' + JSON.stringify(data));
1528    });
1529  } catch (err) {
1530    let e: BusinessError = err as BusinessError;
1531    console.error("deviceFound errCode:" + e.code + ",errMessage:" + e.message);
1532  }
1533  ```
1534
1535### on('discoverFail')
1536
1537on(type: 'discoverFail', callback: Callback&lt;{ subscribeId: number, reason: number }&gt;): void
1538
1539注册设备发现失败回调监听。
1540
1541**需要权限**:ohos.permission.ACCESS_SERVICE_DM
1542
1543**系统能力**:SystemCapability.DistributedHardware.DeviceManager
1544
1545**参数:**
1546
1547  | 参数名       | 类型                                     | 必填   | 说明                             |
1548  | -------- | ---------------------------------------- | ---- | ------------------------------ |
1549  | type     | string                                   | 是    | 注册设备发现失败回调,以便在发现周边设备失败时通知应用程序。 |
1550  | callback | Callback&lt;{&nbsp;subscribeId:&nbsp;number,&nbsp;reason:&nbsp;number&nbsp;}&gt; | 是    | 注册设备发现失败的回调方法。                 |
1551
1552**示例:**
1553
1554  ```ts
1555  import { BusinessError } from '@ohos.base'
1556
1557  class Data {
1558    subscribeId: number = 0
1559    reason: number = 0
1560  }
1561
1562  try {
1563    dmInstance.on('discoverFail', (data: Data) => {
1564        console.info("discoverFail on:" + JSON.stringify(data));
1565    });
1566  } catch (err) {
1567    let e: BusinessError = err as BusinessError;
1568    console.error("discoverFail errCode:" + e.code + ",errMessage:" + e.message);
1569  }
1570  ```
1571
1572### off('discoverFail')
1573
1574off(type: 'discoverFail', callback?: Callback&lt;{ subscribeId: number, reason: number }&gt;): void
1575
1576取消注册设备发现失败回调。
1577
1578**需要权限**:ohos.permission.ACCESS_SERVICE_DM
1579
1580**系统能力**:SystemCapability.DistributedHardware.DeviceManager
1581
1582**参数:**
1583
1584  | 参数名       | 类型                                     | 必填   | 说明                |
1585  | -------- | ---------------------------------------- | ---- | ----------------- |
1586  | type     | string                                   | 是    | 取消注册设备发现失败回调。     |
1587  | callback | Callback&lt;{&nbsp;subscribeId:&nbsp;number,&nbsp;reason:&nbsp;number&nbsp;}&gt; | 否    | 指示要取消注册的设备发现失败回调。 |
1588
1589**示例:**
1590
1591  ```ts
1592  import { BusinessError } from '@ohos.base'
1593
1594  class Data {
1595    subscribeId: number = 0
1596    reason: number = 0
1597  }
1598
1599  try {
1600    dmInstance.off('discoverFail', (data: Data) => {
1601      console.info('discoverFail' + JSON.stringify(data));
1602    });
1603  } catch (err) {
1604    let e: BusinessError = err as BusinessError;
1605    console.error("discoverFail errCode:" + e.code + ",errMessage:" + e.message);
1606  }
1607  ```
1608
1609### on('publishSuccess')<sup>9+</sup>
1610
1611on(type: 'publishSuccess', callback: Callback&lt;{ publishId: number }&gt;): void
1612
1613注册发布设备发现回调监听。
1614
1615**需要权限**:ohos.permission.ACCESS_SERVICE_DM
1616
1617**系统能力**:SystemCapability.DistributedHardware.DeviceManager
1618
1619**参数:**
1620
1621  | 参数名     | 类型                                 | 必填 | 说明                       |
1622  | -------- | ---------------------------------------- | ---- | -------------------------- |
1623  | type     | string                                   | 是   | 注册发布设备成功回调,以便将发布成功时通知应用程序。 |
1624  | callback | Callback&lt;{&nbsp;publishId:&nbsp;number&nbsp;}&gt;    | 是   | 注册设备发布成功的回调方法。               |
1625
1626
1627**示例:**
1628
1629  ```ts
1630  import { BusinessError } from '@ohos.base'
1631
1632  class Data {
1633    publishId: number = 0
1634  }
1635
1636  try {
1637    dmInstance.on('publishSuccess', (data: Data) => {
1638      console.info("publishSuccess:" + JSON.stringify(data));
1639    });
1640  } catch (err) {
1641    let e: BusinessError = err as BusinessError;
1642    console.error("publishSuccess errCode:" + e.code + ",errMessage:" + e.message);
1643  }
1644  ```
1645
1646### off('publishSuccess')<sup>9+</sup>
1647
1648off(type: 'publishSuccess', callback?: Callback&lt;{ publishId: number }&gt;): void
1649
1650取消注册设备发布成功回调。
1651
1652**需要权限**:ohos.permission.ACCESS_SERVICE_DM
1653
1654**系统能力**:SystemCapability.DistributedHardware.DeviceManager
1655
1656**参数:**
1657
1658  | 参数名     | 类型                                 | 必填 | 说明                          |
1659  | -------- | ---------------------------------------- | ---- | --------------------------- |
1660  | type     | string                                   | 是   | 取消注册设备发布成功回调。                 |
1661  | callback | Callback&lt;{&nbsp;publishId:&nbsp;number&nbsp;}&gt;    | 否   | 指示要取消注册的设备发布成功回调。 |
1662
1663**示例:**
1664
1665  ```ts
1666  import { BusinessError } from '@ohos.base'
1667
1668  class Data {
1669    publishId: number = 0
1670  }
1671
1672  try {
1673    dmInstance.off('publishSuccess', (data: Data) => {
1674      console.info('publishSuccess' + JSON.stringify(data));
1675    });
1676  } catch (err) {
1677    let e: BusinessError = err as BusinessError;
1678    console.error("publishSuccess errCode:" + e.code + ",errMessage:" + e.message);
1679  }
1680  ```
1681
1682### on('publishFail')<sup>9+</sup>
1683
1684on(type: 'publishFail', callback: Callback&lt;{ publishId: number, reason: number }&gt;): void
1685
1686注册设备发布失败回调监听。
1687
1688**需要权限**:ohos.permission.ACCESS_SERVICE_DM
1689
1690**系统能力**:SystemCapability.DistributedHardware.DeviceManager
1691
1692**参数:**
1693
1694  | 参数名     | 类型                                              | 必填 | 说明                             |
1695  | -------- | ----------------------------------------------------- | ---- | ------------------------------ |
1696  | type     | string                                                | 是   | 注册设备发布失败回调,以便在发布设备失败时通知应用程序。 |
1697  | callback | Callback&lt;{&nbsp;publishId:&nbsp;number,&nbsp;reason:&nbsp;number&nbsp;}&gt; | 是   | 注册设备发布失败的回调方法。                 |
1698
1699**示例:**
1700
1701  ```ts
1702  import { BusinessError } from '@ohos.base'
1703
1704  class Data {
1705    publishId: number = 0
1706    reason: number = 0
1707  }
1708
1709  try {
1710    dmInstance.on('publishFail', (data: Data) => {
1711      console.info("publishFail on:" + JSON.stringify(data));
1712    });
1713  } catch (err) {
1714    let e: BusinessError = err as BusinessError;
1715    console.error("publishFail errCode:" + e.code + ",errMessage:" + e.message);
1716  }
1717  ```
1718
1719### off('publishFail')<sup>9+</sup>
1720
1721off(type: 'publishFail', callback?: Callback&lt;{ publishId: number, reason: number }&gt;): void
1722
1723取消注册设备发布失败回调。
1724
1725**需要权限**:ohos.permission.ACCESS_SERVICE_DM
1726
1727**系统能力**:SystemCapability.DistributedHardware.DeviceManager
1728
1729**参数:**
1730
1731  | 参数名     | 类型                                              | 必填 | 说明                |
1732  | -------- | ----------------------------------------------------- | ---- | ----------------- |
1733  | type     | string                                                | 是   | 取消注册设备发布失败回调。     |
1734  | callback | Callback&lt;{&nbsp;publishId:&nbsp;number,&nbsp;reason:&nbsp;number&nbsp;}&gt; | 否   | 指示要取消注册设备发布失败回调。 |
1735
1736**示例:**
1737
1738  ```ts
1739  import { BusinessError } from '@ohos.base'
1740
1741  class Data {
1742    publishId: number = 0
1743    reason: number = 0
1744  }
1745
1746  try {
1747    dmInstance.off('publishFail', (data: Data) => {
1748      console.info('publishFail' + JSON.stringify(data));
1749    });
1750  } catch (err) {
1751    let e: BusinessError = err as BusinessError;
1752    console.error("publishFail errCode:" + e.code + ",errMessage:" + e.message);
1753  }
1754  ```
1755
1756### on('serviceDie')
1757
1758on(type: 'serviceDie', callback: () =&gt; void): void
1759
1760注册设备管理服务死亡监听。
1761
1762**需要权限**:ohos.permission.ACCESS_SERVICE_DM
1763
1764**系统能力**:SystemCapability.DistributedHardware.DeviceManager
1765
1766**参数:**
1767
1768  | 参数名       | 类型                    | 必填   | 说明                                       |
1769  | -------- | ----------------------- | ---- | ---------------------------------------- |
1770  | type     | string                  | 是    | 注册serviceDie回调,以便在devicemanager服务异常终止时通知应用程序。 |
1771  | callback | ()&nbsp;=&gt;&nbsp;void | 是    | 注册serviceDie的回调方法。                       |
1772
1773**示例:**
1774
1775  ```ts
1776  import { BusinessError } from '@ohos.base'
1777
1778  try {
1779    dmInstance.on("serviceDie", () => {
1780      console.info("serviceDie on");
1781    });
1782  } catch (err) {
1783    let e: BusinessError = err as BusinessError;
1784    console.error("serviceDie errCode:" + e.code + ",errMessage:" + e.message);
1785  }
1786  ```
1787
1788### off('serviceDie')
1789
1790off(type: 'serviceDie', callback?: () =&gt; void): void
1791
1792取消注册设备管理服务死亡监听。
1793
1794**需要权限**:ohos.permission.ACCESS_SERVICE_DM
1795
1796**系统能力**:SystemCapability.DistributedHardware.DeviceManager
1797
1798**参数:**
1799
1800  | 参数名       | 类型                    | 必填   | 说明                                       |
1801  | -------- | ----------------------- | ---- | ---------------------------------------- |
1802  | type     | string                  | 是    | 取消注册serviceDie回调,以便在devicemanager服务异常终止时通知应用程序。 |
1803  | callback | ()&nbsp;=&gt;&nbsp;void | 否    | 取消注册serviceDie的回调方法。                     |
1804
1805**示例:**
1806
1807  ```ts
1808  import { BusinessError } from '@ohos.base'
1809
1810  try {
1811    dmInstance.off("serviceDie", () => {
1812      console.info("serviceDie off");
1813    });
1814  } catch (err) {
1815    let e: BusinessError = err as BusinessError;
1816    console.error("serviceDie errCode:" + e.code + ",errMessage:" + e.message);
1817  }
1818  ```