You need to sign in or sign up before continuing.
subsys-usbservice-guide.md 1.1 KB
Newer Older
1
# USB服务子系统使用指导<a name="ZH-CN_TOPIC_0000001077367159"></a>
2 3


4
下面使用步骤以bulktransfer为例。
5 6 7 8 9

## 使用步骤<a name="section18816105182315"></a>

1.  获取usb service实例

W
wu-chengwen 已提交
10
```cpp
11
static OHOS::USB::UsbSrvClient &g_usbClient = OHOS::USB::UsbSrvClient::GetInstance();
12 13 14 15
```

2.  获取usb设备列表

W
wu-chengwen 已提交
16
```cpp
17 18
std::vector<OHOS::USB::UsbDevice> deviceList;
int32_t ret = g_usbClient.GetDevices(deviceList);
19 20 21 22
```

3.  申请设备权限

W
wu-chengwen 已提交
23
```cpp
24
int32_t ret = g_usbClient.RequestRight(device.GetName());
25 26 27 28
```

4.  打开设备

W
wu-chengwen 已提交
29
```cpp
30
USBDevicePipe pip;
31
int32_t et = g_usbClient.OpenDevice(device, pip);
32 33 34 35
```

5.  配置设备接口

W
wu-chengwen 已提交
36
```cpp
37
ret = g_usbClient.ClaimInterface(pip, interface, true);
38 39 40 41 42
interfacedeviceListdeviceinterface
```

6.  数据传输

W
wu-chengwen 已提交
43
```cpp
44 45
srvClient.BulkTransfer(pipe, endpoint, vdata, timeout);
```
46
pipe为打开设备后的数据传输通道,endpoint为device中数据传输的端点,vdata是需要传输或读取的二进制数据块,timeout为传输超时时长。
47 48 49

7.  关闭设备

W
wu-chengwen 已提交
50
```cpp
51
ret = g_usbClient.Close(pip);
52
```