1# @ohos.distributedHardware.deviceManager (Device Management) 2 3The APIs of this module are deprecated. Use [@ohos.distributedDeviceManager](js-apis-distributedDeviceManager.md) instead. 4 5The **deviceManager** module provides APIs for distributed device management. 6 7System applications can call the APIs to do the following: 8 9- Subscribe to or unsubscribe from device state changes. 10- Discover peripheral untrusted devices. 11- Authenticate or deauthenticate a device. 12- Query the trusted device list. 13- Query local device information, including the device name, type, and ID. 14- Publish device information for discovery purposes. 15 16> **NOTE** 17> 18> - The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. 19> - The APIs of this module are system APIs and cannot be called by third-party applications. 20 21 22## Modules to Import 23 24```ts 25import deviceManager from '@ohos.distributedHardware.deviceManager'; 26``` 27 28 29## deviceManager.createDeviceManager 30 31createDeviceManager(bundleName: string, callback: AsyncCallback<DeviceManager>): void 32 33Creates a **DeviceManager** instance. 34 35**System capability**: SystemCapability.DistributedHardware.DeviceManager 36 37**Parameters** 38 39| Name | Type | Mandatory| Description | 40| ---------- | ---------------------------------------------------- | ---- | ----------------------------------------------------------- | 41| bundleName | string | Yes | Bundle name of the application. | 42| callback | AsyncCallback<[DeviceManager](#devicemanager)> | Yes | Callback invoked to return the **DeviceManager** instance created.| 43 44**Example** 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 68Defines device information. 69 70**System capability**: SystemCapability.DistributedHardware.DeviceManager 71 72| Name | Type | Mandatory | Description | 73| ---------------------- | ------------------------- | ---- | -------- | 74| deviceId | string | Yes | Unique identifier of the device.| 75| deviceName | string | Yes | Device name. | 76| deviceType | [DeviceType](#devicetype) | Yes | Device type. | 77| networkId<sup>8+</sup> | string | Yes | Network ID of the device. | 78| range<sup>9+</sup> | number | Yes | Distance between the discovered device and the device that initiates device discovery. | 79| authForm<sup>10+</sup> | [AuthForm](#authform10) | Yes | Authentication type of the device. | 80 81## DeviceType 82 83Enumerates the device types. 84 85**System capability**: SystemCapability.DistributedHardware.DeviceManager 86 87| Name | Value | Description | 88| ------------ | ---- | ---- | 89| SPEAKER | 0x0A | Smart speaker.| 90| PHONE | 0x0E | Phone. | 91| TABLET | 0x11 | Tablet. | 92| WEARABLE | 0x6D | Wearable.| 93| TV | 0x9C | Smart TV. | 94| CAR | 0x83 | Car. | 95| UNKNOWN_TYPE | 0 | Unknown device type.| 96 97## AuthForm<sup>10+</sup> 98 99Enumerates the device authentication types. 100 101**System capability**: SystemCapability.DistributedHardware.DeviceManager 102 103| Name | Value | Description | 104| ------------------- | ---- | --------------- | 105| INVALID_TYPE | -1 | No authentication.| 106| PEER_TO_PEER | 0 | Point-to-point authentication for devices without accounts. | 107| IDENTICAL_ACCOUNT | 1 | Authentication for devices using the same account. | 108| ACROSS_ACCOUNT | 2 | Authentication for devices using different accounts.| 109 110## DeviceStateChangeAction 111 112Enumerates the device states. 113 114**System capability**: SystemCapability.DistributedHardware.DeviceManager 115 116| Name | Value | Description | 117| ------- | ---- | --------------- | 118| ONLINE | 0 | The device is physically online. | 119| READY | 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| OFFLINE | 2 | The device is physically offline. | 121| CHANGE | 3 | The device information is changed. | 122 123## SubscribeInfo 124 125Defines subscription information. 126 127**System capability**: SystemCapability.DistributedHardware.DeviceManager 128 129| Name | Type | Mandatory | Description | 130| ------------- | --------------------------------- | ---- | ----------------- | 131| subscribeId | number | Yes | Subscription ID, used to identify a device discovery period.| 132| mode | [DiscoverMode ](#discovermode) | Yes | Device discovery mode. | 133| medium | [ExchangeMedium](#exchangemedium) | Yes | Medium used for device discovery. | 134| freq | [ExchangeFreq](#exchangefreq) | Yes | Frequency of device discovery. | 135| isSameAccount | boolean | No | Whether the same account is used on the discovered device. | 136| isWakeRemote | boolean | No | Whether to wake up the discovered device. | 137| capability | [SubscribeCap](#subscribecap) | Yes | Discovery capability. | 138 139 140## DiscoverMode 141 142Enumerates the device discovery modes. 143 144**System capability**: SystemCapability.DistributedHardware.DeviceManager 145 146| Name | Value | Description | 147| --------------------- | ---- | ----- | 148| DISCOVER_MODE_PASSIVE | 0x55 | Passive discovery.| 149| DISCOVER_MODE_ACTIVE | 0xAA | Active discovery.| 150 151 152## ExchangeMedium 153 154Enumerates the media used for device discovery. 155 156**System capability**: SystemCapability.DistributedHardware.DeviceManager 157 158| Name | Value | Description | 159| ---- | ---- | --------- | 160| AUTO | 0 | Automatic. | 161| BLE | 1 | Bluetooth. | 162| COAP | 2 | Wi-Fi.| 163| USB | 3 | USB. | 164 165## ExchangeFreq 166 167Enumerates the device discovery frequencies. 168 169**System capability**: SystemCapability.DistributedHardware.DeviceManager 170 171| Name | Value | Description | 172| ---------- | ---- | ----- | 173| LOW | 0 | Low frequency. | 174| MID | 1 | Medium frequency. | 175| HIGH | 2 | High frequency. | 176| SUPER_HIGH | 3 | Ultra-high frequency.| 177 178 179## SubscribeCap 180 181Enumerates the discovery capabilities. 182 183**System capability**: SystemCapability.DistributedHardware.DeviceManager 184 185| Name | Value | Description | 186| ------------------------- | ---- | -------------- | 187| SUBSCRIBE_CAPABILITY_DDMP | 0 | DDMP capability. This will be deprecated later.| 188| SUBSCRIBE_CAPABILITY_OSD | 1 | OSD capability. | 189 190 191## AuthParam 192 193Defines the authentication parameters. 194 195**System capability**: SystemCapability.DistributedHardware.DeviceManager 196 197| Name | Type | Mandatory | Description | 198| --------- | -------------------- | ---- | ---------- | 199| authType | number | Yes | Authentication type. | 200| extraInfo | {[key:string] : any} | No | Extended field. The default value is **undefined**.| 201 202## AuthInfo 203 204Defines authentication information. 205 206**System capability**: SystemCapability.DistributedHardware.DeviceManager 207 208| Name | Type | Mandatory | Description | 209| --------- | -------------------- | ---- | ---------- | 210| authType | number | Yes | Authentication type. | 211| token | number | Yes | Authentication token. | 212| extraInfo | {[key:string] : any} | No | Extended field. The default value is **undefined**.| 213 214## PublishInfo<sup>9+</sup> 215 216Defines published device information. 217 218**System capability**: SystemCapability.DistributedHardware.DeviceManager 219 220| Name | Type | Mandatory | Description | 221| ------------- | --------------------------------- | ---- | ----------------- | 222| publishId | number | Yes | ID used to identify a publication period.| 223| mode | [DiscoverMode ](#discovermode) | Yes | Device discovery mode. | 224| freq | [ExchangeFreq](#exchangefreq) | Yes | Frequency of device discovery. | 225| ranging | boolean | Yes | Whether the device supports distance reporting. | 226 227## DeviceManager 228 229Provides 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**. 230 231### release 232 233release(): void 234 235Releases this **DeviceManager** instance when it is no longer used. 236 237**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 238 239**System capability**: SystemCapability.DistributedHardware.DeviceManager 240 241**Error codes** 242 243For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md). 244 245| ID| Error Message | 246| -------- | --------------------------------------------------------------- | 247| 11600101 | Failed to execute the function. | 248 249**Example** 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<DeviceInfo> 265 266Obtains all trusted devices synchronously. 267 268**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 269 270**System capability**: SystemCapability.DistributedHardware.DeviceManager 271 272**Return value** 273 274| Name | Description | 275| -------------------------------------- | --------- | 276| Array<[DeviceInfo](#deviceinfo)> | List of trusted devices obtained.| 277 278**Error codes** 279 280For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md). 281 282| ID| Error Message | 283| -------- | --------------------------------------------------------------- | 284| 11600101 | Failed to execute the function. | 285 286**Example** 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<DeviceInfo> 303 304Enables the DSoftBus heartbeat mode to quickly bring offline trusted devices online and updates the list of online trusted devices. 305 306**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 307 308**System capability**: SystemCapability.DistributedHardware.DeviceManager 309 310**Parameters** 311 312| Name | Type | Mandatory| Description | 313| ------------- | --------------------------------- | ---- | ---------------------------------- | 314| isRefresh | boolean | Yes | Whether to enable the heartbeat mode and update the list of online trusted devices. | 315 316**Return value** 317 318| Name | Description | 319| -------------------------------------- | ---------------- | 320| Array<[DeviceInfo](#deviceinfo)> | List of trusted devices obtained.| 321 322**Error codes** 323 324For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md). 325 326| ID| Error Message | 327| -------- | --------------------------------------------------------------- | 328| 11600101 | Failed to execute the function. | 329 330**Example** 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<Array<DeviceInfo>>): void 347 348Obtains all trusted devices. This API uses an asynchronous callback to return the result. 349 350**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 351 352**System capability**: SystemCapability.DistributedHardware.DeviceManager 353 354**Parameters** 355 356| Name | Type | Mandatory | Description | 357| -------- | ---------------------------------------- | ---- | --------------------- | 358| callback | AsyncCallback<Array<[DeviceInfo](#deviceinfo)>> | Yes | Callback invoked to return the list of trusted devices.| 359 360**Example** 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<Array<DeviceInfo>> 382 383Obtains all trusted devices. This API uses a promise to return the result. 384 385**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 386 387**System capability**: SystemCapability.DistributedHardware.DeviceManager 388 389**Return value** 390 391| Type | Description | 392| ---------------------------------------- | --------------------- | 393| Promise<Array<[DeviceInfo](#deviceinfo)>> | Promise used to return the result.| 394 395**Example** 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 412Obtains local device information synchronously. 413 414**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 415 416**System capability**: SystemCapability.DistributedHardware.DeviceManager 417 418**Return value** 419 420| Name | Description | 421| ------------------------- | ---------------- | 422| [DeviceInfo](#deviceinfo) | List of local devices obtained.| 423 424**Error codes** 425 426For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md). 427 428| ID| Error Message | 429| -------- | --------------------------------------------------------------- | 430| 11600101 | Failed to execute the function. | 431 432**Example** 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<DeviceInfo>): void 449 450Obtains local device information. This API uses an asynchronous callback to return the result. 451 452**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 453 454**System capability**: SystemCapability.DistributedHardware.DeviceManager 455 456**Parameters** 457 458| Name | Type | Mandatory | Description | 459| -------- | ---------------------------------------- | ---- | --------- | 460| callback | AsyncCallback<[DeviceInfo](#deviceinfo)> | Yes | Callback invoked to return the local device information.| 461 462**Example** 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<DeviceInfo> 486 487Obtains local device information. This API uses a promise to return the result. 488 489**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 490 491**System capability**: SystemCapability.DistributedHardware.DeviceManager 492 493**Return value** 494 495| Type | Description | 496| ---------------------------------------- | --------------------- | 497| Promise<[DeviceInfo](#deviceinfo)> | Promise used to return the result.| 498 499**Example** 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<DeviceInfo>): void 515 516Obtains the information about a specific device based on the network ID. This API uses an asynchronous callback to return the result. 517 518**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 519 520**System capability**: SystemCapability.DistributedHardware.DeviceManager 521 522**Parameters** 523 524| Name | Type | Mandatory | Description | 525| -------- | ---------------------------------------- | ---- | --------- | 526| networkId| string | Yes | Network ID of the device.| 527| callback | AsyncCallback<[DeviceInfo](#deviceinfo)> | Yes | Callback invoked to return the information about the specified device.| 528 529**Example** 530 531 ```ts 532 import deviceManager from '@ohos.distributedHardware.deviceManager'; 533 import { BusinessError } from '@ohos.base' 534 535 try { 536 // Network ID of the device, which can be obtained from the trusted device list 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<DeviceInfo> 554 555Obtains the information about a specific device based on the network ID. This API uses a promise to return the result. 556 557**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 558 559**System capability**: SystemCapability.DistributedHardware.DeviceManager 560 561**Parameters** 562 563| Name | Type | Mandatory| Description | 564| -------- | ---------------------------------------- | ---- | --------- | 565| networkId| string | Yes | Network ID of the device.| 566 567**Return value** 568 569| Type | Description | 570| ---------------------------------------- | --------------------- | 571| Promise<[DeviceInfo](#deviceinfo)> | Promise used to return the result.| 572 573**Example** 574 575 ```ts 576 import deviceManager from '@ohos.distributedHardware.deviceManager'; 577 import { BusinessError } from '@ohos.base' 578 579 // Network ID of the device, which can be obtained from the trusted device list 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 592Starts to discover peripheral devices. The discovery process lasts 2 minutes. A maximum of 99 devices can be discovered. 593 594**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 595 596**System capability**: SystemCapability.DistributedHardware.DeviceManager 597 598**Parameters** 599 600| Name | Type | Mandatory| Description | 601| ------------- | ------------------------------- | ---- | ----- | 602| subscribeInfo | [SubscribeInfo](#subscribeinfo) | Yes | Subscription information.| 603 604**Error codes** 605 606For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md). 607 608| ID| Error Message | 609| -------- | --------------------------------------------------------------- | 610| 11600101 | Failed to execute the function. | 611| 11600104 | Discovery invalid. | 612 613**Example** 614 615 ```ts 616 import { BusinessError } from '@ohos.base' 617 618 interface SubscribeInfo { 619 subscribeId: number, 620 mode: number, // Active discovery 621 medium: number, // Automatic. Multiple media can be used for device discovery. 622 freq: number, // High frequency 623 isSameAccount: boolean, 624 isWakeRemote: boolean, 625 capability: number 626 }; 627 628 // Automatically generate a unique subscription ID. 629 let subscribeId = Math.floor(Math.random() * 10000 + 1000); 630 let subscribeInfo: SubscribeInfo = { 631 subscribeId: subscribeId, 632 mode: 0xAA, // Active discovery 633 medium: 0, // Automatic. Multiple media can be used for device discovery. 634 freq: 2, // High frequency 635 isSameAccount: false, 636 isWakeRemote: false, 637 capability: 1 638 }; 639 try { 640 dmInstance.startDeviceDiscovery(subscribeInfo); // The deviceFound callback is called to notify the application when a device is discovered. 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 651Starts to discover peripheral devices and filters discovered devices. The discovery process lasts 2 minutes. A maximum of 99 devices can be discovered. 652 653**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 654 655**System capability**: SystemCapability.DistributedHardware.DeviceManager 656 657**Parameters** 658 659| Name | Type | Mandatory | Description | 660| ------------- | ------------------------------- | ---- | ----- | 661| subscribeInfo | [SubscribeInfo](#subscribeinfo) | Yes | Subscription information.| 662| filterOptions | string | No | Options for filtering discovered devices. The default value is **undefined**, which indicates discovery of offline devices.| 663 664**Error codes** 665 666For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md). 667 668| ID| Error Message | 669| -------- | --------------------------------------------------------------- | 670| 11600101 | Failed to execute the function. | 671| 11600104 | Discovery invalid. | 672 673**Example** 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, // Optional. The default value is OR. 685 filters: Filters[] 686 } 687 688 interface SubscribeInfo { 689 subscribeId: number, 690 mode: number, // Active discovery 691 medium: number, // Automatic. Multiple media can be used for device discovery. 692 freq: number, // High frequency 693 isSameAccount: boolean, 694 isWakeRemote: boolean, 695 capability: number 696 } 697 698 // Automatically generate a unique subscription ID. 699 let subscribeId = Math.floor(Math.random() * 10000 + 1000); 700 let subscribeInfo: SubscribeInfo = { 701 subscribeId: subscribeId, 702 mode: 0xAA, // Active discovery 703 medium: 0, // Automatic. Multiple media can be used for device discovery. 704 freq: 2, // High frequency 705 isSameAccount: false, 706 isWakeRemote: false, 707 capability: 1 708 }; 709 710 let filters: Filters[] = [ 711 { 712 type: "range", 713 value: 50 // Filter discovered devices based on the distance (in cm). 714 } 715 ] 716 717 let filterOptions: FilterOptions = { 718 filter_op: "OR", // Optional. The default value is OR. 719 filters: filters 720 }; 721 try { 722 dmInstance.startDeviceDiscovery(subscribeInfo, JSON.stringify(filterOptions)); // The deviceFound callback is invoked to notify the application when a device is discovered. 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 733Stops device discovery. 734 735**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 736 737**System capability**: SystemCapability.DistributedHardware.DeviceManager 738 739**Parameters** 740 741| Name | Type | Mandatory | Description | 742| ----------- | ------ | ---- | ----- | 743| subscribeId | number | Yes | Subscription ID.| 744 745**Error codes** 746 747For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md). 748 749| ID| Error Message | 750| -------- | --------------------------------------------------------------- | 751| 11600101 | Failed to execute the function. | 752 753**Example** 754 755 ```ts 756 import { BusinessError } from '@ohos.base' 757 758 try { 759 // stopDeviceDiscovery and startDeviceDiscovery must be used in pairs, and the input parameter **subscribeId** passed in them must be the same. 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 772Publishes device information for discovery purposes. The publish process lasts 2 minutes. 773 774**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 775 776**System capability**: SystemCapability.DistributedHardware.DeviceManager 777 778**Parameters** 779 780| Name | Type | Mandatory| Description | 781| ------------- | ------------------------------- | ---- | ----- | 782| publishInfo | [PublishInfo](#publishinfo9) | Yes | Device information to publish.| 783 784**Error codes** 785 786For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md). 787 788| ID| Error Message | 789| -------- | --------------------------------------------------------------- | 790| 11600101 | Failed to execute the function. | 791| 11600105 | Publish invalid. | 792 793**Example** 794 795 ```ts 796 import { BusinessError } from '@ohos.base' 797 798 interface PublishInfo { 799 publishId: number, 800 mode: number, // Active discovery 801 freq: number, // High frequency 802 ranging: boolean // Whether the device supports reporting the distance to the discovery initiator. 803 }; 804 805 // Automatically generate a unique subscription ID. 806 let publishId = Math.floor(Math.random() * 10000 + 1000); 807 let publishInfo: PublishInfo = { 808 publishId: publishId, 809 mode: 0xAA, // Active discovery 810 freq: 2, // High frequency 811 ranging: true // The device supports reporting the distance to the discovery initiator. 812 }; 813 814 try { 815 dmInstance.publishDeviceDiscovery(publishInfo); // A callback is invoked to notify the application when the device information is published. 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 826Stops publishing device information. 827 828**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 829 830**System capability**: SystemCapability.DistributedHardware.DeviceManager 831 832**Parameters** 833 834| Name | Type| Mandatory| Description | 835| ----------- | -------- | ---- | ----- | 836| publishId | number | Yes | Publish ID.| 837 838**Error codes** 839 840For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md). 841 842| ID| Error Message | 843| -------- | --------------------------------------------------------------- | 844| 11600101 | Failed to execute the function. | 845 846**Example** 847 848 ```ts 849 import { BusinessError } from '@ohos.base' 850 851 try { 852 // unPublishDeviceDiscovery and publishDeviceDiscovery must be used in pairs, and the input parameter **publishId** passed in them must be the same. 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<{deviceId: string, pinToken ?: number}>): void 864 865Authenticates a device. 866 867**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 868 869**System capability**: SystemCapability.DistributedHardware.DeviceManager 870 871**Parameters** 872 873| Name | Type | Mandatory | Description | 874| ---------- | ---------------------------------------- | ---- | ------- | 875| deviceInfo | [DeviceInfo](#deviceinfo) | Yes | Device information. | 876| authParam | [AuthParam](#authparam) | Yes | Authentication parameter. | 877| callback | AsyncCallback<{deviceId: string, pinToken ?: number}> | Yes | Callback invoked to return the authentication result.| 878 879**Example** 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,//Authentication type. The value 1 means PIN authentication. 906 extraInfo: ExtraInfo 907 } 908 909 // Information about the device to authenticate. The information can be obtained from the device discovery result. 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,// Authentication type. The value 1 means no account PIN authentication. 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 948Deauthenticates a device. 949 950**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 951 952**System capability**: SystemCapability.DistributedHardware.DeviceManager 953 954**Parameters** 955 956| Name | Type | Mandatory | Description | 957| ---------- | ------------------------- | ---- | ----- | 958| deviceInfo | [DeviceInfo](#deviceinfo) | Yes | Device information.| 959 960**Error codes** 961 962For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md). 963 964| ID| Error Message | 965| -------- | --------------------------------------------------------------- | 966| 11600101 | Failed to execute the function. | 967 968**Example** 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<{deviceId: string, level: number}>): void 1000 1001Verifies authentication information. 1002 1003**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 1004 1005**System capability**: SystemCapability.DistributedHardware.DeviceManager 1006 1007**Parameters** 1008 1009| Name | Type | Mandatory | Description | 1010| -------- | ---------------------------------------- | ---- | ------- | 1011| authInfo | [AuthInfo](#authinfo) | Yes | Authentication information. | 1012| callback | AsyncCallback<{deviceId: string, level: number}> | Yes | Callback invoked to return the verification result.| 1013 1014**Example** 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 1063Sets a user operation. 1064 1065**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 1066 1067**System capability**: SystemCapability.DistributedHardware.DeviceManager 1068 1069**Parameters** 1070 1071| Name | Type | Mandatory | Description | 1072| ------------- | --------------- | ---- | ------------------- | 1073| operateAction | number | Yes | User operation. | 1074| params | string | Yes | Input parameters of the user.| 1075 1076**Example** 1077 1078 ```ts 1079 import { BusinessError } from '@ohos.base' 1080 1081 try { 1082 /* 1083 operateAction = 0 - Grant the permission. 1084 operateAction = 1 - Revoke the permission. 1085 operateAction = 2 - The user operation in the permission request dialog box times out. 1086 operateAction = 3 - Cancel the display of the PIN box. 1087 operateAction = 4 - Cancel the display of the PIN input box. 1088 operateAction = 5 - Confirm the input in the PIN input box. 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 1102Obtains the registration information of the credential. 1103 1104**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 1105 1106**System capability**: SystemCapability.DistributedHardware.DeviceManager 1107 1108**Parameters** 1109 1110| Name | Type | Mandatory | Description | 1111| ------------- | --------------- | ---- | ------------------- | 1112| requestInfo | string | Yes | Request credential information. | 1113| callback | AsyncCallback<{registerInfo: string}> | Yes | Callback invoked to return the credential registration information.| 1114 1115**Example** 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 1152Imports credential information. 1153 1154**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 1155 1156**System capability**: SystemCapability.DistributedHardware.DeviceManager 1157 1158**Parameters** 1159 1160| Name | Type | Mandatory | Description | 1161| ------------- | --------------- | ---- | ------------------- | 1162| credentialInfo| string | Yes | Credential information to import. | 1163| callback | AsyncCallback<{resultInfo: string}> | Yes | Callback invoked to return the result.| 1164 1165**Example** 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 1233Deletes credential information. 1234 1235**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 1236 1237**System capability**: SystemCapability.DistributedHardware.DeviceManager 1238 1239**Parameters** 1240 1241| Name | Type | Mandatory | Description | 1242| ------------- | --------------- | ---- | ------------------- | 1243| queryInfo | string | Yes | Credential information to delete. | 1244| callback | AsyncCallback<{resultInfo: string}> | Yes | Callback invoked to return the result.| 1245 1246**Example** 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<{ param: string}>): void; 1285 1286Subscribes to UI status changes. 1287 1288**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 1289 1290**System capability**: SystemCapability.DistributedHardware.DeviceManager 1291 1292**Parameters** 1293 1294| Name | Type | Mandatory| Description | 1295| -------- | ------------------------------------ | ---- | ------------------------------ | 1296| type | string | Yes | Event type. The value is **'uiStateChange'**, which indicates UI status changes.| 1297| callback | Callback<{ param: string}> | Yes | Callback invoked to return the UI status change. | 1298 1299**Example** 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<{ param: string}>): void; 1328 1329Unsubscribes from UI status changes. 1330 1331**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 1332 1333**System capability**: SystemCapability.DistributedHardware.DeviceManager 1334 1335**Parameters** 1336 1337| Name | Type | Mandatory| Description | 1338| -------- | ------------------------------------- | ---- | ------------------------------ | 1339| type | string | Yes | Event type. The value is **'uiStateChange'**, which indicates UI status changes.| 1340| callback | Callback<{ param: string}> | No | Callback to unregister.| 1341 1342**Example** 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<{ action: DeviceStateChangeAction, device: DeviceInfo }>): void 1358 1359Subscribes to device state changes. 1360 1361**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 1362 1363**System capability**: SystemCapability.DistributedHardware.DeviceManager 1364 1365**Parameters** 1366 1367| Name | Type | Mandatory | Description | 1368| -------- | ---------------------------------------- | ---- | ------------------------------ | 1369| type | string | Yes | Event type. The value is **deviceStateChange**, which indicates device state changes.| 1370| callback | Callback<{ action: [DeviceStateChangeAction](#devicestatechangeaction), device: [DeviceInfo](#deviceinfo) }> | Yes | Callback invoked to return the device state change. | 1371 1372**Example** 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<{ action: DeviceStateChangeAction, device: DeviceInfo }>): void 1403 1404Unsubscribes from device state changes. 1405 1406**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 1407 1408**System capability**: SystemCapability.DistributedHardware.DeviceManager 1409 1410**Parameters** 1411 1412| Name | Type | Mandatory | Description | 1413| -------- | ---------------------------------------- | ---- | --------------------------- | 1414| type | string | Yes | Event type. The value is **'deviceStateChange'**, which indicates device state changes. | 1415| callback | Callback<{ action: [DeviceStateChangeAction](#devicestatechangeaction), device: [DeviceInfo](#deviceinfo) }> | No | Callback to unregister.| 1416 1417**Example** 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<{ subscribeId: number, device: DeviceInfo }>): void 1448 1449Subscribes to device discovery events. 1450 1451**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 1452 1453**System capability**: SystemCapability.DistributedHardware.DeviceManager 1454 1455**Parameters** 1456 1457| Name | Type | Mandatory | Description | 1458| -------- | ---------------------------------------- | ---- | -------------------------- | 1459| type | string | Yes | Event type. The value is **'deviceFound'**, which indicates discovery of a device.| 1460| callback | Callback<{ subscribeId: number, device: [DeviceInfo](#deviceinfo) }> | Yes | Callback invoked to return the discovery of a device. | 1461 1462**Example** 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<{ subscribeId: number, device: DeviceInfo }>): void 1493 1494Unsubscribes from device discovery events. 1495 1496**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 1497 1498**System capability**: SystemCapability.DistributedHardware.DeviceManager 1499 1500**Parameters** 1501 1502| Name | Type | Mandatory | Description | 1503| -------- | ---------------------------------------- | ---- | --------------------------- | 1504| type | string | Yes | Event type. The value is **'deviceFound'**, which indicates discovery of a device. | 1505| callback | Callback<{ subscribeId: number, device: [DeviceInfo](#deviceinfo) }> | No | Callback to unregister.| 1506 1507**Example** 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<{ subscribeId: number, reason: number }>): void 1538 1539Subscribes to device discovery failures. 1540 1541**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 1542 1543**System capability**: SystemCapability.DistributedHardware.DeviceManager 1544 1545**Parameters** 1546 1547| Name | Type | Mandatory | Description | 1548| -------- | ---------------------------------------- | ---- | ------------------------------ | 1549| type | string | Yes | Event type. The value is **'discoverFail'**, which indicates a failure in discovering devices.| 1550| callback | Callback<{ subscribeId: number, reason: number }> | Yes | Callback invoked to return a device discovery failure. | 1551 1552**Example** 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<{ subscribeId: number, reason: number }>): void 1575 1576Unsubscribes from device discovery failures. 1577 1578**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 1579 1580**System capability**: SystemCapability.DistributedHardware.DeviceManager 1581 1582**Parameters** 1583 1584| Name | Type | Mandatory | Description | 1585| -------- | ---------------------------------------- | ---- | ----------------- | 1586| type | string | Yes | Event type. The value is **'discoverFail'**, which indicates a failure in discovering devices. | 1587| callback | Callback<{ subscribeId: number, reason: number }> | No | Callback to unregister.| 1588 1589**Example** 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<{ publishId: number }>): void 1612 1613Subscribes to device information publication success events. 1614 1615**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 1616 1617**System capability**: SystemCapability.DistributedHardware.DeviceManager 1618 1619**Parameters** 1620 1621| Name | Type | Mandatory| Description | 1622| -------- | ---------------------------------------- | ---- | -------------------------- | 1623| type | string | Yes | Event type. The value is **'publishSuccess'**, which indicates an event of the success in publishing device information.| 1624| callback | Callback<{ publishId: number }> | Yes | Callback invoked to return the publish ID. | 1625 1626 1627**Example** 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<{ publishId: number }>): void 1649 1650Unsubscribes from device information publication success events. 1651 1652**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 1653 1654**System capability**: SystemCapability.DistributedHardware.DeviceManager 1655 1656**Parameters** 1657 1658| Name | Type | Mandatory| Description | 1659| -------- | ---------------------------------------- | ---- | --------------------------- | 1660| type | string | Yes | Event type. The value is **'publishSuccess'**, which indicates an event of the success in publishing device information. | 1661| callback | Callback<{ publishId: number }> | No | Callback to unregister.| 1662 1663**Example** 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<{ publishId: number, reason: number }>): void 1685 1686Subscribes to device information publication failures. 1687 1688**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 1689 1690**System capability**: SystemCapability.DistributedHardware.DeviceManager 1691 1692**Parameters** 1693 1694| Name | Type | Mandatory| Description | 1695| -------- | ----------------------------------------------------- | ---- | ------------------------------ | 1696| type | string | Yes | Event type. The value is **'publishFail'**, which indicates a failure in publishing device information.| 1697| callback | Callback<{ publishId: number, reason: number }> | Yes | Callback invoked to return a failure in publishing device information. | 1698 1699**Example** 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<{ publishId: number, reason: number }>): void 1722 1723Unsubscribes from device information publication failures. 1724 1725**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 1726 1727**System capability**: SystemCapability.DistributedHardware.DeviceManager 1728 1729**Parameters** 1730 1731| Name | Type | Mandatory| Description | 1732| -------- | ----------------------------------------------------- | ---- | ----------------- | 1733| type | string | Yes | Event type. The value is **'publishFail'**, which indicates a failure in publishing device information. | 1734| callback | Callback<{ publishId: number, reason: number }> | No | Callback to unregister.| 1735 1736**Example** 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: () => void): void 1759 1760Subscribes to dead events of the **DeviceManager** service. 1761 1762**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 1763 1764**System capability**: SystemCapability.DistributedHardware.DeviceManager 1765 1766**Parameters** 1767 1768| Name | Type | Mandatory | Description | 1769| -------- | ----------------------- | ---- | ---------------------------------------- | 1770| type | string | Yes | Event type. The value is **'serviceDie'**, which indicates unexpected termination of the **DeviceManager** service.| 1771| callback | () => void | Yes | Callback invoked when the **DeviceManager** service is unexpectedly terminated. | 1772 1773**Example** 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?: () => void): void 1791 1792Unsubscribes from dead events of the **DeviceManager** service. 1793 1794**Required permissions**: ohos.permission.ACCESS_SERVICE_DM 1795 1796**System capability**: SystemCapability.DistributedHardware.DeviceManager 1797 1798**Parameters** 1799 1800| Name | Type | Mandatory | Description | 1801| -------- | ----------------------- | ---- | ---------------------------------------- | 1802| type | string | Yes | Event type. The value is **'serviceDie'**, which indicates unexpected termination of the **DeviceManager** service.| 1803| callback | () => void | No | Callback to unregister. | 1804 1805**Example** 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 ``` 1819