changelogs-device-manager.md 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# 设备管理changeLog

## cl.device_manager.1 API错误信息返回方式变更

设备管理接口使用业务逻辑返回值表示错误信息,不符合OpenHarmony接口错误码规范。从API9开始作以下变更:

异步接口:通过AsyncCallback或Promise的error对象返回错误信息。

同步接口:通过抛出异常的方式返回错误信息。

**变更影响**

基于此前版本开发的应用,需适配接口的错误信息返回方式,否则会影响原有业务逻辑。

**关键接口/组件变更**

  - createDeviceManager(bundleName: string, callback: AsyncCallback<DeviceManager>): void;
  - release(): void;
  - getTrustedDeviceListSync(): Array<DeviceInfo>
  - getTrustedDeviceList(callback:AsyncCallback<Array<DeviceInfo>>): void;
  - getTrustedDeviceList(): Promise<Array<DeviceInfo>>
22
  - getLocalDeviceInfoSync(): DeviceInfo;
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  - getLocalDeviceInfo(callback:AsyncCallback<DeviceInfo>): void;
  - getLocalDeviceInfo(): Promise<DeviceInfo>
  - startDeviceDiscovery(subscribeInfo: SubscribeInfo): void;
  - startDeviceDiscovery(subscribeInfo: SubscribeInfo, filterOptions?: string): void;
  - stopDeviceDiscovery(subscribeId: number): void;
  - publishDeviceDiscovery(publishInfo: PublishInfo): void;
  - unPublishDeviceDiscovery(publishId: number): void;
  - authenticateDevice(deviceInfo: DeviceInfo, authParam: AuthParam, callback: AsyncCallback<{deviceId: string, pinToken ?: number}>): void;
  - unAuthenticateDevice(deviceInfo: DeviceInfo): void;
  - verifyAuthInfo(authInfo: AuthInfo, callback: AsyncCallback<{deviceId: string, level: number}>): void;
  - setUserOperation(operateAction: number, params: string): void;
  - on(type: 'uiStateChange', callback: Callback<{ param: string}>): void;
  - off(type: 'uiStateChange', callback?: Callback<{ param: string}>): void;
  - on(type: 'deviceStateChange',  callback: Callback<{ action: DeviceStateChangeAction, device: DeviceInfo }>): void;
  - off(type: 'deviceStateChange', callback?: Callback<{ action: DeviceStateChangeAction, device: DeviceInfo }>): void;
  - on(type: 'deviceFound', callback: Callback<{ subscribeId: number, device: DeviceInfo }>): void;
  - off(type: 'deviceFound', callback?: Callback<{ subscribeId: number, device: DeviceInfo }>): void;
  - on(type: 'discoverFail', callback: Callback<{ subscribeId: number, reason: number }>): void;
  - off(type: 'discoverFail', callback?: Callback<{ subscribeId: number, reason: number }>): void;
  - on(type: 'publishSuccess', callback: Callback<{ publishId: number }>): void;
  - off(type: 'publishSuccess', callback?: Callback<{ publishId: number }>): void;
  - on(type: 'publishFail', callback: Callback<{ publishId: number, reason: number }>): void;
  - off(type: 'publishFail', callback?: Callback<{ publishId: number, reason: number }>): void;
  - on(type: 'serviceDie', callback: () => void): void;
  - off(type: 'serviceDie', callback?: () => void): void;

**适配指导**

异步接口以getTrustedDeviceList为例,示例代码如下:

```js
import account_osAccount from "@ohos.distributedHardware.deviceManager"
dmInstance.getTrustedDeviceList((err, data) => {
    console.log("getTrustedDeviceList err: " + JSON.stringify(err));
    console.log('get trusted device info: ' + JSON.stringify(data));
});

try {
  dmInstance.getTrustedDeviceList((err, data) => {
    if (err) {
      console.error("getTrustedDeviceList errCode:" + err.code + ",errMessage:" + err.message);
      return;
    }
    console.log('get trusted device info: ' + JSON.stringify(data));
  });
} catch (err) {
  console.error("getTrustedDeviceList errCode:" + err.code + ",errMessage:" + err.message);
}
```

同步接口以startDeviceDiscovery为例,示例代码如下:

```js
// 生成发现标识,随机数确保每次调用发现接口的标识不一致
var subscribeId = Math.floor(Math.random() * 10000 + 1000);
var subscribeInfo = {
    "subscribeId": subscribeId,
    "mode": 0xAA, // 主动模式
    "medium": 0,  // 自动发现类型,同时支持多种发现类型
    "freq": 2,    // 高频率
    "isSameAccount": false,
    "isWakeRemote": false,
    "capability": 1
};
dmInstance.startDeviceDiscovery(subscribeInfo); // 当有设备发现时,通过deviceFound回调通知给应用程序

// 生成发现标识,随机数确保每次调用发现接口的标识不一致
var subscribeId = Math.floor(Math.random() * 10000 + 1000);
var subscribeInfo = {
    "subscribeId": subscribeId,
    "mode": 0xAA, // 主动模式
    "medium": 0,  // 自动发现类型,同时支持多种发现类型
    "freq": 2,    // 高频率
    "isSameAccount": false,
    "isWakeRemote": false,
    "capability": 1
};
try {
  dmInstance.startDeviceDiscovery(subscribeInfo); // 当有设备发现时,通过deviceFound回调通知给应用程序
} catch (err) {
  console.error("startDeviceDiscovery errCode:" + err.code + ",errMessage:" + err.message);
}
```