js-apis-usb.md 18.2 KB
Newer Older
S
shawn_he 已提交
1
# USB
Z
zengyawen 已提交
2

S
shawn_he 已提交
3 4
> ![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.
Z
zengyawen 已提交
5

S
shawn_he 已提交
6
## Modules to Import
Z
zengyawen 已提交
7 8 9 10 11

```
import usb from "@ohos.usb";
```

S
shawn_he 已提交
12
## usb.getDevices
Z
zengyawen 已提交
13

S
shawn_he 已提交
14
getDevices(): Array<Readonly<USBDevice>>
Z
zengyawen 已提交
15 16 17

Obtains the USB device list.

S
shawn_he 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
**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

S
shawn_he 已提交
84
connectDevice(device: USBDevice): Readonly<USBDevicePipe>
Z
zengyawen 已提交
85 86 87

Connects to a USB device.

S
shawn_he 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
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

S
shawn_he 已提交
111
hasRight(deviceName: string): boolean
S
shawn_he 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

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

S
shawn_he 已提交
137
requestRight(deviceName: string): Promise<boolean>
Z
zengyawen 已提交
138 139 140

Requests the temporary permission for the application to access the USB device.

S
shawn_he 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
**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

S
shawn_he 已提交
164
claimInterface(pipe: USBDevicePipe, iface: USBInterface, force?: boolean): number
Z
zengyawen 已提交
165 166 167

Claims a USB interface.

S
shawn_he 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
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

S
shawn_he 已提交
193
releaseInterface(pipe: USBDevicePipe, iface: USBInterface): number
Z
zengyawen 已提交
194 195 196

Releases a USB interface.

S
shawn_he 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
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

S
shawn_he 已提交
221
setConfiguration(pipe: USBDevicePipe, config: USBConfig): number
Z
zengyawen 已提交
222 223 224

Sets the device configuration.

S
shawn_he 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
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

S
shawn_he 已提交
249
setInterface(pipe: USBDevicePipe, iface: USBInterface): number
Z
zengyawen 已提交
250 251 252

Sets a USB interface.

S
shawn_he 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
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

S
shawn_he 已提交
277
getRawDescriptor(pipe: USBDevicePipe): Uint8Array
Z
zengyawen 已提交
278 279 280

Obtains the raw USB descriptor.

S
shawn_he 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
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

S
shawn_he 已提交
303
getFileDescriptor(pipe: USBDevicePipe): number
Z
zengyawen 已提交
304 305 306

Obtains the file descriptor.

S
shawn_he 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
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

S
shawn_he 已提交
329
controlTransfer(pipe: USBDevicePipe, contrlparam: USBControlParams, timeout?: number): Promise<number>
Z
zengyawen 已提交
330 331 332

Performs control transfer.

S
shawn_he 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
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

S
shawn_he 已提交
359
bulkTransfer(pipe: USBDevicePipe, endpoint: USBEndpoint, buffer: Uint8Array, timeout?: number): Promise<number>
Z
zengyawen 已提交
360 361 362

Performs bulk transfer.

S
shawn_he 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
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

S
shawn_he 已提交
393
closePipe(pipe: USBDevicePipe): number
Z
zengyawen 已提交
394 395 396

Closes a USB device pipe.

S
shawn_he 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
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).

S
shawn_he 已提交
422 423
**System capability**: SystemCapability.USB.USBManager

