From cfe593aa4e3b43413f9f75b34868dfa609263624 Mon Sep 17 00:00:00 2001 From: shawn_he Date: Mon, 7 Mar 2022 18:01:16 +0800 Subject: [PATCH] update docs Signed-off-by: shawn_he --- .../reference/apis/js-apis-usb.md | 1867 +++++------------ 1 file changed, 491 insertions(+), 1376 deletions(-) diff --git a/en/application-dev/reference/apis/js-apis-usb.md b/en/application-dev/reference/apis/js-apis-usb.md index 94c9f4a7a5..f51e459ac3 100644 --- a/en/application-dev/reference/apis/js-apis-usb.md +++ b/en/application-dev/reference/apis/js-apis-usb.md @@ -1,1432 +1,547 @@ -# USB +# USB ->**NOTE:** ->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. +> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE** +> 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. -## Modules to Import +## Modules to Import ``` import usb from "@ohos.usb"; ``` -## Required Permissions +## usb.getDevices -None. - -## usb.getDevices - -usb.getDevices\(\): Array\> +usb.getDevices(): Array<Readonly<USBDevice>> Obtains the USB device list. -- Return values - - - - - - - - - - -

Type

-

Description

-

Array<Readonly<USBDevice>>

-

USB device list.

-
- -- Example - - ``` - let devicesList = usb.getDevices(); - console.log(`devicesList = ${JSON.stringify(devicesList)}`); - // devicesList is a list of USB devices. - // A simple example of devicesList is provided as follows: - [ - { - name: "1-1", - serial: "", - manufacturerName: "", - productName: "", - version: "", - vendorId: 7531, - productId: 2, - clazz: 9, - subclass: 0, - protocol: 1, - devAddress: 1, - busNum: 1, - configs: [ - { - id: 1, - attributes: 224, - isRemoteWakeup: true, - isSelfPowered: true, - maxPower: 0, - name: "1-1", - interfaces: [ - { - id: 0, - protocol: 0, - clazz: 9, - subclass: 0, - alternateSetting: 0, - name: "1-1", - endpoints: [ - { - address: 129, - attributes: 3, - interval: 12, - maxPacketSize: 4, - direction: 128, - number: 1, - type: 3, - interfaceId: 0, - }, - ], - }, - ], - }, - ], - }, - ] - ``` - - -## usb.connectDevice - -usb.connectDevice\(device: USBDevice\): Readonly +**System capability**: SystemCapability.USB.USBManager + +- Return value + | Type| Description| + | -------- | -------- | + | Array<Readonly<[USBDevice](#usbdevice)>> | Device information list.| + +- Example + ``` + let devicesList = usb.getDevices(); + console.log(`devicesList = ${JSON.stringify(devicesList)}`); + // devicesList is a list of USB devices. + // A simple example of devicesList is provided as follows: + [ + { + name: "1-1", + serial: "", + manufacturerName: "", + productName: "", + version: "", + vendorId: 7531, + productId: 2, + clazz: 9, + subclass: 0, + protocol: 1, + devAddress: 1, + busNum: 1, + configs: [ + { + id: 1, + attributes: 224, + isRemoteWakeup: true, + isSelfPowered: true, + maxPower: 0, + name: "1-1", + interfaces: [ + { + id: 0, + protocol: 0, + clazz: 9, + subclass: 0, + alternateSetting: 0, + name: "1-1", + endpoints: [ + { + address: 129, + attributes: 3, + interval: 12, + maxPacketSize: 4, + direction: 128, + number: 1, + type: 3, + interfaceId: 0, + }, + ], + }, + ], + }, + ], + }, + ] + ``` + + +## usb.connectDevice + +usb.connectDevice(device: USBDevice): Readonly<USBDevicePipe> Connects to a USB device. -Before you do this, call [usb.getDevices](#section1885592111287) to obtain the USB device list, and then call [usb.requestRight](#section1865915394353) to request the device access permission. - -- Parameters - - - - - - - - - - - - - - -

Name

-

Type

-

Mandatory

-

Description

-

device

-

USBDevice

-

Yes

-

USB device information.

-
- - -- Return values - - - - - - - - - - -

Type

-

Description

-

Readonly<USBDevicePipe>

-

USB device pipe for data transfer.

-
- -- Example - - ``` - let devicepipe= usb.connectDevice(device); - console.log(`devicepipe = ${JSON.stringify(devicepipe)}`); - ``` - - -## usb.hasRight - -usb.hasRight\(deviceName: string\): boolean - -Checks whether the user has the permission to access the device. - -- Parameters - - - - - - - - - - - - - - -

