js-apis-device-manager.md 51.3 KB
Newer Older
G
gloria 已提交
1
# @ohos.distributedHardware.deviceManager (Device Management)
Z
zengyawen 已提交
2

S
shawn_he 已提交
3
The **deviceManager** module provides APIs for distributed device management.
4 5 6 7 8 9 10 11

System applications can call the APIs to do the following:

- Subscribe to or unsubscribe from device state changes.
- Discover peripheral untrusted devices.
- Authenticate or deauthenticate a device.
- Query the trusted device list.
- Query local device information, including the device name, type, and ID.
12
- Publish device information for discovery purposes.
G
Gloria 已提交
13

14 15
> **NOTE**
>
W
wusongqing 已提交
16 17
> - 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.
> - The APIs of this module are system APIs and cannot be called by third-party applications.
Z
zengyawen 已提交
18

W
wusongqing 已提交
19

20
## Modules to Import
Z
zengyawen 已提交
21

G
Gloria 已提交
22
```js
Z
zengyawen 已提交
23 24 25
import deviceManager from '@ohos.distributedHardware.deviceManager';
```

W
wusongqing 已提交
26 27 28 29 30 31 32 33 34

## deviceManager.createDeviceManager

createDeviceManager(bundleName: string, callback: AsyncCallback<DeviceManager>): void

Creates a **DeviceManager** instance.

**System capability**: SystemCapability.DistributedHardware.DeviceManager

35
**Parameters**
G
Gloria 已提交
36

G
gloria 已提交
37 38 39 40
| Name    | Type                                                | Mandatory| Description                                                       |
| ---------- | ---------------------------------------------------- | ---- | ----------------------------------------------------------- |
| bundleName | string                                               | Yes  | Bundle name of the application.                                 |
| callback   | AsyncCallback<[DeviceManager](#devicemanager)> | Yes  | Callback used to return the **DeviceManager** instance created.|
G
Gloria 已提交
41 42 43 44 45 46 47 48 49

**Error codes**

For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).

| ID| Error Message                                                       |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
| 11600102 | Failed to obtain the service.                                   |
W
wusongqing 已提交
50

51 52
**Example**

G
Gloria 已提交
53 54 55 56 57
  ```js
  try {
    deviceManager.createDeviceManager("ohos.samples.jshelloworld", (err, data) => {
      if (err) { 
        console.error("createDeviceManager errCode:" + err.code + ",errMessage:" + err.message);
58
        return;
G
Gloria 已提交
59 60 61 62 63 64 65 66
      }
      console.info("createDeviceManager success");
      let dmInstance = data;
    });
  } catch(err) {
    console.error("createDeviceManager errCode:" + err.code + ",errMessage:" + err.message);
  }
  ```
W
wusongqing 已提交
67

68 69 70 71 72 73 74 75 76 77 78 79
## DeviceInfo

Defines device information.

**System capability**: SystemCapability.DistributedHardware.DeviceManager

