1e41f4b71Sopenharmony_ci# @ohos.usbManager (USB Manager)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThe **usbManager** module provides USB device management functions, including USB device list query, bulk data transfer, control transfer, and permission control on the host side as well as USB interface management, and function switch and query on the device side.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci>  **NOTE**
6e41f4b71Sopenharmony_ci> 
7e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci## Modules to Import
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci```ts
12e41f4b71Sopenharmony_ciimport { usbManager } from '@kit.BasicServicesKit';
13e41f4b71Sopenharmony_ci```
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci## usbManager.getDevices
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_cigetDevices(): Array<Readonly<USBDevice>>
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ciObtains the list of USB devices connected to the host. If no device is connected, an empty list is returned.
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci**Return value**
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci| Type                                                  | Description     |
26e41f4b71Sopenharmony_ci| ---------------------------------------------------- | ------- |
27e41f4b71Sopenharmony_ci| Array<Readonly<[USBDevice](#usbdevice)>> | USB device list.|
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci**Example**
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci```ts
32e41f4b71Sopenharmony_cilet devicesList: Array<usbManager.USBDevice> = usbManager.getDevices();
33e41f4b71Sopenharmony_ciconsole.log(`devicesList = ${devicesList}`);
34e41f4b71Sopenharmony_ci/*
35e41f4b71Sopenharmony_ciThe following is a simple example of the data structure for devicesList:
36e41f4b71Sopenharmony_ci[
37e41f4b71Sopenharmony_ci  {
38e41f4b71Sopenharmony_ci    name: "1-1",
39e41f4b71Sopenharmony_ci    serial: "",
40e41f4b71Sopenharmony_ci    manufacturerName: "",
41e41f4b71Sopenharmony_ci    productName: "",
42e41f4b71Sopenharmony_ci    version: "",
43e41f4b71Sopenharmony_ci    vendorId: 7531,
44e41f4b71Sopenharmony_ci    productId: 2,
45e41f4b71Sopenharmony_ci    clazz: 9,
46e41f4b71Sopenharmony_ci    subClass: 0,
47e41f4b71Sopenharmony_ci    protocol: 1,
48e41f4b71Sopenharmony_ci    devAddress: 1,
49e41f4b71Sopenharmony_ci    busNum: 1,
50e41f4b71Sopenharmony_ci    configs: [
51e41f4b71Sopenharmony_ci      {
52e41f4b71Sopenharmony_ci        id: 1,
53e41f4b71Sopenharmony_ci        attributes: 224,
54e41f4b71Sopenharmony_ci        isRemoteWakeup: true,
55e41f4b71Sopenharmony_ci        isSelfPowered: true,
56e41f4b71Sopenharmony_ci        maxPower: 0,
57e41f4b71Sopenharmony_ci        name: "1-1",
58e41f4b71Sopenharmony_ci        interfaces: [
59e41f4b71Sopenharmony_ci          {
60e41f4b71Sopenharmony_ci            id: 0,
61e41f4b71Sopenharmony_ci            protocol: 0,
62e41f4b71Sopenharmony_ci            clazz: 9,
63e41f4b71Sopenharmony_ci            subClass: 0,
64e41f4b71Sopenharmony_ci            alternateSetting: 0,
65e41f4b71Sopenharmony_ci            name: "1-1",
66e41f4b71Sopenharmony_ci            endpoints: [
67e41f4b71Sopenharmony_ci              {
68e41f4b71Sopenharmony_ci                address: 129,
69e41f4b71Sopenharmony_ci                attributes: 3,
70e41f4b71Sopenharmony_ci                interval: 12,
71e41f4b71Sopenharmony_ci                maxPacketSize: 4,
72e41f4b71Sopenharmony_ci                direction: 128,
73e41f4b71Sopenharmony_ci                number: 1,
74e41f4b71Sopenharmony_ci                type: 3,
75e41f4b71Sopenharmony_ci                interfaceId: 0,
76e41f4b71Sopenharmony_ci              },
77e41f4b71Sopenharmony_ci            ],
78e41f4b71Sopenharmony_ci          },
79e41f4b71Sopenharmony_ci        ],
80e41f4b71Sopenharmony_ci      },
81e41f4b71Sopenharmony_ci    ],
82e41f4b71Sopenharmony_ci  },
83e41f4b71Sopenharmony_ci]
84e41f4b71Sopenharmony_ci*/
85e41f4b71Sopenharmony_ci```
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci## usbManager.connectDevice
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ciconnectDevice(device: USBDevice): Readonly&lt;USBDevicePipe&gt;
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ciConnects to the USB device based on the device information returned by **getDevices()**.
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ciBefore you do this, call [usbManager.getDevices](#usbmanagergetdevices) to obtain the USB device list and device information, and then call [usbManager.getDevices](#usbmanagergetdevices) to request the device access permission.
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci**Parameters**
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
100e41f4b71Sopenharmony_ci| -------- | -------- | -------- | ---------------- |
101e41f4b71Sopenharmony_ci| device | [USBDevice](#usbdevice) | Yes| USB device. The **busNum** and **devAddress** parameters obtained by **getDevices** are used to determine a USB device. Other parameters are passed transparently.|
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci**Return value**
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci| Type| Description|
106e41f4b71Sopenharmony_ci| -------- | -------- |
107e41f4b71Sopenharmony_ci| Readonly&lt;[USBDevicePipe](#usbdevicepipe)&gt; | USB device pipe for data transfer.|
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci**Error codes**
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ciFor details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci| ID| Error Message                                                    |
114e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
115e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
116e41f4b71Sopenharmony_ci| 14400001 | Permission denied. Call requestRight to get the permission first. |
117e41f4b71Sopenharmony_ci
118e41f4b71Sopenharmony_ci**Example**
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ci```ts
121e41f4b71Sopenharmony_cilet devicesList: Array<usbManager.USBDevice> = usbManager.getDevices();
122e41f4b71Sopenharmony_ciif (devicesList.length == 0) {
123e41f4b71Sopenharmony_ci  console.log(`device list is empty`);
124e41f4b71Sopenharmony_ci}
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_cilet device: usbManager.USBDevice = devicesList[0];
127e41f4b71Sopenharmony_ciusbManager.requestRight(device.name);
128e41f4b71Sopenharmony_cilet devicepipe: usbManager.USBDevicePipe = usbManager.connectDevice(device);
129e41f4b71Sopenharmony_ciconsole.log(`devicepipe = ${devicepipe}`);
130e41f4b71Sopenharmony_ci```
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ci## usbManager.hasRight
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_cihasRight(deviceName: string): boolean
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_ciChecks whether the application has the permission to access the device.
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ciChecks whether the user, for example, the application or system, has the device access permissions. The value **true** is returned if the user has the device access permissions; the value **false** is returned otherwise.
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci**Parameters**
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
145e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
146e41f4b71Sopenharmony_ci| deviceName | string | Yes| Device name, which can be obtained from the device list returned by **getDevices**.|
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ci**Error codes**
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ciFor details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci| ID| Error Message                                                    |
153e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
154e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ci**Return value**
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ci| Type| Description|
159e41f4b71Sopenharmony_ci| -------- | -------- |
160e41f4b71Sopenharmony_ci| boolean | Returns **true** if the application has the permission to access the device; returns **false** otherwise.|
161e41f4b71Sopenharmony_ci
162e41f4b71Sopenharmony_ci**Example**
163e41f4b71Sopenharmony_ci
164e41f4b71Sopenharmony_ci```ts
165e41f4b71Sopenharmony_cilet devicesList: Array<usbManager.USBDevice> = usbManager.getDevices();
166e41f4b71Sopenharmony_ciif (devicesList.length == 0) {
167e41f4b71Sopenharmony_ci  console.log(`device list is empty`);
168e41f4b71Sopenharmony_ci}
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_cilet device: usbManager.USBDevice = devicesList[0];
171e41f4b71Sopenharmony_ciusbManager.requestRight(device.name);
172e41f4b71Sopenharmony_cilet right: boolean = usbManager.hasRight(device.name);
173e41f4b71Sopenharmony_ciconsole.log(`${right}`);
174e41f4b71Sopenharmony_ci```
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ci## usbManager.requestRight
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_cirequestRight(deviceName: string): Promise&lt;boolean&gt;
179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_ciRequests the temporary device access permission for the application. This API uses a promise to return the result. System applications are granted the device access permission by default, and you do not need to apply for the permission separately.
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ci**Parameters**
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
187e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
188e41f4b71Sopenharmony_ci| deviceName | string | Yes| Device name, which can be obtained from the device list returned by **getDevices**.|
189e41f4b71Sopenharmony_ci
190e41f4b71Sopenharmony_ci**Error codes**
191e41f4b71Sopenharmony_ci
192e41f4b71Sopenharmony_ciFor details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
193e41f4b71Sopenharmony_ci
194e41f4b71Sopenharmony_ci| ID| Error Message                                                    |
195e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
196e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
197e41f4b71Sopenharmony_ci
198e41f4b71Sopenharmony_ci**Return value**
199e41f4b71Sopenharmony_ci
200e41f4b71Sopenharmony_ci| Type| Description|
201e41f4b71Sopenharmony_ci| -------- | -------- |
202e41f4b71Sopenharmony_ci| Promise&lt;boolean&gt; | Promise used to return the result. The value **true** indicates that the temporary device access permissions are granted; and the value **false** indicates the opposite.|
203e41f4b71Sopenharmony_ci
204e41f4b71Sopenharmony_ci**Example**
205e41f4b71Sopenharmony_ci
206e41f4b71Sopenharmony_ci```ts
207e41f4b71Sopenharmony_cilet devicesList: Array<usbManager.USBDevice> = usbManager.getDevices();
208e41f4b71Sopenharmony_ciif (devicesList.length == 0) {
209e41f4b71Sopenharmony_ci  console.log(`device list is empty`);
210e41f4b71Sopenharmony_ci}
211e41f4b71Sopenharmony_ci
212e41f4b71Sopenharmony_cilet device: usbManager.USBDevice = devicesList[0];
213e41f4b71Sopenharmony_ciusbManager.requestRight(device.name).then(ret => {
214e41f4b71Sopenharmony_ci  console.log(`requestRight = ${ret}`);
215e41f4b71Sopenharmony_ci});
216e41f4b71Sopenharmony_ci```
217e41f4b71Sopenharmony_ci
218e41f4b71Sopenharmony_ci## usbManager.removeRight
219e41f4b71Sopenharmony_ci
220e41f4b71Sopenharmony_ciremoveRight(deviceName: string): boolean
221e41f4b71Sopenharmony_ci
222e41f4b71Sopenharmony_ciRemoves the device access permission for the application. System applications are granted the device access permission by default, and calling this API will not revoke the permission.
223e41f4b71Sopenharmony_ci
224e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
225e41f4b71Sopenharmony_ci
226e41f4b71Sopenharmony_ci**Parameters**
227e41f4b71Sopenharmony_ci
228e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
229e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
230e41f4b71Sopenharmony_ci| deviceName | string | Yes| Device name, which can be obtained from the device list returned by **getDevices**.|
231e41f4b71Sopenharmony_ci
232e41f4b71Sopenharmony_ci**Error codes**
233e41f4b71Sopenharmony_ci
234e41f4b71Sopenharmony_ciFor details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
235e41f4b71Sopenharmony_ci
236e41f4b71Sopenharmony_ci| ID| Error Message                                                    |
237e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
238e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
239e41f4b71Sopenharmony_ci
240e41f4b71Sopenharmony_ci**Return value**
241e41f4b71Sopenharmony_ci
242e41f4b71Sopenharmony_ci| Type| Description|
243e41f4b71Sopenharmony_ci| -------- | -------- |
244e41f4b71Sopenharmony_ci| boolean | Permission removal result. The value **true** indicates that the access permission is removed successfully; and the value **false** indicates the opposite.|
245e41f4b71Sopenharmony_ci
246e41f4b71Sopenharmony_ci**Example**
247e41f4b71Sopenharmony_ci
248e41f4b71Sopenharmony_ci```ts
249e41f4b71Sopenharmony_cilet devicesList: Array<usbManager.USBDevice> = usbManager.getDevices();
250e41f4b71Sopenharmony_ciif (devicesList.length == 0) {
251e41f4b71Sopenharmony_ci  console.log(`device list is empty`);
252e41f4b71Sopenharmony_ci}
253e41f4b71Sopenharmony_ci
254e41f4b71Sopenharmony_cilet device: usbManager.USBDevice = devicesList[0];
255e41f4b71Sopenharmony_ciif (usbManager.removeRight(device.name)) {
256e41f4b71Sopenharmony_ci  console.log(`Succeed in removing right`);
257e41f4b71Sopenharmony_ci}
258e41f4b71Sopenharmony_ci```
259e41f4b71Sopenharmony_ci
260e41f4b71Sopenharmony_ci## usbManager.claimInterface
261e41f4b71Sopenharmony_ci
262e41f4b71Sopenharmony_ciclaimInterface(pipe: USBDevicePipe, iface: USBInterface, force ?: boolean): number
263e41f4b71Sopenharmony_ci
264e41f4b71Sopenharmony_ciClaims a USB interface.
265e41f4b71Sopenharmony_ci
266e41f4b71Sopenharmony_ciBefore you do this, call [usbManager.getDevices](#usbmanagergetdevices) to obtain the USB device list and USB interfaces, call [usbManager.requestRight](#usbmanagerrequestright) to request the device access permission, and call [usbManager.connectDevice](#usbmanagerconnectdevice) to obtain **devicepipe** as an input parameter.
267e41f4b71Sopenharmony_ci
268e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
269e41f4b71Sopenharmony_ci
270e41f4b71Sopenharmony_ci**Parameters**
271e41f4b71Sopenharmony_ci
272e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
273e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
274e41f4b71Sopenharmony_ci| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| USB device pipe, which is used to determine the bus number and device address. You need to call **connectDevice** to obtain its value.|
275e41f4b71Sopenharmony_ci| iface | [USBInterface](#usbinterface) | Yes| USB interface. You can use **getDevices** to obtain device information and identify the USB interface based on its ID.|
276e41f4b71Sopenharmony_ci| force | boolean | No| Whether to forcibly claim the USB interface. Whether to forcibly claim a USB interface. The default value is **false**, which means not to forcibly claim a USB interface. You can set the value as required.|
277e41f4b71Sopenharmony_ci
278e41f4b71Sopenharmony_ci**Error codes**
279e41f4b71Sopenharmony_ci
280e41f4b71Sopenharmony_ciFor details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
281e41f4b71Sopenharmony_ci
282e41f4b71Sopenharmony_ci| ID| Error Message                                                    |
283e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
284e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
285e41f4b71Sopenharmony_ci
286e41f4b71Sopenharmony_ci**Return value**
287e41f4b71Sopenharmony_ci
288e41f4b71Sopenharmony_ci| Type| Description|
289e41f4b71Sopenharmony_ci| -------- | -------- |
290e41f4b71Sopenharmony_ci| number | Returns **0** if the USB interface is successfully claimed; returns an error code otherwise.|
291e41f4b71Sopenharmony_ci
292e41f4b71Sopenharmony_ci**Example**
293e41f4b71Sopenharmony_ci
294e41f4b71Sopenharmony_ci```ts
295e41f4b71Sopenharmony_cilet devicesList: Array<usbManager.USBDevice> = usbManager.getDevices();
296e41f4b71Sopenharmony_ciif (devicesList.length == 0) {
297e41f4b71Sopenharmony_ci  console.log(`device list is empty`);
298e41f4b71Sopenharmony_ci}
299e41f4b71Sopenharmony_ci
300e41f4b71Sopenharmony_cilet device: usbManager.USBDevice = devicesList[0];
301e41f4b71Sopenharmony_ciusbManager.requestRight(device.name);
302e41f4b71Sopenharmony_cilet devicepipe: usbManager.USBDevicePipe = usbManager.connectDevice(device);
303e41f4b71Sopenharmony_cilet interfaces: usbManager.USBInterface = device.configs[0].interfaces[0];
304e41f4b71Sopenharmony_cilet ret: number= usbManager.claimInterface(devicepipe, interfaces);
305e41f4b71Sopenharmony_ciconsole.log(`claimInterface = ${ret}`);
306e41f4b71Sopenharmony_ci```
307e41f4b71Sopenharmony_ci
308e41f4b71Sopenharmony_ci## usbManager.releaseInterface
309e41f4b71Sopenharmony_ci
310e41f4b71Sopenharmony_cireleaseInterface(pipe: USBDevicePipe, iface: USBInterface): number
311e41f4b71Sopenharmony_ci
312e41f4b71Sopenharmony_ciReleases a USB interface.
313e41f4b71Sopenharmony_ci
314e41f4b71Sopenharmony_ciBefore you do this, ensure that you have claimed the interface by calling [usbManager.claimInterface](#usbmanagerclaiminterface).
315e41f4b71Sopenharmony_ci
316e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
317e41f4b71Sopenharmony_ci
318e41f4b71Sopenharmony_ci**Parameters**
319e41f4b71Sopenharmony_ci
320e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
321e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
322e41f4b71Sopenharmony_ci| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| USB device pipe, which is used to determine the bus number and device address. You need to call **connectDevice** to obtain its value.|
323e41f4b71Sopenharmony_ci| iface | [USBInterface](#usbinterface) | Yes| USB interface. You can use **getDevices** to obtain device information and identify the USB interface based on its ID.|
324e41f4b71Sopenharmony_ci
325e41f4b71Sopenharmony_ci**Error codes**
326e41f4b71Sopenharmony_ci
327e41f4b71Sopenharmony_ciFor details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
328e41f4b71Sopenharmony_ci
329e41f4b71Sopenharmony_ci| ID| Error Message                                                    |
330e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
331e41f4b71Sopenharmony_ci| 401      | Parameter error.Possible causes:1.Mandatory parameters are left unspecified.2.Incorrect parameter types. |
332e41f4b71Sopenharmony_ci
333e41f4b71Sopenharmony_ci**Return value**
334e41f4b71Sopenharmony_ci
335e41f4b71Sopenharmony_ci| Type| Description|
336e41f4b71Sopenharmony_ci| -------- | -------- |
337e41f4b71Sopenharmony_ci| number | Returns **0** if the USB interface is successfully released; returns an error code otherwise.|
338e41f4b71Sopenharmony_ci
339e41f4b71Sopenharmony_ci**Example**
340e41f4b71Sopenharmony_ci
341e41f4b71Sopenharmony_ci```ts
342e41f4b71Sopenharmony_cilet devicesList: Array<usbManager.USBDevice> = usbManager.getDevices();
343e41f4b71Sopenharmony_ciif (devicesList.length == 0) {
344e41f4b71Sopenharmony_ci  console.log(`device list is empty`);
345e41f4b71Sopenharmony_ci}
346e41f4b71Sopenharmony_ci
347e41f4b71Sopenharmony_cilet device: usbManager.USBDevice = devicesList[0];
348e41f4b71Sopenharmony_ciusbManager.requestRight(device.name);
349e41f4b71Sopenharmony_cilet devicepipe: usbManager.USBDevicePipe = usbManager.connectDevice(device);
350e41f4b71Sopenharmony_cilet interfaces: usbManager.USBInterface = device.configs[0].interfaces[0];
351e41f4b71Sopenharmony_cilet ret: number = usbManager.claimInterface(devicepipe, interfaces);
352e41f4b71Sopenharmony_ciret = usbManager.releaseInterface(devicepipe, interfaces);
353e41f4b71Sopenharmony_ciconsole.log(`releaseInterface = ${ret}`);
354e41f4b71Sopenharmony_ci```
355e41f4b71Sopenharmony_ci
356e41f4b71Sopenharmony_ci## usbManager.setConfiguration
357e41f4b71Sopenharmony_ci
358e41f4b71Sopenharmony_cisetConfiguration(pipe: USBDevicePipe, config: USBConfiguration): number
359e41f4b71Sopenharmony_ci
360e41f4b71Sopenharmony_ciSets the device configuration.
361e41f4b71Sopenharmony_ci
362e41f4b71Sopenharmony_ciBefore you do this, call [usbManager.getDevices](#usbmanagergetdevices) to obtain the USB device list and device configuration, call [usbManager.requestRight](#usbmanagerrequestright) to request the device access permission, and call [usbManager.connectDevice](#usbmanagerconnectdevice) to obtain **devicepipe** as an input parameter.
363e41f4b71Sopenharmony_ci
364e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
365e41f4b71Sopenharmony_ci
366e41f4b71Sopenharmony_ci**Parameters**
367e41f4b71Sopenharmony_ci
368e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
369e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
370e41f4b71Sopenharmony_ci| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| USB device pipe, which is used to determine the bus number and device address. You need to call **connectDevice** to obtain its value.|
371e41f4b71Sopenharmony_ci| config | [USBConfiguration](#usbconfiguration) | Yes| USB configuration. You can use **getDevices** to obtain device information and identify the USB configuration based on the ID.|
372e41f4b71Sopenharmony_ci
373e41f4b71Sopenharmony_ci**Error codes**
374e41f4b71Sopenharmony_ci
375e41f4b71Sopenharmony_ciFor details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
376e41f4b71Sopenharmony_ci
377e41f4b71Sopenharmony_ci| ID| Error Message                                                    |
378e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
379e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
380e41f4b71Sopenharmony_ci
381e41f4b71Sopenharmony_ci**Return value**
382e41f4b71Sopenharmony_ci
383e41f4b71Sopenharmony_ci| Type| Description|
384e41f4b71Sopenharmony_ci| -------- | -------- |
385e41f4b71Sopenharmony_ci| number | Returns **0** if the USB configuration is successfully set; returns an error code otherwise.|
386e41f4b71Sopenharmony_ci
387e41f4b71Sopenharmony_ci**Example**
388e41f4b71Sopenharmony_ci
389e41f4b71Sopenharmony_ci```ts
390e41f4b71Sopenharmony_cilet devicesList: Array<usbManager.USBDevice> = usbManager.getDevices();
391e41f4b71Sopenharmony_ciif (devicesList.length == 0) {
392e41f4b71Sopenharmony_ci  console.log(`device list is empty`);
393e41f4b71Sopenharmony_ci}
394e41f4b71Sopenharmony_ci
395e41f4b71Sopenharmony_cilet device: usbManager.USBDevice = devicesList[0];
396e41f4b71Sopenharmony_ciusbManager.requestRight(device.name);
397e41f4b71Sopenharmony_cilet devicepipe: usbManager.USBDevicePipe = usbManager.connectDevice(device);
398e41f4b71Sopenharmony_cilet config: usbManager.USBConfiguration = device.configs[0];
399e41f4b71Sopenharmony_cilet ret: number= usbManager.setConfiguration(devicepipe, config);
400e41f4b71Sopenharmony_ciconsole.log(`setConfiguration = ${ret}`);
401e41f4b71Sopenharmony_ci```
402e41f4b71Sopenharmony_ci
403e41f4b71Sopenharmony_ci## usbManager.setInterface
404e41f4b71Sopenharmony_ci
405e41f4b71Sopenharmony_cisetInterface(pipe: USBDevicePipe, iface: USBInterface): number
406e41f4b71Sopenharmony_ci
407e41f4b71Sopenharmony_ciSets a USB interface.
408e41f4b71Sopenharmony_ci
409e41f4b71Sopenharmony_ciBefore you do this, call [usbManager.getDevices](#usbmanagergetdevices) to obtain the USB device list and interfaces, call [usbManager.requestRight](#usbmanagerrequestright) to request the device access permission, call [usbManager.connectDevice](#usbmanagerconnectdevice) to obtain **devicepipe** as an input parameter, and call [usbManager.connectDevice](#usbmanagerconnectdevice) to claim a USB interface.
410e41f4b71Sopenharmony_ci
411e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
412e41f4b71Sopenharmony_ci
413e41f4b71Sopenharmony_ci**Parameters**
414e41f4b71Sopenharmony_ci
415e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
416e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
417e41f4b71Sopenharmony_ci| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| USB device pipe, which is used to determine the bus number and device address. You need to call **connectDevice** to obtain its value.|
418e41f4b71Sopenharmony_ci| iface | [USBInterface](#usbinterface)   | Yes| USB interface. You can use **getDevices** to obtain device information and identify the USB interface based on its **id** and **alternateSetting**.|
419e41f4b71Sopenharmony_ci
420e41f4b71Sopenharmony_ci**Error codes**
421e41f4b71Sopenharmony_ci
422e41f4b71Sopenharmony_ciFor details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
423e41f4b71Sopenharmony_ci
424e41f4b71Sopenharmony_ci| ID| Error Message                                                    |
425e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
426e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
427e41f4b71Sopenharmony_ci
428e41f4b71Sopenharmony_ci**Return value**
429e41f4b71Sopenharmony_ci
430e41f4b71Sopenharmony_ci| Type| Description|
431e41f4b71Sopenharmony_ci| -------- | -------- |
432e41f4b71Sopenharmony_ci| number | Returns **0** if the USB interface is successfully set; returns an error code otherwise.|
433e41f4b71Sopenharmony_ci
434e41f4b71Sopenharmony_ci**Example**
435e41f4b71Sopenharmony_ci
436e41f4b71Sopenharmony_ci```ts
437e41f4b71Sopenharmony_cilet devicesList: Array<usbManager.USBDevice> = usbManager.getDevices();
438e41f4b71Sopenharmony_ciif (devicesList.length == 0) {
439e41f4b71Sopenharmony_ci  console.log(`device list is empty`);
440e41f4b71Sopenharmony_ci}
441e41f4b71Sopenharmony_ci
442e41f4b71Sopenharmony_cilet device: usbManager.USBDevice = devicesList[0];
443e41f4b71Sopenharmony_ciusbManager.requestRight(device.name);
444e41f4b71Sopenharmony_cilet devicepipe: usbManager.USBDevicePipe = usbManager.connectDevice(device);
445e41f4b71Sopenharmony_cilet interfaces: usbManager.USBInterface = device.configs[0].interfaces[0];
446e41f4b71Sopenharmony_cilet ret: number = usbManager.claimInterface(devicepipe, interfaces);
447e41f4b71Sopenharmony_ciret = usbManager.setInterface(devicepipe, interfaces);
448e41f4b71Sopenharmony_ciconsole.log(`setInterface = ${ret}`);
449e41f4b71Sopenharmony_ci```
450e41f4b71Sopenharmony_ci
451e41f4b71Sopenharmony_ci## usbManager.getRawDescriptor
452e41f4b71Sopenharmony_ci
453e41f4b71Sopenharmony_cigetRawDescriptor(pipe: USBDevicePipe): Uint8Array
454e41f4b71Sopenharmony_ci
455e41f4b71Sopenharmony_ciObtains the raw USB descriptor.
456e41f4b71Sopenharmony_ci
457e41f4b71Sopenharmony_ciBefore you do this, call [usbManager.getDevices](#usbmanagergetdevices) to obtain the USB device list, call [usbManager.requestRight](#usbmanagerrequestright) to request the device access permission, and call [usbManager.connectDevice](#usbmanagerconnectdevice) to obtain **devicepipe** as an input parameter.
458e41f4b71Sopenharmony_ci
459e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
460e41f4b71Sopenharmony_ci
461e41f4b71Sopenharmony_ci**Parameters**
462e41f4b71Sopenharmony_ci
463e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
464e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
465e41f4b71Sopenharmony_ci| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| USB device pipe, which is used to determine the bus number and device address. You need to call **connectDevice** to obtain its value.|
466e41f4b71Sopenharmony_ci
467e41f4b71Sopenharmony_ci**Error codes**
468e41f4b71Sopenharmony_ci
469e41f4b71Sopenharmony_ciFor details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
470e41f4b71Sopenharmony_ci
471e41f4b71Sopenharmony_ci| ID| Error Message                                                    |
472e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
473e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
474e41f4b71Sopenharmony_ci
475e41f4b71Sopenharmony_ci**Return value**
476e41f4b71Sopenharmony_ci
477e41f4b71Sopenharmony_ci| Type| Description|
478e41f4b71Sopenharmony_ci| -------- | -------- |
479e41f4b71Sopenharmony_ci| Uint8Array | Returns the raw USB descriptor if the operation is successful; returns **undefined** otherwise.|
480e41f4b71Sopenharmony_ci
481e41f4b71Sopenharmony_ci**Example**
482e41f4b71Sopenharmony_ci
483e41f4b71Sopenharmony_ci```ts
484e41f4b71Sopenharmony_cilet devicesList: Array<usbManager.USBDevice> = usbManager.getDevices();
485e41f4b71Sopenharmony_ciif (devicesList.length == 0) {
486e41f4b71Sopenharmony_ci  console.log(`device list is empty`);
487e41f4b71Sopenharmony_ci}
488e41f4b71Sopenharmony_ci
489e41f4b71Sopenharmony_ciusbManager.requestRight(devicesList[0].name);
490e41f4b71Sopenharmony_cilet devicepipe: usbManager.USBDevicePipe = usbManager.connectDevice(devicesList[0]);
491e41f4b71Sopenharmony_cilet ret: Uint8Array = usbManager.getRawDescriptor(devicepipe);
492e41f4b71Sopenharmony_ci```
493e41f4b71Sopenharmony_ci
494e41f4b71Sopenharmony_ci## usbManager.getFileDescriptor
495e41f4b71Sopenharmony_ci
496e41f4b71Sopenharmony_cigetFileDescriptor(pipe: USBDevicePipe): number
497e41f4b71Sopenharmony_ci
498e41f4b71Sopenharmony_ciObtains the file descriptor.
499e41f4b71Sopenharmony_ci
500e41f4b71Sopenharmony_ciBefore you do this, call [usbManager.getDevices](#usbmanagergetdevices) to obtain the USB device list, call [usbManager.requestRight](#usbmanagerrequestright) to request the device access permission, and call [usbManager.connectDevice](#usbmanagerconnectdevice) to obtain **devicepipe** as an input parameter.
501e41f4b71Sopenharmony_ci
502e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
503e41f4b71Sopenharmony_ci
504e41f4b71Sopenharmony_ci**Parameters**
505e41f4b71Sopenharmony_ci
506e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
507e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
508e41f4b71Sopenharmony_ci| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| USB device pipe, which is used to determine the bus number and device address. You need to call **connectDevice** to obtain its value.|
509e41f4b71Sopenharmony_ci
510e41f4b71Sopenharmony_ci**Error codes**
511e41f4b71Sopenharmony_ci
512e41f4b71Sopenharmony_ciFor details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
513e41f4b71Sopenharmony_ci
514e41f4b71Sopenharmony_ci| ID| Error Message                                                    |
515e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
516e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
517e41f4b71Sopenharmony_ci
518e41f4b71Sopenharmony_ci**Return value**
519e41f4b71Sopenharmony_ci
520e41f4b71Sopenharmony_ci| Type    | Description                  |
521e41f4b71Sopenharmony_ci| ------ | -------------------- |
522e41f4b71Sopenharmony_ci| number | Returns the file descriptor of the USB device if the operation is successful; returns **-1** otherwise.|
523e41f4b71Sopenharmony_ci
524e41f4b71Sopenharmony_ci**Example**
525e41f4b71Sopenharmony_ci
526e41f4b71Sopenharmony_ci```ts
527e41f4b71Sopenharmony_cilet devicesList: Array<usbManager.USBDevice> = usbManager.getDevices();
528e41f4b71Sopenharmony_ciif (devicesList.length == 0) {
529e41f4b71Sopenharmony_ci  console.log(`device list is empty`);
530e41f4b71Sopenharmony_ci}
531e41f4b71Sopenharmony_ci
532e41f4b71Sopenharmony_ciusbManager.requestRight(devicesList[0].name);
533e41f4b71Sopenharmony_cilet devicepipe: usbManager.USBDevicePipe = usbManager.connectDevice(devicesList[0]);
534e41f4b71Sopenharmony_cilet ret: number = usbManager.getFileDescriptor(devicepipe);
535e41f4b71Sopenharmony_ci```
536e41f4b71Sopenharmony_ci
537e41f4b71Sopenharmony_ci## usbManager.controlTransfer<sup>(deprecated)</sup>
538e41f4b71Sopenharmony_ci
539e41f4b71Sopenharmony_cicontrolTransfer(pipe: USBDevicePipe, controlparam: USBControlParams, timeout ?: number): Promise&lt;number&gt;
540e41f4b71Sopenharmony_ci
541e41f4b71Sopenharmony_ciPerforms control transfer.
542e41f4b71Sopenharmony_ci
543e41f4b71Sopenharmony_ciBefore you do this, call [usbManager.getDevices](#usbmanagergetdevices) to obtain the USB device list, call [usbManager.requestRight](#usbmanagerrequestright) to request the device access permission, and call [usbManager.connectDevice](#usbmanagerconnectdevice) to obtain **devicepipe** as an input parameter.
544e41f4b71Sopenharmony_ci
545e41f4b71Sopenharmony_ci**NOTE**
546e41f4b71Sopenharmony_ci
547e41f4b71Sopenharmony_ci> This API is supported since API version 9 and deprecated since API version 12. You are advised to use [usbControlTransfer](#usbmanagerusbcontroltransfer12).
548e41f4b71Sopenharmony_ci
549e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
550e41f4b71Sopenharmony_ci
551e41f4b71Sopenharmony_ci**Parameters**
552e41f4b71Sopenharmony_ci
553e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
554e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
555e41f4b71Sopenharmony_ci| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| USB device pipe. You need to call **connectDevice** to obtain its value.|
556e41f4b71Sopenharmony_ci| controlparam | [USBControlParams](#usbcontrolparams) | Yes| Control transfer parameters. Set the parameters as required. For details, see the USB protocol.|
557e41f4b71Sopenharmony_ci| timeout | number | No| Timeout period, in ms. This parameter is optional. The default value is **0**. You can set this parameter as required.|
558e41f4b71Sopenharmony_ci
559e41f4b71Sopenharmony_ci**Error codes**
560e41f4b71Sopenharmony_ci
561e41f4b71Sopenharmony_ciFor details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
562e41f4b71Sopenharmony_ci
563e41f4b71Sopenharmony_ci| ID| Error Message                                                    |
564e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
565e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
566e41f4b71Sopenharmony_ci
567e41f4b71Sopenharmony_ci**Return value**
568e41f4b71Sopenharmony_ci
569e41f4b71Sopenharmony_ci| Type| Description|
570e41f4b71Sopenharmony_ci| -------- | -------- |
571e41f4b71Sopenharmony_ci| Promise&lt;number&gt; | Promise used to return the result, which is the size of the transmitted or received data block if the transfer is successful, or **-1** if an exception has occurred.|
572e41f4b71Sopenharmony_ci
573e41f4b71Sopenharmony_ci**Example**
574e41f4b71Sopenharmony_ci
575e41f4b71Sopenharmony_ci```ts
576e41f4b71Sopenharmony_ciclass PARA {
577e41f4b71Sopenharmony_ci  request: number = 0
578e41f4b71Sopenharmony_ci  reqType: usbManager.USBControlRequestType = 0
579e41f4b71Sopenharmony_ci  target: usbManager.USBRequestTargetType = 0
580e41f4b71Sopenharmony_ci  value: number = 0
581e41f4b71Sopenharmony_ci  index: number = 0
582e41f4b71Sopenharmony_ci  data: Uint8Array = new Uint8Array()
583e41f4b71Sopenharmony_ci}
584e41f4b71Sopenharmony_ci
585e41f4b71Sopenharmony_cilet param: PARA = {
586e41f4b71Sopenharmony_ci  request: 0x06,
587e41f4b71Sopenharmony_ci  reqType: 0x80,
588e41f4b71Sopenharmony_ci  target:0,
589e41f4b71Sopenharmony_ci  value: 0x01 << 8 | 0,
590e41f4b71Sopenharmony_ci  index: 0,
591e41f4b71Sopenharmony_ci  data: new Uint8Array(18)
592e41f4b71Sopenharmony_ci};
593e41f4b71Sopenharmony_ci
594e41f4b71Sopenharmony_cilet devicesList: Array<usbManager.USBDevice> = usbManager.getDevices();
595e41f4b71Sopenharmony_ciif (devicesList.length == 0) {
596e41f4b71Sopenharmony_ci  console.log(`device list is empty`);
597e41f4b71Sopenharmony_ci}
598e41f4b71Sopenharmony_ci
599e41f4b71Sopenharmony_ciusbManager.requestRight(devicesList[0].name);
600e41f4b71Sopenharmony_cilet devicepipe: usbManager.USBDevicePipe = usbManager.connectDevice(devicesList[0]);
601e41f4b71Sopenharmony_ciusbManager.controlTransfer(devicepipe, param).then((ret: number) => {
602e41f4b71Sopenharmony_ciconsole.log(`controlTransfer = ${ret}`);
603e41f4b71Sopenharmony_ci})
604e41f4b71Sopenharmony_ci```
605e41f4b71Sopenharmony_ci
606e41f4b71Sopenharmony_ci## usbManager.usbControlTransfer<sup>12+</sup>
607e41f4b71Sopenharmony_ci
608e41f4b71Sopenharmony_ciusbControlTransfer(pipe: USBDevicePipe, requestparam: USBDeviceRequestParams, timeout ?: number): Promise&lt;number&gt;
609e41f4b71Sopenharmony_ci
610e41f4b71Sopenharmony_ciPerforms control transfer.
611e41f4b71Sopenharmony_ci
612e41f4b71Sopenharmony_ciBefore you do this, call [usbManager.getDevices](#usbmanagergetdevices) to obtain the USB device list, call [usbManager.requestRight](#usbmanagerrequestright) to request the device access permission, and call [usbManager.connectDevice](#usbmanagerconnectdevice) to obtain **devicepipe** as an input parameter.
613e41f4b71Sopenharmony_ci
614e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
615e41f4b71Sopenharmony_ci
616e41f4b71Sopenharmony_ci**Parameters**
617e41f4b71Sopenharmony_ci
618e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
619e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
620e41f4b71Sopenharmony_ci| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| USB device pipe, which is used to determine the USB device.|
621e41f4b71Sopenharmony_ci| requestparam | [USBDeviceRequestParams](#usbdevicerequestparams12) | Yes| Control transfer parameters. Set the parameters as required. For details, see the USB protocol.|
622e41f4b71Sopenharmony_ci| timeout | number | No| Timeout duration in ms. This parameter is optional. The default value is **0**, indicating no timeout.|
623e41f4b71Sopenharmony_ci
624e41f4b71Sopenharmony_ci**Error codes**
625e41f4b71Sopenharmony_ci
626e41f4b71Sopenharmony_ciFor details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
627e41f4b71Sopenharmony_ci
628e41f4b71Sopenharmony_ci| ID| Error Message                                                    |
629e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
630e41f4b71Sopenharmony_ci| 401      | Parameter error.Possible causes:1.Mandatory parameters are left unspecified.2.Incorrect parameter types |
631e41f4b71Sopenharmony_ci
632e41f4b71Sopenharmony_ci**Return value**
633e41f4b71Sopenharmony_ci
634e41f4b71Sopenharmony_ci| Type| Description|
635e41f4b71Sopenharmony_ci| -------- | -------- |
636e41f4b71Sopenharmony_ci| Promise&lt;number&gt; | Promise used to return the result, which is the size of the transmitted or received data block if the transfer is successful, or **-1** if an exception has occurred.|
637e41f4b71Sopenharmony_ci
638e41f4b71Sopenharmony_ci**Example**
639e41f4b71Sopenharmony_ci
640e41f4b71Sopenharmony_ci```ts
641e41f4b71Sopenharmony_ciclass PARA {
642e41f4b71Sopenharmony_ci  bmRequestType: number = 0
643e41f4b71Sopenharmony_ci  bRequest: number = 0
644e41f4b71Sopenharmony_ci  wValue: number = 0
645e41f4b71Sopenharmony_ci  wIndex: number = 0
646e41f4b71Sopenharmony_ci  wLength: number = 0
647e41f4b71Sopenharmony_ci  data: Uint8Array = new Uint8Array()
648e41f4b71Sopenharmony_ci}
649e41f4b71Sopenharmony_ci
650e41f4b71Sopenharmony_cilet param: PARA = {
651e41f4b71Sopenharmony_ci  bmRequestType: 0x80,
652e41f4b71Sopenharmony_ci  bRequest: 0x06,
653e41f4b71Sopenharmony_ci
654e41f4b71Sopenharmony_ci  wValue:0x01 << 8 | 0,
655e41f4b71Sopenharmony_ci  wIndex: 0,
656e41f4b71Sopenharmony_ci  wLength: 18,
657e41f4b71Sopenharmony_ci  data: new Uint8Array(18)
658e41f4b71Sopenharmony_ci};
659e41f4b71Sopenharmony_ci
660e41f4b71Sopenharmony_cilet devicesList: Array<usbManager.USBDevice> = usbManager.getDevices();
661e41f4b71Sopenharmony_ciif (devicesList.length == 0) {
662e41f4b71Sopenharmony_ci  console.log(`device list is empty`);
663e41f4b71Sopenharmony_ci}
664e41f4b71Sopenharmony_ci
665e41f4b71Sopenharmony_ciusbManager.requestRight(devicesList[0].name);
666e41f4b71Sopenharmony_cilet devicepipe: usbManager.USBDevicePipe = usbManager.connectDevice(devicesList[0]);
667e41f4b71Sopenharmony_ciusbManager.usbControlTransfer(devicepipe, param).then((ret: number) => {
668e41f4b71Sopenharmony_ciconsole.log(`usbControlTransfer = ${ret}`);
669e41f4b71Sopenharmony_ci})
670e41f4b71Sopenharmony_ci```
671e41f4b71Sopenharmony_ci
672e41f4b71Sopenharmony_ci## usbManager.bulkTransfer
673e41f4b71Sopenharmony_ci
674e41f4b71Sopenharmony_cibulkTransfer(pipe: USBDevicePipe, endpoint: USBEndpoint, buffer: Uint8Array, timeout ?: number): Promise&lt;number&gt;
675e41f4b71Sopenharmony_ci
676e41f4b71Sopenharmony_ciPerforms bulk transfer.
677e41f4b71Sopenharmony_ci
678e41f4b71Sopenharmony_ciBefore you do this, call [usbManager.getDevices](#usbmanagergetdevices) to obtain the USB device list and endpoints, call [usbManager.requestRight](#usbmanagerrequestright) to request the device access permission, call [usbManager.connectDevice](#usbmanagerconnectdevice) to obtain **devicepipe** as an input parameter, and call [usbManager.claimInterface](#usbmanagerclaiminterface) to claim a USB interface.
679e41f4b71Sopenharmony_ci
680e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
681e41f4b71Sopenharmony_ci
682e41f4b71Sopenharmony_ci**Parameters**
683e41f4b71Sopenharmony_ci
684e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
685e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
686e41f4b71Sopenharmony_ci| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| USB device pipe. You need to call **connectDevice** to obtain its value.|
687e41f4b71Sopenharmony_ci| endpoint | [USBEndpoint](#usbendpoint) | Yes| USB endpoint, which is used to determine the USB interface for data transfer. You need to call **getDevices** to obtain the device information list and endpoint. Wherein, **address** is used to determine the endpoint address, **direction** is used to determine the endpoint direction, and **interfaceId** is used to determine the USB interface to which the endpoint belongs. Other parameters are passed transparently.|
688e41f4b71Sopenharmony_ci| buffer | Uint8Array | Yes| Buffer used to write or read data.|
689e41f4b71Sopenharmony_ci| timeout | number | No| Timeout period, in ms. This parameter is optional. The default value is **0**. You can set this parameter as required.|
690e41f4b71Sopenharmony_ci
691e41f4b71Sopenharmony_ci**Error codes**
692e41f4b71Sopenharmony_ci
693e41f4b71Sopenharmony_ciFor details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
694e41f4b71Sopenharmony_ci
695e41f4b71Sopenharmony_ci| ID| Error Message                                                    |
696e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
697e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
698e41f4b71Sopenharmony_ci
699e41f4b71Sopenharmony_ci**Return value**
700e41f4b71Sopenharmony_ci
701e41f4b71Sopenharmony_ci| Type| Description|
702e41f4b71Sopenharmony_ci| -------- | -------- |
703e41f4b71Sopenharmony_ci| Promise&lt;number&gt; | Promise used to return the result, which is the size of the transmitted or received data block if the transfer is successful, or **-1** if an exception has occurred.|
704e41f4b71Sopenharmony_ci
705e41f4b71Sopenharmony_ci**Example**
706e41f4b71Sopenharmony_ci
707e41f4b71Sopenharmony_ci```ts
708e41f4b71Sopenharmony_ci// Call usbManager.getDevices to obtain a data set. Then, obtain a USB device and its access permission.
709e41f4b71Sopenharmony_ci// Pass the obtained USB device as a parameter to usbManager.connectDevice. Then, call usbManager.connectDevice to connect the USB device.
710e41f4b71Sopenharmony_ci// Call usbManager.claimInterface to claim a USB interface. After that, call usbManager.bulkTransfer to start bulk transfer.
711e41f4b71Sopenharmony_cilet devicesList: Array<usbManager.USBDevice> = usbManager.getDevices();
712e41f4b71Sopenharmony_ciif (devicesList.length == 0) {
713e41f4b71Sopenharmony_ci  console.log(`device list is empty`);
714e41f4b71Sopenharmony_ci}
715e41f4b71Sopenharmony_ci
716e41f4b71Sopenharmony_cilet device: usbManager.USBDevice = devicesList[0];
717e41f4b71Sopenharmony_ciusbManager.requestRight(device.name);
718e41f4b71Sopenharmony_ci
719e41f4b71Sopenharmony_cilet devicepipe: usbManager.USBDevicePipe = usbManager.connectDevice(device);
720e41f4b71Sopenharmony_cifor (let i = 0; i < device.configs[0].interfaces.length; i++) {
721e41f4b71Sopenharmony_ci  if (device.configs[0].interfaces[i].endpoints[0].attributes == 2) {
722e41f4b71Sopenharmony_ci    let endpoint: usbManager.USBEndpoint = device.configs[0].interfaces[i].endpoints[0];
723e41f4b71Sopenharmony_ci    let interfaces: usbManager.USBInterface = device.configs[0].interfaces[i];
724e41f4b71Sopenharmony_ci    let ret: number = usbManager.claimInterface(devicepipe, interfaces);
725e41f4b71Sopenharmony_ci    let buffer =  new Uint8Array(128);
726e41f4b71Sopenharmony_ci    usbManager.bulkTransfer(devicepipe, endpoint, buffer).then((ret: number) => {
727e41f4b71Sopenharmony_ci      console.log(`bulkTransfer = ${ret}`);
728e41f4b71Sopenharmony_ci    });
729e41f4b71Sopenharmony_ci  }
730e41f4b71Sopenharmony_ci}
731e41f4b71Sopenharmony_ci```
732e41f4b71Sopenharmony_ci
733e41f4b71Sopenharmony_ci## usbManager.closePipe
734e41f4b71Sopenharmony_ci
735e41f4b71Sopenharmony_ciclosePipe(pipe: USBDevicePipe): number
736e41f4b71Sopenharmony_ci
737e41f4b71Sopenharmony_ciCloses a USB device pipe.
738e41f4b71Sopenharmony_ci
739e41f4b71Sopenharmony_ciBefore you do this, call [usbManager.getDevices](#usbmanagergetdevices) to obtain the USB device list, call [usbManager.requestRight](#usbmanagerrequestright) to request the device access permission, and call [usbManager.connectDevice](#usbmanagerconnectdevice) to obtain **devicepipe** as an input parameter.
740e41f4b71Sopenharmony_ci
741e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
742e41f4b71Sopenharmony_ci
743e41f4b71Sopenharmony_ci**Parameters**
744e41f4b71Sopenharmony_ci
745e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
746e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
747e41f4b71Sopenharmony_ci| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| USB device pipe, which is used to determine the message control channel. You need to call **connectDevice** to obtain its value.|
748e41f4b71Sopenharmony_ci
749e41f4b71Sopenharmony_ci**Error codes**
750e41f4b71Sopenharmony_ci
751e41f4b71Sopenharmony_ciFor details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
752e41f4b71Sopenharmony_ci
753e41f4b71Sopenharmony_ci| ID| Error Message                                                    |
754e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ |
755e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
756e41f4b71Sopenharmony_ci
757e41f4b71Sopenharmony_ci**Return value**
758e41f4b71Sopenharmony_ci
759e41f4b71Sopenharmony_ci| Type| Description|
760e41f4b71Sopenharmony_ci| -------- | -------- |
761e41f4b71Sopenharmony_ci| number | Returns **0** if the USB device pipe is closed successfully; returns an error code otherwise.|
762e41f4b71Sopenharmony_ci
763e41f4b71Sopenharmony_ci**Example**
764e41f4b71Sopenharmony_ci
765e41f4b71Sopenharmony_ci```ts
766e41f4b71Sopenharmony_cilet devicesList: Array<usbManager.USBDevice> = usbManager.getDevices();
767e41f4b71Sopenharmony_ciif (devicesList.length == 0) {
768e41f4b71Sopenharmony_ci  console.log(`device list is empty`);
769e41f4b71Sopenharmony_ci}
770e41f4b71Sopenharmony_ci
771e41f4b71Sopenharmony_ciusbManager.requestRight(devicesList[0].name);
772e41f4b71Sopenharmony_cilet devicepipe: usbManager.USBDevicePipe = usbManager.connectDevice(devicesList[0]);
773e41f4b71Sopenharmony_cilet ret: number = usbManager.closePipe(devicepipe);
774e41f4b71Sopenharmony_ciconsole.log(`closePipe = ${ret}`);
775e41f4b71Sopenharmony_ci```
776e41f4b71Sopenharmony_ci
777e41f4b71Sopenharmony_ci## USBEndpoint
778e41f4b71Sopenharmony_ci
779e41f4b71Sopenharmony_ciRepresents the USB endpoint from which data is sent or received. You can obtain the USB endpoint through [USBInterface](#usbinterface).
780e41f4b71Sopenharmony_ci
781e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
782e41f4b71Sopenharmony_ci
783e41f4b71Sopenharmony_ci| Name           | Type                                       | Mandatory           |Description           |
784e41f4b71Sopenharmony_ci| ------------- | ------------------------------------------- | ------------- |------------- |
785e41f4b71Sopenharmony_ci| address       | number                                      | Yes|Endpoint address.        |
786e41f4b71Sopenharmony_ci| attributes    | number                                      | Yes|Endpoint attributes.        |
787e41f4b71Sopenharmony_ci| interval      | number                                      | Yes|Endpoint interval.        |
788e41f4b71Sopenharmony_ci| maxPacketSize | number                                      | Yes|Maximum size of data packets on the endpoint.   |
789e41f4b71Sopenharmony_ci| direction     | [USBRequestDirection](#usbrequestdirection) | Yes|Endpoint direction.       |
790e41f4b71Sopenharmony_ci| number        | number                                      | Yes|Endpoint number.         |
791e41f4b71Sopenharmony_ci| type          | number                                      | Yes|Endpoint type.        |
792e41f4b71Sopenharmony_ci| interfaceId   | number                                      | Yes|Unique ID of the interface to which the endpoint belongs.|
793e41f4b71Sopenharmony_ci
794e41f4b71Sopenharmony_ci## USBInterface
795e41f4b71Sopenharmony_ci
796e41f4b71Sopenharmony_ciRepresents a USB interface. One [USBConfiguration](#usbconfiguration) object can contain multiple **USBInterface** instances, each providing a specific function.
797e41f4b71Sopenharmony_ci
798e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
799e41f4b71Sopenharmony_ci
800e41f4b71Sopenharmony_ci| Name              | Type                                    | Mandatory           |Description                   |
801e41f4b71Sopenharmony_ci| ---------------- | ---------------------------------------- | ------------- |--------------------- |
802e41f4b71Sopenharmony_ci| id               | number                                   | Yes|Unique ID of the USB interface.             |
803e41f4b71Sopenharmony_ci| protocol         | number                                   | Yes|Interface protocol.               |
804e41f4b71Sopenharmony_ci| clazz            | number                                   | Yes|Device type.                |
805e41f4b71Sopenharmony_ci| subClass         | number                                   | Yes|Device subclass.                |
806e41f4b71Sopenharmony_ci| alternateSetting | number                                   | Yes|Settings for alternating between descriptors of the same USB interface.|
807e41f4b71Sopenharmony_ci| name             | string                                   | Yes|Interface name.                |
808e41f4b71Sopenharmony_ci| endpoints        | Array&lt;[USBEndpoint](#usbendpoint)&gt; | Yes|Endpoints that belong to the USB interface.          |
809e41f4b71Sopenharmony_ci
810e41f4b71Sopenharmony_ci## USBConfiguration
811e41f4b71Sopenharmony_ci
812e41f4b71Sopenharmony_ciRepresents the USB configuration. One [USBDevice](#usbdevice) can contain multiple **USBConfig** instances.
813e41f4b71Sopenharmony_ci
814e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
815e41f4b71Sopenharmony_ci
816e41f4b71Sopenharmony_ci| Name            | Type                                            | Mandatory |Description             |
817e41f4b71Sopenharmony_ci| -------------- | ------------------------------------------------ | --------------- |--------------- |
818e41f4b71Sopenharmony_ci| id             | number                                           | Yes|Unique ID of the USB configuration.       |
819e41f4b71Sopenharmony_ci| attributes     | number                                           | Yes|Configuration attributes.         |
820e41f4b71Sopenharmony_ci| maxPower       | number                                           | Yes|Maximum power consumption, in mA.   |
821e41f4b71Sopenharmony_ci| name           | string                                           | Yes|Configuration name, which can be left empty.    |
822e41f4b71Sopenharmony_ci| isRemoteWakeup | boolean                                          | Yes|Support for remote wakeup.|
823e41f4b71Sopenharmony_ci| isSelfPowered  | boolean                                          | Yes| Support for independent power supplies.|
824e41f4b71Sopenharmony_ci| interfaces     | Array&nbsp;&lt;[USBInterface](#usbinterface)&gt; | Yes|Supported interface attributes.     |
825e41f4b71Sopenharmony_ci
826e41f4b71Sopenharmony_ci## USBDevice
827e41f4b71Sopenharmony_ci
828e41f4b71Sopenharmony_ciRepresents the USB device information.
829e41f4b71Sopenharmony_ci
830e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
831e41f4b71Sopenharmony_ci
832e41f4b71Sopenharmony_ci| Name              | Type                                | Mandatory        |Description        |
833e41f4b71Sopenharmony_ci| ---------------- | ------------------------------------ | ---------- |---------- |
834e41f4b71Sopenharmony_ci| busNum           | number                               | Yes|Bus address.     |
835e41f4b71Sopenharmony_ci| devAddress       | number                               | Yes|Device address.     |
836e41f4b71Sopenharmony_ci| serial           | string                               | Yes|Sequence number.      |
837e41f4b71Sopenharmony_ci| name             | string                               | Yes|Device name.     |
838e41f4b71Sopenharmony_ci| manufacturerName | string                               | Yes| Device manufacturer.     |
839e41f4b71Sopenharmony_ci| productName      | string                               | Yes|Product name.     |
840e41f4b71Sopenharmony_ci| version          | string                               | Yes|Version number.       |
841e41f4b71Sopenharmony_ci| vendorId         | number                               | Yes|Vendor ID.     |
842e41f4b71Sopenharmony_ci| productId        | number                               | Yes|Product ID.     |
843e41f4b71Sopenharmony_ci| clazz            | number                               | Yes|Device class.      |
844e41f4b71Sopenharmony_ci| subClass         | number                               | Yes|Device subclass.     |
845e41f4b71Sopenharmony_ci| protocol         | number                               | Yes|Device protocol code.    |
846e41f4b71Sopenharmony_ci| configs          | Array&lt;[USBConfiguration](#usbconfiguration)&gt; | Yes|Device configuration descriptor information.|
847e41f4b71Sopenharmony_ci
848e41f4b71Sopenharmony_ci## USBDevicePipe
849e41f4b71Sopenharmony_ci
850e41f4b71Sopenharmony_ciRepresents a USB device pipe, which is used to determine a USB device.
851e41f4b71Sopenharmony_ci
852e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
853e41f4b71Sopenharmony_ci
854e41f4b71Sopenharmony_ci| Name        | Type  | Mandatory   |Description   |
855e41f4b71Sopenharmony_ci| ---------- | ------ | ----- |----- |
856e41f4b71Sopenharmony_ci| busNum     | number |Yes| Bus address.|
857e41f4b71Sopenharmony_ci| devAddress | number |Yes| Device address.|
858e41f4b71Sopenharmony_ci
859e41f4b71Sopenharmony_ci## USBControlParams
860e41f4b71Sopenharmony_ci
861e41f4b71Sopenharmony_ciRepresents control transfer parameters.
862e41f4b71Sopenharmony_ci
863e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
864e41f4b71Sopenharmony_ci
865e41f4b71Sopenharmony_ci| Name     | Type                                           | Mandatory              |Description              |
866e41f4b71Sopenharmony_ci| ------- | ----------------------------------------------- | ---------------- |---------------- |
867e41f4b71Sopenharmony_ci| request | number                                          | Yes  |Request type.           |
868e41f4b71Sopenharmony_ci| target  | [USBRequestTargetType](#usbrequesttargettype)   | Yes  |Request target type.         |
869e41f4b71Sopenharmony_ci| reqType | [USBControlRequestType](#usbcontrolrequesttype) | Yes  |Control request type.         |
870e41f4b71Sopenharmony_ci| value   | number                                          | Yes  |Request parameter value.           |
871e41f4b71Sopenharmony_ci| index   | number                                          | Yes  |Index of the request parameter value.|
872e41f4b71Sopenharmony_ci| data    | Uint8Array                                      | Yes  |Buffer for writing or reading data.    |
873e41f4b71Sopenharmony_ci
874e41f4b71Sopenharmony_ci## USBDeviceRequestParams<sup>12+</sup>
875e41f4b71Sopenharmony_ci
876e41f4b71Sopenharmony_ciRepresents control transfer parameters.
877e41f4b71Sopenharmony_ci
878e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
879e41f4b71Sopenharmony_ci
880e41f4b71Sopenharmony_ci| Name     | Type                                           | Mandatory              |Description              |
881e41f4b71Sopenharmony_ci| ------- | ----------------------------------------------- | ---------------- |---------------- |
882e41f4b71Sopenharmony_ci| bmRequestType | number                                    | Yes  |Control request type.           |
883e41f4b71Sopenharmony_ci| bRequest  | number                                        | Yes  |Request type.         |
884e41f4b71Sopenharmony_ci| wValue | number                                           | Yes  |Request parameter value.         |
885e41f4b71Sopenharmony_ci| wIndex   | number                                         | Yes  |Index of the request parameter value.           |
886e41f4b71Sopenharmony_ci| wLength   | number                                        | Yes  |Length of the requested data.|
887e41f4b71Sopenharmony_ci| data    | Uint8Array                                      | Yes  |Buffer for writing or reading data.    |
888e41f4b71Sopenharmony_ci
889e41f4b71Sopenharmony_ci## USBRequestTargetType
890e41f4b71Sopenharmony_ci
891e41f4b71Sopenharmony_ciEnumerates request target types.
892e41f4b71Sopenharmony_ci
893e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
894e41f4b71Sopenharmony_ci
895e41f4b71Sopenharmony_ci| Name                        | Value  | Description  |
896e41f4b71Sopenharmony_ci| ---------------------------- | ---- | ------ |
897e41f4b71Sopenharmony_ci| USB_REQUEST_TARGET_DEVICE    | 0    | Device.|
898e41f4b71Sopenharmony_ci| USB_REQUEST_TARGET_INTERFACE | 1    | Interface.|
899e41f4b71Sopenharmony_ci| USB_REQUEST_TARGET_ENDPOINT  | 2    | Endpoint.|
900e41f4b71Sopenharmony_ci| USB_REQUEST_TARGET_OTHER     | 3    | Other.|
901e41f4b71Sopenharmony_ci
902e41f4b71Sopenharmony_ci## USBControlRequestType
903e41f4b71Sopenharmony_ci
904e41f4b71Sopenharmony_ciEnumerates control request types.
905e41f4b71Sopenharmony_ci
906e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
907e41f4b71Sopenharmony_ci
908e41f4b71Sopenharmony_ci| Name                     | Value  | Description  |
909e41f4b71Sopenharmony_ci| ------------------------- | ---- | ------ |
910e41f4b71Sopenharmony_ci| USB_REQUEST_TYPE_STANDARD | 0    | Standard.|
911e41f4b71Sopenharmony_ci| USB_REQUEST_TYPE_CLASS    | 1    | Class.  |
912e41f4b71Sopenharmony_ci| USB_REQUEST_TYPE_VENDOR   | 2    | Vendor.|
913e41f4b71Sopenharmony_ci
914e41f4b71Sopenharmony_ci## USBRequestDirection
915e41f4b71Sopenharmony_ci
916e41f4b71Sopenharmony_ciEnumerates request directions.
917e41f4b71Sopenharmony_ci
918e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
919e41f4b71Sopenharmony_ci
920e41f4b71Sopenharmony_ci| Name                       | Value  | Description                    |
921e41f4b71Sopenharmony_ci| --------------------------- | ---- | ------------------------ |
922e41f4b71Sopenharmony_ci| USB_REQUEST_DIR_TO_DEVICE   | 0    | Request for writing data from the host to the device.|
923e41f4b71Sopenharmony_ci| USB_REQUEST_DIR_FROM_DEVICE | 0x80 | Request for reading data from the device to the host.|
924