Name

-

Type

-

Mandatory

-

Description

-

deviceName

-

string

-

Yes

-

Device name.

-
- -- Return values - - - - - - - - - - -

Type

-

Description

-

boolean

-

Returns true if the user has the permission to access the device; returns false otherwise.

-
- -- Example - - ``` - let divicesName="1-1"; - let bool = usb.hasRight(divicesName); - console.log(bool); - ``` - - -## usb.requestRight - -usb.requestRight\(deviceName: string\): Promise +Before 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. + +**System capability**: SystemCapability.USB.USBManager + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | device | [USBDevice](#usbdevice) | Yes| USB device information.| + +- Return value + | Type| Description| + | -------- | -------- | + | Readonly<[USBDevicePipe](#usbdevicepipe)> | USB device pipe for data transfer.| + +- Example + ``` + let devicepipe= usb.connectDevice(device); + console.log(`devicepipe = ${JSON.stringify(devicepipe)}`); + ``` + + +## usb.hasRight + +usb.hasRight(deviceName: string): boolean + +Checks whether the application has the permission to access the device. + +**System capability**: SystemCapability.USB.USBManager + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | deviceName | string | Yes| Device name.| + +- Return value + | Type| Description| + | -------- | -------- | + | boolean | Returns **true** if the application has the permission to access the device; returns **false** otherwise.| + +- Example + ``` + let divicesName="1-1"; + let bool = usb.hasRight(divicesName); + console.log(bool); + ``` + + +## usb.requestRight + +usb.requestRight(deviceName: string): Promise<boolean> Requests the temporary permission for the application to access the USB device. -- Parameters - - - - - - - - - - - - - - -

Name

-

Type

-

Mandatory

-

Description

-

deviceName

-

string

-

Yes

-

Device name.

-
- -- Return values - - - - - - - - - - -

Type

-

Description

-

Promise<boolean>

-

Returns true if the temporary device access permissions are granted; returns false otherwise.

-
- -- Example - - ``` - let divicesName="1-1"; - usb.requestRight(divicesName).then((ret) => { - console.log(`requestRight = ${JSON.stringify(ret)}`); - }); - ``` - - -## usb.claimInterface - -usb.claimInterface\(pipe: USBDevicePipe, iface: USBInterface, force?: boolean\): number +**System capability**: SystemCapability.USB.USBManager + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | deviceName | string | Yes| Device name.| + +- Return value + | Type| Description| + | -------- | -------- | + | Promise<boolean> | Returns **true** if the temporary device access permissions are granted; returns **false** otherwise.| + +- Example + ``` + let divicesName="1-1"; + usb.requestRight(divicesName).then((ret) => { + console.log(`requestRight = ${JSON.stringify(ret)}`); + }); + ``` + + +## usb.claimInterface + +usb.claimInterface(pipe: USBDevicePipe, iface: USBInterface, force?: boolean): number Claims a USB interface. -Before you do this, call [usb.getDevices](#section1885592111287) to obtain the USB device list and USB interfaces, call [usb.requestRight](#section1865915394353) to request the device access permission, and call [usb.connectDevice](#section910254722918) to obtain **devicepipe** as an input parameter. - -- Parameters - - - - - - - - - - - - - - - - - - - - - - - - -

Name

-

Type

-

Mandatory

-

Description

-

pipe

-

USBDevicePipe

-

Yes

-

Device pipe, which is used to determine the bus number and device address.

-

iface

-

USBInterface

-

Yes

-

USB interface, which is used to determine the index of the interface to claim.

-

force

-

boolean

-

No

-

Optional parameter that determines whether to forcibly claim the USB interface. The default value is false, indicating not to forcibly claim the USB interface.

-
- -- Return values - - - - - - - - - - -

Type

-

Description

-

number

-

Returns 0 if the USB interface is successfully claimed; returns an error code otherwise.

