1e41f4b71Sopenharmony_ci# @ohos.usb (USB Manager) (No Longer Maintained)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThe **usb** module provides USB device management functions, including USB device list query, bulk data transfer, control transfer, and permission control.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci>  **NOTE**
6e41f4b71Sopenharmony_ci> 
7e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci> The APIs provided by this module are no longer maintained since API version 9. You are advised to use [@ohos.usbManager](js-apis-usbManager.md).
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci## Modules to Import
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci```js
14e41f4b71Sopenharmony_ciimport usb from "@ohos.usb";
15e41f4b71Sopenharmony_ci```
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci## usb.getDevices
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_cigetDevices(): Array<Readonly<USBDevice>>
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ciObtains the USB device list.
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci**Return value**
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci| Type                                                  | Description     |
28e41f4b71Sopenharmony_ci| ---------------------------------------------------- | ------- |
29e41f4b71Sopenharmony_ci| Array<Readonly<[USBDevice](#usbdevice)>> | USB device list.|
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci**Example**
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci```js
34e41f4b71Sopenharmony_cilet devicesList = usb.getDevices();
35e41f4b71Sopenharmony_ciconsole.log(`devicesList = ${devicesList}`);
36e41f4b71Sopenharmony_ci// devicesList is a list of USB devices.
37e41f4b71Sopenharmony_ci// A simple example of devicesList is provided as follows:
38e41f4b71Sopenharmony_ci/*[
39e41f4b71Sopenharmony_ci  {
40e41f4b71Sopenharmony_ci    name: "1-1",
41e41f4b71Sopenharmony_ci    serial: "",
42e41f4b71Sopenharmony_ci    manufacturerName: "",
43e41f4b71Sopenharmony_ci    productName: "",
44e41f4b71Sopenharmony_ci    version: "",
45e41f4b71Sopenharmony_ci    vendorId: 7531,
46e41f4b71Sopenharmony_ci    productId: 2,
47e41f4b71Sopenharmony_ci    clazz: 9,
48e41f4b71Sopenharmony_ci    subClass: 0,
49e41f4b71Sopenharmony_ci    protocol: 1,
50e41f4b71Sopenharmony_ci    devAddress: 1,
51e41f4b71Sopenharmony_ci    busNum: 1,
52e41f4b71Sopenharmony_ci    configs: [
53e41f4b71Sopenharmony_ci      {
54e41f4b71Sopenharmony_ci        id: 1,
55e41f4b71Sopenharmony_ci        attributes: 224,
56e41f4b71Sopenharmony_ci        isRemoteWakeup: true,
57e41f4b71Sopenharmony_ci        isSelfPowered: true,
58e41f4b71Sopenharmony_ci        maxPower: 0,
59e41f4b71Sopenharmony_ci        name: "1-1",
60e41f4b71Sopenharmony_ci        interfaces: [
61e41f4b71Sopenharmony_ci          {
62e41f4b71Sopenharmony_ci            id: 0,
63e41f4b71Sopenharmony_ci            protocol: 0,
64e41f4b71Sopenharmony_ci            clazz: 9,
65e41f4b71Sopenharmony_ci            subClass: 0,
66e41f4b71Sopenharmony_ci            alternateSetting: 0,
67e41f4b71Sopenharmony_ci            name: "1-1",
68e41f4b71Sopenharmony_ci            endpoints: [
69e41f4b71Sopenharmony_ci              {
70e41f4b71Sopenharmony_ci                address: 129,
71e41f4b71Sopenharmony_ci                attributes: 3,
72e41f4b71Sopenharmony_ci                interval: 12,
73e41f4b71Sopenharmony_ci                maxPacketSize: 4,
74e41f4b71Sopenharmony_ci                direction: 128,
75e41f4b71Sopenharmony_ci                number: 1,
76e41f4b71Sopenharmony_ci                type: 3,
77e41f4b71Sopenharmony_ci                interfaceId: 0,
78e41f4b71Sopenharmony_ci              },
79e41f4b71Sopenharmony_ci            ],
80e41f4b71Sopenharmony_ci          },
81e41f4b71Sopenharmony_ci        ],
82e41f4b71Sopenharmony_ci      },
83e41f4b71Sopenharmony_ci    ],
84e41f4b71Sopenharmony_ci  },
85e41f4b71Sopenharmony_ci]*/
86e41f4b71Sopenharmony_ci```
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci## usb.connectDevice
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ciconnectDevice(device: USBDevice): Readonly<USBDevicePipe>
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ciConnects to a USB device.
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ciBefore you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB device list, and then call [usb.requestRight](#usbrequestright) to request the device access permission.
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci**Parameters**
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
101e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
102e41f4b71Sopenharmony_ci| device | [USBDevice](#usbdevice) | Yes| USB device information.|
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci**Return value**
105e41f4b71Sopenharmony_ci
106e41f4b71Sopenharmony_ci| Type| Description|
107e41f4b71Sopenharmony_ci| -------- | -------- |
108e41f4b71Sopenharmony_ci| Readonly<[USBDevicePipe](#usbdevicepipe)> | USB device pipe for data transfer.|
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci**Example**
111e41f4b71Sopenharmony_ci
112e41f4b71Sopenharmony_ci```js
113e41f4b71Sopenharmony_cilet devicepipe= usb.connectDevice(device);
114e41f4b71Sopenharmony_ciconsole.log(`devicepipe = ${devicepipe}`);
115e41f4b71Sopenharmony_ci```
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci## usb.hasRight
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_cihasRight(deviceName: string): boolean
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ciChecks whether the application has the permission to access the device.
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ci**Parameters**
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
128e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
129e41f4b71Sopenharmony_ci| deviceName | string | Yes| Device name.|
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ci**Return value**
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ci| Type| Description|
134e41f4b71Sopenharmony_ci| -------- | -------- |
135e41f4b71Sopenharmony_ci| boolean | Returns **true** if the application has the permission to access the device; returns **false** otherwise.|
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_ci**Example**
138e41f4b71Sopenharmony_ci
139e41f4b71Sopenharmony_ci```js
140e41f4b71Sopenharmony_cilet devicesName= "1-1";
141e41f4b71Sopenharmony_cilet bool = usb.hasRight(devicesName);
142e41f4b71Sopenharmony_ciconsole.log(`hasRight = ${bool}`);
143e41f4b71Sopenharmony_ci```
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_ci## usb.requestRight
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_cirequestRight(deviceName: string): Promise<boolean>
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ciRequests the temporary permission for the application to access a USB device. 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.
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ci**Parameters**
154e41f4b71Sopenharmony_ci
155e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
156e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
157e41f4b71Sopenharmony_ci| deviceName | string | Yes| Device name.|
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ci**Return value**
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ci| Type| Description|
162e41f4b71Sopenharmony_ci| -------- | -------- |
163e41f4b71Sopenharmony_ci| Promise<boolean> | 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.|
164e41f4b71Sopenharmony_ci
165e41f4b71Sopenharmony_ci**Example**
166e41f4b71Sopenharmony_ci
167e41f4b71Sopenharmony_ci```js
168e41f4b71Sopenharmony_cilet devicesName= "1-1";
169e41f4b71Sopenharmony_ciusb.requestRight(devicesName).then((ret) => {
170e41f4b71Sopenharmony_ci  console.log(`requestRight = ${ret}`);
171e41f4b71Sopenharmony_ci});
172e41f4b71Sopenharmony_ci```
173e41f4b71Sopenharmony_ci
174e41f4b71Sopenharmony_ci## usb.claimInterface
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ciclaimInterface(pipe: USBDevicePipe, iface: USBInterface, force ?: boolean): number
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ciClaims a USB interface.
179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_ciBefore you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB device list and USB interfaces, call [usb.requestRight](#usbrequestright) to request the device access permission, and call [usb.connectDevice](#usbconnectdevice) to obtain **devicepipe** as an input parameter.
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| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| Device pipe, which is used to determine the bus number and device address.|
189e41f4b71Sopenharmony_ci| iface | [USBInterface](#usbinterface) | Yes| USB interface, which is used to determine the index of the interface to claim.|
190e41f4b71Sopenharmony_ci| force | boolean | No| Whether to forcibly claim the USB interface. The default value is **false**, indicating not to forcibly claim the USB interface.|
191e41f4b71Sopenharmony_ci
192e41f4b71Sopenharmony_ci**Return value**
193e41f4b71Sopenharmony_ci
194e41f4b71Sopenharmony_ci| Type| Description|
195e41f4b71Sopenharmony_ci| -------- | -------- |
196e41f4b71Sopenharmony_ci| number | Returns **0** if the USB interface is successfully claimed; returns an error code otherwise.|
197e41f4b71Sopenharmony_ci
198e41f4b71Sopenharmony_ci**Example**
199e41f4b71Sopenharmony_ci
200e41f4b71Sopenharmony_ci```js
201e41f4b71Sopenharmony_cilet ret = usb.claimInterface(devicepipe, interfaces);
202e41f4b71Sopenharmony_ciconsole.log(`claimInterface = ${ret}`);
203e41f4b71Sopenharmony_ci```
204e41f4b71Sopenharmony_ci
205e41f4b71Sopenharmony_ci## usb.releaseInterface
206e41f4b71Sopenharmony_ci
207e41f4b71Sopenharmony_cireleaseInterface(pipe: USBDevicePipe, iface: USBInterface): number
208e41f4b71Sopenharmony_ci
209e41f4b71Sopenharmony_ciReleases a USB interface.
210e41f4b71Sopenharmony_ci
211e41f4b71Sopenharmony_ciBefore you do this, ensure that you have claimed the interface by calling [usb.claimInterface](#usbclaiminterface).
212e41f4b71Sopenharmony_ci
213e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
214e41f4b71Sopenharmony_ci
215e41f4b71Sopenharmony_ci**Parameters**
216e41f4b71Sopenharmony_ci
217e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
218e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
219e41f4b71Sopenharmony_ci| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| Device pipe, which is used to determine the bus number and device address.|
220e41f4b71Sopenharmony_ci| iface | [USBInterface](#usbinterface) | Yes| USB interface, which is used to determine the index of the interface to release.|
221e41f4b71Sopenharmony_ci
222e41f4b71Sopenharmony_ci**Return value**
223e41f4b71Sopenharmony_ci
224e41f4b71Sopenharmony_ci| Type| Description|
225e41f4b71Sopenharmony_ci| -------- | -------- |
226e41f4b71Sopenharmony_ci| number | Returns **0** if the USB interface is successfully released; returns an error code otherwise.|
227e41f4b71Sopenharmony_ci
228e41f4b71Sopenharmony_ci**Example**
229e41f4b71Sopenharmony_ci
230e41f4b71Sopenharmony_ci```js
231e41f4b71Sopenharmony_cilet ret = usb.releaseInterface(devicepipe, interfaces);
232e41f4b71Sopenharmony_ciconsole.log(`releaseInterface = ${ret}`);
233e41f4b71Sopenharmony_ci```
234e41f4b71Sopenharmony_ci
235e41f4b71Sopenharmony_ci## usb.setConfiguration
236e41f4b71Sopenharmony_ci
237e41f4b71Sopenharmony_cisetConfiguration(pipe: USBDevicePipe, config: USBConfig): number
238e41f4b71Sopenharmony_ci
239e41f4b71Sopenharmony_ciSets the device configuration.
240e41f4b71Sopenharmony_ci
241e41f4b71Sopenharmony_ciBefore you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB device list and device configuration, call [usb.requestRight](#usbrequestright) to request the device access permission, and call [usb.connectDevice](#usbconnectdevice) to obtain **devicepipe** as an input parameter.
242e41f4b71Sopenharmony_ci
243e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
244e41f4b71Sopenharmony_ci
245e41f4b71Sopenharmony_ci**Parameters**
246e41f4b71Sopenharmony_ci
247e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
248e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
249e41f4b71Sopenharmony_ci| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| Device pipe, which is used to determine the bus number and device address.|
250e41f4b71Sopenharmony_ci| config | [USBConfig](#usbconfig) | Yes| USB configuration to set.|
251e41f4b71Sopenharmony_ci
252e41f4b71Sopenharmony_ci**Return value**
253e41f4b71Sopenharmony_ci
254e41f4b71Sopenharmony_ci| Type| Description|
255e41f4b71Sopenharmony_ci| -------- | -------- |
256e41f4b71Sopenharmony_ci| number | Returns **0** if the USB configuration is successfully set; returns an error code otherwise.|
257e41f4b71Sopenharmony_ci
258e41f4b71Sopenharmony_ci**Example**
259e41f4b71Sopenharmony_ci
260e41f4b71Sopenharmony_ci```js
261e41f4b71Sopenharmony_cilet ret = usb.setConfiguration(devicepipe, config);
262e41f4b71Sopenharmony_ciconsole.log(`setConfiguration = ${ret}`);
263e41f4b71Sopenharmony_ci```
264e41f4b71Sopenharmony_ci
265e41f4b71Sopenharmony_ci## usb.setInterface
266e41f4b71Sopenharmony_ci
267e41f4b71Sopenharmony_cisetInterface(pipe: USBDevicePipe, iface: USBInterface): number
268e41f4b71Sopenharmony_ci
269e41f4b71Sopenharmony_ciSets a USB interface.
270e41f4b71Sopenharmony_ci
271e41f4b71Sopenharmony_ciBefore you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB device list and interfaces, call [usb.requestRight](#usbrequestright) to request the device access permission, call [usb.connectDevice](#usbconnectdevice) to obtain **devicepipe** as an input parameter, and call [usb.claimInterface](#usbclaiminterface) to claim the USB interface.
272e41f4b71Sopenharmony_ci
273e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
274e41f4b71Sopenharmony_ci
275e41f4b71Sopenharmony_ci**Parameters**
276e41f4b71Sopenharmony_ci
277e41f4b71Sopenharmony_ci| Name  | Type                             | Mandatory | Description           |
278e41f4b71Sopenharmony_ci| ----- | ------------------------------- | --- | ------------- |
279e41f4b71Sopenharmony_ci| pipe  | [USBDevicePipe](#usbdevicepipe) | Yes  | Device pipe, which is used to determine the bus number and device address.|
280e41f4b71Sopenharmony_ci| iface | [USBInterface](#usbinterface)   | Yes  | USB interface to set. |
281e41f4b71Sopenharmony_ci
282e41f4b71Sopenharmony_ci**Return value**
283e41f4b71Sopenharmony_ci
284e41f4b71Sopenharmony_ci| Type| Description|
285e41f4b71Sopenharmony_ci| -------- | -------- |
286e41f4b71Sopenharmony_ci| number | Returns **0** if the USB interface is successfully set; returns an error code otherwise.|
287e41f4b71Sopenharmony_ci
288e41f4b71Sopenharmony_ci**Example**
289e41f4b71Sopenharmony_ci
290e41f4b71Sopenharmony_ci```js
291e41f4b71Sopenharmony_cilet ret = usb.setInterface(devicepipe, interfaces);
292e41f4b71Sopenharmony_ciconsole.log(`setInterface = ${ret}`);
293e41f4b71Sopenharmony_ci```
294e41f4b71Sopenharmony_ci
295e41f4b71Sopenharmony_ci## usb.getRawDescriptor
296e41f4b71Sopenharmony_ci
297e41f4b71Sopenharmony_cigetRawDescriptor(pipe: USBDevicePipe): Uint8Array
298e41f4b71Sopenharmony_ci
299e41f4b71Sopenharmony_ciObtains the raw USB descriptor.
300e41f4b71Sopenharmony_ci
301e41f4b71Sopenharmony_ciBefore you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB device list, call [usb.requestRight](#usbrequestright) to request the device access permission, and call [usb.connectDevice](#usbconnectdevice) to obtain **devicepipe** as an input parameter.
302e41f4b71Sopenharmony_ci
303e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
304e41f4b71Sopenharmony_ci
305e41f4b71Sopenharmony_ci**Parameters**
306e41f4b71Sopenharmony_ci
307e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
308e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
309e41f4b71Sopenharmony_ci| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| Device pipe, which is used to determine the bus number and device address.|
310e41f4b71Sopenharmony_ci
311e41f4b71Sopenharmony_ci**Return value**
312e41f4b71Sopenharmony_ci
313e41f4b71Sopenharmony_ci| Type| Description|
314e41f4b71Sopenharmony_ci| -------- | -------- |
315e41f4b71Sopenharmony_ci| Uint8Array | Returns the raw USB descriptor if the operation is successful; returns **undefined** otherwise.|
316e41f4b71Sopenharmony_ci
317e41f4b71Sopenharmony_ci**Example**
318e41f4b71Sopenharmony_ci
319e41f4b71Sopenharmony_ci```js
320e41f4b71Sopenharmony_cilet ret = usb.getRawDescriptor(devicepipe);
321e41f4b71Sopenharmony_ci```
322e41f4b71Sopenharmony_ci
323e41f4b71Sopenharmony_ci## usb.getFileDescriptor
324e41f4b71Sopenharmony_ci
325e41f4b71Sopenharmony_cigetFileDescriptor(pipe: USBDevicePipe): number
326e41f4b71Sopenharmony_ci
327e41f4b71Sopenharmony_ciObtains the file descriptor.
328e41f4b71Sopenharmony_ci
329e41f4b71Sopenharmony_ciBefore you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB device list, call [usb.requestRight](#usbrequestright) to request the device access permission, and call [usb.connectDevice](#usbconnectdevice) to obtain **devicepipe** as an input parameter.
330e41f4b71Sopenharmony_ci
331e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
332e41f4b71Sopenharmony_ci
333e41f4b71Sopenharmony_ci**Parameters**
334e41f4b71Sopenharmony_ci
335e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
336e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
337e41f4b71Sopenharmony_ci| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| Device pipe, which is used to determine the bus number and device address.|
338e41f4b71Sopenharmony_ci
339e41f4b71Sopenharmony_ci**Return value**
340e41f4b71Sopenharmony_ci
341e41f4b71Sopenharmony_ci| Type    | Description                  |
342e41f4b71Sopenharmony_ci| ------ | -------------------- |
343e41f4b71Sopenharmony_ci| number | Returns the file descriptor of the USB device if the operation is successful; returns **-1** otherwise.|
344e41f4b71Sopenharmony_ci
345e41f4b71Sopenharmony_ci**Example**
346e41f4b71Sopenharmony_ci
347e41f4b71Sopenharmony_ci```js
348e41f4b71Sopenharmony_cilet ret = usb.getFileDescriptor(devicepipe);
349e41f4b71Sopenharmony_ci```
350e41f4b71Sopenharmony_ci
351e41f4b71Sopenharmony_ci## usb.controlTransfer
352e41f4b71Sopenharmony_ci
353e41f4b71Sopenharmony_cicontrolTransfer(pipe: USBDevicePipe, controlparam: USBControlParams, timeout ?: number): Promise<number>
354e41f4b71Sopenharmony_ci
355e41f4b71Sopenharmony_ciPerforms control transfer.
356e41f4b71Sopenharmony_ci
357e41f4b71Sopenharmony_ciBefore you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB device list, call [usb.requestRight](#usbrequestright) to request the device access permission, and call [usb.connectDevice](#usbconnectdevice) to obtain **devicepipe** as an input parameter.
358e41f4b71Sopenharmony_ci
359e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
360e41f4b71Sopenharmony_ci
361e41f4b71Sopenharmony_ci**Parameters**
362e41f4b71Sopenharmony_ci
363e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
364e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
365e41f4b71Sopenharmony_ci| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| USB device pipe, which is used to determine the USB device.|
366e41f4b71Sopenharmony_ci| controlparam | [USBControlParams](#usbcontrolparams) | Yes| Control transfer parameters.|
367e41f4b71Sopenharmony_ci| timeout | number | No| Timeout duration in ms. This parameter is optional. The default value is **0**, indicating no timeout.|
368e41f4b71Sopenharmony_ci
369e41f4b71Sopenharmony_ci**Return value**
370e41f4b71Sopenharmony_ci
371e41f4b71Sopenharmony_ci| Type| Description|
372e41f4b71Sopenharmony_ci| -------- | -------- |
373e41f4b71Sopenharmony_ci| Promise<number> | 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.|
374e41f4b71Sopenharmony_ci
375e41f4b71Sopenharmony_ci**Example**
376e41f4b71Sopenharmony_ci
377e41f4b71Sopenharmony_ci```js
378e41f4b71Sopenharmony_cilet param = {
379e41f4b71Sopenharmony_ci  request: 0,
380e41f4b71Sopenharmony_ci  reqType: 0,
381e41f4b71Sopenharmony_ci  target:0,
382e41f4b71Sopenharmony_ci  value: 0,
383e41f4b71Sopenharmony_ci  index: 0,
384e41f4b71Sopenharmony_ci  data: null
385e41f4b71Sopenharmony_ci};
386e41f4b71Sopenharmony_ciusb.controlTransfer(devicepipe, param).then((ret) => {
387e41f4b71Sopenharmony_ci console.log(`controlTransfer = ${ret}`);
388e41f4b71Sopenharmony_ci})
389e41f4b71Sopenharmony_ci```
390e41f4b71Sopenharmony_ci
391e41f4b71Sopenharmony_ci## usb.bulkTransfer
392e41f4b71Sopenharmony_ci
393e41f4b71Sopenharmony_cibulkTransfer(pipe: USBDevicePipe, endpoint: USBEndpoint, buffer: Uint8Array, timeout ?: number): Promise<number>
394e41f4b71Sopenharmony_ci
395e41f4b71Sopenharmony_ciPerforms bulk transfer.
396e41f4b71Sopenharmony_ci
397e41f4b71Sopenharmony_ciBefore you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB device list and endpoints, call [usb.requestRight](#usbrequestright) to request the device access permission, call [usb.connectDevice](#usbconnectdevice) to obtain **devicepipe** as an input parameter, and call [usb.claimInterface](#usbclaiminterface) to claim the USB interface.
398e41f4b71Sopenharmony_ci
399e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
400e41f4b71Sopenharmony_ci
401e41f4b71Sopenharmony_ci**Parameters**
402e41f4b71Sopenharmony_ci
403e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
404e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
405e41f4b71Sopenharmony_ci| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| USB device pipe, which is used to determine the USB device.|
406e41f4b71Sopenharmony_ci| endpoint | [USBEndpoint](#usbendpoint) | Yes| USB endpoint, which is used to determine the USB port for data transfer.|
407e41f4b71Sopenharmony_ci| buffer | Uint8Array | Yes| Buffer for writing or reading data.|
408e41f4b71Sopenharmony_ci| timeout | number | No| Timeout duration in ms. This parameter is optional. The default value is **0**, indicating no timeout.|
409e41f4b71Sopenharmony_ci
410e41f4b71Sopenharmony_ci**Return value**
411e41f4b71Sopenharmony_ci
412e41f4b71Sopenharmony_ci| Type| Description|
413e41f4b71Sopenharmony_ci| -------- | -------- |
414e41f4b71Sopenharmony_ci| Promise<number> | 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.|
415e41f4b71Sopenharmony_ci
416e41f4b71Sopenharmony_ci**Example**
417e41f4b71Sopenharmony_ci
418e41f4b71Sopenharmony_ci```js
419e41f4b71Sopenharmony_ci// Call usb.getDevices to obtain a data set. Then, obtain a USB device and its access permission.
420e41f4b71Sopenharmony_ci// Pass the obtained USB device as a parameter to usb.connectDevice. Then, call usb.connectDevice to connect the USB device.
421e41f4b71Sopenharmony_ci// Call usb.claimInterface to claim the USB interface. After that, call usb.bulkTransfer to start bulk transfer.
422e41f4b71Sopenharmony_ciusb.bulkTransfer(devicepipe, endpoint, buffer).then((ret) => {
423e41f4b71Sopenharmony_ci console.log(`bulkTransfer = ${ret}`);
424e41f4b71Sopenharmony_ci});
425e41f4b71Sopenharmony_ci```
426e41f4b71Sopenharmony_ci
427e41f4b71Sopenharmony_ci## usb.closePipe
428e41f4b71Sopenharmony_ci
429e41f4b71Sopenharmony_ciclosePipe(pipe: USBDevicePipe): number
430e41f4b71Sopenharmony_ci
431e41f4b71Sopenharmony_ciCloses a USB device pipe.
432e41f4b71Sopenharmony_ci
433e41f4b71Sopenharmony_ciBefore you do this, call [usb.getDevices](#usbgetdevices) to obtain the USB device list, call [usb.requestRight](#usbrequestright) to request the device access permission, and call [usb.connectDevice](#usbconnectdevice) to obtain **devicepipe** as an input parameter.
434e41f4b71Sopenharmony_ci
435e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
436e41f4b71Sopenharmony_ci
437e41f4b71Sopenharmony_ci**Parameters**
438e41f4b71Sopenharmony_ci
439e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
440e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
441e41f4b71Sopenharmony_ci| pipe | [USBDevicePipe](#usbdevicepipe) | Yes| USB device pipe.|
442e41f4b71Sopenharmony_ci
443e41f4b71Sopenharmony_ci**Return value**
444e41f4b71Sopenharmony_ci
445e41f4b71Sopenharmony_ci| Type| Description|
446e41f4b71Sopenharmony_ci| -------- | -------- |
447e41f4b71Sopenharmony_ci| number | Returns **0** if the USB device pipe is closed successfully; returns an error code otherwise.|
448e41f4b71Sopenharmony_ci
449e41f4b71Sopenharmony_ci**Example**
450e41f4b71Sopenharmony_ci
451e41f4b71Sopenharmony_ci```js
452e41f4b71Sopenharmony_cilet ret = usb.closePipe(devicepipe);
453e41f4b71Sopenharmony_ciconsole.log(`closePipe = ${ret}`);
454e41f4b71Sopenharmony_ci```
455e41f4b71Sopenharmony_ci
456e41f4b71Sopenharmony_ci## USBEndpoint
457e41f4b71Sopenharmony_ci
458e41f4b71Sopenharmony_ciRepresents the USB endpoint from which data is sent or received. You can obtain the USB endpoint through [USBInterface](#usbinterface).
459e41f4b71Sopenharmony_ci
460e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
461e41f4b71Sopenharmony_ci
462e41f4b71Sopenharmony_ci| Name           | Type                                       |   Mandatory     | Description           |
463e41f4b71Sopenharmony_ci| ------------- | ------------------------------------------- | ------------- |------------ |
464e41f4b71Sopenharmony_ci| address       | number                                      | Yes  |Endpoint address.        |
465e41f4b71Sopenharmony_ci| attributes    | number                                      | Yes  |Endpoint attributes.        |
466e41f4b71Sopenharmony_ci| interval      | number                                      | Yes  |Endpoint interval.        |
467e41f4b71Sopenharmony_ci| maxPacketSize | number                                      | Yes  |Maximum size of data packets on the endpoint.   |
468e41f4b71Sopenharmony_ci| direction     | [USBRequestDirection](#usbrequestdirection) | Yes  |Endpoint direction.       |
469e41f4b71Sopenharmony_ci| number        | number                                      | Yes  |Endpoint number.         |
470e41f4b71Sopenharmony_ci| type          | number                                      | Yes  |Endpoint type.        |
471e41f4b71Sopenharmony_ci| interfaceId   | number                                      | Yes  |Unique ID of the interface to which the endpoint belongs.|
472e41f4b71Sopenharmony_ci
473e41f4b71Sopenharmony_ci## USBInterface
474e41f4b71Sopenharmony_ci
475e41f4b71Sopenharmony_ciRepresents a USB interface. One [USBConfig](#usbconfig) can contain multiple **USBInterface** instances, each providing a specific function.
476e41f4b71Sopenharmony_ci
477e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
478e41f4b71Sopenharmony_ci
479e41f4b71Sopenharmony_ci| Name              | Type                                    |  Mandatory     |Description                   |
480e41f4b71Sopenharmony_ci| ---------------- | ---------------------------------------- | ------------- |--------------------- |
481e41f4b71Sopenharmony_ci| id               | number                                   | Yes  |Unique ID of the USB interface.             |
482e41f4b71Sopenharmony_ci| protocol         | number                                   | Yes  |Interface protocol.               |
483e41f4b71Sopenharmony_ci| clazz            | number                                   | Yes  |Device type.                |
484e41f4b71Sopenharmony_ci| subClass         | number                                   | Yes  |Device subclass.                |
485e41f4b71Sopenharmony_ci| alternateSetting | number                                   | Yes  |Settings for alternating between descriptors of the same USB interface.|
486e41f4b71Sopenharmony_ci| name             | string                                   | Yes  |Interface name.                |
487e41f4b71Sopenharmony_ci| endpoints        | Array<[USBEndpoint](#usbendpoint)> | Yes  |Endpoints that belong to the USB interface.          |
488e41f4b71Sopenharmony_ci
489e41f4b71Sopenharmony_ci## USBConfig
490e41f4b71Sopenharmony_ci
491e41f4b71Sopenharmony_ciRepresents the USB configuration. One [USBDevice](#usbdevice) can contain multiple **USBConfig** instances.
492e41f4b71Sopenharmony_ci
493e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
494e41f4b71Sopenharmony_ci
495e41f4b71Sopenharmony_ci| Name            | Type                                            | Mandatory  |Description             |
496e41f4b71Sopenharmony_ci| -------------- | ------------------------------------------------ | --------------- |----------- |
497e41f4b71Sopenharmony_ci| id             | number                                           | Yes  |Unique ID of the USB configuration.       |
498e41f4b71Sopenharmony_ci| attributes     | number                                           | Yes  |Configuration attributes.         |
499e41f4b71Sopenharmony_ci| maxPower       | number                                           | Yes  |Maximum power consumption, in mA.   |
500e41f4b71Sopenharmony_ci| name           | string                                           | Yes  |Configuration name, which can be left empty.    |
501e41f4b71Sopenharmony_ci| isRemoteWakeup | boolean                                          | Yes  |Support for remote wakeup.|
502e41f4b71Sopenharmony_ci| isSelfPowered  | boolean                                          | Yes  |Support for independent power supplies.|
503e41f4b71Sopenharmony_ci| interfaces     | Array <[USBInterface](#usbinterface)> | Yes  |Supported interface attributes.     |
504e41f4b71Sopenharmony_ci
505e41f4b71Sopenharmony_ci## USBDevice
506e41f4b71Sopenharmony_ci
507e41f4b71Sopenharmony_ciRepresents the USB device information.
508e41f4b71Sopenharmony_ci
509e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
510e41f4b71Sopenharmony_ci
511e41f4b71Sopenharmony_ci| Name              | Type                                | Mandatory  |Description        |
512e41f4b71Sopenharmony_ci| ---------------- | ------------------------------------ | ---------- |---------- |
513e41f4b71Sopenharmony_ci| busNum           | number                               | Yes  |Bus address.     |
514e41f4b71Sopenharmony_ci| devAddress       | number                               | Yes  |Device address.     |
515e41f4b71Sopenharmony_ci| serial           | string                               | Yes  |Sequence number.      |
516e41f4b71Sopenharmony_ci| name             | string                               | Yes  |Device name.     |
517e41f4b71Sopenharmony_ci| manufacturerName | string                               | Yes  |Device manufacturer.     |
518e41f4b71Sopenharmony_ci| productName      | string                               | Yes  |Product name.     |
519e41f4b71Sopenharmony_ci| version          | string                               | Yes  |Version.       |
520e41f4b71Sopenharmony_ci| vendorId         | number                               | Yes  |Vendor ID.     |
521e41f4b71Sopenharmony_ci| productId        | number                               | Yes  |Product ID.     |
522e41f4b71Sopenharmony_ci| clazz            | number                               | Yes  |Device class.      |
523e41f4b71Sopenharmony_ci| subClass         | number                               | Yes  |Device subclass.     |
524e41f4b71Sopenharmony_ci| protocol         | number                               | Yes  |Device protocol code.    |
525e41f4b71Sopenharmony_ci| configs          | Array<[USBConfig](#usbconfig)> | Yes  |Device configuration descriptor information.|
526e41f4b71Sopenharmony_ci
527e41f4b71Sopenharmony_ci## USBDevicePipe
528e41f4b71Sopenharmony_ci
529e41f4b71Sopenharmony_ciRepresents a USB device pipe, which is used to determine a USB device.
530e41f4b71Sopenharmony_ci
531e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
532e41f4b71Sopenharmony_ci
533e41f4b71Sopenharmony_ci| Name      | Type  | Mandatory |Description   |
534e41f4b71Sopenharmony_ci| ---------- | ------ | ----- |----- |
535e41f4b71Sopenharmony_ci| busNum     | number | Yes  |Bus address.|
536e41f4b71Sopenharmony_ci| devAddress | number | Yes  |Device address.|
537e41f4b71Sopenharmony_ci
538e41f4b71Sopenharmony_ci## USBControlParams
539e41f4b71Sopenharmony_ci
540e41f4b71Sopenharmony_ciRepresents control transfer parameters.
541e41f4b71Sopenharmony_ci
542e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
543e41f4b71Sopenharmony_ci
544e41f4b71Sopenharmony_ci| Name     | Type                                           | Mandatory|Description              |
545e41f4b71Sopenharmony_ci| ------- | ----------------------------------------------- | ---------------- |---------------- |
546e41f4b71Sopenharmony_ci| request | number                                          | Yes  |Request type.           |
547e41f4b71Sopenharmony_ci| target  | [USBRequestTargetType](#usbrequesttargettype)   | Yes  |Request target type.         |
548e41f4b71Sopenharmony_ci| reqType | [USBControlRequestType](#usbcontrolrequesttype) | Yes  |Control request type.         |
549e41f4b71Sopenharmony_ci| value   | number                                          | Yes  |Request parameter value.           |
550e41f4b71Sopenharmony_ci| index   | number                                          | Yes  |Index of the request parameter value.|
551e41f4b71Sopenharmony_ci| data    | Uint8Array                                      | Yes  |Buffer for writing or reading data.    |
552e41f4b71Sopenharmony_ci
553e41f4b71Sopenharmony_ci
554e41f4b71Sopenharmony_ci## USBRequestTargetType
555e41f4b71Sopenharmony_ci
556e41f4b71Sopenharmony_ciEnumerates request target types.
557e41f4b71Sopenharmony_ci
558e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
559e41f4b71Sopenharmony_ci
560e41f4b71Sopenharmony_ci| Name                        | Value  | Description  |
561e41f4b71Sopenharmony_ci| ---------------------------- | ---- | ------ |
562e41f4b71Sopenharmony_ci| USB_REQUEST_TARGET_DEVICE    | 0    | Device.|
563e41f4b71Sopenharmony_ci| USB_REQUEST_TARGET_INTERFACE | 1    | Interface.|
564e41f4b71Sopenharmony_ci| USB_REQUEST_TARGET_ENDPOINT  | 2    | Endpoint.|
565e41f4b71Sopenharmony_ci| USB_REQUEST_TARGET_OTHER     | 3    | Other.|
566e41f4b71Sopenharmony_ci
567e41f4b71Sopenharmony_ci## USBControlRequestType
568e41f4b71Sopenharmony_ci
569e41f4b71Sopenharmony_ciEnumerates control request types.
570e41f4b71Sopenharmony_ci
571e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
572e41f4b71Sopenharmony_ci
573e41f4b71Sopenharmony_ci| Name                     | Value  | Description  |
574e41f4b71Sopenharmony_ci| ------------------------- | ---- | ------ |
575e41f4b71Sopenharmony_ci| USB_REQUEST_TYPE_STANDARD | 0    | Standard.|
576e41f4b71Sopenharmony_ci| USB_REQUEST_TYPE_CLASS    | 1    | Class.  |
577e41f4b71Sopenharmony_ci| USB_REQUEST_TYPE_VENDOR   | 2    | Vendor.|
578e41f4b71Sopenharmony_ci
579e41f4b71Sopenharmony_ci## USBRequestDirection
580e41f4b71Sopenharmony_ci
581e41f4b71Sopenharmony_ciEnumerates request directions.
582e41f4b71Sopenharmony_ci
583e41f4b71Sopenharmony_ci**System capability**: SystemCapability.USB.USBManager
584e41f4b71Sopenharmony_ci
585e41f4b71Sopenharmony_ci| Name                       | Value  | Description                    |
586e41f4b71Sopenharmony_ci| --------------------------- | ---- | ------------------------ |
587e41f4b71Sopenharmony_ci| USB_REQUEST_DIR_TO_DEVICE   | 0    | Request for writing data from the host to the device.|
588e41f4b71Sopenharmony_ci| USB_REQUEST_DIR_FROM_DEVICE | 0x80 | Request for reading data from the device to the host.|
589