subsys-usbservice-guide.md 1.1 KB
Newer Older
1
# USB服务子系统使用指导<a name="ZH-CN_TOPIC_0000001077367159"></a>
2 3 4

-   [使用步骤](#section18816105182315)

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

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

1.  获取usb service实例

```
12
static OHOS::USB::UsbSrvClient &g_usbClient = OHOS::USB::UsbSrvClient::GetInstance();
13 14 15 16 17
```

2.  获取usb设备列表

```
18 19
std::vector<OHOS::USB::UsbDevice> deviceList;
int32_t ret = g_usbClient.GetDevices(deviceList);
20 21 22 23 24
```

3.  申请设备权限

```
25
int32_t ret = g_usbClient.RequestRight(device.GetName());
26 27 28 29 30 31
```

4.  打开设备

```
USBDevicePipe pip;
32
int32_t et = g_usbClient.OpenDevice(device, pip);
33 34 35 36 37
```

5.  配置设备接口

```
38
ret = g_usbClient.ClaimInterface(pip, interface, true);
39 40 41 42 43 44 45 46
interface为deviceList中device的interface。
```

6.  数据传输

```
srvClient.BulkTransfer(pipe, endpoint, vdata, timeout);
```
47
pipe为打开设备后的数据传输通道,endpoint为device中数据传输的端点,vdata是需要传输或读取的二进制数据块,timeout为传输超时时长。
48 49 50 51

7.  关闭设备

```
52
ret = g_usbClient.Close(pip);
53
```