设备管理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>>
  • getLocalDeviceInfoSync(): DeviceInfo;
  • 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为例,示例代码如下:

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为例,示例代码如下:

// 生成发现标识,随机数确保每次调用发现接口的标识不一致
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);
}