• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# USB Usage Guidelines<a name="EN-US_TOPIC_0000001077367159"></a>
2
3
4The following procedure uses bulk transfer as an example.
5
6## Procedure<a name="section18816105182315"></a>
7
81.  Obtain a USB service instance.
9
10```cpp
11static OHOS::USB::UsbSrvClient &g_usbClient = OHOS::USB::UsbSrvClient::GetInstance();
12```
13
142.  Obtain the USB device list.
15
16```cpp
17std::vector<OHOS::USB::UsbDevice> deviceList;
18int32_t ret = g_usbClient.GetDevices(deviceList);
19```
20
213.  Apply for device access permissions.
22
23```cpp
24int32_t ret = g_usbClient.RequestRight(device.GetName());
25```
26
274.  Open the USB device.
28
29```cpp
30USBDevicePipe pip;
31int32_t et = g_usbClient.OpenDevice(device, pip);
32```
33
345.  Configure the USB interface.
35
36```cpp
37ret = g_usbClient.ClaimInterface(pip, interface, true);
38**interface** indicates an interface of the USB device in **deviceList**.
39```
40
416.  Transfer data.
42
43```cpp
44srvClient.BulkTransfer(pipe, endpoint, vdata, timeout);
45```
46- **pipe** indicates the pipe for data transfer of the USB device opened.
47- **endpoint** indicates the endpoint for data transfer on the USB device.
48- **vdata** indicates the binary data block to be transferred or read.
49- **timeout** indicates the timeout duration of data transfer.
50
517.  Close the USB device.
52
53```cpp
54ret = g_usbClient.Close(pip);
55```
56