• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# USB服务子系统使用指导<a name="ZH-CN_TOPIC_0000001077367159"></a>
2
3
4下面使用步骤以bulktransfer为例。
5
6## 使用步骤<a name="section18816105182315"></a>
7
81.  获取usb service实例
9
10```cpp
11static OHOS::USB::UsbSrvClient &g_usbClient = OHOS::USB::UsbSrvClient::GetInstance();
12```
13
142.  获取usb设备列表
15
16```cpp
17std::vector<OHOS::USB::UsbDevice> deviceList;
18int32_t ret = g_usbClient.GetDevices(deviceList);
19```
20
213.  申请设备权限
22
23```cpp
24int32_t ret = g_usbClient.RequestRight(device.GetName());
25```
26
274.  打开设备
28
29```cpp
30USBDevicePipe pip;
31int32_t et = g_usbClient.OpenDevice(device, pip);
32```
33
345.  配置设备接口
35
36```cpp
37ret = g_usbClient.ClaimInterface(pip, interface, true);
38interface为deviceList中device的interface。
39```
40
416.  数据传输
42
43```cpp
44srvClient.BulkTransfer(pipe, endpoint, vdata, timeout);
45```
46pipe为打开设备后的数据传输通道,endpoint为device中数据传输的端点,vdata是需要传输或读取的二进制数据块,timeout为传输超时时长.
47
487.  关闭设备
49
50```cpp
51ret = g_usbClient.Close(pip);
52```
53