subsys-usbservice-guide.md 1.2 KB
Newer Older
A
annie_wangli 已提交
1
# USB Usage<a name="EN-US_TOPIC_0000001077367159"></a>
S
shawn_he 已提交
2

A
annie_wangli 已提交
3 4

The following uses bulk transfer as an example.
S
shawn_he 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

## Procedure<a name="section18816105182315"></a>

1.  Obtain a USB service instance.

```
static OHOS::USB::UsbSrvClient &g_usbClient = OHOS::USB::UsbSrvClient::GetInstance();
```

2.  Obtain the USB device list.

```
std::vector<OHOS::USB::UsbDevice> deviceList;
int32_t ret = g_usbClient.GetDevices(deviceList);
```

3.  Apply for device access permissions.

```
int32_t ret = g_usbClient.RequestRight(device.GetName());
```

A
annie_wangli 已提交
27
4.  Opens a device.
S
shawn_he 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40

```
USBDevicePipe pip;
int32_t et = g_usbClient.OpenDevice(device, pip);
```

5.  Configure the USB interface.

```
ret = g_usbClient.ClaimInterface(pip, interface, true);
**interface** indicates an interface of the USB device in **deviceList**.
```

A
annie_wangli 已提交
41
6.  Perform data transfer.
S
shawn_he 已提交
42 43 44 45

```
srvClient.BulkTransfer(pipe, endpoint, vdata, timeout);
```
A
annie_wangli 已提交
46 47 48 49
- **pipe** indicates the pipe for data transfer of the USB device opened. 
- **endpoint** indicates the endpoint for data transfer on the USB device. 
- **vdata** indicates the binary data block to be transferred or read. 
- **timeout** indicates the timeout duration of data transfer.
S
shawn_he 已提交
50 51 52 53 54 55

7.  Close the USB device.

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