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