1# USB服务开发指导 2 3 4 5## 场景介绍 6 7Host模式下,可以获取到已经连接的USB设备列表,并根据需要打开和关闭设备、控制设备权限、进行数据传输等。 8 9 10## 接口说明 11 12USB服务主要提供的功能有:查询USB设备列表、批量数据传输、控制命令传输、权限控制等。 13 14USB类开放能力如下,具体请查阅[API参考文档](../reference/apis/js-apis-usbManager.md)。 15 16**表1** USB类的开放能力接口 17 18| 接口名 | 描述 | 19| ------------------------------------------------------------ | ------------------------------------------------------------ | 20| hasRight(deviceName: string): boolean | 判断是否有权访问该设备 | 21| requestRight(deviceName: string): Promise<boolean> | 请求软件包的临时权限以访问设备。使用Promise异步回调。 | 22| removeRight(deviceName: string): boolean | 移除软件包对设备的访问权限。| 23| connectDevice(device: USBDevice): Readonly<USBDevicePipe> | 根据`getDevices()`返回的设备信息打开USB设备。 | 24| getDevices(): Array<Readonly<USBDevice>> | 获取接入主设备的USB设备列表。如果没有设备接入,那么将会返回一个空的列表。 | 25| setConfiguration(pipe: USBDevicePipe, config: USBConfiguration): number | 设置设备的配置。 | 26| setInterface(pipe: USBDevicePipe, iface: USBInterface): number | 设置设备的接口。 | 27| claimInterface(pipe: USBDevicePipe, iface: USBInterface, force ?: boolean): number | 注册通信接口。 | 28| bulkTransfer(pipe: USBDevicePipe, endpoint: USBEndpoint, buffer: Uint8Array, timeout ?: number): Promise<number> | 批量传输。 | 29| closePipe(pipe: USBDevicePipe): number | 关闭设备消息控制通道。 | 30| releaseInterface(pipe: USBDevicePipe, iface: USBInterface): number | 释放注册过的通信接口。 | 31| getFileDescriptor(pipe: USBDevicePipe): number | 获取文件描述符。 | 32| getRawDescriptor(pipe: USBDevicePipe): Uint8Array | 获取原始的USB描述符。 | 33| controlTransfer(pipe: USBDevicePipe, controlparam: USBControlParams, timeout ?: number): Promise<number> | 控制传输。 | 34 35 36## 开发步骤 37 38USB设备可作为Host设备连接Device设备进行数据传输。开发示例如下: 39 40 411. 获取设备列表。 42 43 ```ts 44 // 导入USB接口api包。 45 import usb from '@ohos.usbManager'; 46 // 获取设备列表。 47 let deviceList : Array<usb.USBDevice> = usb.getDevices(); 48 /* 49 deviceList结构示例 50 [ 51 { 52 name: "1-1", 53 serial: "", 54 manufacturerName: "", 55 productName: "", 56 version: "", 57 vendorId: 7531, 58 productId: 2, 59 clazz: 9, 60 subClass: 0, 61 protocol: 1, 62 devAddress: 1, 63 busNum: 1, 64 configs: [ 65 { 66 id: 1, 67 attributes: 224, 68 isRemoteWakeup: true, 69 isSelfPowered: true, 70 maxPower: 0, 71 name: "1-1", 72 interfaces: [ 73 { 74 id: 0, 75 protocol: 0, 76 clazz: 9, 77 subClass: 0, 78 alternateSetting: 0, 79 name: "1-1", 80 endpoints: [ 81 { 82 address: 129, 83 attributes: 3, 84 interval: 12, 85 maxPacketSize: 4, 86 direction: 128, 87 number: 1, 88 type: 3, 89 interfaceId: 0, 90 } 91 ] 92 } 93 ] 94 } 95 ] 96 } 97 ] 98 */ 99 ``` 100 1012. 获取设备操作权限。 102 103 ```ts 104 import usb from '@ohos.usbManager'; 105 import { BusinessError } from '@ohos.base'; 106 107 let deviceName : string = deviceList[0].name; 108 // 申请操作指定的device的操作权限。 109 usb.requestRight(deviceName).then((hasRight : boolean) => { 110 console.info("usb device request right result: " + hasRight); 111 }).catch((error : BusinessError)=> { 112 console.info("usb device request right failed : " + error); 113 }); 114 ``` 115 1163. 打开Device设备。 117 118 ```ts 119 // 打开设备,获取数据传输通道。 120 let pipe : USBDevicePipe = usb.connectDevice(deviceList[0]); 121 let interface1 : number = deviceList[0].configs[0].interfaces[0]; 122 /* 123 打开对应接口,在设备信息(deviceList)中选取对应的interface。 124 interface1为设备配置中的一个接口。 125 */ 126 usb.claimInterface(pipe, interface1, true); 127 ``` 128 1294. 数据传输。 130 131 ```ts 132 import usb from '@ohos.usbManager'; 133 import { BusinessError } from '@ohos.base'; 134 /* 135 读取数据,在device信息中选取对应数据接收的endpoint来做数据传输 136 (endpoint.direction == 0x80);dataUint8Array是要读取的数据,类型为Uint8Array。 137 */ 138 let inEndpoint : USBEndpoint = interface1.endpoints[2]; 139 let outEndpoint : USBEndpoint = interface1.endpoints[1]; 140 let dataUint8Array : Array<number> = new Uint8Array(1024); 141 usb.bulkTransfer(pipe, inEndpoint, dataUint8Array, 15000).then((dataLength : number) => { 142 if (dataLength >= 0) { 143 console.info("usb readData result Length : " + dataLength); 144 } else { 145 console.info("usb readData failed : " + dataLength); 146 } 147 }).catch((error : BusinessError) => { 148 console.info("usb readData error : " + JSON.stringify(error)); 149 }); 150 // 发送数据,在device信息中选取对应数据发送的endpoint来做数据传输。(endpoint.direction == 0) 151 usb.bulkTransfer(pipe, outEndpoint, dataUint8Array, 15000).then((dataLength : number) => { 152 if (dataLength >= 0) { 153 console.info("usb writeData result write length : " + dataLength); 154 } else { 155 console.info("writeData failed"); 156 } 157 }).catch((error : BusinessError) => { 158 console.info("usb writeData error : " + JSON.stringify(error)); 159 }); 160 ``` 161 1625. 释放接口,关闭设备。 163 164 ```ts 165 usb.releaseInterface(pipe, interface1); 166 usb.closePipe(pipe); 167 ``` 168 169## 相关实例 170 171针对USB管理开发,有以下相关实例可供参考: 172 173- [`DeviceManagementCollection`:设备管理合集(ArkTS)(API9)](https://gitee.com/openharmony/applications_app_samples/tree/OpenHarmony-4.0-Release/code/BasicFeature/DeviceManagement/DeviceManagementCollection)