-
- -- Example - - ``` - let ret = usb.claimInterface(devicepipe, interfaces); - console.log(`claimInterface = ${ret}`); - ``` - - -## usb.releaseInterface - -usb.releaseInterface\(pipe: USBDevicePipe, iface: USBInterface\): number +Before 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. + +**System capability**: SystemCapability.USB.USBManager + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | pipe | [USBDevicePipe](#usbdevicepipe) | Yes| Device pipe, which is used to determine the bus number and device address.| + | iface | [USBInterface](#usbinterface) | Yes| USB interface, which is used to determine the index of the interface to claim.| + | force | boolean | No| Whether to forcibly claim the USB interface. The default value is **false**, indicating not to forcibly claim the USB interface.| + +- Return value + | Type| Description| + | -------- | -------- | + | number | Returns **0** if the USB interface is successfully claimed; returns an error code otherwise.| + +- Example + ``` + let ret = usb.claimInterface(devicepipe, interfaces); + console.log(`claimInterface = ${ret}`); + ``` + + +## usb.releaseInterface + +usb.releaseInterface(pipe: USBDevicePipe, iface: USBInterface): number Releases a USB interface. -Before you do this, ensure that you have claimed the interface by calling [usb.claimInterface](#section41056254494). - -- Parameters - - - - - - - - - - - - - - - - - - - -

Name

-

Type

-

Mandatory

-

Description

-

pipe

-

USBDevicePipe

-

Yes

-

Device pipe, which is used to determine the bus number and device address.

-

iface

-

USBInterface

-

Yes

-

USB interface, which is used to determine the index of the interface to release.

-
- -- Return values - - - - - - - - - - -

Type

-

Description

-

number

-

Returns 0 if the USB interface is successfully released; returns an error code otherwise.

-
- -- Example - - ``` - let ret = usb.releaseInterface(devicepipe, interfaces); - console.log(`releaseInterface = ${ret}`); - ``` - - -## usb.setConfiguration - -usb.setConfiguration\(pipe: USBDevicePipe, config: USBConfig\): number +Before you do this, ensure that you have claimed the interface by calling [usb.claimInterface](#usbclaiminterface). + +**System capability**: SystemCapability.USB.USBManager + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | pipe | [USBDevicePipe](#usbdevicepipe) | Yes| Device pipe, which is used to determine the bus number and device address.| + | iface | [USBInterface](#usbinterface) | Yes| USB interface, which is used to determine the index of the interface to release.| + +- Return value + | Type| Description| + | -------- | -------- | + | number | Returns **0** if the USB interface is successfully released; returns an error code otherwise.| + +- Example + ``` + let ret = usb.releaseInterface(devicepipe, interfaces); + console.log(`releaseInterface = ${ret}`); + ``` + + +## usb.setConfiguration + +usb.setConfiguration(pipe: USBDevicePipe, config: USBConfig): number Sets the device configuration. -Before you do this, call [usb.getDevices](#section1885592111287) to obtain the USB device list and device configuration, call [usb.requestRight](#section1865915394353) to request the device access permission, and call [usb.connectDevice](#section910254722918) to obtain **devicepipe** as an input parameter. - -- Parameters - - - - - - - - - - - - - - - - - - - -

Name

-

Type

-

Mandatory

-

Description

-

pipe

-

USBDevicePipe

-

Yes

-

Device pipe, which is used to determine the bus number and device address.

-

config

-

USBConfig

-

Yes

-

USB configuration to set.

-
- -- Return values - - - - - - - - - - -

Type

-

Description

-

number

-

Returns 0 if the USB configuration is successfully set; returns an error code otherwise.

-
- -- Example - - ``` - let ret = usb.setConfiguration(devicepipe, config); - console.log(`setConfiguration = ${ret}`); - ``` - - -## usb.setInterface - -usb.setInterface\(pipe: USBDevicePipe, iface: USBInterface\): number +Before 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. + +**System capability**: SystemCapability.USB.USBManager + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | pipe | [USBDevicePipe](#usbdevicepipe) | Yes| Device pipe, which is used to determine the bus number and device address.| + | config | [USBConfig](#usbconfig) | Yes| USB configuration to set.| + +- Return value + | Type| Description| + | -------- | -------- | + | number | Returns **0** if the USB configuration is successfully set; returns an error code otherwise.| + +- Example + ``` + let ret = usb.setConfiguration(devicepipe, config); + console.log(`setConfiguration = ${ret}`); + ``` + + +## usb.setInterface + +usb.setInterface(pipe: USBDevicePipe, iface: USBInterface): number Sets a USB interface. -Before you do this, call [usb.getDevices](#section1885592111287) to obtain the USB device list and interfaces, call [usb.requestRight](#section1865915394353) to request the device access permission, and call [usb.connectDevice](#section910254722918) to obtain **devicepipe** as an input parameter. - -- Parameters - - - - - - - - - - - - - - - - - - - -

Name

-

Type

-

Mandatory

-

Description

-

pipe

-

USBDevicePipe

-

Yes

-

Device pipe, which is used to determine the bus number and device address.

-

iface

-

USBInterface

-

Yes

-

USB interface to set.

