|
Name |
|
Date |
Size |
#Lines |
LOC |
| .. | | - | - |
| common/ | | 12-May-2024 | - | 3,886 | 2,117 |
| display/ | | 12-May-2024 | - | 3,369 | 2,715 |
| ext/ | | 12-May-2024 | - | 1,100 | 736 |
| figures/ | | 12-May-2024 | - | | |
| interfaces/ | | 12-May-2024 | - | 10,832 | 8,727 |
| sa_profile/ | | 12-May-2024 | - | 72 | 51 |
| services/ | | 12-May-2024 | - | 13,939 | 10,237 |
| test/ | | 12-May-2024 | - | 32,874 | 18,507 |
| utils/ | | 12-May-2024 | - | 818 | 567 |
| BUILD.gn | D | 12-May-2024 | 1.9 KiB | 62 | 61 |
| 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.3 KiB | 316 | 270 |
| README_zh.md | D | 12-May-2024 | 16.5 KiB | 344 | 297 |
| bundle.json | D | 12-May-2024 | 3 KiB | 87 | 86 |
| device_manager.gni | D | 12-May-2024 | 1.7 KiB | 43 | 36 |
| 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 "appIcon": new Uint8Array(), // App icon. This parameter is optional.
262 "appThumbnail": new Uint8Array(), // App thumbnail. This parameter is optional.
263 "appName": "xxxxxxxx", // App name.
264 "appDescription": "xxxxxxxx", // App description.
265 "business": '0',
266 "displayOwner": 0,
267}
268let authParam = {
269 "authType": 1,
270 "extraInfo": extraInfo
271}
272dmClass.authenticateDevice(this.deviceInfo, authParam, (err, data) => {
273 if (err) {
274 this.logList.push("authenticateDevice err:" + JSON.stringify(err));
275 console.info(TAG + "authenticateDevice err:" + JSON.stringify(err));
276 return;
277 }
278 this.logList.push("authenticateDevice result:" + JSON.stringify(data));
279 console.info(TAG + "authenticateDevice result:" + JSON.stringify(data));
280 token = data.pinToken;
281});
282
283// Cancel device authentication.
284dmClass.unAuthenticateDevice(this.deviceInfo);
285```
286## System Dialog Box ServiceExtensionAbility
287
288Only 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.
289
290Currently, the system does not support the dialog box display through the native layer. Therefore, a temporary ServiceExtensionAbility is used to display a dialog box.
291
292This ServiceExtensionAbility is called **DeviceManager_UI.hap**, which is preset as a system application.
293
294- Compilation and running
295
296 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.
297
298- Compilation environment: IDE 2.2 SDK6
299
300- Storage location of **DeviceManager_UI.hap** demo: [device_manager repository](https://gitee.com/openharmony/distributedhardware_device_manager/tree/master/display)
301
302- UI display
303
304 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.
305
306 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**.
307
308### Repositories Involved
309****
310
311[**interface_sdk-js**](https://gitee.com/openharmony/interface_sdk-js/)
312
313[**applications_hap**](https://gitee.com/openharmony/applications_hap)
314
315**device_manager**
316
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├── sa_profile # SA进程配置相关文件存放目录
58├── services
59│ ├── implementation # devicemanagerservice服务实现核心代码
60│ │ ├── include
61│ │ │ ├── ability # 与PIN码显示ServiceExtensionAbility拉起管理相关头文件
62│ │ │ ├── adapter # DM适配扩展功能相关头文件
63│ │ │ ├── authentication # device_auth交互相关头文件
64│ │ │ ├── config # DM适配扩展相关配置功能头文件
65│ │ │ ├── credential # 凭据管理功能相关头文件
66│ │ │ ├── dependency # 外部依赖模块相关头文件
67│ │ │ │ ├── commonevent # 事件处理功能相关头文件
68│ │ │ │ ├── hichain # 与hichain功能交互相关头文件
69│ │ │ │ ├── mini # mini
70│ │ │ │ ├── multipleuser # 多用户功能相关头文件
71│ │ │ │ ├── softbus # 与软总线功能交互相关头文件
72│ │ │ │ └── timer # 定时器处理相关头文件
73│ │ │ ├── devicestate # 设备状态管理功能相关头文件
74│ │ │ ├── discovery # 设备发现功能相关头文件
75│ │ │ ├── dispath # L0上功能实现相关头文件
76│ │ │ └── publish # 设备主动发现功能相关头文件
77│ │ └── src
78│ │ ├── ability # 与PIN码显示ServiceExtensionAbility拉起管理相关功能代码
79│ │ ├── adapter # DM适配扩展功能代码
80│ │ ├── authentication # device_auth交互相关核心代码
81│ │ ├── config # DM适配扩展相关配置功能代码
82│ │ ├── credential # 凭据管理功能代码
83│ │ ├── dependency # 外部依赖模块功能代码
84│ │ │ ├── commonevent # 事件处理功能代码
85│ │ │ ├── hichain # 与hichain功能交互代码
86│ │ │ ├── mini # mini
87│ │ │ ├── multipleuser # 多用户功能代码
88│ │ │ ├── softbus # 与软总线功能交互代码
89│ │ │ └── timer # 定时器处理相关功能代码
90│ │ ├── devicestate # 设备状态管理功能代码
91│ │ ├── discovery # 设备发现功能代码
92│ │ ├── dispath # mini上功能实现代码
93│ │ └── publish # 设备主动发现功能
94│ └── service # devicemanagerservice服务实现核心代码
95│ ├── include
96│ │ ├── ipc # 进程间通信相关头文件
97│ │ │ ├── lite # small
98│ │ │ └── standard # standard
99│ │ └── softbus # 软总线相关头文件
100│ └── src
101│ ├── ipc # 进程间通信相关功能代码
102│ │ ├── lite # small
103│ │ └── standard # standard
104│ └── softbus # 通道建立功能核心代码
105└── utils # 公共能力头文件存放目录
106 ├── include
107 │ ├── fwkload # 拉起其他sa功能相关头文件
108 │ └── permission # 权限校验相关头文件存放目录
109 └── src
110 ├── fwkload # 拉起其他sa功能相关功能代码
111 └── permission # 权限校验相关功能代码
112```
113
114## 约束
115
116- 开发语言:JS、C++
117- 适用于Hi3516DV300单板等OpenHarmony设备
118
119
120## 接口说明
121
122当前版本设备管理服务不具备权限管理的能力,接口中的system api仅供系统调用,后续版本会进行严格的权限管控。
123接口参见[**interface_sdk-js仓库的**](https://gitee.com/openharmony/interface_sdk-js/) *ohos.distributedHardware.deviceManager.d.ts*
124
125- 公共接口:
126
127 使用DeviceManager相关接口之前,需要通过createDeviceManager接口创建DeviceManager实例;
128
129 不使用DeviceManager接口的时候需要释放对应的DeviceManager实例。
130
131| 原型 | 描述 |
132| ------------------------------------------------------------ | ------------------------------- |
133| createDeviceManager(bundleName: string, callback: AsyncCallback<DeviceManager>): void; | 以异步方法获取DeviceManager实例 |
134| release(): void; | 释放DeviceManager实例 |
135
136
137- 系统能力接口:
138
139 提供可信设备列表获取、可信设备状态监听、周边设备发现、设备认证等相关接口,该部分作为系统能力接口,仅供系统应用调用。
140
141 开始设备发现、停止发现设备接口要配对使用,使用同一个subscribeId。
142
143| 原型 | 描述 |
144| ------------------------------------------------------------ | -------------------- |
145| getTrustedDeviceListSync(): Array<DeviceInfo>; | 获取信任设备列表 |
146| on(type: 'deviceStateChange', callback: Callback<{ action: DeviceStateChangeAction, device: DeviceInfo }>): void; | 设备状态变更回调 |
147| off(type: 'deviceStateChange', callback?: Callback<{ action: DeviceStateChangeAction, device: DeviceInfo }>): void; | 取消设备状态变更回调 |
148| on(type: 'serviceDie', callback: () => void): void; | 服务错误回调 |
149| off(type: 'serviceDie', callback?: () => void): void; | 取消服务错误回调 |
150| startDeviceDiscovery(subscribeInfo: SubscribeInfo): void; | 开始设备发现 |
151| stopDeviceDiscovery(subscribeId: number): void; | 停止发现设备 |
152| authenticateDevice(deviceInfo: DeviceInfo, authparam: AuthParam, callback: AsyncCallback<{deviceId: string, pinTone ?: number}>): void; | 设备认证接口 |
153| setUserOperation(operateAction: number, params: string): void; | 设置用户ui操作行为 |
154| verifyAuthInfo(authInfo: AuthInfo, callback: AsyncCallback<{deviceId: string, level: number}>): void; | 设备认证信息校验 |
155| on(type: 'deviceFound', callback: Callback<{ subscribeId: number, device: DeviceInfo }>): void; | 发现设备列表回调 |
156| off(type: 'deviceFound', callback?: Callback<{ subscribeId: number, device: DeviceInfo }>): void; | 取消发现设备列表回调 |
157| on(type: 'discoverFail', callback: Callback<{ subscribeId: number, reason: number }>): void; | 发现设备失败回调 |
158| off(type: 'discoverFail', callback?: Callback<{ subscribeId: number, reason: number }>): void; | 取消发现设备失败回调 |
159| on(type: 'publishSuccess', callback: Callback<{ publishId: number }>): void; | 发布设备成功回调 |
160| off(type: 'publishSuccess', callback?: Callback<{ publishId: number }>): void; | 取消发布设备成功回调 |
161| on(type: 'publishFail', callback: Callback<{ publishId: number, reason: number }>): void; | 发布设备失败回调 |
162| off(type: 'publishFail', callback?: Callback<{ publishId: number, reason: number }>): void; | 取消发布设备失败回调 |
163| on(type: 'uiStateChange', callback: Callback<{ param: string}>): void; | ui状态变更回调 |
164| off(type: 'uiStateChange', callback?: Callback<{ param: string}>): void; | 取消ui状态变更回调 |
165- 示例如下:
166
167```js
168// 创建DeviceManager实例:
169deviceManager.createDeviceManager('com.ohos.xxxx', (err, dm) => {
170 this.log("createDeviceManager err:" + JSON.stringify(err) + ' --success:' + JSON.stringify(dm))
171 if (err) return;
172 dmClass = dm;
173 dmClass.on('serviceDie', data => this.log("serviceDie on:" + JSON.stringify(data)))
174});
175
176// 注册/去注册设备上下线监听
177dmClass.on('deviceStateChange', (data) => {
178 this.log("deviceStateChange on:" + JSON.stringify(data));
179 switch (data.action) {
180 case ONLINE:
181 // 设备物理上线状态
182 break;
183 case READY:
184 // 设备可用状态,表示设备间信息已在分布式数据中同步完成,可以运行分布式业务
185 break;
186 case OFFLINE:
187 // 设备物理下线状态
188 break;
189 case CHANGE:
190 // 设备信息变更
191 break;
192 default:
193 break;
194 }
195});
196dmClass.off('deviceStateChange')
197
198// 查询可信设备列表
199var array = dmClass.getTrustedDeviceListSync();
200
201// 获取本地设备信息
202var localDeviceInfo = dmClass.getLocalDeviceInfoSync();
203
204// 开始设备发现(发现周边不可信设备)
205var subscribeId = 0;
206dmClass.on('deviceFound', (data) => {
207 if (data == null) {
208 this.log("deviceFound error data=null")
209 return;
210 }
211 this.logList.push("deviceFound:" + JSON.stringify(data));
212});
213dmClass.on('discoverFail', (data) => {
214 this.log("discoverFail on:" + JSON.stringify(data));
215});
216subscribeId = Math.floor(Math.random() * 10000 + 1000)
217var info = {
218 "subscribeId": subscribeId,
219 "mode": 0xAA,
220 "medium": 0,
221 "freq": 2,
222 "isSameAccount": false,
223 "isWakeRemote": true,
224 "capability": 0
225};
226var filterOptions = {
227 "filter_op": "OR", //可选,默认"OR"
228 "filters": [
229 {
230 "type": "range",
231 "value": 50 // 需要过滤发现设备的距离,单位(cm)
232 }
233 ]
234};
235dmClass.startDeviceDiscovery(info, JSON.stringify(filterOptions));
236
237// 停止设备发现(需要和startDeviceDiscovery接口配对使用)
238dmClass.stopDeviceDiscovery(subscribeId);
239
240// 开始发布设备发现
241var publishId = 0;
242dmClass.on('publishSuccess', (data) => {
243 if (data == null) {
244 this.log("publishSuccess error data=null")
245 return;
246 }
247 this.logList.push("publishSuccess:" + JSON.stringify(data));
248});
249dmClass.on('publishFailed', (data) => {
250 this.log("publishFailed on:" + JSON.stringify(data));
251});
252publishId = Math.floor(Math.random() * 10000 + 1000)
253var info = {
254 "publishId": publishId,
255 "mode": 0xAA,
256 "freq": 2,
257 "ranging": 1
258};
259dmClass.publishDeviceDiscovery(info);
260
261// 停止发布设备发现(需要和publishDeviceDiscovery接口配对使用)
262dmClass.unPublishDeviceDiscovery(publishId);
263
264// 设置用户ui操作行为
265/* operateAction = 0 - 允许授权
266 operateAction = 1 - 取消授权
267 operateAction = 2 - 授权框用户操作超时
268 operateAction = 3 - 取消pin码框展示
269 operateAction = 4 - 取消pin码输入框展示
270 operateAction = 5 - pin码输入框确定操作
271*/
272dmClass.setUserOperation(operation, "extra")
273dmClass.on('uiStateChange', (data) => {
274 console.log("uiStateChange executed, dialog closed" + JSON.stringify(data))
275 var tmpStr = JSON.parse(data.param)
276 this.isShow = tmpStr.verifyFailed
277 console.log("uiStateChange executed, dialog closed" + this.isShow)
278 if (!this.isShow) {
279 this.destruction()
280 }
281});
282dmClass.off('uiStateChange')
283
284// 设备认证
285var deviceInfo ={
286 "deviceId": "XXXXXXXX",
287 "deviceName": "",
288 deviceType: 0
289};
290let extraInfo = {
291 "appIcon": new Uint8Array(), // app图标,可选参数,可不填
292 "appThumbnail": new Uint8Array(), // app缩略图,可选参数,可不填
293 "appName": "xxxxxxxx", // 对端设备应用名称
294 "appDescription": "xxxxxxxx", // app描述
295 "business": '0',
296 "displayOwner": 0,
297}
298let authParam = {
299 "authType": 1,
300 "extraInfo": extraInfo
301}
302dmClass.authenticateDevice(this.deviceInfo, authParam, (err, data) => {
303 if (err) {
304 this.logList.push("authenticateDevice err:" + JSON.stringify(err));
305 console.info(TAG + "authenticateDevice err:" + JSON.stringify(err));
306 return;
307 }
308 this.logList.push("authenticateDevice result:" + JSON.stringify(data));
309 console.info(TAG + "authenticateDevice result:" + JSON.stringify(data));
310 token = data.pinToken;
311});
312
313// 设备取消认证
314dmClass.unAuthenticateDevice(this.deviceInfo);
315```
316## 系统弹框ServiceExtensionAbility
317
318当前版本只支持PIN码认证,需要提供PIN码认证的授权提示界面、PIN码显示界面、PIN码输入界面;
319
320当前,由于系统通过native层直接进行弹窗的能力尚不具备,这里使用ServiceExtensionAbility来进行对应界面的弹窗。
321
322该ServiceExtensionAbility为:DeviceManager_UI.hap,作为系统应用进行预置。
323
324- 编译运行:
325
326 将devicemanager/display工程导入DevEco Studio 2.2 Beta1,复制display目录下的@ohos.distributedHardware.deviceManager.d.ts文件到Sdk\js\2.2.0.1\api\common目录下,进行编译构建及运行调试.
327
328- 编译环境依赖:IDE 2.2 SDK6
329
330- DeviceManager_UI.hap包源码存放位置:[device_manager仓库](https://gitee.com/openharmony/distributedhardware_device_manager/tree/master/display)
331
332- UI显示:
333
334 DeviceManager作为认证被控端,授权提示界面、PIN码显示界面由DeviceManager_UI ServiceExtensionAbility默认进行显示;
335
336 DeviceManager作为认证发起端,PIN码输入界面可以选择由DeviceManager_UI ServiceExtensionAbility进行显示,还是由开发者自行显示。开发者如需自己定制PIN码输入界面,需要在authenticateDevice接口的认证参数AuthParam中,extraInfo属性里面指定displayOwner参数为1。
337
338## 相关仓
339****
340
341[**interface_sdk-js**](https://gitee.com/openharmony/interface_sdk-js/)
342
343[**applications_hap**](https://gitee.com/openharmony/applications_hap)
344