179a732c7Sopenharmony_ci# **DeviceManager部件** 279a732c7Sopenharmony_ci 379a732c7Sopenharmony_ci## 简介 479a732c7Sopenharmony_ci 579a732c7Sopenharmony_ciDeviceManager组件在OpenHarmony上提供账号无关的分布式设备的认证组网能力,并为开发者提供了一套用于分布式设备间监听、发现和认证的接口。 679a732c7Sopenharmony_ci 779a732c7Sopenharmony_ci其组成及依赖如下所示: 879a732c7Sopenharmony_ci 979a732c7Sopenharmony_ci 1079a732c7Sopenharmony_ci 1179a732c7Sopenharmony_cidsoftbus提供能力: 1279a732c7Sopenharmony_ci 提供设备上下线通知及设备信息,设备认证通道和设备发现能力。 1379a732c7Sopenharmony_cideviceauth提供能力: 1479a732c7Sopenharmony_ci 提供设备群组管理和群组认证能力。 1579a732c7Sopenharmony_ci 1679a732c7Sopenharmony_ci 1779a732c7Sopenharmony_ci## 目录 1879a732c7Sopenharmony_ci 1979a732c7Sopenharmony_ci``` 2079a732c7Sopenharmony_cifoundation/distributedhardware/distributedhardware_device_manager 2179a732c7Sopenharmony_ci├── common # 公共能力头文件存放目录 2279a732c7Sopenharmony_ci│ ├── include 2379a732c7Sopenharmony_ci│ │ ├── dfx # dfx功能头文件存放目录 2479a732c7Sopenharmony_ci│ │ └── ipc # ipc功能头文件存放目录 2579a732c7Sopenharmony_ci│ │ ├── lite # small 2679a732c7Sopenharmony_ci│ │ ├── model # ipc功能模块头文件存放目录 2779a732c7Sopenharmony_ci│ │ └── standard # standard 2879a732c7Sopenharmony_ci│ └── src 2979a732c7Sopenharmony_ci│ ├── dfx # dfx功能实现相关代码 3079a732c7Sopenharmony_ci│ └── ipc # ipc公共功能代码 3179a732c7Sopenharmony_ci│ ├── lite # small 3279a732c7Sopenharmony_ci│ └── standard # standard 3379a732c7Sopenharmony_ci├── display # DM显示hap代码 3479a732c7Sopenharmony_ci│ └── entry 3579a732c7Sopenharmony_ci│ └── src 3679a732c7Sopenharmony_ci│ └── main 3779a732c7Sopenharmony_ci│ ├── js # DM PIN码显示ServiceExtensionAbility相关JS代码 3879a732c7Sopenharmony_ci│ └── resources # DM PIN码显示ServiceExtensionAbility相关资源配置文件目录 3979a732c7Sopenharmony_ci├── figures 4079a732c7Sopenharmony_ci├── interfaces 4179a732c7Sopenharmony_ci│ ├── inner_kits # 内部接口及实现存放目录 4279a732c7Sopenharmony_ci│ │ └── native_cpp # 内部native接口及实现存放目录 4379a732c7Sopenharmony_ci│ │ ├── include 4479a732c7Sopenharmony_ci│ │ │ ├── ipc # ipc头文件存放目录 4579a732c7Sopenharmony_ci│ │ │ │ ├── lite # small 4679a732c7Sopenharmony_ci│ │ │ │ └── standard # standard 4779a732c7Sopenharmony_ci│ │ │ └── notify # ipc回调通知头文件目录 4879a732c7Sopenharmony_ci│ │ └── src 4979a732c7Sopenharmony_ci│ │ ├── ipc # ipc功能代码 5079a732c7Sopenharmony_ci│ │ │ ├── lite # small 5179a732c7Sopenharmony_ci│ │ │ └── standard # standard 5279a732c7Sopenharmony_ci│ │ └── notify # ipc回调通知功能代码 5379a732c7Sopenharmony_ci│ └── kits # 外部接口及实现存放目录 5479a732c7Sopenharmony_ci│ ├── js # 外部JS接口及实现存放目录 5579a732c7Sopenharmony_ci│ │ ├── include # 外部JS接口及实现头文件存放目录 5679a732c7Sopenharmony_ci│ │ └── src # 外部JS接口及实现代码 5779a732c7Sopenharmony_ci| └── js4.0 # 从Openharmony 4.0.9.2版本开始新增外部JS接口及实现存放目录 5879a732c7Sopenharmony_ci| ├── include # 新增外部JS接口及实现头文件存放目录 5979a732c7Sopenharmony_ci| └── src # 新增外部JS接口及实现代码 6079a732c7Sopenharmony_ci| 6179a732c7Sopenharmony_ci├── sa_profile # SA进程配置相关文件存放目录 6279a732c7Sopenharmony_ci├── services 6379a732c7Sopenharmony_ci│ ├── implementation # devicemanagerservice服务实现核心代码 6479a732c7Sopenharmony_ci│ │ ├── include 6579a732c7Sopenharmony_ci│ │ │ ├── ability # 与PIN码显示ServiceExtensionAbility拉起管理相关头文件 6679a732c7Sopenharmony_ci│ │ │ ├── adapter # DM适配扩展功能相关头文件 6779a732c7Sopenharmony_ci│ │ │ ├── authentication # device_auth交互相关头文件 6879a732c7Sopenharmony_ci│ │ │ ├── config # DM适配扩展相关配置功能头文件 6979a732c7Sopenharmony_ci│ │ │ ├── credential # 凭据管理功能相关头文件 7079a732c7Sopenharmony_ci│ │ │ ├── dependency # 外部依赖模块相关头文件 7179a732c7Sopenharmony_ci│ │ │ │ ├── commonevent # 事件处理功能相关头文件 7279a732c7Sopenharmony_ci│ │ │ │ ├── hichain # 与hichain功能交互相关头文件 7379a732c7Sopenharmony_ci│ │ │ │ ├── mini # mini 7479a732c7Sopenharmony_ci│ │ │ │ ├── multipleuser # 多用户功能相关头文件 7579a732c7Sopenharmony_ci│ │ │ │ ├── softbus # 与软总线功能交互相关头文件 7679a732c7Sopenharmony_ci│ │ │ │ └── timer # 定时器处理相关头文件 7779a732c7Sopenharmony_ci│ │ │ ├── devicestate # 设备状态管理功能相关头文件 7879a732c7Sopenharmony_ci│ │ │ ├── discovery # 设备发现功能相关头文件 7979a732c7Sopenharmony_ci│ │ │ ├── dispath # L0上功能实现相关头文件 8079a732c7Sopenharmony_ci│ │ │ └── publish # 设备主动发现功能相关头文件 8179a732c7Sopenharmony_ci│ │ └── src 8279a732c7Sopenharmony_ci│ │ ├── ability # 与PIN码显示ServiceExtensionAbility拉起管理相关功能代码 8379a732c7Sopenharmony_ci│ │ ├── adapter # DM适配扩展功能代码 8479a732c7Sopenharmony_ci│ │ ├── authentication # device_auth交互相关核心代码 8579a732c7Sopenharmony_ci│ │ ├── config # DM适配扩展相关配置功能代码 8679a732c7Sopenharmony_ci│ │ ├── credential # 凭据管理功能代码 8779a732c7Sopenharmony_ci│ │ ├── dependency # 外部依赖模块功能代码 8879a732c7Sopenharmony_ci│ │ │ ├── commonevent # 事件处理功能代码 8979a732c7Sopenharmony_ci│ │ │ ├── hichain # 与hichain功能交互代码 9079a732c7Sopenharmony_ci│ │ │ ├── mini # mini 9179a732c7Sopenharmony_ci│ │ │ ├── multipleuser # 多用户功能代码 9279a732c7Sopenharmony_ci│ │ │ ├── softbus # 与软总线功能交互代码 9379a732c7Sopenharmony_ci│ │ │ └── timer # 定时器处理相关功能代码 9479a732c7Sopenharmony_ci│ │ ├── devicestate # 设备状态管理功能代码 9579a732c7Sopenharmony_ci│ │ ├── discovery # 设备发现功能代码 9679a732c7Sopenharmony_ci│ │ ├── dispath # mini上功能实现代码 9779a732c7Sopenharmony_ci│ │ └── publish # 设备主动发现功能 9879a732c7Sopenharmony_ci│ └── service # devicemanagerservice服务实现核心代码 9979a732c7Sopenharmony_ci│ ├── include 10079a732c7Sopenharmony_ci│ │ ├── ipc # 进程间通信相关头文件 10179a732c7Sopenharmony_ci│ │ │ ├── lite # small 10279a732c7Sopenharmony_ci│ │ │ └── standard # standard 10379a732c7Sopenharmony_ci│ │ └── softbus # 软总线相关头文件 10479a732c7Sopenharmony_ci│ └── src 10579a732c7Sopenharmony_ci│ ├── ipc # 进程间通信相关功能代码 10679a732c7Sopenharmony_ci│ │ ├── lite # small 10779a732c7Sopenharmony_ci│ │ └── standard # standard 10879a732c7Sopenharmony_ci│ └── softbus # 通道建立功能核心代码 10979a732c7Sopenharmony_ci└── utils # 公共能力头文件存放目录 11079a732c7Sopenharmony_ci ├── include 11179a732c7Sopenharmony_ci │ ├── fwkload # 拉起其他sa功能相关头文件 11279a732c7Sopenharmony_ci │ └── permission # 权限校验相关头文件存放目录 11379a732c7Sopenharmony_ci └── src 11479a732c7Sopenharmony_ci ├── fwkload # 拉起其他sa功能相关功能代码 11579a732c7Sopenharmony_ci └── permission # 权限校验相关功能代码 11679a732c7Sopenharmony_ci``` 11779a732c7Sopenharmony_ci 11879a732c7Sopenharmony_ci## 约束 11979a732c7Sopenharmony_ci 12079a732c7Sopenharmony_ci- 开发语言:JS、C++ 12179a732c7Sopenharmony_ci- 适用于Hi3516DV300单板等OpenHarmony设备 12279a732c7Sopenharmony_ci 12379a732c7Sopenharmony_ci 12479a732c7Sopenharmony_ci## 废弃接口说明 12579a732c7Sopenharmony_ci 12679a732c7Sopenharmony_ci该部分接口暂时可继续使用,但是暂停维护,建议使用新接口进行开发。 12779a732c7Sopenharmony_ci 12879a732c7Sopenharmony_ci当前版本设备管理服务不具备权限管理的能力,接口中的system api仅供系统调用,后续版本会进行严格的权限管控。 12979a732c7Sopenharmony_ci接口参见[**interface_sdk-js仓库的**](https://gitee.com/openharmony/interface_sdk-js/) *ohos.distributedHardware.deviceManager.d.ts* 13079a732c7Sopenharmony_ci 13179a732c7Sopenharmony_ci- 公共接口: 13279a732c7Sopenharmony_ci 13379a732c7Sopenharmony_ci 使用DeviceManager相关接口之前,需要通过createDeviceManager接口创建DeviceManager实例; 13479a732c7Sopenharmony_ci 13579a732c7Sopenharmony_ci 不使用DeviceManager接口的时候需要释放对应的DeviceManager实例。 13679a732c7Sopenharmony_ci 13779a732c7Sopenharmony_ci| 原型 | 描述 | 13879a732c7Sopenharmony_ci| ------------------------------------------------------------ | ------------------------------- | 13979a732c7Sopenharmony_ci| createDeviceManager(bundleName: string, callback: AsyncCallback<DeviceManager>): void; | 以异步方法获取DeviceManager实例 | 14079a732c7Sopenharmony_ci| release(): void; | 释放DeviceManager实例 | 14179a732c7Sopenharmony_ci 14279a732c7Sopenharmony_ci- 系统能力接口: 14379a732c7Sopenharmony_ci 14479a732c7Sopenharmony_ci 提供可信设备列表获取、可信设备状态监听、周边设备发现、设备认证等相关接口,该部分作为系统能力接口,仅供系统应用调用。 14579a732c7Sopenharmony_ci 14679a732c7Sopenharmony_ci 开始设备发现、停止发现设备接口要配对使用,使用同一个subscribeId。 14779a732c7Sopenharmony_ci 14879a732c7Sopenharmony_ci| 原型 | 描述 | 14979a732c7Sopenharmony_ci| ------------------------------------------------------------ | -------------------- | 15079a732c7Sopenharmony_ci| getTrustedDeviceListSync(): Array<DeviceInfo>; | 获取信任设备列表 | 15179a732c7Sopenharmony_ci| getTrustedDeviceList(callback:AsyncCallback<Array<DeviceInfo>>): void; | 获取信任设备列表 | 15279a732c7Sopenharmony_ci| getTrustedDeviceList(): Promise<Array<DeviceInfo>>; | 获取信任设备列表 | 15379a732c7Sopenharmony_ci| getLocalDeviceInfoSync(): DeviceInfo; | 获取本地设备信息 | 15479a732c7Sopenharmony_ci| getLocalDeviceInfo(callback:AsyncCallback<DeviceInfo>): void; | 获取本地设备信息 | 15579a732c7Sopenharmony_ci| getLocalDeviceInfo(): Promise<DeviceInfo>; | 获取本地设备信息 | 15679a732c7Sopenharmony_ci| on(type: 'deviceStateChange', callback: Callback<{ action: DeviceStateChangeAction, device: DeviceInfo }>): void; | 设备状态变更回调 | 15779a732c7Sopenharmony_ci| off(type: 'deviceStateChange', callback?: Callback<{ action: DeviceStateChangeAction, device: DeviceInfo }>): void; | 取消设备状态变更回调 | 15879a732c7Sopenharmony_ci| on(type: 'serviceDie', callback: () => void): void; | 服务错误回调 | 15979a732c7Sopenharmony_ci| off(type: 'serviceDie', callback?: () => void): void; | 取消服务错误回调 | 16079a732c7Sopenharmony_ci| startDeviceDiscovery(subscribeInfo: SubscribeInfo): void; | 开始设备发现 | 16179a732c7Sopenharmony_ci| stopDeviceDiscovery(subscribeId: number): void; | 停止发现设备 | 16279a732c7Sopenharmony_ci| authenticateDevice(deviceInfo: DeviceInfo, authParam: AuthParam, callback: AsyncCallback<{deviceId: string, pinToken ?: number}>): void; | 设备认证接口 | 16379a732c7Sopenharmony_ci| unAuthenticateDevice(deviceInfo: DeviceInfo): void; | 解除认证设备 | 16479a732c7Sopenharmony_ci| setUserOperation(operateAction: number, params: string): void; | 设置用户ui操作行为 | 16579a732c7Sopenharmony_ci| verifyAuthInfo(authInfo: AuthInfo, callback: AsyncCallback<{deviceId: string, level: number}>): void; | 设备认证信息校验 | 16679a732c7Sopenharmony_ci| startDeviceDiscovery(subscribeInfo: SubscribeInfo, filterOptions?: string): void; | 发现周边设备 | 16779a732c7Sopenharmony_ci| publishDeviceDiscovery(publishInfo: PublishInfo): void; | 发布设备发现 | 16879a732c7Sopenharmony_ci| unPublishDeviceDiscovery(publishId: number): void; | 停止发布设备发现 | 16979a732c7Sopenharmony_ci| on(type: 'deviceFound', callback: Callback<{ subscribeId: number, device: DeviceInfo }>): void; | 发现设备列表回调 | 17079a732c7Sopenharmony_ci| off(type: 'deviceFound', callback?: Callback<{ subscribeId: number, device: DeviceInfo }>): void; | 取消发现设备列表回调 | 17179a732c7Sopenharmony_ci| on(type: 'discoverFail', callback: Callback<{ subscribeId: number, reason: number }>): void; | 发现设备失败回调 | 17279a732c7Sopenharmony_ci| off(type: 'discoverFail', callback?: Callback<{ subscribeId: number, reason: number }>): void; | 取消发现设备失败回调 | 17379a732c7Sopenharmony_ci| on(type: 'publishSuccess', callback: Callback<{ publishId: number }>): void; | 发布设备成功回调 | 17479a732c7Sopenharmony_ci| off(type: 'publishSuccess', callback?: Callback<{ publishId: number }>): void; | 取消发布设备成功回调 | 17579a732c7Sopenharmony_ci| on(type: 'publishFail', callback: Callback<{ publishId: number, reason: number }>): void; | 发布设备失败回调 | 17679a732c7Sopenharmony_ci| off(type: 'publishFail', callback?: Callback<{ publishId: number, reason: number }>): void; | 取消发布设备失败回调 | 17779a732c7Sopenharmony_ci| on(type: 'uiStateChange', callback: Callback<{ param: string}>): void; | ui状态变更回调 | 17879a732c7Sopenharmony_ci| off(type: 'uiStateChange', callback?: Callback<{ param: string}>): void; | 取消ui状态变更回调 | 17979a732c7Sopenharmony_ci 18079a732c7Sopenharmony_ci## 新增接口说明 18179a732c7Sopenharmony_ci 18279a732c7Sopenharmony_ci 从Openharmony 4.0.9.2版本开始,新增设备管理接口。 18379a732c7Sopenharmony_ci 18479a732c7Sopenharmony_ci接口参见[**interface_sdk-js仓库的**](https://gitee.com/openharmony/interface_sdk-js/) *ohos.distributedDeviceManager.d.ts* 18579a732c7Sopenharmony_ci 18679a732c7Sopenharmony_ci- 公共接口: 18779a732c7Sopenharmony_ci 18879a732c7Sopenharmony_ci 调用以下接口,需要申请ohos.permission.DISTRIBUTED_DATASYNC权限才能正常调用。 18979a732c7Sopenharmony_ci 19079a732c7Sopenharmony_ci 使用DeviceManager相关接口之前,需要通过createDeviceManager接口创建DeviceManager实例; 19179a732c7Sopenharmony_ci 19279a732c7Sopenharmony_ci 不使用DeviceManager接口的时候需要释放对应的DeviceManager实例。 19379a732c7Sopenharmony_ci 19479a732c7Sopenharmony_ci 提供可信设备列表获取、可信设备状态监听、周边设备发现、设备认证等相关接口,支持三方应用调用。 19579a732c7Sopenharmony_ci 19679a732c7Sopenharmony_ci| 原型 | 描述 | 19779a732c7Sopenharmony_ci| ------------------------------------------------------------ | ------------------------------- | 19879a732c7Sopenharmony_ci| createDeviceManager(bundleName: string): DeviceManager; | 创建一个设备管理实例。设备管理实例是分布式设备管理方法的调用入口。用于获取可信设备和本地设备的相关信息。 | 19979a732c7Sopenharmony_ci| releaseDeviceManager(deviceManager: DeviceManager): void; | 设备管理实例不再使用后,通过该方法释放DeviceManager实例。 | 20079a732c7Sopenharmony_ci| getAvailableDeviceListSync(): Array<DeviceBasicInfo>; | 同步获取所有可信设备列表。 | 20179a732c7Sopenharmony_ci| getAvailableDeviceList(callback:AsyncCallback<Array<DeviceBasicInfo>>): void; | 获取所有可信设备列表。使用callback异步回调。 | 20279a732c7Sopenharmony_ci| getAvailableDeviceList(): Promise<Array<DeviceBasicInfo>>; | 获取所有可信设备列表。使用Promise异步回调。 | 20379a732c7Sopenharmony_ci| getLocalDeviceNetworkId(): string; | 获取本地设备网络标识。 | 20479a732c7Sopenharmony_ci| getLocalDeviceName(): string; | 获取本地设备名称。 | 20579a732c7Sopenharmony_ci| getLocalDeviceType(): number; | 获取本地设备类型。 | 20679a732c7Sopenharmony_ci| getLocalDeviceId(): string; | 获取本地设备id。 | 20779a732c7Sopenharmony_ci| getDeviceName(networkId: string): string; | 通过指定设备的网络标识获取该设备名称。 | 20879a732c7Sopenharmony_ci| getDeviceType(networkId: string): number; | 通过指定设备的网络标识获取该设备类型。 | 20979a732c7Sopenharmony_ci| startDiscovering(discoverParam: {[key: string]: Object} , filterOptions?: {[key: string]: Object} ): void; | 发现周边设备。发现状态持续两分钟,超过两分钟,会停止发现,最大发现数量99个。 | 21079a732c7Sopenharmony_ci| stopDiscovering(): void; | 停止发现周边设备。 | 21179a732c7Sopenharmony_ci| bindTarget(deviceId: string, bindParam: {[key: string]: Object} , callback: AsyncCallback<{deviceId: string}>): void; | 认证设备。 | 21279a732c7Sopenharmony_ci| unbindTarget(deviceId: string): void; | 解除认证设备。 | 21379a732c7Sopenharmony_ci| on(type: 'deviceStateChange', callback: Callback<{ action: DeviceStateChange, device: DeviceBasicInfo }>): void; | 注册设备状态回调。 | 21479a732c7Sopenharmony_ci| off(type: 'deviceStateChange', callback?: Callback<{ action: DeviceStateChange, device: DeviceBasicInfo }>): void; | 取消注册设备状态回调。 | 21579a732c7Sopenharmony_ci| on(type: 'discoverSuccess', callback: Callback<{ device: DeviceBasicInfo }>): void; | 注册发现设备成功回调监听。 | 21679a732c7Sopenharmony_ci| off(type: 'discoverSuccess', callback?: Callback<{ device: DeviceBasicInfo }>): void; | 取消注册设备发现成功回调。 | 21779a732c7Sopenharmony_ci| on(type: 'deviceNameChange', callback: Callback<{ deviceName: string }>): void; | 注册设备名称变更回调监听。 | 21879a732c7Sopenharmony_ci| off(type: 'deviceNameChange', callback?: Callback<{ deviceName: string }>): void; | 取消注册设备名称变更回调监听。 | 21979a732c7Sopenharmony_ci| on(type: 'discoverFailure', callback: Callback<{ reason: number }>): void; | 注册设备发现失败回调监听。 | 22079a732c7Sopenharmony_ci| off(type: 'discoverFailure', callback?: Callback<{ reason: number }>): void; | 取消注册设备发现失败回调。 | 22179a732c7Sopenharmony_ci| on(type: 'serviceDie', callback?: Callback<{}>): void; | 注册设备管理服务死亡监听。 | 22279a732c7Sopenharmony_ci| off(type: 'serviceDie', callback?: Callback<{}>): void; | 取消注册设备管理服务死亡监听。 | 22379a732c7Sopenharmony_ci 22479a732c7Sopenharmony_ci- 示例如下: 22579a732c7Sopenharmony_ci 22679a732c7Sopenharmony_ci```js 22779a732c7Sopenharmony_citry { 22879a732c7Sopenharmony_ci let dmClass = deviceManager.createDeviceManager("ohos.samples.jshelloworld"); 22979a732c7Sopenharmony_ci} catch(err) { 23079a732c7Sopenharmony_ci console.error("createDeviceManager errCode:" + err.code + ",errMessage:" + err.message); 23179a732c7Sopenharmony_ci} 23279a732c7Sopenharmony_ci 23379a732c7Sopenharmony_citry { 23479a732c7Sopenharmony_ci // 注册设备管理服务死亡监听。 23579a732c7Sopenharmony_ci dmClass.on("serviceDie", () => { 23679a732c7Sopenharmony_ci console.info("serviceDie on"); 23779a732c7Sopenharmony_ci }); 23879a732c7Sopenharmony_ci} catch (err) { 23979a732c7Sopenharmony_ci console.error("serviceDie errCode:" + err.code + ",errMessage:" + err.message); 24079a732c7Sopenharmony_ci} 24179a732c7Sopenharmony_ci 24279a732c7Sopenharmony_citry { 24379a732c7Sopenharmony_ci dmClass.on('deviceStateChange', (data) => { 24479a732c7Sopenharmony_ci console.info("deviceStateChange on:" + JSON.stringify(data)); 24579a732c7Sopenharmony_ci }); 24679a732c7Sopenharmony_ci} catch (err) { 24779a732c7Sopenharmony_ci console.error("deviceStateChange errCode:" + err.code + ",errMessage:" + err.message); 24879a732c7Sopenharmony_ci} 24979a732c7Sopenharmony_ci 25079a732c7Sopenharmony_ci// 同步获取所有可信设备列表。 25179a732c7Sopenharmony_citry { 25279a732c7Sopenharmony_ci let trustList = dmClass.getAvailableDeviceListSync(); 25379a732c7Sopenharmony_ci console.log("trustList:" + JSON.stringify(this.trustList)); 25479a732c7Sopenharmony_ci} catch(err) { 25579a732c7Sopenharmony_ci console.log("getAvailableDeviceList failed: " + JSON.stringify(err)); 25679a732c7Sopenharmony_ci} 25779a732c7Sopenharmony_ci// 获取本地设备类型。 25879a732c7Sopenharmony_civar data = dmClass.getLocalDeviceType(); 25979a732c7Sopenharmony_ci 26079a732c7Sopenharmony_ci// 获取本地设备名称。 26179a732c7Sopenharmony_civar data = dmClass.getLocalDeviceName(); 26279a732c7Sopenharmony_ci 26379a732c7Sopenharmony_ci// 发现周边设备。 26479a732c7Sopenharmony_citry { 26579a732c7Sopenharmony_ci // 注册发现设备成功回调监听。 26679a732c7Sopenharmony_ci dmClass.on('discoverSuccess', (data) => { 26779a732c7Sopenharmony_ci if (data == null) { 26879a732c7Sopenharmony_ci console.log("discoverSuccess error data = null") 26979a732c7Sopenharmony_ci return; 27079a732c7Sopenharmony_ci } 27179a732c7Sopenharmony_ci console.info(TAG + "discoverSuccess:" + JSON.stringify(data)); 27279a732c7Sopenharmony_ci }); 27379a732c7Sopenharmony_ci // 注册设备发现失败回调监听。 27479a732c7Sopenharmony_ci dmClass.on('discoverFailure', (data) => { 27579a732c7Sopenharmony_ci console.info("discoverFailure on:" + JSON.stringify(data)); 27679a732c7Sopenharmony_ci }); 27779a732c7Sopenharmony_ci var discoverParam = { 27879a732c7Sopenharmony_ci 'discoverTargetType': 1 27979a732c7Sopenharmony_ci }; 28079a732c7Sopenharmony_ci var filterOptions = { 28179a732c7Sopenharmony_ci 'availableStatus': 1, 28279a732c7Sopenharmony_ci 'discoverDistance': 50, 28379a732c7Sopenharmony_ci 'authenticationStatus': 0, 28479a732c7Sopenharmony_ci 'authorizationType': 0 28579a732c7Sopenharmony_ci }; 28679a732c7Sopenharmony_ci dmClass.startDiscovering(discoverParam, filterOptions); // 当有设备发现时,通过discoverSuccess回调通知给应用程序 28779a732c7Sopenharmony_ci} catch (err) { 28879a732c7Sopenharmony_ci console.log("startDiscovering err:" + err.code + "," + err.message); 28979a732c7Sopenharmony_ci} 29079a732c7Sopenharmony_ci 29179a732c7Sopenharmony_ci// 停止发现周边设备。 29279a732c7Sopenharmony_citry { 29379a732c7Sopenharmony_ci dmClass.stopDiscovering(); 29479a732c7Sopenharmony_ci // 取消注册设备发现成功回调。 29579a732c7Sopenharmony_ci dmClass.off('discoverSuccess'); 29679a732c7Sopenharmony_ci // 取消注册设备发现失败回调。 29779a732c7Sopenharmony_ci dmClass.off('discoverFailure'); 29879a732c7Sopenharmony_ci} catch (err) { 29979a732c7Sopenharmony_ci console.log("err:" + err.code + "," + err.message); 30079a732c7Sopenharmony_ci} 30179a732c7Sopenharmony_ci 30279a732c7Sopenharmony_ci// 设备认证 30379a732c7Sopenharmony_civar deviceId ="XXXXXXXX"; 30479a732c7Sopenharmony_cilet bindParam = { 30579a732c7Sopenharmony_ci 'bindType': 1,// 认证类型: 1 - 无帐号PIN码认证 30679a732c7Sopenharmony_ci 'targetPkgName': 'xxxx', 30779a732c7Sopenharmony_ci 'appName': 'xxxx', 30879a732c7Sopenharmony_ci 'appOperation': 'xxxx', 30979a732c7Sopenharmony_ci 'customDescription': 'xxxx' 31079a732c7Sopenharmony_ci} 31179a732c7Sopenharmony_citry { 31279a732c7Sopenharmony_ci // 设备认证 31379a732c7Sopenharmony_ci dmClass.bindTarget(deviceId, bindParam, (err, data) => { 31479a732c7Sopenharmony_ci if (err) { 31579a732c7Sopenharmony_ci console.info(TAG + "bindTarget err:" + JSON.stringify(err)); 31679a732c7Sopenharmony_ci return; 31779a732c7Sopenharmony_ci } else { 31879a732c7Sopenharmony_ci console.info(TAG + "bindTarget result:" + JSON.stringify(data)); 31979a732c7Sopenharmony_ci let token = data.pinTone; 32079a732c7Sopenharmony_ci } 32179a732c7Sopenharmony_ci }); 32279a732c7Sopenharmony_ci} catch (err) { 32379a732c7Sopenharmony_ci console.log("bindTarget err:" + err.code + "," + err.message); 32479a732c7Sopenharmony_ci} 32579a732c7Sopenharmony_ci// 设备取消认证 32679a732c7Sopenharmony_citry { 32779a732c7Sopenharmony_ci var deviceId = "xxxxxxxx"; 32879a732c7Sopenharmony_ci let result = dmClass.unbindTarget(deviceId); 32979a732c7Sopenharmony_ci console.log("unbindTarget last device: " + JSON.stringify(deviceId) + " and result = " 33079a732c7Sopenharmony_ci + result); 33179a732c7Sopenharmony_ci } catch (err) { 33279a732c7Sopenharmony_ci console.log("unbindTarget err:" + err.code + "," + err.message); 33379a732c7Sopenharmony_ci} 33479a732c7Sopenharmony_ci 33579a732c7Sopenharmony_citry { 33679a732c7Sopenharmony_ci // 取消注册设备管理服务死亡监听。 33779a732c7Sopenharmony_ci dmClass.off('serviceDie'); 33879a732c7Sopenharmony_ci // 取消注册设备状态回调。 33979a732c7Sopenharmony_ci dmClass.off('deviceStateChange', (data) => { 34079a732c7Sopenharmony_ci console.info('deviceStateChange' + JSON.stringify(data)); 34179a732c7Sopenharmony_ci }); 34279a732c7Sopenharmony_ci // 释放DeviceManager实例。 34379a732c7Sopenharmony_ci deviceManager.releaseDeviceManager(dmClass); 34479a732c7Sopenharmony_ci} catch (err) { 34579a732c7Sopenharmony_ci console.log("err:" + err.code + "," + err.message); 34679a732c7Sopenharmony_ci} 34779a732c7Sopenharmony_ci``` 34879a732c7Sopenharmony_ci 34979a732c7Sopenharmony_ci- 系统接口: 35079a732c7Sopenharmony_ci 35179a732c7Sopenharmony_ci 该部分接口仅支持系统应用调用。 35279a732c7Sopenharmony_ci 调用以下接口,需要申请ohos.permission.ACCESSS_SERVICE_DM权限才能正常调用。 35379a732c7Sopenharmony_ci 35479a732c7Sopenharmony_ci| 原型 | 描述 | 35579a732c7Sopenharmony_ci| ------------------------------------------------------------ | -------------------- | 35679a732c7Sopenharmony_ci| replyUiAction(action: number, actionResult: string): void; | 回复用户ui操作行为。此接口只能被devicemanager的PIN码hap使用。 | 35779a732c7Sopenharmony_ci| on(type: 'replyResult', callback: Callback<{ param: string}>): void; | 回复UI操作结果回调。 | 35879a732c7Sopenharmony_ci| off(type: 'replyResult', callback?: Callback<{ param: string}>): void; | 取消回复UI操作结果回调。 | 35979a732c7Sopenharmony_ci 36079a732c7Sopenharmony_ci- 示例如下: 36179a732c7Sopenharmony_ci 36279a732c7Sopenharmony_ci```js 36379a732c7Sopenharmony_ci// 设置用户ui操作行为 36479a732c7Sopenharmony_ci/* action = 0 - 允许授权 36579a732c7Sopenharmony_ci action = 1 - 取消授权 36679a732c7Sopenharmony_ci action = 2 - 授权框用户操作超时 36779a732c7Sopenharmony_ci action = 3 - 取消pin码框展示 36879a732c7Sopenharmony_ci action = 4 - 取消pin码输入框展示 36979a732c7Sopenharmony_ci action = 5 - pin码输入框确定操作 37079a732c7Sopenharmony_ci*/ 37179a732c7Sopenharmony_citry { 37279a732c7Sopenharmony_ci let action = 0; 37379a732c7Sopenharmony_ci dmClass.replyUiAction(action, "extra") 37479a732c7Sopenharmony_ci dmClass.on('replyResult', (data) => { 37579a732c7Sopenharmony_ci console.log("replyResult executed, dialog closed" + JSON.stringify(data)) 37679a732c7Sopenharmony_ci var tmpStr = JSON.parse(data.param) 37779a732c7Sopenharmony_ci var isShow = tmpStr.verifyFailed 37879a732c7Sopenharmony_ci console.log("replyResult executed, dialog closed" + isShow) 37979a732c7Sopenharmony_ci }); 38079a732c7Sopenharmony_ci} catch (err) { 38179a732c7Sopenharmony_ci console.log("err:" + err.code + "," + err.message); 38279a732c7Sopenharmony_ci} 38379a732c7Sopenharmony_ci 38479a732c7Sopenharmony_ci``` 38579a732c7Sopenharmony_ci 38679a732c7Sopenharmony_ci详细接口说明请参考[**API文档**](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-distributedservice-kit/js-apis-distributedDeviceManager.md) 38779a732c7Sopenharmony_ci 38879a732c7Sopenharmony_ci## 系统弹框ServiceExtensionAbility 38979a732c7Sopenharmony_ci 39079a732c7Sopenharmony_ci当前版本只支持PIN码认证,需要提供PIN码认证的授权提示界面、PIN码显示界面、PIN码输入界面; 39179a732c7Sopenharmony_ci 39279a732c7Sopenharmony_ci当前,由于系统通过native层直接进行弹窗的能力尚不具备,这里使用ServiceExtensionAbility来进行对应界面的弹窗。 39379a732c7Sopenharmony_ci 39479a732c7Sopenharmony_ci该ServiceExtensionAbility为:DeviceManager_UI.hap,作为系统应用进行预置。 39579a732c7Sopenharmony_ci 39679a732c7Sopenharmony_ci- 编译运行: 39779a732c7Sopenharmony_ci 39879a732c7Sopenharmony_ci 将devicemanager/display工程导入DevEco Studio 2.2 Beta1,复制display目录下的@ohos.distributedHardware.deviceManager.d.ts文件到Sdk\js\2.2.0.1\api\common目录下,进行编译构建及运行调试. 39979a732c7Sopenharmony_ci 40079a732c7Sopenharmony_ci- 编译环境依赖:IDE 2.2 SDK6 40179a732c7Sopenharmony_ci 40279a732c7Sopenharmony_ci- DeviceManager_UI.hap包源码存放位置:[device_manager仓库](https://gitee.com/openharmony/distributedhardware_device_manager/tree/master/display) 40379a732c7Sopenharmony_ci 40479a732c7Sopenharmony_ci- UI显示: 40579a732c7Sopenharmony_ci 40679a732c7Sopenharmony_ci DeviceManager作为认证被控端,授权提示界面、PIN码显示界面由DeviceManager_UI ServiceExtensionAbility默认进行显示; 40779a732c7Sopenharmony_ci 40879a732c7Sopenharmony_ci DeviceManager作为认证发起端,PIN码输入界面可以选择由DeviceManager_UI ServiceExtensionAbility进行显示,还是由开发者自行显示。开发者如需自己定制PIN码输入界面,需要在authenticateDevice接口的认证参数AuthParam中,extraInfo属性里面指定displayOwner参数为1。 40979a732c7Sopenharmony_ci 41079a732c7Sopenharmony_ci## 相关仓 41179a732c7Sopenharmony_ci**** 41279a732c7Sopenharmony_ci 41379a732c7Sopenharmony_ci[**interface_sdk-js**](https://gitee.com/openharmony/interface_sdk-js/) 41479a732c7Sopenharmony_ci 41579a732c7Sopenharmony_ci[**applications_hap**](https://gitee.com/openharmony/applications_hap) 416