-
- -- Return values - - - - - - - - - - -

Type

-

Description

-

number

-

Returns 0 if the USB interface is successfully set; returns an error code otherwise.

-
- -- Example - - ``` - let ret = usb.setInterface(devicepipe, interfaces); - console.log(`setInterface = ${ret}`); - ``` - - -## usb.getRawDescriptor - -usb.getRawDescriptor\(pipe: USBDevicePipe\): Uint8Array +Before 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, and call [usb.connectDevice](#usbconnectdevice) to obtain **devicepipe** as an input parameter. + +**System capability**: SystemCapability.USB.USBManager + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | pipe | [USBDevicePipe](#usbdevicepipe) | Yes| Device pipe, which is used to determine the bus number and device address.| + | iface | [USBInterface](#usbinterface) | Yes| USB interface to set.| + +- Return value + | Type| Description| + | -------- | -------- | + | number | Returns **0** if the USB interface is successfully set; returns an error code otherwise.| + +- Example + ``` + let ret = usb.setInterface(devicepipe, interfaces); + console.log(`setInterface = ${ret}`); + ``` + + +## usb.getRawDescriptor + +usb.getRawDescriptor(pipe: USBDevicePipe): Uint8Array Obtains the raw USB descriptor. -Before you do this, call [usb.getDevices](#section1885592111287) to obtain the USB device list, call [usb.requestRight](#section1865915394353) to request the device access permission, and call [usb.connectDevice](#section910254722918) to obtain **devicepipe** as an input parameter. - -- Parameters - - - - - - - - - - - - - - -

Name

-

Type

-

Mandatory

-

Description

-

pipe

-

USBDevicePipe

-

Yes

-

Device pipe, which is used to determine the bus number and device address.

-
- -- Return values - - - - - - - - - - -

Type

-

Description

-

Uint8Array

-

Raw descriptor data.

-
- -- Example - - ``` - let ret = usb.getRawDescriptor(devicepipe); - ``` - - -## usb.getFileDescriptor - -usb.getFileDescriptor\(pipe: USBDevicePipe\): number +Before 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. + +**System capability**: SystemCapability.USB.USBManager + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | pipe | [USBDevicePipe](#usbdevicepipe) | Yes| Device pipe, which is used to determine the bus number and device address.| + +- Return value + | Type| Description| + | -------- | -------- | + | Uint8Array | Raw descriptor data.| + +- Example + ``` + let ret = usb.getRawDescriptor(devicepipe); + ``` + + +## usb.getFileDescriptor + +usb.getFileDescriptor(pipe: USBDevicePipe): number Obtains the file descriptor. -Before you do this, call [usb.getDevices](#section1885592111287) to obtain the USB device list, call [usb.requestRight](#section1865915394353) to request the device access permission, and call [usb.connectDevice](#section910254722918) to obtain **devicepipe** as an input parameter. - -- Parameters - - - - - - - - - - - - - - -

Name

-

Type

-

Mandatory

-

Description

-

pipe

-

USBDevicePipe

-

Yes

-

Device pipe, which is used to determine the bus number and device address.

-
- -- Return values - - - - - - - - - - -

Type

-

Description

-

number

-

File descriptor of the USB device.

-
- -- Example - - ``` - let ret = usb.getFileDescriptor(devicepipe); - ``` - - -## usb.controlTransfer - -usb.controlTransfer\(pipe: USBDevicePipe, contrlparam: USBControlParams, timeout?: number\): Promise +Before 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. + +**System capability**: SystemCapability.USB.USBManager + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | pipe | [USBDevicePipe](#usbdevicepipe) | Yes| Device pipe, which is used to determine the bus number and device address.| + +- Return value + | Type| Description| + | -------- | -------- | + | number | File descriptor of the USB device.| + +- Example + ``` + let ret = usb.getFileDescriptor(devicepipe); + ``` + + +## usb.controlTransfer + +usb.controlTransfer(pipe: USBDevicePipe, contrlparam: USBControlParams, timeout?: number): Promise<number> Performs control transfer. -Before you do this, call [usb.getDevices](#section1885592111287) to obtain the USB device list, call [usb.requestRight](#section1865915394353) to request the device access permission, and call [usb.connectDevice](#section910254722918) to obtain **devicepipe** as an input parameter. - -- Parameters - - - - - - - - - - - - - - - - - - - - - - - - -

Name

-

Type

-

Mandatory

-

Description

-

pipe

-

USBDevicePipe

-

Yes

-

USB device pipe, which is used to determine the USB device.

-

