• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# USB设备管理
2
3## 场景介绍
4
5当有USB设备插入时,可以通过`usbManager`获取一些USB设备的基本信息,如设备类型、支持的功能等。 Host侧主要通过封装的pipe来完成和USB设备的通信。在OpenHarmony系统中,USB管理服务是核心组件,负责管理与USB设备的连接和通信。通过USB管理服务,应用程序可以检测USB设备的连接与断开,管理USB设备的权限请求和设备配置,以及进行数据传输和设备控制。
6
7## 环境准备
8
9### 环境要求
10
11- 开发工具及配置:
12
13  DevEco Studio作为驱动开发工具,是进行驱动开发必备条件之一,开发者可以使用该工具进行开发、调试、打包等操作。请[下载安装](https://developer.huawei.com/consumer/cn/download/)该工具,并参考[DevEco Studio使用指南](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V14/ide-tools-overview-V14)中的[创建工程及运行](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V14/ide-create-new-project-V14)进行基本的操作验证,保证DevEco Studio可正常运行。
14
15
16- SDK版本配置:
17
18  扩展外设管理提供的ArkTs接口,所需SDK版本为API16及以上才可使用。
19
20
21- HDC配置:
22
23  HDC(HarmonyOS Device Connector)是为开发人员提供的用于调试的命令行工具,通过该工具可以在Windows/Linux/Mac系统上与真实设备或者模拟器进行交互,详细参考[HDC配置](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/hdc-V5)24
25### 搭建环境
26
27- 在PC上安装[DevEco Studio](https://developer.huawei.com/consumer/cn/download/deveco-studio),要求版本在4.1及以上。
28- 将public-SDK更新到API 16或以上,更新SDK的具体操作可参见[更新指南](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/faqs/full-sdk-switch-guide.md)29- PC安装HDC工具(HarmonyOS Device Connector),通过该工具可以在Windows/Linux/Mac系统上与真实设备或者模拟器进行交互,详细参考[HDC配置](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/hdc-V5)30- 用USB线缆将搭载OpenHarmony的设备连接到PC。
31
32## 开发指导
33
34### 接口说明
35
36USB设备管理主要提供的功能有:查询USB设备列表、USB设备权限控制、设置USB设备配置等。
37
38USB类开放能力如下,具体请查阅[API参考文档](../../../../reference/apis-basic-services-kit/js-apis-usbManager.md)。
39
40**表1** USB类的开放能力接口
41
42| 接口名                                                       | 描述                                                         |
43| ------------------------------------------------------------ | ------------------------------------------------------------ |
44| hasRight(deviceName: string): boolean                         | 判断是否有权访问该设备。 |
45| requestRight(deviceName: string): Promise<boolean>       | 请求软件包的临时权限以访问设备。使用Promise异步回调。                        |
46| removeRight(deviceName: string): boolean | 移除软件包对设备的访问权限。|
47| connectDevice(device: USBDevice): Readonly<USBDevicePipe> | 根据`getDevices()`返回的设备信息打开USB设备。                |
48| getDevices(): Array<Readonly<USBDevice>>          | 获取接入主设备的USB设备列表。如果没有设备接入,那么将会返回一个空的列表。                                            |
49| setConfiguration(pipe: USBDevicePipe, config: USBConfiguration): number | 设置设备的配置。                                             |
50| setInterface(pipe: USBDevicePipe, iface: USBInterface): number   | 设置设备的接口。                                             |
51| claimInterface(pipe: USBDevicePipe, iface: USBInterface, force ?: boolean): number | 注册通信接口。                                                   |
52| closePipe(pipe: USBDevicePipe): number                         | 关闭设备消息控制通道。                                       |
53| releaseInterface(pipe: USBDevicePipe, iface: USBInterface): number | 释放注册过的通信接口。                                                   |
54| getFileDescriptor(pipe: USBDevicePipe): number                 | 获取文件描述符。                                             |
55| getRawDescriptor(pipe: USBDevicePipe): Uint8Array              | 获取原始的USB描述符。                                        |
56
57### 开发步骤
58
59USB设备可作为Host连接Device进行设备管理,开发示例如下:
60
61
621. 导入模块。
63
64   ```ts
65   // 导入usbManager模块。
66   import { usbManager } from '@kit.BasicServicesKit';
67   import { BusinessError } from '@kit.BasicServicesKit';
68   ```
69
702. 获取设备列表。
71
72   ```ts
73   // 获取设备列表。
74   let deviceList : Array<usbManager.USBDevice> = usbManager.getDevices();
75   /*
76   deviceList结构示例
77   [
78     {
79       name: "1-1",
80       serial: "",
81       manufacturerName: "",
82       productName: "",
83       version: "",
84       vendorId: 7531,
85       productId: 2,
86       clazz: 9,
87       subClass: 0,
88       protocol: 1,
89       devAddress: 1,
90       busNum: 1,
91       configs: [
92         {
93           id: 1,
94           attributes: 224,
95           isRemoteWakeup: true,
96           isSelfPowered: true,
97           maxPower: 0,
98           name: "1-1",
99           interfaces: [
100             {
101               id: 0,
102               protocol: 0,
103               clazz: 9,
104               subClass: 0,
105               alternateSetting: 0,
106               name: "1-1",
107               endpoints: [
108                 {
109                   address: 129,
110                   attributes: 3,
111                   interval: 12,
112                   maxPacketSize: 4,
113                   direction: 128,
114                   number: 1,
115                   type: 3,
116                   interfaceId: 0,
117                 }
118               ]
119             }
120           ]
121         }
122       ]
123     }
124   ]
125   */
126   ```
127
1283. 获取设备操作权限。
129
130   ```ts
131   let deviceName : string = deviceList[0].name;
132   // 申请操作指定的device的操作权限。
133   usbManager.requestRight(deviceName).then((hasRight : boolean) => {
134     console.info("usb device request right result: " + hasRight);
135   }).catch((error : BusinessError)=> {
136     console.info("usb device request right failed : " + error);
137   });
138   ```
139
1404. 打开Device设备。
141
142   ```ts
143   // 打开设备,获取数据传输通道。
144   let pipe : usbManager.USBDevicePipe = usbManager.connectDevice(deviceList[0]);
145   let interface1 : usbManager.USBInterface = deviceList[0].configs[0].interfaces[0];
146   /*
147    打开对应接口,在设备信息(deviceList)中选取对应的interface。
148   interface1为设备配置中的一个接口。
149   */
150   usbManager.claimInterface(pipe, interface1, true);
151   ```
152
1535. 释放接口,关闭设备。
154
155   ```ts
156   usbManager.releaseInterface(pipe, interface1);
157   usbManager.closePipe(pipe);
158   ```
159
160### 相关实例
161
162针对USB管理开发,有以下相关实例可供参考:
163
164- [`DeviceManagementCollection`:设备管理合集(ArkTS)(API9)](https://gitee.com/openharmony/applications_app_samples/tree/master/code/BasicFeature/DeviceManagement/DeviceManagementCollection)