|
Name |
|
Date |
Size |
#Lines |
LOC |
| .. | | - | - |
| common/ | | 12-May-2024 | - | 4,753 | 2,505 |
| display/ | | 12-May-2024 | - | 4,062 | 2,799 |
| ext/ | | 12-May-2024 | - | 1,089 | 723 |
| figures/ | | 12-May-2024 | - | | |
| interfaces/ | | 12-May-2024 | - | 16,821 | 13,753 |
| sa_profile/ | | 12-May-2024 | - | 59 | 53 |
| services/ | | 12-May-2024 | - | 16,381 | 12,262 |
| test/ | | 12-May-2024 | - | 39,927 | 22,749 |
| utils/ | | 12-May-2024 | - | 730 | 478 |
| BUILD.gn | D | 12-May-2024 | 1.9 KiB | 61 | 60 |
| CODEOWNERS | D | 12-May-2024 | 773 | 17 | 16 |
| LICENSE | D | 12-May-2024 | 10.1 KiB | 179 | 150 |
| OAT.xml | D | 12-May-2024 | 4 KiB | 68 | 11 |
| README.md | D | 12-May-2024 | 15.2 KiB | 312 | 266 |
| README_zh.md | D | 12-May-2024 | 23.9 KiB | 416 | 353 |
| bundle.json | D | 12-May-2024 | 2.9 KiB | 84 | 84 |
| device_manager.gni | D | 12-May-2024 | 2 KiB | 49 | 42 |
| hisysevent.yaml | D | 12-May-2024 | 5.6 KiB | 125 | 71 |
README.md
1# **DeviceManager**
2
3## Introduction
4
5DeviceManager supports authentication and networking for account-independent distributed devices. It provides a set of APIs for listening, discovery, and authentication of distributed devices.
6
7The figure below shows the architecture and dependencies of DeviceManager:
8
9
10
11## Directory Structure
12
13```
14foundation/distributedhardware/device_manager
15├── common # Header files of common capabilities
16│ └── include
17│ └── ipc
18│ └── model # Header files of the IPC function module
19├── display # Display HAP code
20│ └── entry
21│ └── src
22│ └── main
23│ ├── js # JS code for the PIN display ServiceExtensionAbility
24│ └── resources # Resource configuration files for PIN display ServiceExtensionAbility
25├── figures
26├── interfaces
27│ ├── inner_kits # Internal interfaces and their implementation
28│ │ └── native_cpp # Internal native interfaces and their implementation
29│ │ ├── include
30│ │ │ ├── ipc # IPC header files
31│ │ │ │ ├── lite # Small system
32│ │ │ │ └── standard # Standard system
33│ │ │ └── notify # IPC callback header files
34│ │ └── src
35│ │ ├── ipc # IPC core code
36│ │ │ ├── lite # Small system
37│ │ │ └── standard # Standard system
38│ │ └── notify IPC callback core code
39│ └── kits # External interfaces and their implementation
40│ └── js # External JS interfaces and their implementation
41│ ├── include # External JS interfaces and their implementation files
42│ └── src # External JS interface and their implementation code
43├── sa_profile
44├── services
45│ └── devicemanagerservice # devicemanagerservice core code
46│ ├── include
47│ │ ├── ability # Header files related to PIN display ServiceExtensionAbility startup management
48│ │ ├── auth # Header files related to device authentication
49│ │ ├── ipc # IPC header files
50│ │ │ ├── lite # Small system
51│ │ │ └── standard # Standard system
52│ │ ├── message # Header files related to message parsing
53│ │ ├── requestauth # Header files related to device authentication
54│ │ ├── softbus # Header files related to DSoftBus
55│ │ └── timer # Header files related to timer processing
56│ └── src
57│ ├── ability # Core code related to PIN display ServiceExtensionAbility startup management
58│ │ ├── lite # Small system
59│ │ └── standard # Standard system
60│ ├── auth # Core code related to device authentication
61│ ├── ipc # IPC core code
62│ │ ├── lite # Small system
63│ │ └── standard # Standard system
64│ ├── message # Core code for message parsing
65│ ├── requestauth # Core code for device authentication
66│ ├── softbus # Core code for DSoftBus
67│ └── timer # Core code for timer processing
68└── utils # Header files of utils
69 ├── include
70 │ ├── cipher # Header files related to encryption and decryption
71 │ ├── ipc # IPC common header files
72 │ │ ├── lite # Small system
73 │ │ └── standard # Standard system
74 │ └── log # Log-related header files
75 └── src
76 ├── cipher # Core code of encryption and decryption
77 ├── ipc # IPC common core code
78 │ ├── lite # Small system
79 │ └── standard # Standard system
80 └── log # Log-related core code
81```
82
83## Constraints
84
85- Programming languages: JS and C++
86- Applicable devices: OpenHarmony devices such as the Hi3516D V300 development board
87
88
89## Available APIs
90
91Currently, DeviceManager does not support permission management. Its system APIs can be called only by system applications. In later versions, strict permission control will be implemented.
92For details about the APIs, see *ohos.distributedHardware.deviceManager.d.ts* in [**interface_sdk-js repository**](https://gitee.com/openharmony/interface_sdk-js/).
93
94- Public APIs
95
96 Before using APIs in **DeviceManager**, call **createDeviceManager** to create a **DeviceManager** instance.
97
98 If you no longer need to use the APIs, release the **DeviceManager** instance.
99
100| Prototype | Description |
101| ------------------------------------------------------------ | ------------------------------- |
102| createDeviceManager(bundleName: string, callback: AsyncCallback<DeviceManager>): void; | Obtains a **DeviceManager** instance in asynchronous mode.|
103| release(): void; | Releases a **DeviceManager** instance. |
104
105
106- System APIs
107
108 DeviceManager provides APIs related to trusted device list obtaining, device state listening, device discovery, and device authentication. These APIs are system APIs and can be invoked only by system applications.
109
110 The APIs for starting and stopping device discovery must be used in pairs, with the same subscribe ID.
111
112| Prototype | Description |
113| ------------------------------------------------------------ | -------------------- |
114| getTrustedDeviceListSync(): Array<DeviceInfo>; | Obtains the trusted device list.|
115| on(type: 'deviceStateChange', callback: Callback<{ action: DeviceStateChangeAction, device: DeviceInfo }>): void; | Subscribes to device state changes.|
116| off(type: 'deviceStateChange', callback?: Callback<{ action: DeviceStateChangeAction, device: DeviceInfo }>): void; | Unsubscribes from device state changes.|
117| on(type: 'serviceDie', callback: () => void): void; | Subscribes to device errors.|
118| off(type: 'serviceDie', callback?: () => void): void; | Unsubscribes from service errors.|
119| startDeviceDiscovery(subscribeInfo: SubscribeInfo): void; | Starts device discovery. |
120| stopDeviceDiscovery(subscribeId: number): void; | Stops device discovery. |
121| authenticateDevice(deviceInfo: DeviceInfo, authparam: AuthParam, callback: AsyncCallback<{deviceId: string, pinTone ?: number}>): void; | Authenticates a device. |
122| setUserOperation(operateAction: number, params: string): void; | Set user ui operation behavior |
123| verifyAuthInfo(authInfo: AuthInfo, callback: AsyncCallback<{deviceId: string, level: number}>): void; | Verifies device authentication information. |
124| on(type: 'deviceFound', callback: Callback<{ subscribeId: number, device: DeviceInfo }>): void; | Subscribes to discovered device list changes. |
125| off(type: 'deviceFound', callback?: Callback<{ subscribeId: number, device: DeviceInfo }>): void; | Unsubscribes from discovered device list changes.|
126| on(type: 'discoverFail', callback: Callback<{ subscribeId: number, reason: number }>): void; | Subscribes to device discovery failures. |
127| off(type: 'discoverFail', callback?: Callback<{ subscribeId: number, reason: number }>): void; | Unsubscribes from device discovery failures.|
128| on(type: 'publishSuccess', callback: Callback<{ publishId: number }>): void; | publish device success |
129| off(type: 'publishSuccess', callback?: Callback<{ publishId: number }>): void; | delete unpublish device success |
130| on(type: 'publishFail', callback: Callback<{ publishId: number, reason: number }>): void; | publish device fail |
131| off(type: 'publishFail', callback?: Callback<{ publishId: number, reason: number }>): void; | delete unpublish device fail |
132| on(type: 'uiStateChange', callback: Callback<{ param: string}>): void; | UI status change callback |
133| off(type: 'uiStateChange', callback?: Callback<{ param: string}>): void; | Cancel UI status change callback |
134## Sample Code
135
136```
137// Create a DeviceManager instance.
138deviceManager.createDeviceManager('com.ohos.xxxx', (err, dm) => {
139 this.log("createDeviceManager err:" + JSON.stringify(err) + ' --success:' + JSON.stringify(dm))
140 if (err) return;
141 dmClass = dm;
142 dmClass.on('serviceDie', data => this.log("serviceDie on:" + JSON.stringify(data)))
143});
144
145// Subscribe to/Unsubscribe from device state changes.
146dmClass.on('deviceStateChange', (data) => {
147 this.log("deviceStateChange on:" + JSON.stringify(data));
148 switch (data.action) {
149 case ONLINE:
150 // the device is physically online
151 break;
152 case READY:
153 // the information between devices has been synchronized in the Distributed Data Service (DDS) module,
154 // and the device is ready for running distributed services
155 break;
156 case OFFLINE:
157 // the device is physically offline
158 break;
159 case CHANGE:
160 // the device information is changed
161 break;
162 default:
163 break;
164 }
165});
166dmClass.off('deviceStateChange')
167
168// Query the trusted device list.
169var array = dmClass.getTrustedDeviceListSync();
170
171// Obtain the local device information.
172var localDeviceInfo = dmClass.getLocalDeviceInfoSync();
173
174// Start device discovery (to discover peripheral untrusted devices).
175var subscribeId = 0;
176dmClass.on('deviceFound', (data) => {
177 if (data == null) {
178 this.log("deviceFound error data=null")
179 return;
180 }
181 this.logList.push("deviceFound:" + JSON.stringify(data));
182});
183dmClass.on('discoverFail', (data) => {
184 this.log("discoverFail on:" + JSON.stringify(data));
185});
186subscribeId = Math.floor(Math.random() * 10000 + 1000)
187var info = {
188 "subscribeId": subscribeId,
189 "mode": 0xAA,
190 "medium": 0,
191 "freq": 2,
192 "isSameAccount": false,
193 "isWakeRemote": true,
194 "capability": 0
195};
196var filterOptions = {
197 "filter_op": "OR", // optional, "OR" default
198 "filters": [
199 {
200 "type": "range",
201 "value": 50 // need to filter the distance of dicovered device, in centimeters(cm).
202 }
203 ]
204};
205dmClass.startDeviceDiscovery(info, JSON.stringify(filterOptions));
206
207// Stop device discovery (used with startDeviceDiscovery).
208dmClass.stopDeviceDiscovery(subscribeId);
209
210// publish device discovery
211var publishId = 0;
212dmClass.on('publishSuccess', (data) => {
213 if (data == null) {
214 this.log("publishSuccess error data=null")
215 return;
216 }
217 this.logList.push("publishSuccess:" + JSON.stringify(data));
218});
219dmClass.on('publishFailed', (data) => {
220 this.log("publishFailed on:" + JSON.stringify(data));
221});
222publishId = Math.floor(Math.random() * 10000 + 1000)
223var info = {
224 "publishId": publishId,
225 "mode": 0xAA,
226 "freq": 2,
227 "ranging": 1
228};
229dmClass.publishDeviceDiscovery(info);
230
231// unPublish device discovery(used with publishDeviceDiscovery).
232dmClass.unPublishDeviceDiscovery(publishId);
233
234// operateAction User Operation Actions.
235/* operateAction = 0 - allow authentication
236 operateAction = 1 - cancel authentication
237 operateAction = 2 - user operation timeout for authentication confirm
238 operateAction = 3 - cancel pincode display
239 operateAction = 4 - cancel pincode input
240 operateAction = 5 - confirm pincode input
241*/
242dmClass.setUserOperation(operation, "extra")
243dmClass.on('uiStateChange', (data) => {
244 console.log("uiStateChange executed, dialog closed" + JSON.stringify(data))
245 var tmpStr = JSON.parse(data.param)
246 this.isShow = tmpStr.verifyFailed
247 console.log("uiStateChange executed, dialog closed" + this.isShow)
248 if (!this.isShow) {
249 this.destruction()
250 }
251});
252dmClass.off('uiStateChange')
253
254// Authenticate a device.
255var deviceInfo ={
256 "deviceId": "XXXXXXXX",
257 "deviceName": "",
258 deviceType: 0
259};
260let extraInfo = {
261 "appOperation": "xxxxxxxx", // App operation. Support for user customization.
262 "customDescription": "xxxxxxxx", // Custom description. Support for user customization.
263}
264let authParam = {
265 "authType": 1,
266 "extraInfo": extraInfo
267}
268dmClass.authenticateDevice(this.deviceInfo, authParam, (err, data) => {
269 if (err) {
270 this.logList.push("authenticateDevice err:" + JSON.stringify(err));
271 console.info(TAG + "authenticateDevice err:" + JSON.stringify(err));
272 return;
273 }
274 this.logList.push("authenticateDevice result:" + JSON.stringify(data));
275 console.info(TAG + "authenticateDevice result:" + JSON.stringify(data));
276 token = data.pinToken;
277});
278
279// Cancel device authentication.
280dmClass.unAuthenticateDevice(this.deviceInfo);
281```
282## System Dialog Box ServiceExtensionAbility
283
284Only PIN authentication is supported in the current version. To support PIN authentication, an authorization prompt page, a PIN display page, and a PIN input page must be provided.
285
286Currently, the system does not support the dialog box display through the native layer. Therefore, a temporary ServiceExtensionAbility is used to display a dialog box.
287
288This ServiceExtensionAbility is called **DeviceManager_UI.hap**, which is preset as a system application.
289
290- Compilation and running
291
292 Import the **device_manager/display** project to DevEco Studio 2.2 Beta1. Copy the **@ohos.distributedHardware.deviceManager.d.ts** file in the **display** directory to the **Sdk\js\2.2.0.1\api\common** directory for compilation, building, and debugging.
293
294- Compilation environment: IDE 2.2 SDK6
295
296- Storage location of **DeviceManager_UI.hap** demo: [device_manager repository](https://gitee.com/openharmony/distributedhardware_device_manager/tree/master/display)
297
298- UI display
299
300 When DeviceManager functions as the authenticated party, the authorization prompt page and PIN display page are controlled by the **DeviceManager_UI.hap** ServiceExtensionAbility by default.
301
302 When DeviceManager functions as the authentication initiator, the PIN input page can be displayed either by **DeviceManager_UI.hap** ServiceExtensionAbility or a developer-customized page. To customize the PIN input page, set the **displayOwner** parameter in the **extraInfo** attribute of the **AuthParam** parameter of the **authenticateDevice** API to **1**.
303
304### Repositories Involved
305****
306
307[**interface_sdk-js**](https://gitee.com/openharmony/interface_sdk-js/)
308
309[**applications_hap**](https://gitee.com/openharmony/applications_hap)
310
311**device_manager**
312
README_zh.md
1# **DeviceManager部件**
2
3## 简介
4
5DeviceManager组件在OpenHarmony上提供账号无关的分布式设备的认证组网能力,并为开发者提供了一套用于分布式设备间监听、发现和认证的接口。
6
7其组成及依赖如下所示:
8
9
10
11dsoftbus提供能力:
12 提供设备上下线通知及设备信息,设备认证通道和设备发现能力。
13deviceauth提供能力:
14 提供设备群组管理和群组认证能力。
15
16
17## 目录
18
19```
20foundation/distributedhardware/distributedhardware_device_manager
21├── common # 公共能力头文件存放目录
22│ ├── include
23│ │ ├── dfx # dfx功能头文件存放目录
24│ │ └── ipc # ipc功能头文件存放目录
25│ │ ├── lite # small
26│ │ ├── model # ipc功能模块头文件存放目录
27│ │ └── standard # standard
28│ └── src
29│ ├── dfx # dfx功能实现相关代码
30│ └── ipc # ipc公共功能代码
31│ ├── lite # small
32│ └── standard # standard
33├── display # DM显示hap代码
34│ └── entry
35│ └── src
36│ └── main
37│ ├── js # DM PIN码显示ServiceExtensionAbility相关JS代码
38│ └── resources # DM PIN码显示ServiceExtensionAbility相关资源配置文件目录
39├── figures
40├── interfaces
41│ ├── inner_kits # 内部接口及实现存放目录
42│ │ └── native_cpp # 内部native接口及实现存放目录
43│ │ ├── include
44│ │ │ ├── ipc # ipc头文件存放目录
45│ │ │ │ ├── lite # small
46│ │ │ │ └── standard # standard
47│ │ │ └── notify # ipc回调通知头文件目录
48│ │ └── src
49│ │ ├── ipc # ipc功能代码
50│ │ │ ├── lite # small
51│ │ │ └── standard # standard
52│ │ └── notify # ipc回调通知功能代码
53│ └── kits # 外部接口及实现存放目录
54│ ├── js # 外部JS接口及实现存放目录
55│ │ ├── include # 外部JS接口及实现头文件存放目录
56│ │ └── src # 外部JS接口及实现代码
57| └── js4.0 # 从Openharmony 4.0.9.2版本开始新增外部JS接口及实现存放目录
58| ├── include # 新增外部JS接口及实现头文件存放目录
59| └── src # 新增外部JS接口及实现代码
60|
61├── sa_profile # SA进程配置相关文件存放目录
62├── services
63│ ├── implementation # devicemanagerservice服务实现核心代码
64│ │ ├── include
65│ │ │ ├── ability # 与PIN码显示ServiceExtensionAbility拉起管理相关头文件
66│ │ │ ├── adapter # DM适配扩展功能相关头文件
67│ │ │ ├── authentication # device_auth交互相关头文件
68│ │ │ ├── config # DM适配扩展相关配置功能头文件
69│ │ │ ├── credential # 凭据管理功能相关头文件
70│ │ │ ├── dependency # 外部依赖模块相关头文件
71│ │ │ │ ├── commonevent # 事件处理功能相关头文件
72│ │ │ │ ├── hichain # 与hichain功能交互相关头文件
73│ │ │ │ ├── mini # mini
74│ │ │ │ ├── multipleuser # 多用户功能相关头文件
75│ │ │ │ ├── softbus # 与软总线功能交互相关头文件
76│ │ │ │ └── timer # 定时器处理相关头文件
77│ │ │ ├── devicestate # 设备状态管理功能相关头文件
78│ │ │ ├── discovery # 设备发现功能相关头文件
79│ │ │ ├── dispath # L0上功能实现相关头文件
80│ │ │ └── publish # 设备主动发现功能相关头文件
81│ │ └── src
82│ │ ├── ability # 与PIN码显示ServiceExtensionAbility拉起管理相关功能代码
83│ │ ├── adapter # DM适配扩展功能代码
84│ │ ├── authentication # device_auth交互相关核心代码
85│ │ ├── config # DM适配扩展相关配置功能代码
86│ │ ├── credential # 凭据管理功能代码
87│ │ ├── dependency # 外部依赖模块功能代码
88│ │ │ ├── commonevent # 事件处理功能代码
89│ │ │ ├── hichain # 与hichain功能交互代码
90│ │ │ ├── mini # mini
91│ │ │ ├── multipleuser # 多用户功能代码
92│ │ │ ├── softbus # 与软总线功能交互代码
93│ │ │ └── timer # 定时器处理相关功能代码
94│ │ ├── devicestate # 设备状态管理功能代码
95│ │ ├── discovery # 设备发现功能代码
96│ │ ├── dispath # mini上功能实现代码
97│ │ └── publish # 设备主动发现功能
98│ └── service # devicemanagerservice服务实现核心代码
99│ ├── include
100│ │ ├── ipc # 进程间通信相关头文件
101│ │ │ ├── lite # small
102│ │ │ └── standard # standard
103│ │ └── softbus # 软总线相关头文件
104│ └── src
105│ ├── ipc # 进程间通信相关功能代码
106│ │ ├── lite # small
107│ │ └── standard # standard
108│ └── softbus # 通道建立功能核心代码
109└── utils # 公共能力头文件存放目录
110 ├── include
111 │ ├── fwkload # 拉起其他sa功能相关头文件
112 │ └── permission # 权限校验相关头文件存放目录
113 └── src
114 ├── fwkload # 拉起其他sa功能相关功能代码
115 └── permission # 权限校验相关功能代码
116```
117
118## 约束
119
120- 开发语言:JS、C++
121- 适用于Hi3516DV300单板等OpenHarmony设备
122
123
124## 废弃接口说明
125
126该部分接口暂时可继续使用,但是暂停维护,建议使用新接口进行开发。
127
128当前版本设备管理服务不具备权限管理的能力,接口中的system api仅供系统调用,后续版本会进行严格的权限管控。
129接口参见[**interface_sdk-js仓库的**](https://gitee.com/openharmony/interface_sdk-js/) *ohos.distributedHardware.deviceManager.d.ts*
130
131- 公共接口:
132
133 使用DeviceManager相关接口之前,需要通过createDeviceManager接口创建DeviceManager实例;
134
135 不使用DeviceManager接口的时候需要释放对应的DeviceManager实例。
136
137| 原型 | 描述 |
138| ------------------------------------------------------------ | ------------------------------- |
139| createDeviceManager(bundleName: string, callback: AsyncCallback<DeviceManager>): void; | 以异步方法获取DeviceManager实例 |
140| release(): void; | 释放DeviceManager实例 |
141
142- 系统能力接口:
143
144 提供可信设备列表获取、可信设备状态监听、周边设备发现、设备认证等相关接口,该部分作为系统能力接口,仅供系统应用调用。
145
146 开始设备发现、停止发现设备接口要配对使用,使用同一个subscribeId。
147
148| 原型 | 描述 |
149| ------------------------------------------------------------ | -------------------- |
150| getTrustedDeviceListSync(): Array<DeviceInfo>; | 获取信任设备列表 |
151| getTrustedDeviceList(callback:AsyncCallback<Array<DeviceInfo>>): void; | 获取信任设备列表 |
152| getTrustedDeviceList(): Promise<Array<DeviceInfo>>; | 获取信任设备列表 |
153| getLocalDeviceInfoSync(): DeviceInfo; | 获取本地设备信息 |
154| getLocalDeviceInfo(callback:AsyncCallback<DeviceInfo>): void; | 获取本地设备信息 |
155| getLocalDeviceInfo(): Promise<DeviceInfo>; | 获取本地设备信息 |
156| on(type: 'deviceStateChange', callback: Callback<{ action: DeviceStateChangeAction, device: DeviceInfo }>): void; | 设备状态变更回调 |
157| off(type: 'deviceStateChange', callback?: Callback<{ action: DeviceStateChangeAction, device: DeviceInfo }>): void; | 取消设备状态变更回调 |
158| on(type: 'serviceDie', callback: () => void): void; | 服务错误回调 |
159| off(type: 'serviceDie', callback?: () => void): void; | 取消服务错误回调 |
160| startDeviceDiscovery(subscribeInfo: SubscribeInfo): void; | 开始设备发现 |
161| stopDeviceDiscovery(subscribeId: number): void; | 停止发现设备 |
162| authenticateDevice(deviceInfo: DeviceInfo, authParam: AuthParam, callback: AsyncCallback<{deviceId: string, pinToken ?: number}>): void; | 设备认证接口 |
163| unAuthenticateDevice(deviceInfo: DeviceInfo): void; | 解除认证设备 |
164| setUserOperation(operateAction: number, params: string): void; | 设置用户ui操作行为 |
165| verifyAuthInfo(authInfo: AuthInfo, callback: AsyncCallback<{deviceId: string, level: number}>): void; | 设备认证信息校验 |
166| startDeviceDiscovery(subscribeInfo: SubscribeInfo, filterOptions?: string): void; | 发现周边设备 |
167| publishDeviceDiscovery(publishInfo: PublishInfo): void; | 发布设备发现 |
168| unPublishDeviceDiscovery(publishId: number): void; | 停止发布设备发现 |
169| on(type: 'deviceFound', callback: Callback<{ subscribeId: number, device: DeviceInfo }>): void; | 发现设备列表回调 |
170| off(type: 'deviceFound', callback?: Callback<{ subscribeId: number, device: DeviceInfo }>): void; | 取消发现设备列表回调 |
171| on(type: 'discoverFail', callback: Callback<{ subscribeId: number, reason: number }>): void; | 发现设备失败回调 |
172| off(type: 'discoverFail', callback?: Callback<{ subscribeId: number, reason: number }>): void; | 取消发现设备失败回调 |
173| on(type: 'publishSuccess', callback: Callback<{ publishId: number }>): void; | 发布设备成功回调 |
174| off(type: 'publishSuccess', callback?: Callback<{ publishId: number }>): void; | 取消发布设备成功回调 |
175| on(type: 'publishFail', callback: Callback<{ publishId: number, reason: number }>): void; | 发布设备失败回调 |
176| off(type: 'publishFail', callback?: Callback<{ publishId: number, reason: number }>): void; | 取消发布设备失败回调 |
177| on(type: 'uiStateChange', callback: Callback<{ param: string}>): void; | ui状态变更回调 |
178| off(type: 'uiStateChange', callback?: Callback<{ param: string}>): void; | 取消ui状态变更回调 |
179
180## 新增接口说明
181
182 从Openharmony 4.0.9.2版本开始,新增设备管理接口。
183
184接口参见[**interface_sdk-js仓库的**](https://gitee.com/openharmony/interface_sdk-js/) *ohos.distributedDeviceManager.d.ts*
185
186- 公共接口:
187
188 调用以下接口,需要申请ohos.permission.DISTRIBUTED_DATASYNC权限才能正常调用。
189
190 使用DeviceManager相关接口之前,需要通过createDeviceManager接口创建DeviceManager实例;
191
192 不使用DeviceManager接口的时候需要释放对应的DeviceManager实例。
193
194 提供可信设备列表获取、可信设备状态监听、周边设备发现、设备认证等相关接口,支持三方应用调用。
195
196| 原型 | 描述 |
197| ------------------------------------------------------------ | ------------------------------- |
198| createDeviceManager(bundleName: string): DeviceManager; | 创建一个设备管理实例。设备管理实例是分布式设备管理方法的调用入口。用于获取可信设备和本地设备的相关信息。 |
199| releaseDeviceManager(deviceManager: DeviceManager): void; | 设备管理实例不再使用后,通过该方法释放DeviceManager实例。 |
200| getAvailableDeviceListSync(): Array<DeviceBasicInfo>; | 同步获取所有可信设备列表。 |
201| getAvailableDeviceList(callback:AsyncCallback<Array<DeviceBasicInfo>>): void; | 获取所有可信设备列表。使用callback异步回调。 |
202| getAvailableDeviceList(): Promise<Array<DeviceBasicInfo>>; | 获取所有可信设备列表。使用Promise异步回调。 |
203| getLocalDeviceNetworkId(): string; | 获取本地设备网络标识。 |
204| getLocalDeviceName(): string; | 获取本地设备名称。 |
205| getLocalDeviceType(): number; | 获取本地设备类型。 |
206| getLocalDeviceId(): string; | 获取本地设备id。 |
207| getDeviceName(networkId: string): string; | 通过指定设备的网络标识获取该设备名称。 |
208| getDeviceType(networkId: string): number; | 通过指定设备的网络标识获取该设备类型。 |
209| startDiscovering(discoverParam: {[key: string]: Object} , filterOptions?: {[key: string]: Object} ): void; | 发现周边设备。发现状态持续两分钟,超过两分钟,会停止发现,最大发现数量99个。 |
210| stopDiscovering(): void; | 停止发现周边设备。 |
211| bindTarget(deviceId: string, bindParam: {[key: string]: Object} , callback: AsyncCallback<{deviceId: string}>): void; | 认证设备。 |
212| unbindTarget(deviceId: string): void; | 解除认证设备。 |
213| on(type: 'deviceStateChange', callback: Callback<{ action: DeviceStateChange, device: DeviceBasicInfo }>): void; | 注册设备状态回调。 |
214| off(type: 'deviceStateChange', callback?: Callback<{ action: DeviceStateChange, device: DeviceBasicInfo }>): void; | 取消注册设备状态回调。 |
215| on(type: 'discoverSuccess', callback: Callback<{ device: DeviceBasicInfo }>): void; | 注册发现设备成功回调监听。 |
216| off(type: 'discoverSuccess', callback?: Callback<{ device: DeviceBasicInfo }>): void; | 取消注册设备发现成功回调。 |
217| on(type: 'deviceNameChange', callback: Callback<{ deviceName: string }>): void; | 注册设备名称变更回调监听。 |
218| off(type: 'deviceNameChange', callback?: Callback<{ deviceName: string }>): void; | 取消注册设备名称变更回调监听。 |
219| on(type: 'discoverFailure', callback: Callback<{ reason: number }>): void; | 注册设备发现失败回调监听。 |
220| off(type: 'discoverFailure', callback?: Callback<{ reason: number }>): void; | 取消注册设备发现失败回调。 |
221| on(type: 'serviceDie', callback?: Callback<{}>): void; | 注册设备管理服务死亡监听。 |
222| off(type: 'serviceDie', callback?: Callback<{}>): void; | 取消注册设备管理服务死亡监听。 |
223
224- 示例如下:
225
226```js
227try {
228 let dmClass = deviceManager.createDeviceManager("ohos.samples.jshelloworld");
229} catch(err) {
230 console.error("createDeviceManager errCode:" + err.code + ",errMessage:" + err.message);
231}
232
233try {
234 // 注册设备管理服务死亡监听。
235 dmClass.on("serviceDie", () => {
236 console.info("serviceDie on");
237 });
238} catch (err) {
239 console.error("serviceDie errCode:" + err.code + ",errMessage:" + err.message);
240}
241
242try {
243 dmClass.on('deviceStateChange', (data) => {
244 console.info("deviceStateChange on:" + JSON.stringify(data));
245 });
246} catch (err) {
247 console.error("deviceStateChange errCode:" + err.code + ",errMessage:" + err.message);
248}
249
250// 同步获取所有可信设备列表。
251try {
252 let trustList = dmClass.getAvailableDeviceListSync();
253 console.log("trustList:" + JSON.stringify(this.trustList));
254} catch(err) {
255 console.log("getAvailableDeviceList failed: " + JSON.stringify(err));
256}
257// 获取本地设备类型。
258var data = dmClass.getLocalDeviceType();
259
260// 获取本地设备名称。
261var data = dmClass.getLocalDeviceName();
262
263// 发现周边设备。
264try {
265 // 注册发现设备成功回调监听。
266 dmClass.on('discoverSuccess', (data) => {
267 if (data == null) {
268 console.log("discoverSuccess error data = null")
269 return;
270 }
271 console.info(TAG + "discoverSuccess:" + JSON.stringify(data));
272 });
273 // 注册设备发现失败回调监听。
274 dmClass.on('discoverFailure', (data) => {
275 console.info("discoverFailure on:" + JSON.stringify(data));
276 });
277 var discoverParam = {
278 'discoverTargetType': 1
279 };
280 var filterOptions = {
281 'availableStatus': 1,
282 'discoverDistance': 50,
283 'authenticationStatus': 0,
284 'authorizationType': 0
285 };
286 dmClass.startDiscovering(discoverParam, filterOptions); // 当有设备发现时,通过discoverSuccess回调通知给应用程序
287} catch (err) {
288 console.log("startDiscovering err:" + err.code + "," + err.message);
289}
290
291// 停止发现周边设备。
292try {
293 dmClass.stopDiscovering();
294 // 取消注册设备发现成功回调。
295 dmClass.off('discoverSuccess');
296 // 取消注册设备发现失败回调。
297 dmClass.off('discoverFailure');
298} catch (err) {
299 console.log("err:" + err.code + "," + err.message);
300}
301
302// 设备认证
303var deviceId ="XXXXXXXX";
304let bindParam = {
305 'bindType': 1,// 认证类型: 1 - 无帐号PIN码认证
306 'targetPkgName': 'xxxx',
307 'appName': 'xxxx',
308 'appOperation': 'xxxx',
309 'customDescription': 'xxxx'
310}
311try {
312 // 设备认证
313 dmClass.bindTarget(deviceId, bindParam, (err, data) => {
314 if (err) {
315 console.info(TAG + "bindTarget err:" + JSON.stringify(err));
316 return;
317 } else {
318 console.info(TAG + "bindTarget result:" + JSON.stringify(data));
319 let token = data.pinTone;
320 }
321 });
322} catch (err) {
323 console.log("bindTarget err:" + err.code + "," + err.message);
324}
325// 设备取消认证
326try {
327 var deviceId = "xxxxxxxx";
328 let result = dmClass.unbindTarget(deviceId);
329 console.log("unbindTarget last device: " + JSON.stringify(deviceId) + " and result = "
330 + result);
331 } catch (err) {
332 console.log("unbindTarget err:" + err.code + "," + err.message);
333}
334
335try {
336 // 取消注册设备管理服务死亡监听。
337 dmClass.off('serviceDie');
338 // 取消注册设备状态回调。
339 dmClass.off('deviceStateChange', (data) => {
340 console.info('deviceStateChange' + JSON.stringify(data));
341 });
342 // 释放DeviceManager实例。
343 deviceManager.releaseDeviceManager();
344} catch (err) {
345 console.log("err:" + err.code + "," + err.message);
346}
347```
348
349- 系统接口:
350
351 该部分接口仅支持系统应用调用。
352 调用以下接口,需要申请ohos.permission.ACCESSS_SERVICE_DM权限才能正常调用。
353
354| 原型 | 描述 |
355| ------------------------------------------------------------ | -------------------- |
356| replyUiAction(action: number, actionResult: string): void; | 回复用户ui操作行为。此接口只能被devicemanager的PIN码hap使用。 |
357| on(type: 'replyResult', callback: Callback<{ param: string}>): void; | 回复UI操作结果回调。 |
358| off(type: 'replyResult', callback?: Callback<{ param: string}>): void; | 取消回复UI操作结果回调。 |
359
360- 示例如下:
361
362```js
363// 设置用户ui操作行为
364/* action = 0 - 允许授权
365 action = 1 - 取消授权
366 action = 2 - 授权框用户操作超时
367 action = 3 - 取消pin码框展示
368 action = 4 - 取消pin码输入框展示
369 action = 5 - pin码输入框确定操作
370*/
371try {
372 let action = 0;
373 dmClass.replyUiAction(action, "extra")
374 dmClass.on('replyResult', (data) => {
375 console.log("replyResult executed, dialog closed" + JSON.stringify(data))
376 var tmpStr = JSON.parse(data.param)
377 var isShow = tmpStr.verifyFailed
378 console.log("replyResult executed, dialog closed" + isShow)
379 });
380} catch (err) {
381 console.log("err:" + err.code + "," + err.message);
382}
383
384```
385
386详细接口说明请参考[**API文档**](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-distributedDeviceManager.md)
387
388## 系统弹框ServiceExtensionAbility
389
390当前版本只支持PIN码认证,需要提供PIN码认证的授权提示界面、PIN码显示界面、PIN码输入界面;
391
392当前,由于系统通过native层直接进行弹窗的能力尚不具备,这里使用ServiceExtensionAbility来进行对应界面的弹窗。
393
394该ServiceExtensionAbility为:DeviceManager_UI.hap,作为系统应用进行预置。
395
396- 编译运行:
397
398 将devicemanager/display工程导入DevEco Studio 2.2 Beta1,复制display目录下的@ohos.distributedHardware.deviceManager.d.ts文件到Sdk\js\2.2.0.1\api\common目录下,进行编译构建及运行调试.
399
400- 编译环境依赖:IDE 2.2 SDK6
401
402- DeviceManager_UI.hap包源码存放位置:[device_manager仓库](https://gitee.com/openharmony/distributedhardware_device_manager/tree/master/display)
403
404- UI显示:
405
406 DeviceManager作为认证被控端,授权提示界面、PIN码显示界面由DeviceManager_UI ServiceExtensionAbility默认进行显示;
407
408 DeviceManager作为认证发起端,PIN码输入界面可以选择由DeviceManager_UI ServiceExtensionAbility进行显示,还是由开发者自行显示。开发者如需自己定制PIN码输入界面,需要在authenticateDevice接口的认证参数AuthParam中,extraInfo属性里面指定displayOwner参数为1。
409
410## 相关仓
411****
412
413[**interface_sdk-js**](https://gitee.com/openharmony/interface_sdk-js/)
414
415[**applications_hap**](https://gitee.com/openharmony/applications_hap)
416