contrlparam

-

USBControlParams

-

Yes

-

Control transfer parameters.

-

timeout

-

number

-

No

-

Timeout duration. This parameter is optional. The default value is 0, indicating no timeout.

-
- -- Return values - - - - - - - - - - -

Type

-

Description

-

Promise<number>

-

Returns the size of the transmitted or received data block if the control transfer is successful; returns -1 if an exception occurs.

-
- -- Example - - ``` - usb.controlTransfer(devicepipe, USBControlParams).then((ret) => { - console.log(`controlTransfer = ${JSON.stringify(ret)}`); - }) - ``` - - -## usb.bulkTransfer - -usb.bulkTransfer\(pipe: USBDevicePipe, endpoint: USBEndpoint, buffer: Uint8Array, timeout?: number\): Promise +Before 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. + +**System capability**: SystemCapability.USB.USBManager + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | pipe | [USBDevicePipe](#usbdevicepipe) | Yes| USB device pipe, which is used to determine the USB device.| + | contrlparam | [USBControlParams](#usbcontrolparams) | Yes| Control transfer parameters.| + | timeout | number | No| Timeout duration. The default value is **0**, indicating no timeout.| + +- Return value + | Type| Description| + | -------- | -------- | + | Promise<number> | Returns the size of the transmitted or received data block if the control transfer is successful; returns **-1** if an exception occurs.| + +- Example + ``` + usb.controlTransfer(devicepipe, USBControlParams).then((ret) => { + console.log(`controlTransfer = ${JSON.stringify(ret)}`); + }) + ``` + + +## usb.bulkTransfer + +usb.bulkTransfer(pipe: USBDevicePipe, endpoint: USBEndpoint, buffer: Uint8Array, timeout?: number): Promise<number> Performs bulk transfer. -Before you do this, call [usb.getDevices](#section1885592111287) to obtain the USB device list and endpoints, call [usb.requestRight](#section1865915394353) to request the device access permission, call [usb.connectDevice](#section910254722918) to obtain **devicepipe** as an input parameter, and call [usb.claimInterface](#section41056254494) to claim the USB interface. - -- Parameters - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Name

-

Type

-

Mandatory

-

Description

-

pipe

-

USBDevicePipe

-

Yes

-

USB device pipe, which is used to determine the USB device.

-

endpoint

-

USBEndpoint

-

Yes

-

USB endpoint, which is used to determine the USB port for data transfer.

-

buffer

-

Uint8Array

-

Yes

-

Buffer for writing or reading data.

-

timeout

-

number

-

No

-

Timeout duration. This parameter is optional. The default value is 0, indicating no timeout.

-
- -- Return values - - - - - - - - - - -

Type

-

Description

-

Promise<number>

-

Returns the size of the transmitted or received data block if the control transfer is successful; returns -1 if an exception occurs.

-
- -- Example - - ``` - // Call usb.getDevices to obtain a data set. Then, obtain a USB device and its access permission. - // Pass the obtained USB device as a parameter to usb.connectDevice. Then, call usb.connectDevice to connect the USB device. - // Call usb.claimInterface to claim the USB interface. After that, call usb.bulkTransfer to start bulk transfer. - usb.bulkTransfer(devicepipe, endpoint, buffer).then((ret) => { - console.log(`bulkTransfer = ${JSON.stringify(ret)}`); - }); - ``` - - -## usb.closePipe - -usb.closePipe\(pipe: USBDevicePipe\): number +Before 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. + +**System capability**: SystemCapability.USB.USBManager + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | pipe | [USBDevicePipe](#usbdevicepipe) | Yes| USB device pipe, which is used to determine the USB device.| + | endpoint | [USBEndpoint](#usbendpoint) | Yes| USB endpoint, which is used to determine the USB port for data transfer.| + | buffer | Uint8Array | Yes| Buffer for writing or reading data.| + | timeout | number | No| Timeout duration. The default value is **0**, indicating no timeout.| + +- Return value + | Type| Description| + | -------- | -------- | + | Promise<number> | Returns the size of the transmitted or received data block if the control transfer is successful; returns **-1** if an exception occurs.| + +- Example + ``` + // Call usb.getDevices to obtain a data set. Then, obtain a USB device and its access permission. + // Pass the obtained USB device as a parameter to usb.connectDevice. Then, call usb.connectDevice to connect the USB device. + // Call usb.claimInterface to claim the USB interface. After that, call usb.bulkTransfer to start bulk transfer. + usb.bulkTransfer(devicepipe, endpoint, buffer).then((ret) => { + console.log(`bulkTransfer = ${JSON.stringify(ret)}`); + }); + ``` + + +## usb.closePipe + +usb.closePipe(pipe: USBDevicePipe): number Closes a USB device pipe. -Before you do this, call [usb.getDevices](#section1885592111287) to obtain the USB device list, call [usb.requestRight](#section1865915394353) to request the device access permission, and call [usb.connectDevice](#section910254722918) to obtain **devicepipe** as an input parameter. - -- Parameters - - - - - - - - - - - - - - -