| Name                    | Type                       | Mandatory  | Description      |
| ---------------------- | ------------------------- | ---- | -------- |
| deviceId               | string                    | Yes   | Unique identifier of a device.|
| deviceName             | string                    | Yes   | Device name.   |
| deviceType             | [DeviceType](#devicetype) | Yes   | Device type.   |
| networkId<sup>8+</sup> | string                    | Yes   | Network ID of the device. |
80
| range<sup>9+</sup>     | number                    | Yes   | Distance between the device (discovered device) and the device that initiates device discovery. |
G
Gloria 已提交
81
| authForm<sup>10+</sup> | [AuthForm](#authform)     | Yes   | Authentication type of the device. |
82 83 84 85 86 87 88

## DeviceType

Enumerates the device types.

**System capability**: SystemCapability.DistributedHardware.DeviceManager

G
gloria 已提交
89
| Name          | Value | Description  |
90 91 92 93 94 95 96 97 98
| ------------ | ---- | ---- |
| SPEAKER      | 0x0A | Smart speaker.|
| PHONE        | 0x0E | Phone.  |
| TABLET       | 0x11 | Tablet.  |
| WEARABLE     | 0x6D | Wearable.|
| TV           | 0x9C | Smart TV. |
| CAR          | 0x83 | Car.   |
| UNKNOWN_TYPE | 0    | Unknown device type.|

G
Gloria 已提交
99 100 101 102 103 104 105 106 107 108 109 110
## AuthForm

Enumerates the device authentication types.

**System capability**: SystemCapability.DistributedHardware.DeviceManager

| Name                | Value | Description            |
| ------------------- | ---- | --------------- |
| INVALID_TYPE        | -1   | No authentication.|
| PEER_TO_PEER        | 0    | Point-to-point authentication for devices without accounts.  |
| IDENTICAL_ACCOUNT   | 1    | Authentication for devices using the same account.  |
| ACROSS_ACCOUNT      | 2    | Authentication for devices using different accounts.|
W
wusongqing 已提交
111 112

## DeviceStateChangeAction
Z
zengyawen 已提交
113 114 115

Enumerates the device states.

W
wusongqing 已提交
116 117
**System capability**: SystemCapability.DistributedHardware.DeviceManager

G
gloria 已提交
118
| Name     | Value | Description             |
119
| ------- | ---- | --------------- |
G
Gloria 已提交
120 121 122
| ONLINE  | 0    | The device is physically online.          |
| 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.|
| OFFLINE | 2    | The device is physically offline.          |
123
| CHANGE  | 3    | The device information is changed.        |
W
wusongqing 已提交
124

125
## SubscribeInfo
W
wusongqing 已提交
126

127
Defines subscription information.
W
wusongqing 已提交
128

129 130 131 132 133
**System capability**: SystemCapability.DistributedHardware.DeviceManager

| Name           | Type                               | Mandatory  | Description               |
| ------------- | --------------------------------- | ---- | ----------------- |
| subscribeId   | number                            | Yes   | Subscription ID, used to identify a device discovery period.|
G
Gloria 已提交
134 135 136 137 138 139
| mode          | [DiscoverMode ](#discovermode)    | Yes   | Device discovery mode.            |
| medium        | [ExchangeMedium](#exchangemedium) | Yes   | Medium used for device discovery.            |
| freq          | [ExchangeFreq](#exchangefreq)     | Yes   | Frequency of device discovery.            |
| isSameAccount | boolean                           | Yes   | Whether the same account is used on the discovered device.           |
| isWakeRemote  | boolean                           | Yes   | Whether to wake up the discovered device.          |
| capability    | [SubscribeCap](#subscribecap)     | Yes   | Discovery capability.            |
140 141 142 143 144


## DiscoverMode 

Enumerates the device discovery modes.
W
wusongqing 已提交
145 146 147

**System capability**: SystemCapability.DistributedHardware.DeviceManager

G
gloria 已提交
148
| Name                   | Value | Description   |
149 150 151
| --------------------- | ---- | ----- |
| DISCOVER_MODE_PASSIVE | 0x55 | Passive discovery.|
| DISCOVER_MODE_ACTIVE  | 0xAA | Active discovery.|
W
wusongqing 已提交
152 153


154
## ExchangeMedium 
Z
zengyawen 已提交
155

156
Enumerates the media used for device discovery.
Z
zengyawen 已提交
157

W
wusongqing 已提交
158
**System capability**: SystemCapability.DistributedHardware.DeviceManager
Z
zengyawen 已提交
159

G
gloria 已提交
160
| Name  | Value | Description       |
161 162 163 164 165 166 167 168 169 170 171 172
| ---- | ---- | --------- |
| AUTO | 0    | Automatic.  |
| BLE  | 1    | Bluetooth.  |
| COAP | 2    | Wi-Fi.|
| USB  | 3    | USB. |

## ExchangeFreq 

Enumerates the device discovery frequencies.

**System capability**: SystemCapability.DistributedHardware.DeviceManager

G
gloria 已提交
173
| Name        | Value | Description   |
174 175 176 177 178 179 180 181 182 183 184 185 186
| ---------- | ---- | ----- |
| LOW        | 0    | Low frequency. |
| MID        | 1    | Medium frequency. |
| HIGH       | 2    | High frequency. |
| SUPER_HIGH | 3    | Ultra-high frequency.|


## SubscribeCap 

Enumerates the discovery capabilities.

**System capability**: SystemCapability.DistributedHardware.DeviceManager

G
gloria 已提交
187
| Name                       | Value | Description            |
188 189 190 191 192 193 194 195 196 197 198 199 200 201
| ------------------------- | ---- | -------------- |
| SUBSCRIBE_CAPABILITY_DDMP | 0    | DDMP capability. This will be deprecated later.|
| SUBSCRIBE_CAPABILITY_OSD  | 1    | OSD capability.        |


## AuthParam

Defines the authentication parameters.

**System capability**: SystemCapability.DistributedHardware.DeviceManager

| Name       | Type                  | Mandatory  | Description        |
| --------- | -------------------- | ---- | ---------- |
| authType  | number               | Yes   | Authentication type.     |
G
gloria 已提交
202
| extraInfo | {[key:string]&nbsp;:&nbsp;any} | No   | Extended field.|
203 204 205 206 207 208 209 210 211 212 213

## AuthInfo

Defines authentication information.

**System capability**: SystemCapability.DistributedHardware.DeviceManager

| Name       | Type                  | Mandatory  | Description        |
| --------- | -------------------- | ---- | ---------- |
| authType  | number               | Yes   | Authentication type.     |
| token     | number               | Yes   | Authentication token.  |
G
gloria 已提交
214
| extraInfo | {[key:string]&nbsp;:&nbsp;any} | No   | Extended field.|
W
wusongqing 已提交
215

G
gloria 已提交
216
## PublishInfo<sup>9+</sup>
217 218 219 220 221

Defines published device information.

**System capability**: SystemCapability.DistributedHardware.DeviceManager

G
Gloria 已提交
222
| Name         | Type                             | Mandatory  | Description               |
223 224 225 226 227
| ------------- | --------------------------------- | ---- | ----------------- |
| publishId     | number                            | Yes   | ID used to identify a publication period.|
| mode          | [DiscoverMode ](#discovermode)    | Yes   | Device discovery mode.            |
| freq          | [ExchangeFreq](#exchangefreq)     | Yes   | Frequency of device discovery.            |
| ranging       | boolean                           | Yes   | Whether the device supports distance reporting.            |
W
wusongqing 已提交
228 229

## DeviceManager
Z
zengyawen 已提交
230

W
wusongqing 已提交
231
Provides 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**.
Z
zengyawen 已提交
232

W
wusongqing 已提交
233
### release
Z
zengyawen 已提交
234

W
wusongqing 已提交
235
release(): void
Z
zengyawen 已提交
236

237
Releases this **DeviceManager** instance when it is no longer used.
Z
zengyawen 已提交
238

W
wusongqing 已提交
239 240
**System capability**: SystemCapability.DistributedHardware.DeviceManager

G
Gloria 已提交
241
**Error codes**
242

G
Gloria 已提交
243 244 245 246 247 248 249
For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).

| ID| Error Message                                                       |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |

**Example**
W
wusongqing 已提交
250

G
Gloria 已提交
251 252 253 254 255 256 257
  ```js
  try {
    dmInstance.release();
  } catch (err) {
    console.error("release errCode:" + err.code + ",errMessage:" + err.message);
  }
  ```
W
wusongqing 已提交
258 259 260 261 262 263

### getTrustedDeviceListSync

getTrustedDeviceListSync(): Array&lt;DeviceInfo&gt;

Obtains all trusted devices synchronously.
Z
zengyawen 已提交
264

G
gloria 已提交
265 266
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

W
wusongqing 已提交
267 268
**System capability**: SystemCapability.DistributedHardware.DeviceManager

269
**Return value**
W
wusongqing 已提交
270

G
Gloria 已提交
271 272 273
  | Name                                    | Description       |
  | -------------------------------------- | --------- |
  | Array&lt;[DeviceInfo](#deviceinfo)&gt; | List of trusted devices obtained.|
274

G
Gloria 已提交
275
**Error codes**
276

G
Gloria 已提交
277
For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).
W
wusongqing 已提交
278

G
Gloria 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291
| ID| Error Message                                                       |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |

**Example**

  ```js
  try {
    var deviceInfoList = dmInstance.getTrustedDeviceListSync();
  } catch (err) {
    console.error("getTrustedDeviceListSync errCode:" + err.code + ",errMessage:" + err.message);
  }
  ```
W
wusongqing 已提交
292

293 294 295 296 297 298
### getTrustedDeviceList<sup>8+</sup>

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

Obtains all trusted devices. This API uses an asynchronous callback to return the result.

G
gloria 已提交
299 300
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

301 302
**System capability**: SystemCapability.DistributedHardware.DeviceManager

303
**Parameters**
G
Gloria 已提交
304 305 306 307 308 309 310 311 312 313 314 315

  | Name      | Type                                    | Mandatory  | Description                   |
  | -------- | ---------------------------------------- | ---- | --------------------- |
  | callback | AsyncCallback&lt;Array&lt;[DeviceInfo](#deviceinfo)&gt;&gt; | Yes   | Callback used to return the list of trusted devices.|

**Error codes**

For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).

| ID| Error Message                                                       |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
316

317
**Example**
G
Gloria 已提交
318

319
  ```js
G
Gloria 已提交
320 321 322 323 324 325
  try {
    dmInstance.getTrustedDeviceList((err, data) => {
      if (err) {
        console.error("getTrustedDeviceList errCode:" + err.code + ",errMessage:" + err.message);
        return;
      }
326
      console.log('get trusted device info: ' + JSON.stringify(data));
G
Gloria 已提交
327 328 329 330
    });
  } catch (err) {
    console.error("getTrustedDeviceList errCode:" + err.code + ",errMessage:" + err.message);
  }
331 332 333 334 335 336 337 338
  ```

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

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

Obtains all trusted devices. This API uses a promise to return the result.

G
gloria 已提交
339 340
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

341 342
**System capability**: SystemCapability.DistributedHardware.DeviceManager

343
**Return value**
G
Gloria 已提交
344 345 346 347 348 349 350 351 352 353 354 355

  | Type                                      | Description                   |
  | ---------------------------------------- | --------------------- |
  | Promise&lt;Array&lt;[DeviceInfo](#deviceinfo)&gt;&gt; | Promise used to return the list of trusted devices.|

**Error codes**

For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).

| ID| Error Message                                                       |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
356

357
**Example**
G
Gloria 已提交
358

359
  ```js
G
Gloria 已提交
360 361 362 363
  dmInstance.getTrustedDeviceList().then((data) => {
    console.log('get trusted device info: ' + JSON.stringify(data));
    }).catch((err) => {
      console.error("getTrustedDeviceList errCode:" + err.code + ",errMessage:" + err.message);
364 365 366 367 368 369 370 371 372
  });
  ```

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

getLocalDeviceInfoSync(): [DeviceInfo](#deviceinfo)

Obtains local device information synchronously.

G
gloria 已提交
373 374
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

375 376
**System capability**: SystemCapability.DistributedHardware.DeviceManager

377
**Return value**
G
Gloria 已提交
378 379 380 381 382 383 384 385 386 387 388 389

  | Name                     | Description             |
  | ------------------------- | ---------------- |
  | [DeviceInfo](#deviceinfo) | List of local devices obtained.|

**Error codes**

For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).

| ID| Error Message                                                       |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
390

391
**Example**
G
Gloria 已提交
392

393
  ```js
G
Gloria 已提交
394 395 396 397 398
  try {
    var deviceInfo = dmInstance.getLocalDeviceInfoSync();
  } catch (err) {
    console.error("getLocalDeviceInfoSync errCode:" + err.code + ",errMessage:" + err.message);
  }
399 400 401 402 403 404 405 406
  ```

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

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

Obtains local device information. This API uses an asynchronous callback to return the result.

G
gloria 已提交
407 408
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

409 410
**System capability**: SystemCapability.DistributedHardware.DeviceManager

411
**Parameters**
G
Gloria 已提交
412 413 414 415 416 417 418 419 420 421 422 423

  | Name      | Type                                    | Mandatory  | Description       |
  | -------- | ---------------------------------------- | ---- | --------- |
  | callback | AsyncCallback&lt;[DeviceInfo](#deviceinfo)&gt; | Yes   | Callback used to return the local device information.|

**Error codes**

For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).

| ID| Error Message                                                       |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
424

425
**Example**
G
Gloria 已提交
426

427
  ```js
G
Gloria 已提交
428 429 430 431 432
  try {
    dmInstance.getLocalDeviceInfo((err, data) => {
    if (err) {
      console.error("getLocalDeviceInfo errCode:" + err.code + ",errMessage:" + err.message);
      return;
433
    }
G
Gloria 已提交
434 435 436 437 438
      console.log('get local device info: ' + JSON.stringify(data));
    });
  } catch (err) {
    console.error("getLocalDeviceInfo errCode:" + err.code + ",errMessage:" + err.message);
  }
439 440 441 442 443 444 445 446
  ```

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

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

Obtains local device information. This API uses a promise to return the result.

G
gloria 已提交
447 448
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

449 450
**System capability**: SystemCapability.DistributedHardware.DeviceManager

451
**Return value**
G
Gloria 已提交
452 453 454 455 456 457 458 459 460 461 462 463

  | Type                                      | Description                   |
  | ---------------------------------------- | --------------------- |
  | Promise&lt;[DeviceInfo](#deviceinfo)&gt; | Promise used to return the local device information.|

**Error codes**

For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).

| ID| Error Message                                                       |
| ------- | --------------------------------------------------------------- |
| 11600101| Failed to execute the function.                                 |
464

465
**Example**
G
Gloria 已提交
466

467
  ```js
G
Gloria 已提交
468 469
  dmInstance.getLocalDeviceInfo().then((data) => {
    console.log('get local device info: ' + JSON.stringify(data));
470
  }).catch((err) => {
G
Gloria 已提交
471
    console.error("getLocalDeviceInfo errCode:" + err.code + ",errMessage:" + err.message);
472 473 474
  });
  ```

G
gloria 已提交
475
### startDeviceDiscovery<sup>8+</sup>
476 477 478 479 480

startDeviceDiscovery(subscribeInfo: SubscribeInfo): void

Starts to discover peripheral devices.

G
gloria 已提交
481 482
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

483 484
**System capability**: SystemCapability.DistributedHardware.DeviceManager

485
**Parameters**
G
Gloria 已提交
486 487 488 489 490 491 492 493 494 495 496 497 498

  | Name           | Type                      | Mandatory| Description   |
  | ------------- | ------------------------------- | ---- | ----- |
  | subscribeInfo | [SubscribeInfo](#subscribeinfo) | Yes  | Subscription information.|

**Error codes**

For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).

| ID| Error Message                                                       |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
| 11600104 | Discovery invalid.                                              |
499

500
**Example**
G
Gloria 已提交
501

502 503 504 505 506 507 508 509 510 511 512 513
  ```js
  // Automatically generate a unique subscription ID.
  var subscribeId = Math.floor(Math.random() * 10000 + 1000);
  var subscribeInfo = {
      "subscribeId": subscribeId,
      "mode": 0xAA, // Active discovery
      "medium": 0, // Automatic. Multiple media can be used for device discovery.
      "freq": 2, // High frequency
      "isSameAccount": false,
      "isWakeRemote": false,
      "capability": 1
  };
G
Gloria 已提交
514 515 516 517 518
  try {
    dmInstance.startDeviceDiscovery(subscribeInfo); // The deviceFound callback is invoked to notify the application when a device is discovered.
  } catch (err) {
    console.error("startDeviceDiscovery errCode:" + err.code + ",errMessage:" + err.message);
  }
519 520
  ```

G
gloria 已提交
521
### startDeviceDiscovery<sup>9+</sup>
522

W
wusongqing 已提交
523
startDeviceDiscovery(subscribeInfo: SubscribeInfo, filterOptions?: string): void
524 525 526

Starts to discover peripheral devices and filters discovered devices.

G
gloria 已提交
527 528
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

529 530 531
**System capability**: SystemCapability.DistributedHardware.DeviceManager

**Parameters**
G
Gloria 已提交
532 533 534 535 536 537 538 539 540 541 542 543 544 545

  | Name           | Type                      | Mandatory  | Description   |
  | ------------- | ------------------------------- | ---- | -----  |
  | subscribeInfo | [SubscribeInfo](#subscribeinfo) | Yes  | Subscription information.|
  | filterOptions | string                          | No  | Options for filtering discovered devices.|

**Error codes**

For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).

| ID| Error Message                                                       |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
| 11600104 | Discovery invalid.                                              |
546 547

**Example**
W
wusongqing 已提交
548

549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
  ```js
  // Automatically generate a unique subscription ID.
  var subscribeId = Math.floor(Math.random() * 10000 + 1000);
  var subscribeInfo = {
      "subscribeId": subscribeId,
      "mode": 0xAA, // Active discovery
      "medium": 0, // Automatic. Multiple media can be used for device discovery.
      "freq": 2, // High frequency
      "isSameAccount": false,
      "isWakeRemote": false,
      "capability": 1
  };
  var filterOptions = {
    "filter_op": "OR", // Optional. The default value is OR.
    "filters": [
        {
            "type": "range",
            "value": 50 // Filter discovered devices based on the distance (in cm).
        }
    ]
  };
G
Gloria 已提交
570 571 572 573 574
  try {
    dmInstance.startDeviceDiscovery(subscribeInfo, JSON.stringify(filterOptions)); // The deviceFound callback is invoked to notify the application when a device is discovered.
  } catch (err) {
    console.error("startDeviceDiscovery errCode:" + err.code + ",errMessage:" + err.message);
  }
575 576
  ```

577 578 579 580 581 582
### stopDeviceDiscovery

stopDeviceDiscovery(subscribeId: number): void

Stops device discovery.

G
gloria 已提交
583 584
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

585 586
**System capability**: SystemCapability.DistributedHardware.DeviceManager

587
**Parameters**
G
Gloria 已提交
588 589 590 591 592 593 594 595 596 597 598 599

  | Name         | Type  | Mandatory  | Description   |
  | ----------- | ------ | ---- | ----- |
  | subscribeId | number | Yes   | Subscription ID.|

**Error codes**

For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).

| ID| Error Message                                                       |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
600

601
**Example**
G
Gloria 已提交
602

603
  ```js
G
Gloria 已提交
604
  try {
G
Gloria 已提交
605 606
    // stopDeviceDiscovery and startDeviceDiscovery must be used in pairs, and the input parameter **subscribeId** passed in them must be the same.
    var subscribeId = 12345;
G
Gloria 已提交
607 608 609 610
    dmInstance.stopDeviceDiscovery(subscribeId);
  } catch (err) {
    console.error("stopDeviceDiscovery errCode:" + err.code + ",errMessage:" + err.message);
  }
611 612
  ```

G
gloria 已提交
613
### publishDeviceDiscovery<sup>9+</sup>
614 615 616 617 618

publishDeviceDiscovery(publishInfo: PublishInfo): void

Publishes device information for discovery purposes.

G
gloria 已提交
619 620
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

621 622 623
**System capability**: SystemCapability.DistributedHardware.DeviceManager

**Parameters**
G
Gloria 已提交
624 625 626 627 628 629 630 631 632 633 634 635 636

  | Name         | Type                       | Mandatory| Description   |
  | ------------- | ------------------------------- | ---- | ----- |
  | publishInfo   | [PublishInfo](#publishinfo)     | Yes  | Device information to publish.|

**Error codes**

For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).

| ID| Error Message                                                       |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
| 11600105 | Publish invalid.                                                |
637 638

**Example**
G
Gloria 已提交
639

640 641 642 643 644 645 646
  ```js
  // Automatically generate a unique subscription ID.
  var publishId = Math.floor(Math.random() * 10000 + 1000);
  var publishInfo = {
      "publishId": publishId,
      "mode": 0xAA, // Active discovery
      "freq": 2, // High frequency
G
Gloria 已提交
647
      "ranging": true  // The device supports reporting the distance to the discovery initiator.
648
  };
G
Gloria 已提交
649 650 651 652 653
  try {
    dmInstance.publishDeviceDiscovery(publishInfo); // A callback is invoked to notify the application when the device information is published.
  } catch (err) {
    console.error("publishDeviceDiscovery errCode:" + err.code + ",errMessage:" + err.message);
  }
654
  ```
G
gloria 已提交
655 656

### unPublishDeviceDiscovery<sup>9+</sup>
657 658 659 660 661

unPublishDeviceDiscovery(publishId: number): void

Stops publishing device information.

G
gloria 已提交
662 663
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

664 665 666 667
**System capability**: SystemCapability.DistributedHardware.DeviceManager

**Parameters**

G
Gloria 已提交
668 669 670 671 672 673 674 675 676 677 678
  | Name       | Type| Mandatory| Description |
  | ----------- | -------- | ---- | ----- |
  | publishId   | number   | Yes  | Publish ID.|

**Error codes**

For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).

| ID| Error Message                                                       |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
679 680

**Example**
G
Gloria 已提交
681

682
  ```js
G
Gloria 已提交
683
  try {
G
Gloria 已提交
684 685
    // unPublishDeviceDiscovery and publishDeviceDiscovery must be used in pairs, and the input parameter **publishId** passed in them must be the same.
    var publishId = 12345;
G
Gloria 已提交
686 687 688 689
    dmInstance.unPublishDeviceDiscovery(publishId);
  } catch (err) {
    console.error("unPublishDeviceDiscovery errCode:" + err.code + ",errMessage:" + err.message);
  }
690 691
  ```

692 693
### authenticateDevice

G
gloria 已提交
694
authenticateDevice(deviceInfo: DeviceInfo, authParam: AuthParam, callback: AsyncCallback&lt;{deviceId: string, pinToken ?: number}&gt;): void
695 696 697

Authenticates a device.

G
gloria 已提交
698 699
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

700 701
**System capability**: SystemCapability.DistributedHardware.DeviceManager

702
**Parameters**
G
Gloria 已提交
703 704 705 706 707

  | Name        | Type                                    | Mandatory  | Description     |
  | ---------- | ---------------------------------------- | ---- | ------- |
  | deviceInfo | [DeviceInfo](#deviceinfo)                | Yes   | Device information.  |
  | authParam  | [AuthParam](#authparam)                  | Yes   | Authentication parameter.  |
G
gloria 已提交
708
  | callback   | AsyncCallback&lt;{deviceId:&nbsp;string,&nbsp;pinToken&nbsp;?:&nbsp;number}&gt; | Yes   | Callback used to return the authentication result.|
G
Gloria 已提交
709 710 711 712 713 714 715 716 717

**Error codes**

For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).

| ID| Error Message                                                       |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
| 11600103 | Authentication invalid.                                         |
718

719
**Example**
G
Gloria 已提交
720

721 722 723 724 725
  ```js
  // Information about the device to authenticate. The information can be obtained from the device discovery result.
  var deviceInfo ={
      "deviceId": "XXXXXXXX",
      "deviceName": "",
G
Gloria 已提交
726 727 728
      "deviceType": 0x0E,
      "networkId" : "xxxxxxx",
      "range" : 0
729
  };
G
Gloria 已提交
730 731 732 733 734 735
  let extraInfo = {
          'targetPkgName': 'ohos.samples.xxx',
          'appName': 'xxx',
          'appDescription': 'xxx',
          'business': '0'
  }
736
  let authParam = {
G
Gloria 已提交
737 738
      'authType': 1, // Authentication type. The value 1 means no account PIN authentication.
      'extraInfo': extraInfo
739
  }
G
Gloria 已提交
740 741
  try {
    dmInstance.authenticateDevice(deviceInfo, authParam, (err, data) => {
742
      if (err) {
G
Gloria 已提交
743
          console.error("authenticateDevice errCode:" + err.code + ",errMessage:" + err.message);
744 745
          return;
      }
G
Gloria 已提交
746 747 748 749 750 751
      console.info("authenticateDevice result:" + JSON.stringify(data));
      let token = data.pinToken;
    });
  } catch (err) {
    console.error("authenticateDevice errCode:" + err.code + ",errMessage:" + err.message);
  }
752 753 754 755 756 757 758 759
  ```

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

unAuthenticateDevice(deviceInfo: DeviceInfo): void

Deauthenticates a device.

G
gloria 已提交
760 761
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

762 763
**System capability**: SystemCapability.DistributedHardware.DeviceManager

764 765
**Parameters**

G
Gloria 已提交
766 767 768 769 770 771 772 773 774 775 776
  | Name        | Type                     | Mandatory  | Description   |
  | ---------- | ------------------------- | ---- | ----- |
  | deviceInfo | [DeviceInfo](#deviceinfo) | Yes   | Device information.|

**Error codes**

For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).

| ID| Error Message                                                       |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
777

778
**Example**
G
Gloria 已提交
779

780
  ```js
G
Gloria 已提交
781
  try {
G
Gloria 已提交
782 783 784 785 786 787 788
    var deviceInfo ={
      "deviceId": "XXXXXXXX",
      "deviceName": "",
      "deviceType": 0x0E,
      "networkId" : "xxxxxxx",
      "range" : 0
    };
G
Gloria 已提交
789 790 791 792
    dmInstance.unAuthenticateDevice(deviceInfo);
  } catch (err) {
    console.error("unAuthenticateDevice errCode:" + err.code + ",errMessage:" + err.message);
  }
793 794 795 796
  ```

### verifyAuthInfo

G
gloria 已提交
797
verifyAuthInfo(authInfo: AuthInfo, callback: AsyncCallback&lt;{deviceId: string, level: number}&gt;): void
798 799 800

Verifies authentication information.

G
gloria 已提交
801 802
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

803 804
**System capability**: SystemCapability.DistributedHardware.DeviceManager

805
**Parameters**
G
Gloria 已提交
806 807 808 809

  | Name      | Type                                    | Mandatory  | Description     |
  | -------- | ---------------------------------------- | ---- | ------- |
  | authInfo | [AuthInfo](#authinfo)                    | Yes   | Authentication information.  |
G
gloria 已提交
810
  | callback | AsyncCallback&lt;{deviceId:&nbsp;string,&nbsp;level:&nbsp;number}&gt; | Yes   | Callback used to return the verification result.|
G
Gloria 已提交
811 812 813 814 815 816 817 818

**Error codes**

For details about the error codes, see [Device Management Error Codes](../errorcodes/errorcode-device-manager.md).

| ID| Error Message                                                       |
| -------- | --------------------------------------------------------------- |
| 11600101 | Failed to execute the function.                                 |
819

820
**Example**
G
Gloria 已提交
821

822 823 824
  ```js
  let authInfo = {
    "authType": 1,
G
Gloria 已提交
825
    "token": 123456,
826 827
    "extraInfo": {}
  }
G
Gloria 已提交
828 829
  try {
    dmInstance.verifyAuthInfo(authInfo, (err, data) => {
830
    if (err) {
G
Gloria 已提交
831
        console.error("verifyAuthInfo errCode:" + err.code + ",errMessage:" + err.message);
832 833
        return;
    }
G
Gloria 已提交
834 835 836 837 838 839 840
    console.info("verifyAuthInfo result:" + JSON.stringify(data));
    });
  } catch (err) {
    console.error("verifyAuthInfo errCode:" + err.code + ",errMessage:" + err.message);
  }
  ```

G
gloria 已提交
841
### setUserOperation<sup>9+</sup>
G
Gloria 已提交
842 843 844

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

G
gloria 已提交
845 846 847
Sets a user operation.

**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)
G
Gloria 已提交
848 849 850 851 852 853 854

**System capability**: SystemCapability.DistributedHardware.DeviceManager

**Parameters**

  | Name      | Type           | Mandatory | Description               |
  | ------------- | --------------- | ---- | ------------------- |
G
gloria 已提交
855
  | operateAction | number          | Yes   | User operation.      |
G
Gloria 已提交
856 857 858 859 860 861 862 863 864 865 866
  | params        | string          | Yes   | Input parameters of the user.|

**Example**

  ```js
 try {
    /*
      operateAction = 0 - Grant the permission.
      operateAction = 1 - Revoke the permission.
      operateAction = 2 - The user operation in the permission request dialog box times out.
      operateAction = 3 - Cancel the display of the PIN box.
G
gloria 已提交
867 868
      operateAction = 4 - Cancel the display of the PIN input box.
      operateAction = 5 - Confirm the input in the PIN input box.
G
Gloria 已提交
869 870
    */
    let operation = 0;
G
Gloria 已提交
871
    dmInstance.setUserOperation(operation, "extra")
G
Gloria 已提交
872 873 874 875 876
    } catch (err) {
      console.error("setUserOperation errCode:" + err.code + ",errMessage:" + err.message);
  }
  ```

G
Gloria 已提交
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
### requestCredentialRegisterInfo<sup>10+</sup>

requestCredentialRegisterInfo(requestInfo: string, callback: AsyncCallback<{registerInfo: string}>): void;

Obtains the registration information of the credential.

**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

**System capability**: SystemCapability.DistributedHardware.DeviceManager

**Parameters**

  | Name      | Type           | Mandatory | Description               |
  | ------------- | --------------- | ---- | ------------------- |
  | requestInfo   | string          | Yes   | Request credential information.      |
  | callback      | AsyncCallback<{registerInfo: string}>         | Yes   | Callback used to return the credential registration information.|

**Example**

  ```js
  let credentialInfo = {
    "version" : "1.2.3",
    "userId" : "123"
  }
  try {
    dmClass.requestCredentialRegisterInfo(credentialInfo, (data) => {
      if (data) {
          console.info("requestCredentialRegisterInfo result:" + JSON.stringify(data));
      } else {
          console.info.push("requestCredentialRegisterInfo result: data is null");
      }
    });
  } catch (err) {
    console.error("requestCredentialRegisterInfo err:" + err.code + "," + err.message);
  }
  ```

### importCredential<sup>10+</sup>

importCredential(credentialInfo: string, callback: AsyncCallback<{resultInfo: string}>): void;

Imports credential information.

**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

**System capability**: SystemCapability.DistributedHardware.DeviceManager

**Parameters**

  | Name      | Type           | Mandatory | Description               |
  | ------------- | --------------- | ---- | ------------------- |
  | credentialInfo| string          | Yes   | Credential information to import.      |
  | callback      | AsyncCallback<{resultInfo: string}>           | Yes   | Callback used to return the result.|

**Example**

  ```js
  let credentialInfo = {
    "processType" : 1,
    "authType" : 1,
    "userId" : "123",
    "deviceId" : "aaa",
    "version" : "1.2.3",
    "devicePk" : "0000",
    "credentialData" : 
    [
      {
        "credentialType" : 2,
        "credentialId" : "102",
        "serverPk" : "3059301306072A8648CE3D020106082A8648CE3D03",
        "pkInfoSignature" : "30440220490BCB4F822004C9A76AB8D97F80041FC0E",
        "pkInfo" : "",
        "authCode" : "",
        "peerDeviceId" : ""
      }
    ]
  }
  try {
    dmClass.importCredential(credentialInfo, (data) => {
      if (data) {
          console.info("importCredential result:" + JSON.stringify(data));
      } else {
          console.info.push("importCredential result: data is null");
      }
    });
  } catch (err) {
    console.error("importCredential err:" + err.code + "," + err.message);
  }
  ```

### deleteCredential<sup>10+</sup>

deleteCredential(queryInfo: string, callback: AsyncCallback<{resultInfo: string}>): void;

Deletes credential information.

**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

**System capability**: SystemCapability.DistributedHardware.DeviceManager

**Parameters**

  | Name      | Type           | Mandatory | Description               |
  | ------------- | --------------- | ---- | ------------------- |
  | queryInfo     | string          | Yes   | Credential information to delete.      |
  | callback      | AsyncCallback<{resultInfo: string}>           | Yes   | Callback used to return the result.|

**Example**

  ```js
  let queryInfo = {
    "processType" : 1,
    "authType" : 1,
    "userId" : "123"
  }
  try {
    dmClass.deleteCredential(queryInfo, (data) => {
      if (data) {
          console.info("deleteCredential result:" + JSON.stringify(data));
      } else {
          console.info.push("deleteCredential result: data is null");
      }
    });
  } catch (err) {
    console.error("deleteCredential err:" + err.code + "," + err.message);
  }
  ```

G
gloria 已提交
1005
### on('uiStateChange')<sup>9+</sup>
G
Gloria 已提交
1006

G
gloria 已提交
1007
on(type: 'uiStateChange', callback: Callback&lt;{ param: string}&gt;): void;
G
Gloria 已提交
1008 1009 1010

Subscribes to UI status changes.

G
gloria 已提交
1011 1012
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

G
Gloria 已提交
1013 1014 1015 1016 1017 1018 1019
**System capability**: SystemCapability.DistributedHardware.DeviceManager

**Parameters**

  | Name     | Type                            | Mandatory| Description                           |
  | -------- | ------------------------------------ | ---- | ------------------------------ |
  | type     | string                                | Yes | Event type. The value **'uiStateChange'** indicates a UI status change event.|
G
gloria 已提交
1020
  | callback | Callback&lt;{&nbsp;param:&nbsp;string}&gt; | Yes | Callback used to return the UI status.       |
G
Gloria 已提交
1021 1022 1023 1024 1025 1026 1027 1028

**Example**

  ```js
  try {
    dmInstance.on('uiStateChange', (data) => {
    console.log("uiStateChange executed, dialog closed" + JSON.stringify(data))
    var tmpStr = JSON.parse(data.param)
G
Gloria 已提交
1029 1030
    var isShow = tmpStr.verifyFailed
    console.log("uiStateChange executed, dialog closed" + isShow)
1031
  });
G
Gloria 已提交
1032 1033 1034
  } catch (err) {
    console.error("uiStateChange errCode:" + err.code + ",errMessage:" + err.message);
  }
1035 1036
  ```

G
gloria 已提交
1037
### off('uiStateChange')<sup>9+</sup>
G
Gloria 已提交
1038

G
gloria 已提交
1039
off(type: 'uiStateChange', callback?: Callback&lt;{ param: string}&gt;): void;
G
Gloria 已提交
1040 1041 1042

Unsubscribes from UI status changes.

G
gloria 已提交
1043 1044
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

G
Gloria 已提交
1045 1046 1047 1048 1049 1050 1051
**System capability**: SystemCapability.DistributedHardware.DeviceManager

**Parameters**

  | Name     | Type                             | Mandatory| Description                           |
  | -------- | ------------------------------------- | ---- | ------------------------------ |
  | type     | string                                | Yes  | Event type. The value **'uiStateChange'** indicates a UI status change event.|
G
gloria 已提交
1052
  | callback | Callback&lt;{&nbsp;param:&nbsp;string}&gt; | No  | Callback used to return the UI status.|
G
Gloria 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062

**Example**

  ```js
  try {
    dmInstance.off('uiStateChange');
  } catch (err) {
    console.error("uiStateChange errCode:" + err.code + ",errMessage:" + err.message);
  }
  ```
1063

W
wusongqing 已提交
1064 1065 1066
### on('deviceStateChange')

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

Subscribes to changes in the device state.

G
gloria 已提交
1070 1071
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

W
wusongqing 已提交
1072
**System capability**: SystemCapability.DistributedHardware.DeviceManager
Z
zengyawen 已提交
1073

1074
**Parameters**
G
Gloria 已提交
1075 1076 1077 1078 1079

  | Name      | Type                                    | Mandatory  | Description                            |
  | -------- | ---------------------------------------- | ---- | ------------------------------ |
  | type     | string                                   | Yes   | Event type. The value **'deviceStateChange'** indicates a device state change event.|
  | callback | Callback&lt;{ action: [DeviceStateChangeAction](#devicestatechangeaction), device: [DeviceInfo](#deviceinfo) }&gt; | Yes   | Callback used to return the device information and state.     |
Z
zengyawen 已提交
1080

1081
**Example**
G
Gloria 已提交
1082

1083
  ```js
G
Gloria 已提交
1084 1085 1086 1087 1088 1089 1090
  try {
    dmInstance.on('deviceStateChange', (data) => {
      console.info("deviceStateChange on:" + JSON.stringify(data));
    });
  } catch (err) {
    console.error("deviceStateChange errCode:" + err.code + ",errMessage:" + err.message);
  }
W
wusongqing 已提交
1091 1092 1093 1094
  ```

### off('deviceStateChange')

1095
off(type: 'deviceStateChange', callback?: Callback&lt;{ action: DeviceStateChangeAction, device: DeviceInfo }&gt;): void
W
wusongqing 已提交
1096 1097 1098

Unsubscribes from changes in the device state.

G
gloria 已提交
1099 1100
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

W
wusongqing 已提交
1101 1102
**System capability**: SystemCapability.DistributedHardware.DeviceManager

1103
**Parameters**
G
Gloria 已提交
1104 1105 1106 1107

  | Name      | Type                                    | Mandatory  | Description                         |
  | -------- | ---------------------------------------- | ---- | --------------------------- |
  | type     | string                                   | Yes   | Event type. The value **'deviceStateChange'** indicates a device state change event.       |
G
gloria 已提交
1108
  | callback | Callback&lt;{ action: [DeviceStateChangeAction](#devicestatechangeaction), device: [DeviceInfo](#deviceinfo) }&gt; | No   | Callback used to return the device information and state.|
W
wusongqing 已提交
1109

1110 1111
**Example**

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

1122 1123 1124 1125 1126 1127
### on('deviceFound')

on(type: 'deviceFound', callback: Callback&lt;{ subscribeId: number, device: DeviceInfo }&gt;): void

Subscribes to device discovery events.

G
gloria 已提交
1128 1129
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

1130 1131
**System capability**: SystemCapability.DistributedHardware.DeviceManager

1132
**Parameters**
G
Gloria 已提交
1133 1134 1135 1136

  | Name      | Type                                    | Mandatory  | Description                        |
  | -------- | ---------------------------------------- | ---- | -------------------------- |
  | type     | string                                   | Yes   | Event type. The value **'deviceFound'** indicates an event reported when a device is discovered.|
G
gloria 已提交
1137
  | callback | Callback&lt;{&nbsp;subscribeId:&nbsp;number,&nbsp;device:&nbsp;[DeviceInfo](#deviceinfo)&nbsp;}&gt; | Yes   | Callback used for device discovery.              |
1138

1139
**Example**
G
Gloria 已提交
1140

1141
  ```js
G
Gloria 已提交
1142 1143 1144 1145 1146 1147 1148
  try {
    dmInstance.on('deviceFound', (data) => {
      console.info("deviceFound:" + JSON.stringify(data));
    });
  } catch (err) {
    console.error("deviceFound errCode:" + err.code + ",errMessage:" + err.message);
  }
1149 1150 1151 1152 1153 1154 1155 1156
  ```

### off('deviceFound')

off(type: 'deviceFound', callback?: Callback&lt;{ subscribeId: number, device: DeviceInfo }&gt;): void

Unsubscribes from device discovery events.

G
gloria 已提交
1157 1158
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

1159 1160
**System capability**: SystemCapability.DistributedHardware.DeviceManager

1161
**Parameters**
G
Gloria 已提交
1162 1163 1164 1165

  | Name      | Type                                    | Mandatory  | Description                         |
  | -------- | ---------------------------------------- | ---- | --------------------------- |
  | type     | string                                   | Yes   | Event type. The value **'deviceFound'** indicates an event reported when a device is discovered.                |
G
gloria 已提交
1166
  | callback | Callback&lt;{&nbsp;subscribeId:&nbsp;number,&nbsp;device:&nbsp;[DeviceInfo](#deviceinfo)&nbsp;}&gt; | No   | Callback used to return the device information and state.|
1167

1168
**Example**
G
Gloria 已提交
1169

1170
  ```js
G
Gloria 已提交
1171 1172 1173 1174 1175 1176 1177
  try {
    dmInstance.off('deviceFound', (data) => {
      console.info('deviceFound' + JSON.stringify(data));
    });
  } catch (err) {
    console.error("deviceFound errCode:" + err.code + ",errMessage:" + err.message);
  }
1178 1179 1180 1181 1182 1183 1184 1185
  ```

### on('discoverFail')

on(type: 'discoverFail', callback: Callback&lt;{ subscribeId: number, reason: number }&gt;): void

Subscribes to device discovery failures.

G
gloria 已提交
1186 1187
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

1188 1189
**System capability**: SystemCapability.DistributedHardware.DeviceManager

1190
**Parameters**
G
Gloria 已提交
1191 1192 1193 1194

  | Name      | Type                                    | Mandatory  | Description                            |
  | -------- | ---------------------------------------- | ---- | ------------------------------ |
  | type     | string                                   | Yes   | Event type. The event **'discoverFail'** indicates an event reported when device discovery fails.|
G
gloria 已提交
1195
  | callback | Callback&lt;{&nbsp;subscribeId:&nbsp;number,&nbsp;reason:&nbsp;number&nbsp;}&gt; | Yes   | Callback used for the device discovery failure.                |
1196

1197
**Example**
G
Gloria 已提交
1198

1199
  ```js
G
Gloria 已提交
1200 1201 1202 1203 1204 1205 1206
  try {
    dmInstance.on('discoverFail', (data) => {
        console.info("discoverFail on:" + JSON.stringify(data));
    });
  } catch (err) {
    console.error("discoverFail errCode:" + err.code + ",errMessage:" + err.message);
  }
1207 1208 1209 1210 1211 1212 1213 1214
  ```

### off('discoverFail')

off(type: 'discoverFail', callback?: Callback&lt;{ subscribeId: number, reason: number }&gt;): void

Unsubscribes from device discovery failures.

G
gloria 已提交
1215 1216
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

1217 1218
**System capability**: SystemCapability.DistributedHardware.DeviceManager

1219
**Parameters**
G
Gloria 已提交
1220 1221 1222 1223

  | Name      | Type                                    | Mandatory  | Description               |
  | -------- | ---------------------------------------- | ---- | ----------------- |
  | type     | string                                   | Yes   | Event type. The event **'discoverFail'** indicates an event reported when device discovery fails.    |
G
gloria 已提交
1224
  | callback | Callback&lt;{&nbsp;subscribeId:&nbsp;number,&nbsp;reason:&nbsp;number&nbsp;}&gt; | No   | Callback used for the device discovery failure.|
1225

1226
**Example**
G
Gloria 已提交
1227

1228
  ```js
G
Gloria 已提交
1229 1230 1231 1232 1233 1234 1235
  try {
    dmInstance.off('discoverFail', (data) => {
      console.info('discoverFail' + JSON.stringify(data));
    });
  } catch (err) {
    console.error("discoverFail errCode:" + err.code + ",errMessage:" + err.message);
  }
1236 1237
  ```

G
gloria 已提交
1238
### on('publishSuccess')<sup>9+</sup>
1239 1240 1241 1242 1243

on(type: 'publishSuccess', callback: Callback&lt;{ publishId: number }&gt;): void

Subscribes to device information publication success events.

G
gloria 已提交
1244 1245
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

1246 1247 1248
**System capability**: SystemCapability.DistributedHardware.DeviceManager

**Parameters**
G
Gloria 已提交
1249 1250 1251 1252

  | Name    | Type                                | Mandatory| Description                      |
  | -------- | ---------------------------------------- | ---- | -------------------------- |
  | type     | string                                   | Yes  | Event type. The value **'publishSuccess'** indicates an event reported when device information is published.|
G
gloria 已提交
1253
  | callback | Callback&lt;{&nbsp;publishId:&nbsp;number&nbsp;}&gt;    | Yes  | Callback used to return the publish ID.              |
G
Gloria 已提交
1254

1255 1256

**Example**
G
Gloria 已提交
1257 1258 1259 1260

  ```js
  try {
    dmInstance.on('publishSuccess', (data) => {
1261
      console.info("publishSuccess:" + JSON.stringify(data));
G
Gloria 已提交
1262 1263 1264 1265 1266
    });
  } catch (err) {
    console.error("publishSuccess errCode:" + err.code + ",errMessage:" + err.message);
  }
  ```
1267

G
gloria 已提交
1268
### off('publishSuccess')<sup>9+</sup>
1269 1270 1271 1272 1273

off(type: 'publishSuccess', callback?: Callback&lt;{ publishId: number }&gt;): void

Unsubscribes from device information publication success events.

G
gloria 已提交
1274 1275
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

1276 1277 1278
**System capability**: SystemCapability.DistributedHardware.DeviceManager

**Parameters**
G
Gloria 已提交
1279 1280 1281 1282

  | Name    | Type                                | Mandatory| Description                         |
  | -------- | ---------------------------------------- | ---- | --------------------------- |
  | type     | string                                   | Yes  | Event type. The value **'publishSuccess'** indicates an event reported when device information is published.                |
G
gloria 已提交
1283
  | callback | Callback&lt;{&nbsp;publishId:&nbsp;number&nbsp;}&gt;    | No  | Callback used to return the publish ID.|
1284 1285

**Example**
G
Gloria 已提交
1286

1287
  ```js
G
Gloria 已提交
1288 1289 1290 1291 1292 1293 1294
  try {
    dmInstance.off('publishSuccess', (data) => {
      console.info('publishSuccess' + JSON.stringify(data));
    });
  } catch (err) {
    console.error("publishSuccess errCode:" + err.code + ",errMessage:" + err.message);
  }
1295 1296
  ```

G
gloria 已提交
1297
### on('publishFail')<sup>9+</sup>
1298 1299 1300 1301 1302

on(type: 'publishFail', callback: Callback&lt;{ publishId: number, reason: number }&gt;): void

Subscribes to device information publication failures.

G
gloria 已提交
1303 1304
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

1305 1306 1307
**System capability**: SystemCapability.DistributedHardware.DeviceManager

**Parameters**
G
Gloria 已提交
1308 1309 1310 1311

  | Name    | Type                                             | Mandatory| Description                            |
  | -------- | ----------------------------------------------------- | ---- | ------------------------------ |
  | type     | string                                                | Yes  | Event type. The event **'publishFail'** indicates an event reported when publishing device information fails.|
G
gloria 已提交
1312
  | callback | Callback&lt;{&nbsp;publishId:&nbsp;number,&nbsp;reason:&nbsp;number&nbsp;}&gt; | Yes  | Callback used for the publication failure.                |
1313 1314

**Example**
G
Gloria 已提交
1315

1316
  ```js
G
Gloria 已提交
1317 1318 1319 1320 1321 1322 1323
  try {
    dmInstance.on('publishFail', (data) => {
      console.info("publishFail on:" + JSON.stringify(data));
    });
  } catch (err) {
    console.error("publishFail errCode:" + err.code + ",errMessage:" + err.message);
  }
1324 1325
  ```

G
gloria 已提交
1326
### off('publishFail')<sup>9+</sup>
1327 1328 1329 1330 1331

off(type: 'publishFail', callback?: Callback&lt;{ publishId: number, reason: number }&gt;): void

Unsubscribes from device information publication failures.

G
gloria 已提交
1332 1333
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

1334 1335 1336
**System capability**: SystemCapability.DistributedHardware.DeviceManager

**Parameters**
G
Gloria 已提交
1337 1338 1339 1340

  | Name    | Type                                             | Mandatory| Description               |
  | -------- | ----------------------------------------------------- | ---- | ----------------- |
  | type     | string                                                | Yes  | Event type. The event **'publishFail'** indicates an event reported when publishing device information fails.    |
G
gloria 已提交
1341
  | callback | Callback&lt;{&nbsp;publishId:&nbsp;number,&nbsp;reason:&nbsp;number&nbsp;}&gt; | No  | Callback used for the device discovery failure.|
1342 1343

**Example**
G
Gloria 已提交
1344

1345
  ```js
G
Gloria 已提交
1346 1347 1348 1349 1350 1351 1352
  try {
    dmInstance.off('publishFail', (data) => {
      console.info('publishFail' + JSON.stringify(data));
    });
  } catch (err) {
    console.error("publishFail errCode:" + err.code + ",errMessage:" + err.message);
  }
1353
  ```
1354

W
wusongqing 已提交
1355 1356 1357 1358 1359 1360
### on('serviceDie')

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

Subscribes to dead events of the **DeviceManager** service.

G
gloria 已提交
1361 1362
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

W
wusongqing 已提交
1363 1364
**System capability**: SystemCapability.DistributedHardware.DeviceManager

1365
**Parameters**
G
Gloria 已提交
1366 1367 1368 1369 1370

  | Name      | Type                   | Mandatory  | Description                                      |
  | -------- | ----------------------- | ---- | ---------------------------------------- |
  | type     | string                  | Yes   | Event type. The value **'serviceDie'** indicates an event reported when the **DeviceManager** service is terminated unexpectedly.|
  | callback | ()&nbsp;=&gt;&nbsp;void | Yes   | Callback invoked when a dead event of the **DeviceManager** service occurs.                      |
W
wusongqing 已提交
1371

1372
**Example**
G
Gloria 已提交
1373

1374
  ```js
G
Gloria 已提交
1375 1376 1377 1378 1379 1380 1381
  try {
    dmInstance.on("serviceDie", () => {
      console.info("serviceDie on");
    });
  } catch (err) {
    console.error("serviceDie errCode:" + err.code + ",errMessage:" + err.message);
  }
W
wusongqing 已提交
1382 1383 1384 1385 1386 1387 1388 1389
  ```

### off('serviceDie')

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

Unsubscribes from dead events of the **DeviceManager** service.

G
gloria 已提交
1390 1391
**Required permissions**: ohos.permission.ACCESS_SERVICE_DM (available only to system applications)

W
wusongqing 已提交
1392
**System capability**: SystemCapability.DistributedHardware.DeviceManager
Z
zengyawen 已提交
1393

1394
**Parameters**
G
Gloria 已提交
1395 1396 1397 1398 1399

  | Name      | Type                   | Mandatory  | Description                                      |
  | -------- | ----------------------- | ---- | ---------------------------------------- |
  | type     | string                  | Yes   | Event type. The value **'serviceDie'** indicates an event reported when the **DeviceManager** service is terminated unexpectedly.|
  | callback | ()&nbsp;=&gt;&nbsp;void | No   | Callback used to return the dead event of the **DeviceManager** service.                    |
Z
zengyawen 已提交
1400

1401
**Example**
G
Gloria 已提交
1402

1403
  ```js
G
Gloria 已提交
1404 1405 1406 1407 1408 1409 1410
  try {
    dmInstance.off("serviceDie", () => {
      console.info("serviceDie off");
    });
  } catch (err) {
    console.error("serviceDie errCode:" + err.code + ",errMessage:" + err.message);
  }
W
wusongqing 已提交
1411
  ```