基本概念
USB 服務是應用訪問底層的一種設備抽象概念。開發者根據提供的 USB?API,可以獲取設備列表、控制設備訪問權限、以及與連接的設備進行數據傳輸、控制命令傳輸等。
運作機制
USB 服務系統包含 USB?API、USB?Service、USB?HAL。
圖 1?USB 服務運作機制
●?USB?API:提供 USB 的基礎 API,主要包含查詢 USB 設備列表、批量數據傳輸、控制命令傳輸、權限控制等。
●?USB?Service:主要實現 HAL 層數據的接收、解析、分發以及對設備的管理等。
●?USB?HAL 層:提供給用戶態可直接調用的驅動能力接口。
場景介紹
Host 模式下,可以獲取到已經連接的 USB 設備列表,并根據需要打開和關閉設備、控制設備權限、進行數據傳輸等。
接口說明
USB 服務主要提供的功能有:查詢 USB 設備列表、批量數據傳輸、控制命令傳輸、權限控制等。
USB 類開放能力如下,具體請查閱API參考文檔。
表 1?USB 類的開放能力接口
開發步驟
USB 設備可作為 Host 設備連接 Device 設備進行數據傳輸。開發示例如下:
1.? 獲取設備列表。
```ts
// 導入USB接口api包。
import usb from '@ohos.usbManager';
// 獲取設備列表。
let deviceList : Array<usb.USBDevice> = usb.getDevices();
/
deviceList結構示例
[
{
name: "1-1",
serial: "",
manufacturerName: "",
productName: "",
version: "",
vendorId: 7531,
productId: 2,
clazz: 9,
subClass: 0,
protocol: 1,
devAddress: 1,
busNum: 1,
configs: [
{
id: 1,
attributes: 224,
isRemoteWakeup: true,
isSelfPowered: true,
maxPower: 0,
name: "1-1",
interfaces: [
{
id: 0,
protocol: 0,
clazz: 9,
subClass: 0,
alternateSetting: 0,
name: "1-1",
endpoints: [
{
address: 129,
attributes: 3,
interval: 12,
maxPacketSize: 4,
direction: 128,
number: 1,
type: 3,
interfaceId: 0,
}
]
}
]
}
]
}
]
/
2.? 獲取設備操作權限。
```ts
import usb from '@ohos.usbManager';
import { BusinessError } from '@ohos.base';
let deviceName : string = deviceList[0].name;
// 申請操作指定的device的操作權限。
usb.requestRight(deviceName).then((hasRight : boolean) => {
console.info("usb device request right result: " + hasRight);
}).catch((error : BusinessError)=> {
console.info("usb device request right failed : " + error);
});
```
3.? 打開 Device 設備。
```ts
// 打開設備,獲取數據傳輸通道。
let interface1 = deviceList[0].configs[0].interfaces[0];
let interface1 : number = deviceList[0].configs[0].interfaces[0];
/
打開對應接口,在設備信息(deviceList)中選取對應的interface。
interface1為設備配置中的一個接口。
/
usb.claimInterface(pipe, interface1, true);
let?pipe?:?USBDevicePipe?=?usb.connectDevice(deviceList[0]);
4.? 數據傳輸。
?
import usb from '@ohos.usbManager';
import { BusinessError } from '@ohos.base';
/*讀取數據,在device信息中選取對應數據接收的endpoint來做數據傳輸
(endpoint.direction == 0x80);dataUint8Array是要讀取的數據,類型為Uint8Array。
*/
let inEndpoint : USBEndpoint = interface1.endpoints[2];
let outEndpoint : USBEndpoint = interface1.endpoints[1];
let dataUint8Array : Array<number> = new Uint8Array(1024);
usb.bulkTransfer(pipe, inEndpoint, dataUint8Array, 15000).then((dataLength : number) => {
if (dataLength >= 0) {console.info("usb readData result Length : " + dataLength);
} else {console.info("usb readData failed : " + dataLength);
}
}).catch((error : BusinessError) => {
console.info("usb readData error : " + JSON.stringify(error));
});
// 發送數據,在device信息中選取對應數據發送的endpoint來做數據傳輸。(endpoint.direction == 0)
usb.bulkTransfer(pipe, outEndpoint, dataUint8Array, 15000).then((dataLength : number) => {if (dataLength >= 0) {console.info("usb writeData result write length : " + dataLength);} else {console.info("writeData failed");}
}).catch((error : BusinessError) => {console.info("usb writeData error : " + JSON.stringify(error));
});
let?inEndpoint?:?USBEndpoint?=?interface1.endpoints[2];
5.? 釋放接口,關閉設備。
```ts
usb.releaseInterface(pipe, interface1);
usb.closePipe(pipe);
```