Name

-

Type

-

Mandatory

-

Description

-

pipe

-

USBDevicePipe

-

Yes

-

USB device pipe.

-
- -- Return values - - - - - - - - - - -

Type

-

Description

-

number

-

Returns 0 if the USB device pipe is closed successfully; returns an error code otherwise.

-
- -- Example - - ``` - let ret = usb.closePipe(devicepipe); - console.log(`closePipe = ${ret}`); - ``` - - -## USBEndpoint - -Represents the USB endpoint from which data is sent or received. You can obtain the USB endpoint through [USBInterface](#section1564414611311). - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Name

-

Type

-

Description

-

address

-

number

-

Endpoint address.

-

attributes

-

number

-

Endpoint attributes.

-

interval

-

number

-

Endpoint interval.

-

maxPacketSize

-

number

-

Maximum size of data packets on the endpoint.

-

direction

-

USBRequestDirection

-

Endpoint direction.

-

number

-

number

-

Endpoint number.

-

type

-

number

-

Endpoint type.

-

interfaceId

-

number

-

Unique ID of the interface to which the endpoint belongs.

-
- -## USBInterface - -Represents a USB interface. One [USBConfig](#section1172111051715) can contain multiple **USBInterface** instances, each providing a specific function. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Name

-

Type

-

Description

-

id

-

number

-

Unique ID of the USB interface.

-

protocol

-

number

-

Interface protocol.

-

clazz

-

number

-

Device type.

-

subClass

-

number

-

Device subclass.

-

alternateSetting

-

number

-

Settings for alternating between descriptors of the same USB interface.

-

name

-

string

-

Interface name.

-

endpoints

-

Array<USBEndpoint>

-

Endpoints that belong to the USB interface.

-
- -## USBConfig - -Represents the USB configuration. One [USBDevice](#section14936845182012) can contain multiple **USBConfig** instances. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Name

-

Type

-

Description

-

id

-

number

-

Unique ID of the USB configuration.

-

attributes

-

number

-

Configuration attributes.

-

maxPower

-

number

-

Maximum power consumption, in mA.

-

name

-

string

-

Configuration name, which can be left empty.

-

isRemoteWakeup

-

boolean

-

Support for remote wakeup.

-

isSelfPowered

-

boolean

-

Support for independent power supplies.

-

interfaces

-

Array <USBInterface>

-

Supported interface attributes.

-
- -## USBDevice - -Represents a USB device. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Name

-

Type

-

Description

-

busNum

-

number

-

Bus address.

-

devAddress

-

number

-

Device address.

-

serial

-

string

-

Device SN.

-

name

-

string

-

Device name.

-

manufacturerName

-

string

-

Device manufacturer.

-

productName

-

string

-

Product name.

-

version

-

string

-

Product version.

-

vendorId

-

number

-

Vendor ID.

-

productId

-

number

-

Product ID.

-

clazz

-

number

-

Device class.

-

subClass

-

number

-

Device subclass.

-

protocol

-

number

-

Device protocol code.

-

configs

-

Array<USBConfig>

-

Device configuration descriptor information.