S
shawn_he 已提交
424 425
| Name| Type| Description|
| -------- | -------- | -------- |
S
shawn_he 已提交
426 427 428 429 430 431 432 433
| address | number | Endpoint address.|
| attributes | number | Endpoint attributes.|
| interval | number | Endpoint interval.|
| maxPacketSize | number | Maximum size of data packets on the endpoint.|
| direction | [USBRequestDirection](#usbrequestdirection) | Endpoint direction.|
| number | number | Endpoint number.|
| type | number | Endpoint type.|
| interfaceId | number | Unique ID of the interface to which the endpoint belongs.|
S
shawn_he 已提交
434 435 436 437 438 439


## USBInterface

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

S
shawn_he 已提交
440 441
**System capability**: SystemCapability.USB.USBManager

S
shawn_he 已提交
442 443
| Name| Type| Description|
| -------- | -------- | -------- |
S
shawn_he 已提交
444 445 446 447 448 449 450
| 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](#usbendpoint)> | Endpoints that belong to the USB interface.|
S
shawn_he 已提交
451 452 453 454 455 456


## USBConfig

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

S
shawn_he 已提交
457 458
**System capability**: SystemCapability.USB.USBManager

S
shawn_he 已提交
459 460
| Name| Type| Description|
| -------- | -------- | -------- |
S
shawn_he 已提交
461 462 463 464 465 466 467
| 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](#usbinterface)> | Supported interface attributes.|
S
shawn_he 已提交
468 469 470 471


## USBDevice

S
shawn_he 已提交
472 473 474
Represents USB device information.

**System capability**: SystemCapability.USB.USBManager
S
shawn_he 已提交
475 476 477

| Name| Type| Description|
| -------- | -------- | -------- |
S
shawn_he 已提交
478 479 480 481 482 483 484 485 486 487 488 489 490
| busNum | number | Bus address.|
| devAddress | number | Device address.|
| serial | string | Device SN.|
| name | string | Device name.|
| manufacturerName | string | Device manufacturer.|
| productName | string | Product information.|
| version | string | 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](#usbconfig)> | Device configuration descriptor information.|
S
shawn_he 已提交
491 492 493


## USBDevicePipe
Z
zengyawen 已提交
494 495 496

Represents a USB device pipe, which is used to determine a USB device.

S
shawn_he 已提交
497 498
**System capability**: SystemCapability.USB.USBManager

S
shawn_he 已提交
499 500
| Name| Type| Description|
| -------- | -------- | -------- |
S
shawn_he 已提交
501 502
| busNum | number | Bus address.|
| devAddress | number | Device address.|
S
shawn_he 已提交
503 504 505


## USBControlParams
Z
zengyawen 已提交
506 507 508

Represents control transfer parameters.

S
shawn_he 已提交
509 510
**System capability**: SystemCapability.USB.USBManager

S
shawn_he 已提交
511 512 513
| Name| Type| Description|
| -------- | -------- | -------- |
| request | number | Request type.|
S
shawn_he 已提交
514 515 516 517 518
| target | [USBRequestTargetType](#usbrequesttargettype) | Represents the request target type.|
| reqType | [USBControlRequestType](#usbcontrolrequesttype) | Request control type.|
| value | number | Request parameters|
| index | number | Index of the request parameter value.|
| data | Uint8Array | Buffer for writing or reading data.|
S
shawn_he 已提交
519 520 521 522 523 524


## USBRequestTargetType

Represents the request target type.

S
shawn_he 已提交
525
**System capability**: SystemCapability.USB.USBManager
S
shawn_he 已提交
526

S
shawn_he 已提交
527
| Name| Default Value | Description|
S
shawn_he 已提交
528
| -------- | -------- | -------- |
S
shawn_he 已提交
529 530 531 532
| USB_REQUEST_TARGET_DEVICE | 0 | Device.|
| USB_REQUEST_TARGET_INTERFACE | 1 | Interface.|
| USB_REQUEST_TARGET_ENDPOINT | 2 | Endpoint|
| USB_REQUEST_TARGET_OTHER | 3 | Others|
S
shawn_he 已提交
533 534 535


## USBControlRequestType
Z
zengyawen 已提交
536 537 538

Enumerates control request types.

S
shawn_he 已提交
539 540
**System capability**: SystemCapability.USB.USBManager

S
shawn_he 已提交
541
| Name| Default Value | Description|
S
shawn_he 已提交
542
| -------- | -------- | -------- |
S
shawn_he 已提交
543 544 545
| USB_REQUEST_TYPE_STANDARD | 0 | Standard|
| USB_REQUEST_TYPE_CLASS | 1 | Class.|
| USB_REQUEST_TYPE_VENDOR | 2 | Vendor|
S
shawn_he 已提交
546 547 548


## USBRequestDirection
Z
zengyawen 已提交
549 550 551

Enumerates request directions.

S
shawn_he 已提交
552 553
**System capability**: SystemCapability.USB.USBManager

S
shawn_he 已提交
554
| Name| Default Value | Description|
S
shawn_he 已提交
555
| -------- | -------- | -------- |
S
shawn_he 已提交
556 557
| USB_REQUEST_DIR_TO_DEVICE | 0 | Request for writing data from the host to the device.|
| USB_REQUEST_DIR_FROM_DEVICE | 0x80 | Request for reading data from the device to the host.|