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