• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.distributedDeviceManager (Device Management)
2
3The **distributedDeviceManager** module provides APIs for distributed device management.
4
5Applications can call the APIs to:
6
7- Subscribe to or unsubscribe from device state changes.
8- Discover untrusted devices nearby.
9- Authenticate or deauthenticate a device.
10- Query the trusted device list.
11- Query local device information, including the device name, type, and ID.
12
13
14> **NOTE**
15>
16> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
17
18
19## Modules to Import
20
21```ts
22import deviceManager from '@ohos.distributedDeviceManager';
23```
24
25
26## deviceManager.createDeviceManager
27
28createDeviceManager(bundleName: string): DeviceManager;
29
30Creates a **DeviceManager** instance. The **DeviceManager** instance is the entry for invoking the APIs for distributed device management. It can be used to obtain information about trusted devices and local devices.
31
32**System capability**: SystemCapability.DistributedHardware.DeviceManager
33
34**Parameters**
35
36| Name    | Type                                                | Mandatory| Description                                                       |
37| ---------- | ---------------------------------------------------- | ---- | ----------------------------------------------------------- |
38| bundleName | string                                               | Yes  | Bundle name of the application.                                 |
39
40**Return value**
41
42| Type                                       | Description       |
43| ------------------------------------------- | --------- |
44| [DeviceManager](#devicemanager) | **DeviceManager** instance created.|
45
46**Example**
47
48  ```ts
49  import deviceManager from '@ohos.distributedDeviceManager';
50  import { BusinessError } from '@ohos.base';
51
52  try {
53    let dmInstance = deviceManager.createDeviceManager('ohos.samples.jsHelloWorld');
54  } catch(err) {
55    let e: BusinessError = err as BusinessError;
56    console.error('createDeviceManager errCode:' + e.code + ',errMessage:' + e.message);
57  }
58  ```
59
60## deviceManager.releaseDeviceManager
61
62releaseDeviceManager(deviceManager: DeviceManager): void;
63
64Releases a **DeviceManager** instance that is no longer used.
65
66**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
67
68**System capability**: SystemCapability.DistributedHardware.DeviceManager
69
70**Parameters**
71
72| Name    | Type                                                | Mandatory| Description                               |
73| ---------- | ---------------------------------------------------- | ---- | --------------------------------- |
74| deviceManager | [DeviceManager](#devicemanager)    | Yes  | **DeviceManager** instance to release.                                 |
75
76**Error codes**
77
78For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).
79
80| ID| Error Message                                                       |
81| -------- | --------------------------------------------------------------- |
82| 11600101 | Failed to execute the function.                                 |
83
84**Example**
85
86  ```ts
87  import { BusinessError } from '@ohos.base';
88  import deviceManager from '@ohos.distributedDeviceManager';
89
90  try {
91    let dmInstance = deviceManager.createDeviceManager('ohos.samples.jsHelloWorld');
92    deviceManager.releaseDeviceManager(dmInstance);
93  } catch (err) {
94    let e: BusinessError = err as BusinessError;
95    console.error('release device manager errCode:' + e.code + ',errMessage:' + e.message);
96  }
97  ```
98
99## DeviceBasicInfo
100
101Represents the basic information about a distributed device.
102
103**System capability**: SystemCapability.DistributedHardware.DeviceManager
104
105**Parameters**
106
107| Name                    | Type                       | Mandatory  | Description      |
108| ---------------------- | ------------------------- | ---- | -------- |
109| deviceId               | string                    | Yes   | Unique ID of the device. The value is the udid-hash (hash value of the UDID) and **appid** encrypted using SHA-256.|
110| deviceName             | string                    | Yes   | Device name.   |
111| deviceType             | string                    | Yes   | Device type.   |
112| networkId              | string                    | No   | Network ID of the device. |
113
114## DeviceStateChange
115
116Enumerates the device states.
117
118**System capability**: SystemCapability.DistributedHardware.DeviceManager
119
120**Parameters**
121
122| Name        | Value | Description             |
123| ----------- | ---- | --------------- |
124| UNKNOWN     | 0    | The device state is unknown after the device goes online. Before the device state changes to available, distributed services cannot be used.          |
125| AVAILABLE   | 1    | The information between devices has been synchronized in the Distributed Data Service (DDS) module, and the device is ready for running distributed services.|
126| UNAVAILABLE | 2    | The device goes offline, and the device state is unknown.          |
127
128
129## DeviceManager
130
131Provides APIs to obtain information about trusted devices and local devices. Before calling any API in **DeviceManager**, you must use **createDeviceManager** to create a **DeviceManager** instance, for example, **dmInstance**.
132
133### getAvailableDeviceListSync
134
135getAvailableDeviceListSync(): Array<DeviceBasicInfo>;
136
137Obtains all trusted devices synchronously.
138
139**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
140
141**System capability**: SystemCapability.DistributedHardware.DeviceManager
142
143**Return value**
144
145| Type                                       | Description       |
146| ------------------------------------------- | --------- |
147| Array<[DeviceBasicInfo](#devicebasicinfo)> | List of trusted devices obtained.|
148
149**Error codes**
150
151For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).
152
153| ID| Error Message                                                       |
154| -------- | --------------------------------------------------------------- |
155| 11600101 | Failed to execute the function.                                 |
156
157**Example**
158
159For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
160  ```ts
161  import deviceManager from '@ohos.distributedDeviceManager';
162  import { BusinessError } from '@ohos.base';
163
164  try {
165    let deviceInfoList: Array<deviceManager.DeviceBasicInfo> = dmInstance.getAvailableDeviceListSync();
166  } catch (err) {
167    let e: BusinessError = err as BusinessError;
168    console.error('getAvailableDeviceListSync errCode:' + e.code + ',errMessage:' + e.message);
169  }
170  ```
171
172### getAvailableDeviceList
173
174getAvailableDeviceList(callback:AsyncCallback&lt;Array&lt;DeviceBasicInfo&gt;&gt;): void;
175
176Obtains all trusted devices. This API uses an asynchronous callback to return the result.
177
178**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
179
180**System capability**: SystemCapability.DistributedHardware.DeviceManager
181
182**Parameters**
183
184| Name      | Type                                    | Mandatory  | Description                   |
185| -------- | ---------------------------------------- | ---- | --------------------- |
186| callback | AsyncCallback&lt;Array&lt;[DeviceBasicInfo](#devicebasicinfo)&gt;&gt; | Yes   | Callback invoked to return the list of trusted devices.|
187
188**Error codes**
189
190For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).
191
192| ID| Error Message                                                       |
193| -------- | --------------------------------------------------------------- |
194| 11600101 | Failed to execute the function.                                 |
195
196**Example**
197
198For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
199  ```ts
200  import deviceManager from '@ohos.distributedDeviceManager';
201  import { BusinessError } from '@ohos.base';
202
203  try {
204    dmInstance.getAvailableDeviceList((err: BusinessError, data: Array<deviceManager.DeviceBasicInfo>) => {
205      if (err) {
206        console.error('getAvailableDeviceList errCode:' + err.code + ',errMessage:' + err.message);
207        return;
208      }
209      console.log('get available device info: ' + JSON.stringify(data));
210    });
211  } catch (err) {
212    let e: BusinessError = err as BusinessError;
213    console.error('getAvailableDeviceList errCode:' + e.code + ',errMessage:' + e.message);
214  }
215  ```
216
217### getAvailableDeviceList
218
219getAvailableDeviceList(): Promise&lt;Array&lt;DeviceBasicInfo&gt;&gt;;
220
221Obtains all trusted devices. This API uses a promise to return the result.
222
223**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
224
225**System capability**: SystemCapability.DistributedHardware.DeviceManager
226
227**Return value**
228
229| Type                                                      | Description                              |
230| ---------------------------------------------------------- | ---------------------------------- |
231| Promise&lt;Array&lt;[DeviceBasicInfo](#devicebasicinfo)&gt;&gt; | Promise used to return the result.|
232
233**Error codes**
234
235For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).
236
237| ID| Error Message                                                       |
238| -------- | --------------------------------------------------------------- |
239| 11600101 | Failed to execute the function.                                 |
240
241**Example**
242
243For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
244  ```ts
245  import deviceManager from '@ohos.distributedDeviceManager';
246  import { BusinessError } from '@ohos.base';
247
248  dmInstance.getAvailableDeviceList().then((data: Array<deviceManager.DeviceBasicInfo>) => {
249    console.log('get available device info: ' + JSON.stringify(data));
250    }).catch((err: BusinessError) => {
251      console.error('getAvailableDeviceList errCode:' + err.code + ',errMessage:' + err.message);
252  });
253  ```
254
255### getLocalDeviceNetworkId
256
257getLocalDeviceNetworkId(): string;
258
259Obtains the network ID of the local device.
260
261**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
262
263**System capability**: SystemCapability.DistributedHardware.DeviceManager
264
265**Return value**
266
267| Type                     | Description             |
268| ------------------------- | ---------------- |
269| string | Network ID of the local device obtained.|
270
271**Error codes**
272
273For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).
274
275| ID| Error Message                                                       |
276| -------- | --------------------------------------------------------------- |
277| 11600101 | Failed to execute the function.                                 |
278
279**Example**
280
281For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
282  ```ts
283  import { BusinessError } from '@ohos.base';
284
285  try {
286    let deviceNetworkId: string = dmInstance.getLocalDeviceNetworkId();
287    console.log('local device networkId: ' + JSON.stringify(deviceNetworkId));
288  } catch (err) {
289    let e: BusinessError = err as BusinessError;
290    console.error('getLocalDeviceNetworkId errCode:' + e.code + ',errMessage:' + e.message);
291  }
292  ```
293
294### getLocalDeviceName
295
296getLocalDeviceName(): string;
297
298Obtains the local device name.
299
300**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
301
302**System capability**: SystemCapability.DistributedHardware.DeviceManager
303
304**Return value**
305
306| Type                     | Description             |
307| ------------------------- | ---------------- |
308| string                    | Name of the local device obtained.|
309
310**Error codes**
311
312For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).
313
314| ID| Error Message                                                       |
315| -------- | --------------------------------------------------------------- |
316| 11600101 | Failed to execute the function.                                 |
317
318**Example**
319
320For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
321  ```ts
322  import { BusinessError } from '@ohos.base';
323
324  try {
325    let deviceName: string = dmInstance.getLocalDeviceName();
326    console.log('local device name: ' + JSON.stringify(deviceName));
327  } catch (err) {
328    let e: BusinessError = err as BusinessError;
329    console.error('getLocalDeviceName errCode:' + e.code + ',errMessage:' + e.message);
330  }
331  ```
332
333### getLocalDeviceType
334
335getLocalDeviceType(): number;
336
337Obtains the local device type.
338
339**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
340
341**System capability**: SystemCapability.DistributedHardware.DeviceManager
342
343**Return value**
344
345| Type                     | Description             |
346| ------------------------- | ---------------- |
347| number                    | Local device type obtained.|
348
349**Error codes**
350
351For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).
352
353| ID| Error Message                                                       |
354| -------- | --------------------------------------------------------------- |
355| 11600101 | Failed to execute the function.                                 |
356
357**Example**
358
359For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
360  ```ts
361  import { BusinessError } from '@ohos.base';
362
363  try {
364    let deviceType: number = dmInstance.getLocalDeviceType();
365    console.log('local device type: ' + JSON.stringify(deviceType));
366  } catch (err) {
367    let e: BusinessError = err as BusinessError;
368    console.error('getLocalDeviceType errCode:' + e.code + ',errMessage:' + e.message);
369  }
370  ```
371
372### getLocalDeviceId
373
374getLocalDeviceId(): string;
375
376Obtains the local device ID.
377
378**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
379
380**System capability**: SystemCapability.DistributedHardware.DeviceManager
381
382**Return value**
383
384| Type                     | Description             |
385| ------------------------- | ---------------- |
386| string                    | Local device ID obtained.|
387
388**Error codes**
389
390For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).
391
392| ID| Error Message                                                       |
393| -------- | --------------------------------------------------------------- |
394| 11600101 | Failed to execute the function.                                 |
395
396**Example**
397
398For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
399  ```ts
400  import { BusinessError } from '@ohos.base';
401
402  try {
403    let deviceId: string = dmInstance.getLocalDeviceId();
404    console.log('local device id: ' + JSON.stringify(deviceId));
405  } catch (err) {
406    let e: BusinessError = err as BusinessError;
407    console.error('getLocalDeviceId errCode:' + e.code + ',errMessage:' + e.message);
408  }
409  ```
410
411### getDeviceName
412
413getDeviceName(networkId: string): string;
414
415Obtains the device name based on the network ID of the specified device.
416
417**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
418
419**System capability**: SystemCapability.DistributedHardware.DeviceManager
420
421**Parameters**
422
423| Name      | Type                                    | Mandatory  | Description       |
424| -------- | ---------------------------------------- | ---- | --------- |
425| networkId| string                                   | Yes  | Network ID of the device.|
426
427**Return value**
428
429| Type                     | Description             |
430| ------------------------- | ---------------- |
431| string                    | Device name obtained.|
432
433**Error codes**
434
435For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).
436
437| ID| Error Message                                                       |
438| -------- | --------------------------------------------------------------- |
439| 11600101 | Failed to execute the function.                                 |
440
441**Example**
442
443For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
444  ```ts
445  import { BusinessError } from '@ohos.base';
446
447  try {
448    // Network ID of the device, which can be obtained from the trusted device list.
449    let networkId = 'xxxxxxx';
450    let deviceName: string = dmInstance.getDeviceName(networkId);
451    console.log('device name: ' + JSON.stringify(deviceName));
452  } catch (err) {
453    let e: BusinessError = err as BusinessError;
454    console.error('getDeviceName errCode:' + e.code + ',errMessage:' + e.message);
455  }
456  ```
457
458### getDeviceType
459
460getDeviceType(networkId: string): number;
461
462Obtains the device type based on the network ID of the specified device.
463
464**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
465
466**System capability**: SystemCapability.DistributedHardware.DeviceManager
467
468**Parameters**
469
470| Name      | Type                                    | Mandatory  | Description       |
471| -------- | ---------------------------------------- | ---- | --------- |
472| networkId| string                                   | Yes  | Network ID of the device.|
473
474**Return value**
475
476| Type                     | Description             |
477| ------------------------- | ---------------- |
478| number                    | Device type obtained.|
479
480**Error codes**
481
482For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).
483
484| ID| Error Message                                                       |
485| -------- | --------------------------------------------------------------- |
486| 11600101 | Failed to execute the function.                                 |
487
488**Example**
489
490For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
491  ```ts
492  import { BusinessError } from '@ohos.base';
493
494  try {
495    // Network ID of the device, which can be obtained from the trusted device list.
496    let networkId = 'xxxxxxx';
497    let deviceType: number = dmInstance.getDeviceType(networkId);
498    console.log('device type: ' + JSON.stringify(deviceType));
499  } catch (err) {
500    let e: BusinessError = err as BusinessError;
501    console.error('getDeviceType errCode:' + e.code + ',errMessage:' + e.message);
502  }
503  ```
504
505### startDiscovering
506
507startDiscovering(discoverParam: {[key:&nbsp;string]:&nbsp;Object} , filterOptions?: {[key:&nbsp;string]:&nbsp;Object} ): void;
508
509Starts to discover devices nearby. The discovery process automatically stops when 2 minutes have elapsed. A maximum of 99 devices can be discovered.
510
511**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
512
513**System capability**: SystemCapability.DistributedHardware.DeviceManager
514
515**Parameters**
516
517| Name           | Type                       | Mandatory  | Description   |
518| ------------- | ------------------------------- | ---- | -----  |
519| discoverParam  | {[key:&nbsp;string]:&nbsp;Object}      | Yes  | Identifier of the device to discover. It specifies the type of the target to discover.<br>**discoverTargetType**: The default discovery target is device. The value is **1**.|
520| filterOptions | {[key:&nbsp;string]:&nbsp;Object}          | No  | Options for filtering the devices to discover. The default value is **undefined**, which means to discover offline devices. The options include the following:<br>**availableStatus(0-1)**: status of the device to discover. The value **0** means the device is untrusted.<br>- **0**: The device is offline. The client needs to call **bindTarget** to bind the device.<br>- **1**: The device is online and can be connected.<br>**discoverDistance(0-100)**: distance of the device to discover, in cm.<br>**authenticationStatus(0-1)**: authentication status of the device to discover.<br>- **0**: The device is not authenticated.<br>The value **1** means the device has been authenticated.<br>**authorizationType(0-2)**: authorization type of the device to discover.<br>- **0**: The device is authenticated by a temporarily agreed session key.<br>- **1**: The device is authenticated by a key of the same account.<br>- **2**: The device is authenticated by a credential key of different accounts. |
521
522**Error codes**
523
524For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).
525
526| ID| Error Message                                                       |
527| -------- | --------------------------------------------------------------- |
528| 11600101 | Failed to execute the function.                                 |
529| 11600104 | Discovery repeats.                                              |
530
531**Example**
532
533For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
534  ```ts
535  import { BusinessError } from '@ohos.base';
536
537  interface DiscoverParam {
538    discoverTargetType: number;
539  }
540
541  interface FilterOptions {
542    availableStatus: number;
543    discoverDistance: number;
544    authenticationStatus: number;
545    authorizationType: number;
546  }
547
548  let discoverParam: Record<string, number> = {
549    'discoverTargetType': 1
550  };
551  let filterOptions: Record<string, number> = {
552    'availableStatus': 0
553  };
554
555  try {
556    dmInstance.startDiscovering(discoverParam, filterOptions); // When devices are discovered, discoverSuccess is called to notify the application.
557  } catch (err) {
558    let e: BusinessError = err as BusinessError;
559    console.error('startDiscovering errCode:' + e.code + ',errMessage:' + e.message);
560  }
561  ```
562
563### stopDiscovering
564
565stopDiscovering(): void;
566
567Stops device discovery.
568
569**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
570
571**System capability**: SystemCapability.DistributedHardware.DeviceManager
572
573**Error codes**
574
575For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).
576
577| ID| Error Message                                                       |
578| -------- | --------------------------------------------------------------- |
579| 11600101 | Failed to execute the function.                                 |
580| 11600104 | Stop discovery repeats.                                         |
581
582**Example**
583
584For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
585  ```ts
586  import { BusinessError } from '@ohos.base';
587
588  try {
589    dmInstance.stopDiscovering();
590  } catch (err) {
591    let e: BusinessError = err as BusinessError;
592    console.error('stopDiscovering errCode:' + e.code + ',errMessage:' + e.message);
593  }
594  ```
595
596### bindTarget
597
598bindTarget(deviceId: string, bindParam: {[key:&nbsp;string]:&nbsp;Object} , callback: AsyncCallback&lt;{deviceId: string}>): void;
599
600Binds a device.
601
602**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
603
604**System capability**: SystemCapability.DistributedHardware.DeviceManager
605
606**Parameters**
607
608| Name    | Type                                               | Mandatory | Description        |
609| ---------- | --------------------------------------------------- | ----- | ------------ |
610| deviceId   | string                                              | Yes   | ID of the device to bind. |
611| bindParam  | {[key:&nbsp;string]:&nbsp;Object}                             | Yes   | Authentication parameters. You can determine the key-value pairs to be passed in. By default, the following keys are carried:<br>**bindType**: binding type, which is mandatory.<br>- **1**: PIN.<br>- **2**: QR code.<br>- **3**: NFC.<br>- **4**: No interaction.<br>**targetPkgName**: bundle name of the device to bind.<br>**appName**: application that attempts to bind the device.<br>**appOperation**: reason for the application to bind the device.<br>**customDescription**: detailed description of the operation.  |
612| callback   | AsyncCallback&lt;{deviceId:&nbsp;string,&nbsp;}&gt; | Yes   | Callback invoked to return the authentication result.|
613
614**Error codes**
615
616For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).
617
618| ID| Error Message                                                       |
619| -------- | --------------------------------------------------------------- |
620| 11600101 | Failed to execute the function.                                 |
621| 11600103 | Bind invalid.                                                   |
622
623**Example**
624
625For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
626  ```ts
627  import { BusinessError } from '@ohos.base';
628
629  class Data {
630    deviceId: string = '';
631  }
632
633  // Information about the device to authenticate. The information can be obtained from the device discovery result.
634  let deviceId = 'XXXXXXXX';
635  let bindParam: Record<string, string | number> = {
636    'bindType': 1, // Authentication type. The value 1 means PIN authentication.
637    'targetPkgName': 'xxxx',
638    'appName': 'xxxx',
639    'appOperation': 'xxxx',
640    'customDescription': 'xxxx'
641  };
642
643  try {
644    dmInstance.bindTarget(deviceId, bindParam, (err: BusinessError, data: Data) => {
645      if (err) {
646        console.error('bindTarget errCode:' + err.code + ',errMessage:' + err.message);
647        return;
648      }
649      console.info('bindTarget result:' + JSON.stringify(data));
650    });
651  } catch (err) {
652    let e: BusinessError = err as BusinessError;
653    console.error('bindTarget errCode:' + e.code + ',errMessage:' + e.message);
654  }
655  ```
656
657### unbindTarget
658
659unbindTarget(deviceId: string): void;
660
661Unbinds a device.
662
663**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
664
665**System capability**: SystemCapability.DistributedHardware.DeviceManager
666
667**Parameters**
668
669| Name  | Type                     | Mandatory| Description      |
670| -------- | ------------------------- | ---- | ---------- |
671| deviceId | string                    | Yes  | Device ID.|
672
673**Error codes**
674
675For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).
676
677| ID| Error Message                                                       |
678| -------- | --------------------------------------------------------------- |
679| 11600101 | Failed to execute the function.                                 |
680
681**Example**
682
683For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
684  ```ts
685  import { BusinessError } from '@ohos.base';
686
687  try {
688    let deviceId = 'XXXXXXXX';
689    dmInstance.unbindTarget(deviceId);
690  } catch (err) {
691    let e: BusinessError = err as BusinessError;
692    console.error('unbindTarget errCode:' + e.code + ',errMessage:' + e.message);
693  }
694  ```
695
696### replyUiAction
697
698replyUiAction(action: number, actionResult: string): void;
699
700Replies to the user's UI operation. This API can be used only by the PIN HAP of the **deviceManager**.
701
702**Required permissions**: ohos.permission.ACCESS_SERVICE_DM
703
704**System capability**: SystemCapability.DistributedHardware.DeviceManager
705
706**System API**: This is a system API.
707
708**Parameters**
709
710| Name      | Type           | Mandatory | Description               |
711| ------------- | --------------- | ---- | ------------------- |
712| action        | number          | Yes   | User operation.      |
713| actionResult        | string          | Yes   | Operation result.|
714
715**Example**
716
717For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
718  ```ts
719  import { BusinessError } from '@ohos.base';
720
721 try {
722    /*
723      action = 0 - Grant the permission.
724      action = 1 - Revoke the permission.
725      action = 2 - The user operation in the permission request dialog box times out.
726      action = 3 - Cancel the display of the PIN box.
727      action = 4 - Cancel the display of the PIN input box.
728      action = 5 - Confirm the input in the PIN input box.
729    */
730    let operation = 0;
731    dmInstance.replyUiAction(operation, 'extra');
732  } catch (err) {
733    let e: BusinessError = err as BusinessError;
734    console.error('replyUiAction errCode:' + e.code + ',errMessage:' + e.message);
735  }
736  ```
737
738### on('replyResult')
739
740on(type: 'replyResult', callback: Callback&lt;{ param: string}&gt;): void;
741
742Subscribes to the reply to the UI operation result.
743
744**Required permissions**: ohos.permission.ACCESS_SERVICE_DM
745
746**System capability**: SystemCapability.DistributedHardware.DeviceManager
747
748**System API**: This is a system API.
749
750**Parameters**
751
752| Name     | Type                            | Mandatory| Description                           |
753| -------- | ------------------------------------ | ---- | ------------------------------ |
754| type     | string                                | Yes | Event type, which has a fixed value of **'replyResult'**.|
755| callback | Callback&lt;{&nbsp;param:&nbsp;string}&gt; | Yes | Callback invoked to return the UI status.       |
756
757**Example**
758
759For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
760  ```ts
761  import { BusinessError } from '@ohos.base';
762
763  class Data {
764    param: string = '';
765  }
766
767  interface TmpStr {
768    verifyFailed: boolean;
769  }
770
771  try {
772    dmInstance.on('replyResult', (data: Data) => {
773      console.log('replyResult executed, dialog closed' + JSON.stringify(data));
774      let tmpStr: TmpStr = JSON.parse(data.param);
775      let isShow = tmpStr.verifyFailed;
776      console.log('replyResult executed, dialog closed' + isShow);
777    });
778  } catch (err) {
779    let e: BusinessError = err as BusinessError;
780    console.error('replyResult errCode:' + e.code + ',errMessage:' + e.message);
781  }
782  ```
783
784### off('replyResult')
785
786off(type: 'replyResult', callback?: Callback&lt;{ param: string}&gt;): void;
787
788Unsubscribes from the reply to the UI operation result.
789
790**Required permissions**: ohos.permission.ACCESS_SERVICE_DM
791
792**System capability**: SystemCapability.DistributedHardware.DeviceManager
793
794**System API**: This is a system API.
795
796**Parameters**
797
798| Name     | Type                             | Mandatory| Description                           |
799| -------- | ------------------------------------- | ---- | ------------------------------ |
800| type     | string                                | Yes  | Event type, which has a fixed value of **'replyResult'**.|
801| callback | Callback&lt;{&nbsp;param:&nbsp;string}&gt; | No  | Callback to unregister.|
802
803**Example**
804
805For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
806  ```ts
807  import { BusinessError } from '@ohos.base';
808
809  try {
810    dmInstance.off('replyResult');
811  } catch (err) {
812    let e: BusinessError = err as BusinessError;
813    console.error('replyResult errCode:' + e.code + ',errMessage:' + e.message);
814  }
815  ```
816
817### on('deviceStateChange')
818
819on(type: 'deviceStateChange', callback: Callback&lt;{ action: DeviceStateChange, device: DeviceBasicInfo }&gt;): void;
820
821Subscribes to the device state changes. The application (identified by the bundle name) will be notified when the device state changes.
822
823**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
824
825**System capability**: SystemCapability.DistributedHardware.DeviceManager
826
827**Parameters**
828
829| Name      | Type                                    | Mandatory  | Description                            |
830| -------- | ---------------------------------------- | ---- | ------------------------------ |
831| type     | string                                   | Yes   | Event type. The value **'deviceStateChange'** indicates device state changes.|
832| callback | Callback&lt;{&nbsp;action:&nbsp;[DeviceStateChange](#devicestatechange),&nbsp;device:&nbsp;[DeviceBasicInfo](#devicebasicinfo)&nbsp;}&gt; | Yes   | Callback invoked to return the device information and state.     |
833
834**Example**
835
836For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
837  ```ts
838  import deviceManager from '@ohos.distributedDeviceManager';
839  import { BusinessError } from '@ohos.base';
840
841  class Data {
842    action: deviceManager.DeviceStateChange = 0;
843    device: deviceManager.DeviceBasicInfo = {
844      deviceId: '',
845      deviceName: '',
846      deviceType: '',
847      networkId: ''
848    };
849  }
850
851  try {
852    dmInstance.on('deviceStateChange', (data: Data) => {
853      console.info('deviceStateChange on:' + JSON.stringify(data));
854    });
855  } catch (err) {
856    let e: BusinessError = err as BusinessError;
857    console.error('deviceStateChange errCode:' + e.code + ',errMessage:' + e.message);
858  }
859  ```
860
861### off('deviceStateChange')
862
863off(type: 'deviceStateChange', callback?: Callback&lt;{ action: DeviceStateChange, device: DeviceBasicInfo }&gt;): void;
864
865Unsubscribes from the device state changes.
866
867**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
868
869**System capability**: SystemCapability.DistributedHardware.DeviceManager
870
871**Parameters**
872
873| Name      | Type                                    | Mandatory  | Description                         |
874| -------- | ---------------------------------------- | ---- | --------------------------- |
875| type     | string                                   | Yes   | Event type. The value **'deviceStateChange'** indicates device state changes.       |
876| callback | Callback&lt;{&nbsp;action:&nbsp;[deviceStateChange](#devicestatechange),&nbsp;device:&nbsp;[DeviceBasicInfo](#devicebasicinfo)&nbsp;}&gt; | No   | Callback to unregister.|
877
878**Example**
879
880For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
881  ```ts
882  import deviceManager from '@ohos.distributedDeviceManager';
883  import { BusinessError } from '@ohos.base';
884
885  class Data {
886    action: deviceManager.DeviceStateChange = 0;
887    device: deviceManager.DeviceBasicInfo = {
888      deviceId: '',
889      deviceName: '',
890      deviceType: '',
891      networkId: ''
892    };
893  }
894
895  try {
896    dmInstance.off('deviceStateChange', (data: Data) => {
897      console.info('deviceStateChange' + JSON.stringify(data));
898    });
899  } catch (err) {
900    let e: BusinessError = err as BusinessError;
901    console.error('deviceStateChange errCode:' + e.code + ',errMessage:' + e.message);
902  }
903  ```
904
905### on('discoverSuccess')
906
907on(type: 'discoverSuccess', callback: Callback&lt;{ device: DeviceBasicInfo }&gt;): void;
908
909Subscribes to the **'discoverSuccess'** event. The application will be notified when a device is successfully discovered.
910
911**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
912
913**System capability**: SystemCapability.DistributedHardware.DeviceManager
914
915**Parameters**
916
917| Name      | Type                                    | Mandatory  | Description                        |
918| -------- | ---------------------------------------- | ---- | -------------------------- |
919| type     | string                                   | Yes   | Event type, which has a fixed value of **'discoverSuccess'**.|
920| callback | Callback&lt;{&nbsp;device:&nbsp;[DeviceBasicInfo](#devicebasicinfo)&nbsp;}&gt; | Yes   | Callback invoked when a device is successfully discovered.              |
921
922**Example**
923
924For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
925  ```ts
926  import deviceManager from '@ohos.distributedDeviceManager';
927  import { BusinessError } from '@ohos.base';
928
929  class Data {
930    device: deviceManager.DeviceBasicInfo = {
931      deviceId: '',
932      deviceName: '',
933      deviceType: '',
934      networkId: ''
935    };
936  }
937
938  try {
939    dmInstance.on('discoverSuccess', (data: Data) => {
940      console.info('discoverSuccess:' + JSON.stringify(data));
941    });
942  } catch (err) {
943    let e: BusinessError = err as BusinessError;
944    console.error('discoverSuccess errCode:' + e.code + ',errMessage:' + e.message);
945  }
946  ```
947
948### off('discoverSuccess')
949
950off(type: 'discoverSuccess', callback?: Callback&lt;{ device: DeviceBasicInfo }&gt;): void;
951
952Unsubscribes from the **'discoverSuccess'** event.
953
954**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
955
956**System capability**: SystemCapability.DistributedHardware.DeviceManager
957
958**Parameters**
959
960| Name      | Type                                    | Mandatory  | Description                         |
961| -------- | ---------------------------------------- | ---- | --------------------------- |
962| type     | string                                   | Yes   | Event type, which has a fixed value of **'discoverSuccess'**.                |
963| callback | Callback&lt;{&nbsp;device:&nbsp;[DeviceBasicInfo](#devicebasicinfo)&nbsp;}&gt; | No   | Callback to unregister.|
964
965**Example**
966
967  ```ts
968  import deviceManager from '@ohos.distributedDeviceManager';
969  import { BusinessError } from '@ohos.base';
970
971  class Data {
972    device: deviceManager.DeviceBasicInfo = {
973      deviceId: '',
974      deviceName: '',
975      deviceType: '',
976      networkId: ''
977    };
978  }
979
980  try {
981    dmInstance.off('discoverSuccess', (data: Data) => {
982      console.info('discoverSuccess' + JSON.stringify(data));
983    });
984  } catch (err) {
985    let e: BusinessError = err as BusinessError;
986    console.error('discoverSuccess errCode:' + e.code + ',errMessage:' + e.message);
987  }
988  ```
989
990### on('deviceNameChange')
991
992on(type: 'deviceNameChange', callback: Callback&lt;{ deviceName: string }&gt;): void;
993
994Subscribes to device name changes. The application will be notified when the name of a device is changed.
995
996**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
997
998**System capability**: SystemCapability.DistributedHardware.DeviceManager
999
1000**Parameters**
1001
1002| Name      | Type                                    | Mandatory  | Description                            |
1003| -------- | ---------------------------------------- | ---- | ------------------------------ |
1004| type     | string                                   | Yes   | Event type, which has a fixed value of **deviceNameChange**.|
1005| callback | Callback&lt;{&nbsp;deviceName:&nbsp;string}&gt; | Yes   | Callback invoked to return the device name change.                |
1006
1007**Example**
1008
1009For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
1010  ```ts
1011  import { BusinessError } from '@ohos.base';
1012
1013  class Data {
1014    deviceName: string = '';
1015  }
1016
1017  try {
1018    dmInstance.on('deviceNameChange', (data: Data) => {
1019      console.info('deviceNameChange on:' + JSON.stringify(data));
1020    });
1021  } catch (err) {
1022    let e: BusinessError = err as BusinessError;
1023    console.error('deviceNameChange errCode:' + e.code + ',errMessage:' + e.message);
1024  }
1025  ```
1026
1027### off('deviceNameChange')
1028
1029off(type: 'deviceNameChange', callback?: Callback&lt;{ deviceName: string }&gt;): void;
1030
1031Unsubscribes from the device name changes.
1032
1033**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
1034
1035**System capability**: SystemCapability.DistributedHardware.DeviceManager
1036
1037**Parameters**
1038
1039| Name      | Type                                    | Mandatory  | Description                            |
1040| -------- | ---------------------------------------- | ---- | ------------------------------ |
1041| type     | string                                   | Yes   | Event type, which has a fixed value of **deviceNameChange**.|
1042| callback | Callback&lt;{&nbsp;deviceName:&nbsp;string}&gt; | No   | Callback to unregister.                |
1043
1044**Example**
1045
1046For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
1047  ```ts
1048  import { BusinessError } from '@ohos.base';
1049
1050  class Data {
1051    deviceName: string = '';
1052  }
1053
1054  try {
1055    dmInstance.off('deviceNameChange', (data: Data) => {
1056      console.info('deviceNameChange' + JSON.stringify(data));
1057    });
1058  } catch (err) {
1059    let e: BusinessError = err as BusinessError;
1060    console.error('deviceNameChange errCode:' + e.code + ',errMessage:' + e.message);
1061  }
1062  ```
1063
1064### on('discoverFailure')
1065
1066on(type: 'discoverFailure', callback: Callback&lt;{ reason: number }&gt;): void;
1067
1068Subscribes to the **'discoverFailure'** event. The application will be notified when a device fails to be discovered.
1069
1070**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
1071
1072**System capability**: SystemCapability.DistributedHardware.DeviceManager
1073
1074**Parameters**
1075
1076| Name      | Type                                    | Mandatory  | Description                            |
1077| -------- | ---------------------------------------- | ---- | ------------------------------ |
1078| type     | string                                   | Yes   | Event type, which has a fixed value of **'discoverFailure'**.|
1079| callback | Callback&lt;{&nbsp;reason:&nbsp;number&nbsp;}&gt; | Yes   | Callback invoked when a device fails to be discovered.                |
1080
1081**Example**
1082
1083For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
1084  ```ts
1085  import { BusinessError } from '@ohos.base';
1086
1087  class Data {
1088    reason: number = 0;
1089  }
1090
1091  try {
1092    dmInstance.on('discoverFailure', (data: Data) => {
1093      console.info('discoverFailure on:' + JSON.stringify(data));
1094    });
1095  } catch (err) {
1096    let e: BusinessError = err as BusinessError;
1097    console.error('discoverFailure errCode:' + e.code + ',errMessage:' + e.message);
1098  }
1099  ```
1100
1101### off('discoverFailure')
1102
1103off(type: 'discoverFailure', callback?: Callback&lt;{ reason: number }&gt;): void;
1104
1105Unsubscribes from the **'discoverFailure'** event.
1106
1107**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
1108
1109**System capability**: SystemCapability.DistributedHardware.DeviceManager
1110
1111**Parameters**
1112
1113| Name      | Type                                    | Mandatory  | Description               |
1114| -------- | ---------------------------------------- | ---- | ----------------- |
1115| type     | string                                   | Yes   | Event type, which has a fixed value of **'discoverFailure'**.    |
1116| callback | Callback&lt;{&nbsp;reason:&nbsp;number&nbsp;}&gt; | No   | Callback to unregister.|
1117
1118**Example**
1119
1120For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
1121  ```ts
1122  import { BusinessError } from '@ohos.base';
1123
1124  class Data {
1125    reason: number = 0;
1126  }
1127
1128  try {
1129    dmInstance.off('discoverFailure', (data: Data) => {
1130      console.info('discoverFailure' + JSON.stringify(data));
1131    });
1132  } catch (err) {
1133    let e: BusinessError = err as BusinessError;
1134    console.error('discoverFailure errCode:' + e.code + ',errMessage:' + e.message);
1135  }
1136  ```
1137
1138### on('serviceDie')
1139
1140on(type: 'serviceDie', callback?: Callback&lt;{}&gt;): void;
1141
1142Subscribes to the dead events of the **DeviceManager** service. The application will be notified when the **DeviceManager** service is terminated unexpectedly.
1143
1144**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
1145
1146**System capability**: SystemCapability.DistributedHardware.DeviceManager
1147
1148**Parameters**
1149
1150| Name      | Type                   | Mandatory  | Description                                      |
1151| -------- | ----------------------- | ---- | ---------------------------------------- |
1152| type     | string                  | Yes   | Event type, which has a fixed value of **'serviceDie'**.|
1153| callback | Callback&lt;{}&gt; | No   | Callback invoked when the **DeviceManager** service is terminated unexpectedly.                       |
1154
1155**Example**
1156
1157For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
1158  ```ts
1159  import { BusinessError } from '@ohos.base';
1160
1161  try {
1162    dmInstance.on('serviceDie', () => {
1163      console.info('serviceDie on');
1164    });
1165  } catch (err) {
1166    let e: BusinessError = err as BusinessError;
1167    console.error('serviceDie errCode:' + e.code + ',errMessage:' + e.message);
1168  }
1169  ```
1170
1171### off('serviceDie')
1172
1173off(type: 'serviceDie', callback?: Callback&lt;{}&gt;): void;
1174
1175Unsubscribes from the dead events of the **DeviceManager** service.
1176
1177**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
1178
1179**System capability**: SystemCapability.DistributedHardware.DeviceManager
1180
1181**Parameters**
1182
1183| Name      | Type                   | Mandatory  | Description                                      |
1184| -------- | ----------------------- | ---- | ---------------------------------------- |
1185| type     | string                  | Yes   | Event type, which has a fixed value of **'serviceDie'**.|
1186| callback | Callback&lt;{}&gt; | No   | Callback to unregister.                    |
1187
1188**Example**
1189
1190For details about how to initialize **dmInstance** in the example, see [deviceManager.createDeviceManager](#devicemanagercreatedevicemanager).
1191  ```ts
1192  import { BusinessError } from '@ohos.base';
1193
1194  try {
1195    dmInstance.off('serviceDie', () => {
1196      console.info('serviceDie off');
1197    });
1198  } catch (err) {
1199    let e: BusinessError = err as BusinessError;
1200    console.error('serviceDie errCode:' + e.code + ',errMessage:' + e.message);
1201  }
1202  ```
1203