-
- -## USBDevicePipe +Before 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. + +**System capability**: SystemCapability.USB.USBManager + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | pipe | [USBDevicePipe](#usbdevicepipe) | Yes| USB device pipe.| + +- Return value + | Type| Description| + | -------- | -------- | + | number | Returns **0** if the USB device pipe is closed successfully; returns an error code otherwise.| + +- Example + ``` + let ret = usb.closePipe(devicepipe); + console.log(`closePipe = ${ret}`); + ``` + + +## USBEndpoint + +Represents the USB endpoint from which data is sent or received. You can obtain the USB endpoint through [USBInterface](#usbinterface). + +| Name| Type| Description| +| -------- | -------- | -------- | +| address | number | Endpoint address.
**System capability**: SystemCapability.USB.USBManager| +| attributes | number | Endpoint attributes.
**System capability**: SystemCapability.USB.USBManager| +| interval | number | Endpoint interval.
**System capability**: SystemCapability.USB.USBManager| +| maxPacketSize | number | Maximum size of data packets on the endpoint.
**System capability**: SystemCapability.USB.USBManager| +| direction | [USBRequestDirection](#usbrequestdirection) | Endpoint direction.
**System capability**: SystemCapability.USB.USBManager| +| number | number | Endpoint number.
**System capability**: SystemCapability.USB.USBManager| +| type | number | Endpoint type.
**System capability**: SystemCapability.USB.USBManager| +| interfaceId | number | Unique ID of the interface to which the endpoint belongs.
**System capability**: SystemCapability.USB.USBManager| + + +## USBInterface + +Represents a USB interface. One [USBConfig](#usbconfig) can contain multiple **USBInterface** instances, each providing a specific function. + +| Name| Type| Description| +| -------- | -------- | -------- | +| id | number | Unique ID of the USB interface.
**System capability**: SystemCapability.USB.USBManager| +| protocol | number | Interface protocol.
**System capability**: SystemCapability.USB.USBManager| +| clazz | number | Device type.
**System capability**: SystemCapability.USB.USBManager| +| subClass | number | Device subclass.
**System capability**: SystemCapability.USB.USBManager| +| alternateSetting | number | Settings for alternating between descriptors of the same USB interface.
**System capability**: SystemCapability.USB.USBManager| +| name | string | Interface name.
**System capability**: SystemCapability.USB.USBManager| +| endpoints | Array<[USBEndpoint](#usbendpoint)> | Endpoints that belong to the USB interface.
**System capability**: SystemCapability.USB.USBManager| + + +## USBConfig + +Represents the USB configuration. One [USBDevice](#usbdevice) can contain multiple **USBConfig** instances. + +| Name| Type| Description| +| -------- | -------- | -------- | +| id | number | Unique ID of the USB configuration.
**System capability**: SystemCapability.USB.USBManager| +| attributes | number | Configuration attributes.
**System capability**: SystemCapability.USB.USBManager| +| maxPower | number | Maximum power consumption, in mA.
**System capability**: SystemCapability.USB.USBManager| +| name | string | Configuration name, which can be left empty.
**System capability**: SystemCapability.USB.USBManager| +| isRemoteWakeup | boolean | Support for remote wakeup.
**System capability**: SystemCapability.USB.USBManager| +| isSelfPowered | boolean | Support for independent power supplies.
**System capability**: SystemCapability.USB.USBManager| +| interfaces | Array <[USBInterface](#usbinterface)> | Supported interface attributes.
**System capability**: SystemCapability.USB.USBManager| + + +## USBDevice + +Represents the USB device information. + +| Name| Type| Description| +| -------- | -------- | -------- | +| busNum | number | Bus address.
**System capability**: SystemCapability.USB.USBManager| +| devAddress | number | Device address.
**System capability**: SystemCapability.USB.USBManager| +| serial | string | Device SN.
**System capability**: SystemCapability.USB.USBManager| +| name | string | Device name.
**System capability**: SystemCapability.USB.USBManager| +| manufacturerName | string | Device manufacturer.
**System capability**: SystemCapability.USB.USBManager| +| productName | string | Product information.
**System capability**: SystemCapability.USB.USBManager| +| version | string | Version.
**System capability**: SystemCapability.USB.USBManager| +| vendorId | number | Vendor ID.
**System capability**: SystemCapability.USB.USBManager| +| productId | number | Product ID.
**System capability**: SystemCapability.USB.USBManager| +| clazz | number | Device class.
**System capability**: SystemCapability.USB.USBManager| +| subClass | number | Device subclass.
**System capability**: SystemCapability.USB.USBManager| +| protocol | number | Device protocol code.
**System capability**: SystemCapability.USB.USBManager| +| configs | Array<[USBConfig](#usbconfig)> | Device configuration descriptor information.
**System capability**: SystemCapability.USB.USBManager| + + +## USBDevicePipe Represents a USB device pipe, which is used to determine a USB device. - - - - - - - - - - - - - - - -

Name

-

Type

-

Description

-

busNum

-

number

-

Bus address.

-

devAddress

-

number

-

Device address.

-
- -## USBControlParams +| Name| Type| Description| +| -------- | -------- | -------- | +| busNum | number | Bus address.
**System capability**: SystemCapability.USB.USBManager| +| devAddress | number | Device address.
**System capability**: SystemCapability.USB.USBManager| + + +## USBControlParams Represents control transfer parameters. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Name

-

Type

-

Description

-

request

-

number

-

Request type.

-

target

-

USBRequestTargetType

-

Request target type.

-

reqType

-

USBControlRequestType

-

Request control type.

-

value

-

number

-

Request parameter value.

-

index

-

number

-

Index of the request parameter value.

-

data

-

Uint8Array

-

Data written to or read from the buffer.

-
- -## USBRequestTargetType - -Enumerates USB request target types. - - - - - - - - - - - - - - - - - - - - - - - - -

Name

-

Default Value

-

Description

-

USB_REQUEST_TARGET_DEVICE

-

0

-

Device

-

USB_REQUEST_TARGET_INTERFACE

-

1

-

Interface

-

USB_REQUEST_TARGET_ENDPOINT

-

2

-

Endpoint

-

USB_REQUEST_TARGET_OTHER

-

3

-

Others

-
- -## USBControlRequestType +| Name| Type| Description| +| -------- | -------- | -------- | +| request | number | Request type.| +| target | [USBRequestTargetType](#usbrequesttargettype) | Request target type.
**System capability**: SystemCapability.USB.USBManager| +| reqType | [USBControlRequestType](#usbcontrolrequesttype) | Request control type.
**System capability**: SystemCapability.USB.USBManager| +| value | number | Request parameters
**System capability**: SystemCapability.USB.USBManager| +| index | number | Index of the request parameter value.
**System capability**: SystemCapability.USB.USBManager| +| data | Uint8Array | Buffer for writing or reading data.
**System capability**: SystemCapability.USB.USBManager| + + +## USBRequestTargetType + +Represents the request target type. + + +| Name| Default Value| Description| +| -------- | -------- | -------- | +| USB\_REQUEST\_TARGET\_DEVICE | 0 | Device.
**System capability**: SystemCapability.USB.USBManager| +| USB\_REQUEST\_TARGET\_INTERFACE | 1 | Interface.
**System capability**: SystemCapability.USB.USBManager| +| USB\_REQUEST\_TARGET\_ENDPOINT | 2 | Endpoint
**System capability**: SystemCapability.USB.USBManager| +| USB_REQUEST_TARGET_OTHER | 3 | Others
**System capability**: SystemCapability.USB.USBManager| + + +## USBControlRequestType Enumerates control request types. - - - - - - - - - - - - - - - - - - - -

Name

-

Default Value

-

Description

-

USB_REQUEST_TYPE_STANDARD

-

0

-

Standard

-

USB_REQUEST_TYPE_CLASS

-

1

-

Class

-

USB_REQUEST_TYPE_VENDOR

-

2

-

Vendor

-
- -## USBRequestDirection +| Name| Default Value| Description| +| -------- | -------- | -------- | +| USB\_REQUEST\_TYPE\_STANDARD | 0 | Standard
**System capability**: SystemCapability.USB.USBManager| +| USB\_REQUEST\_TYPE\_CLASS | 1 | Class
**System capability**: SystemCapability.USB.USBManager| +| USB\_REQUEST\_TYPE\_VENDOR | 2 | Vendor
**System capability**: SystemCapability.USB.USBManager| + + +## USBRequestDirection Enumerates request directions. - - - - - - - - - - - - - - - -

Name

-

Default Value

-

Description

-

USB_REQUEST_TYPE_STANDARD

-

0

-

Request for writing data from the host to the device.

-

USB_REQUEST_TYPE_CLASS

-

0x80

-

Request for reading data from the device to the host.

-
- - - - - - - - - - - - - - - - - - - - - - - - -

Name

-

Default Value

-

Description

-

NONE

-

0

-

None.

-

ACM

-

1

-

Serial port device

-

ECM

-

2

-

Ethernet port device

-

HDC

-

4

-

HDC device

-
+| Name| Default Value| Description| +| -------- | -------- | -------- | +| USB\_REQUEST\_TYPE\_STANDARD | 0 | Request for writing data from the host to the device.
**System capability**: SystemCapability.USB.USBManager| +| USB\_REQUEST\_TYPE\_CLASS | 0x80 | Request for reading data from the device to the host.
**System capability**: SystemCapability.USB.USBManager| + +| Name| Default Value| Description| +| -------- | -------- | -------- | +| NONE | 0 | None
**System capability**: SystemCapability.USB.USBManager| +| ACM | 1 | Serial port device
**System capability**: SystemCapability.USB.USBManager| +| ECM | 2 | Ethernet port device
**System capability**: SystemCapability.USB.USBManager| +| HDC | 4 | HDC device
**System capability**: SystemCapability.USB.USBManager| -- GitLab