1# 设备管理changeLog 2 3## cl.device_manager.1 API错误信息返回方式变更 4 5设备管理接口使用业务逻辑返回值表示错误信息,不符合OpenHarmony接口错误码规范。从API9开始作以下变更: 6 7异步接口:通过AsyncCallback或Promise的error对象返回错误信息。 8 9同步接口:通过抛出异常的方式返回错误信息。 10 11**变更影响** 12 13基于此前版本开发的应用,需适配接口的错误信息返回方式,否则会影响原有业务逻辑。 14 15**关键接口/组件变更** 16 17在以下接口增加错误码处理: 18 - createDeviceManager(bundleName: string, callback: AsyncCallback<DeviceManager>): void; 19 - release(): void; 20 - getTrustedDeviceListSync(): Array<DeviceInfo> 21 - getTrustedDeviceList(callback:AsyncCallback<Array<DeviceInfo>>): void; 22 - getTrustedDeviceList(): Promise<Array<DeviceInfo>> 23 - getLocalDeviceInfoSync(): DeviceInfo; 24 - getLocalDeviceInfo(callback:AsyncCallback<DeviceInfo>): void; 25 - getLocalDeviceInfo(): Promise<DeviceInfo> 26 - startDeviceDiscovery(subscribeInfo: SubscribeInfo): void; 27 - startDeviceDiscovery(subscribeInfo: SubscribeInfo, filterOptions?: string): void; 28 - stopDeviceDiscovery(subscribeId: number): void; 29 - publishDeviceDiscovery(publishInfo: PublishInfo): void; 30 - unPublishDeviceDiscovery(publishId: number): void; 31 - authenticateDevice(deviceInfo: DeviceInfo, authParam: AuthParam, callback: AsyncCallback<{deviceId: string, pinToken ?: number}>): void; 32 - unAuthenticateDevice(deviceInfo: DeviceInfo): void; 33 - verifyAuthInfo(authInfo: AuthInfo, callback: AsyncCallback<{deviceId: string, level: number}>): void; 34 - setUserOperation(operateAction: number, params: string): void; 35 - on(type: 'uiStateChange', callback: Callback<{ param: string}>): void; 36 - off(type: 'uiStateChange', callback?: Callback<{ param: string}>): void; 37 - on(type: 'deviceStateChange', callback: Callback<{ action: DeviceStateChangeAction, device: DeviceInfo }>): void; 38 - off(type: 'deviceStateChange', callback?: Callback<{ action: DeviceStateChangeAction, device: DeviceInfo }>): void; 39 - on(type: 'deviceFound', callback: Callback<{ subscribeId: number, device: DeviceInfo }>): void; 40 - off(type: 'deviceFound', callback?: Callback<{ subscribeId: number, device: DeviceInfo }>): void; 41 - on(type: 'discoverFail', callback: Callback<{ subscribeId: number, reason: number }>): void; 42 - off(type: 'discoverFail', callback?: Callback<{ subscribeId: number, reason: number }>): void; 43 - on(type: 'publishSuccess', callback: Callback<{ publishId: number }>): void; 44 - off(type: 'publishSuccess', callback?: Callback<{ publishId: number }>): void; 45 - on(type: 'publishFail', callback: Callback<{ publishId: number, reason: number }>): void; 46 - off(type: 'publishFail', callback?: Callback<{ publishId: number, reason: number }>): void; 47 - on(type: 'serviceDie', callback: () => void): void; 48 - off(type: 'serviceDie', callback?: () => void): void; 49 50**适配指导** 51 52异步接口以getTrustedDeviceList为例,示例代码如下: 53 54```ts 55import account_osAccount from "@ohos.distributedHardware.deviceManager" 56dmInstance.getTrustedDeviceList((err, data) => { 57 console.log("getTrustedDeviceList err: " + JSON.stringify(err)); 58 console.log('get trusted device info: ' + JSON.stringify(data)); 59}); 60 61try { 62 dmInstance.getTrustedDeviceList((err, data) => { 63 if (err) { 64 console.error("getTrustedDeviceList errCode:" + err.code + ",errMessage:" + err.message); 65 return; 66 } 67 console.log('get trusted device info: ' + JSON.stringify(data)); 68 }); 69} catch (err) { 70 console.error("getTrustedDeviceList errCode:" + err.code + ",errMessage:" + err.message); 71} 72``` 73 74同步接口以startDeviceDiscovery为例,示例代码如下: 75 76```ts 77// 生成发现标识,随机数确保每次调用发现接口的标识不一致 78var subscribeId = Math.floor(Math.random() * 10000 + 1000); 79var subscribeInfo = { 80 "subscribeId": subscribeId, 81 "mode": 0xAA, // 主动模式 82 "medium": 0, // 自动发现类型,同时支持多种发现类型 83 "freq": 2, // 高频率 84 "isSameAccount": false, 85 "isWakeRemote": false, 86 "capability": 1 87}; 88dmInstance.startDeviceDiscovery(subscribeInfo); // 当有设备发现时,通过deviceFound回调通知给应用程序 89 90// 生成发现标识,随机数确保每次调用发现接口的标识不一致 91var subscribeId = Math.floor(Math.random() * 10000 + 1000); 92var subscribeInfo = { 93 "subscribeId": subscribeId, 94 "mode": 0xAA, // 主动模式 95 "medium": 0, // 自动发现类型,同时支持多种发现类型 96 "freq": 2, // 高频率 97 "isSameAccount": false, 98 "isWakeRemote": false, 99 "capability": 1 100}; 101try { 102 dmInstance.startDeviceDiscovery(subscribeInfo); // 当有设备发现时,通过deviceFound回调通知给应用程序 103} catch (err) { 104 console.error("startDeviceDiscovery errCode:" + err.code + ",errMessage:" + err.message); 105} 106``` 107