USB管理

2024-01-23 17:48 更新

本模塊主要提供管理USB設(shè)備的相關(guān)功能,包括主設(shè)備上查詢USB設(shè)備列表、批量數(shù)據(jù)傳輸、控制命令傳輸、權(quán)限控制等;從設(shè)備上端口管理、功能切換及查詢等。

說明

本模塊首批接口從API version 9開始支持。后續(xù)版本的新增接口,采用上角標(biāo)單獨(dú)標(biāo)記接口的起始版本。

導(dǎo)入模塊

  1. ```ts
  2. import usb from "@ohos.usbManager";

usb.getDevices

getDevices(): Array<Readonly<USBDevice>>

獲取接入主設(shè)備的USB設(shè)備列表。如果沒有設(shè)備接入,那么將會(huì)返回一個(gè)空的列表。

系統(tǒng)能力: SystemCapability.USB.USBManager

返回值:

類型

說明

Array<Readonly<USBDevice>>

設(shè)備信息列表。

示例:

  1. ```ts
  2. let devicesList: Array<usb.USBDevice> = usb.getDevices();
  3. console.log(`devicesList = ${devicesList}`);
  4. /*
  5. devicesList 返回的數(shù)據(jù)結(jié)構(gòu),此處提供一個(gè)簡(jiǎn)單的示例,如下
  6. [
  7. {
  8. name: "1-1",
  9. serial: "",
  10. manufacturerName: "",
  11. productName: "",
  12. version: "",
  13. vendorId: 7531,
  14. productId: 2,
  15. clazz: 9,
  16. subClass: 0,
  17. protocol: 1,
  18. devAddress: 1,
  19. busNum: 1,
  20. configs: [
  21. {
  22. id: 1,
  23. attributes: 224,
  24. isRemoteWakeup: true,
  25. isSelfPowered: true,
  26. maxPower: 0,
  27. name: "1-1",
  28. interfaces: [
  29. {
  30. id: 0,
  31. protocol: 0,
  32. clazz: 9,
  33. subClass: 0,
  34. alternateSetting: 0,
  35. name: "1-1",
  36. endpoints: [
  37. {
  38. address: 129,
  39. attributes: 3,
  40. interval: 12,
  41. maxPacketSize: 4,
  42. direction: 128,
  43. number: 1,
  44. type: 3,
  45. interfaceId: 0,
  46. },
  47. ],
  48. },
  49. ],
  50. },
  51. ],
  52. },
  53. ]
  54. */

usb.connectDevice

connectDevice(device: USBDevice): Readonly<USBDevicePipe>

根據(jù)getDevices()返回的設(shè)備信息打開USB設(shè)備。

需要調(diào)用usb.getDevices獲取設(shè)備信息以及device,再調(diào)用usb.requestRight請(qǐng)求使用該設(shè)備的權(quán)限。

系統(tǒng)能力: SystemCapability.USB.USBManager

參數(shù):

參數(shù)名

類型

必填

說明

device

USBDevice

USB設(shè)備信息。

返回值:

類型

說明

Readonly<USBDevicePipe>

指定的傳輸通道對(duì)象。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹參見USB錯(cuò)誤碼。

錯(cuò)誤碼ID

錯(cuò)誤信息

14400001

Permission denied. Need call requestRight to get permission.

示例:

  1. ```ts
  2. let devicesList: Array<usb.USBDevice> = usb.getDevices();
  3. if (devicesList.length == 0) {
  4. console.log(`device list is empty`);
  5. }
  6. let device: USBDevice = devicesList[0];
  7. usb.requestRight(device.name);
  8. let devicepipe: USBDevicePipe = usb.connectDevice(device);
  9. console.log(`devicepipe = ${devicepipe}`);

usb.hasRight

hasRight(deviceName: string): boolean

判斷是否有權(quán)訪問該設(shè)備。

如果“使用者”(如各種App或系統(tǒng))有權(quán)訪問設(shè)備則返回true;無權(quán)訪問設(shè)備則返回false。

系統(tǒng)能力: SystemCapability.USB.USBManager

參數(shù):

參數(shù)名

類型

必填

說明

deviceName

string

設(shè)備名稱。

返回值:

類型

說明

boolean

true表示有訪問設(shè)備的權(quán)限,false表示沒有訪問設(shè)備的權(quán)限。

示例:

  1. let devicesName="1-1";
  2. let bool = usb.hasRight(devicesName);
  3. console.log(`${bool}`);

usb.requestRight

requestRight(deviceName: string): Promise<boolean>

請(qǐng)求軟件包的臨時(shí)權(quán)限以訪問設(shè)備。使用Promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.USB.USBManager

參數(shù):

參數(shù)名

類型

必填

說明

deviceName

string

設(shè)備名稱。

返回值:

類型

說明

Promise<boolean>

Promise對(duì)象,返回臨時(shí)權(quán)限的申請(qǐng)結(jié)果。返回true表示臨時(shí)權(quán)限申請(qǐng)成功;返回false則表示臨時(shí)權(quán)限申請(qǐng)失敗。

示例:

  1. ```ts
  2. let devicesName: string = "1-1";
  3. usb.requestRight(devicesName:).then((ret: number) => {
  4. console.log(`requestRight = ${ret}`);
  5. });

usb.removeRight

removeRight(deviceName: string): boolean

移除軟件包訪問設(shè)備的權(quán)限。

系統(tǒng)能力: SystemCapability.USB.USBManager

參數(shù):

參數(shù)名

類型

必填

說明

deviceName

string

設(shè)備名稱。

返回值:

類型

說明

boolean

返回權(quán)限移除結(jié)果。返回true表示權(quán)限移除成功;返回false則表示權(quán)限移除失敗。

示例:

  1. let devicesName="1-1";
  2. if (usb.removeRight(devicesName)) {
  3. console.log(`Succeed in removing right`);
  4. }

usb.claimInterface

claimInterface(pipe: USBDevicePipe, iface: USBInterface, force ?: boolean): number

注冊(cè)通信接口。

需要調(diào)用usb.getDevices獲取設(shè)備信息以及interfaces;調(diào)用usb.requestRight獲取設(shè)備請(qǐng)求權(quán)限;調(diào)用usb.connectDevice接口得到devicepipe作為參數(shù)。

系統(tǒng)能力: SystemCapability.USB.USBManager

參數(shù):

參數(shù)名

類型

必填

說明

pipe

USBDevicePipe

用于確定總線號(hào)和設(shè)備地址。

iface

USBInterface

用于確定需要獲取接口的索引。

force

boolean

可選參數(shù),是否強(qiáng)制獲取。默認(rèn)值為false ,表示不強(qiáng)制獲取。

返回值:

類型

說明

number

注冊(cè)通信接口成功返回0;注冊(cè)通信接口失敗返回其他錯(cuò)誤碼。

示例:

  1. let ret = usb.claimInterface(devicepipe, interfaces);
  2. console.log(`claimInterface = ${ret}`);

usb.releaseInterface

releaseInterface(pipe: USBDevicePipe, iface: USBInterface): number

釋放注冊(cè)過的通信接口。

需要調(diào)用usb.claimInterface先獲取接口,才能使用此方法釋放接口。

系統(tǒng)能力: SystemCapability.USB.USBManager

參數(shù):

參數(shù)名

類型

必填

說明

pipe

USBDevicePipe

用于確定總線號(hào)和設(shè)備地址。

iface

USBInterface

用于確定需要釋放接口的索引。

返回值:

類型

說明

number

釋放接口成功返回0;釋放接口失敗返回其他錯(cuò)誤碼。

示例:

  1. let ret = usb.releaseInterface(devicepipe, interfaces);
  2. console.log(`releaseInterface = ${ret}`);

usb.setConfiguration

setConfiguration(pipe: USBDevicePipe, config: USBConfiguration): number

設(shè)置設(shè)備配置。

需要調(diào)用usb.getDevices獲取設(shè)備信息以及config;調(diào)用usb.requestRight獲取設(shè)備請(qǐng)求權(quán)限;調(diào)用usb.connectDevice得到devicepipe作為參數(shù)。

系統(tǒng)能力: SystemCapability.USB.USBManager

參數(shù):

參數(shù)名

類型

必填

說明

pipe

USBDevicePipe

用于確定總線號(hào)和設(shè)備地址。

config

USBConfiguration

用于確定需要設(shè)置的配置。

返回值:

類型

說明

number

設(shè)置設(shè)備配置成功返回0;設(shè)置設(shè)備配置失敗返回其他錯(cuò)誤碼。

示例:

  1. let ret = usb.setConfiguration(devicepipe, config);
  2. console.log(`setConfiguration = ${ret}`);

usb.setInterface

setInterface(pipe: USBDevicePipe, iface: USBInterface): number

設(shè)置設(shè)備接口。

需要調(diào)用usb.getDevices獲取設(shè)備列表以及interfaces;調(diào)用usb.requestRight獲取設(shè)備請(qǐng)求權(quán)限;調(diào)用usb.connectDevice得到devicepipe作為參數(shù);調(diào)用usb.claimInterface注冊(cè)通信接口。

系統(tǒng)能力: SystemCapability.USB.USBManager

參數(shù):

參數(shù)名

類型

必填

說明

pipe

USBDevicePipe

用于確定總線號(hào)和設(shè)備地址。

iface

USBInterface

用于確定需要設(shè)置的接口。

返回值:

類型

說明

number

設(shè)置設(shè)備接口成功返回0;設(shè)置設(shè)備接口失敗返回其他錯(cuò)誤碼。

示例:

  1. let ret = usb.setInterface(devicepipe, interfaces);
  2. console.log(`setInterface = ${ret}`);

usb.getRawDescriptor

getRawDescriptor(pipe: USBDevicePipe): Uint8Array

獲取原始的USB描述符。

需要調(diào)用usb.getDevices獲取設(shè)備列表;調(diào)用usb.requestRight獲取設(shè)備請(qǐng)求權(quán)限;調(diào)用usb.connectDevice接口得到devicepipe作為參數(shù)。

系統(tǒng)能力: SystemCapability.USB.USBManager

參數(shù):

參數(shù)名

類型

必填

說明

pipe

USBDevicePipe

用于確定總線號(hào)和設(shè)備地址。

返回值:

類型

說明

Uint8Array

返回獲取的原始數(shù)據(jù);失敗返回undefined。

示例:

  1. let ret = usb.getRawDescriptor(devicepipe);

usb.getFileDescriptor

getFileDescriptor(pipe: USBDevicePipe): number

獲取文件描述符。

需要調(diào)用usb.getDevices獲取設(shè)備列表;調(diào)用usb.requestRight獲取設(shè)備請(qǐng)求權(quán)限;調(diào)用usb.connectDevice接口得到devicepipe作為參數(shù)。

系統(tǒng)能力: SystemCapability.USB.USBManager

參數(shù):

參數(shù)名

類型

必填

說明

pipe

USBDevicePipe

用于確定總線號(hào)和設(shè)備地址。

返回值:

類型

說明

number

返回設(shè)備對(duì)應(yīng)的文件描述符;失敗返回-1。

示例:

  1. let ret = usb.getFileDescriptor(devicepipe);

usb.controlTransfer

controlTransfer(pipe: USBDevicePipe, controlparam: USBControlParams, timeout ?: number): Promise<number>

控制傳輸。

需要調(diào)用usb.getDevices獲取設(shè)備列表;調(diào)用usb.requestRight獲取設(shè)備請(qǐng)求權(quán)限;調(diào)用usb.connectDevice接口得到devicepipe作為參數(shù)。

系統(tǒng)能力: SystemCapability.USB.USBManager

參數(shù):

參數(shù)名

類型

必填

說明

pipe

USBDevicePipe

用于確定設(shè)備。

controlparam

USBControlParams

控制傳輸參數(shù)。

timeout

number

超時(shí)時(shí)間(單位:ms),可選參數(shù),默認(rèn)為0不超時(shí)。

返回值:

類型

說明

Promise<number>

Promise對(duì)象,獲取傳輸或接收到的數(shù)據(jù)塊大小。失敗返回-1。

示例:

  1. let param = {
  2. request: 0,
  3. reqType: 0,
  4. target:0,
  5. value: 0,
  6. index: 0,
  7. data: null
  8. };
  9. usb.controlTransfer(devicepipe, param).then((ret) => {
  10. console.log(`controlTransfer = ${ret}`);
  11. })

usb.bulkTransfer

bulkTransfer(pipe: USBDevicePipe, endpoint: USBEndpoint, buffer: Uint8Array, timeout ?: number): Promise<number>

批量傳輸。

需要調(diào)用usb.getDevices獲取設(shè)備信息列表以及endpoint;再調(diào)用usb.requestRight獲取設(shè)備請(qǐng)求權(quán)限;然后調(diào)用usb.connectDevice接口得到返回?cái)?shù)據(jù)devicepipe之后,再次獲取接口usb.claimInterface;再調(diào)用usb.bulkTransfer接口。

系統(tǒng)能力: SystemCapability.USB.USBManager

參數(shù):

參數(shù)名

類型

必填

說明

pipe

USBDevicePipe

用于確定設(shè)備。

endpoint

USBEndpoint

用于確定傳輸?shù)亩丝凇?/p>

buffer

Uint8Array

用于寫入或讀取的緩沖區(qū)。

timeout

number

超時(shí)時(shí)間(單位:ms),可選參數(shù),默認(rèn)為0不超時(shí)。

返回值:

類型

說明

Promise<number>

Promise對(duì)象,獲取傳輸或接收到的數(shù)據(jù)塊大小。失敗返回-1。

示例:

  1. //usb.getDevices 接口返回?cái)?shù)據(jù)集合,取其中一個(gè)設(shè)備對(duì)象,并獲取權(quán)限 。
  2. //把獲取到的設(shè)備對(duì)象作為參數(shù)傳入usb.connectDevice;當(dāng)usb.connectDevice接口成功返回之后;
  3. //才可以調(diào)用第三個(gè)接口usb.claimInterface.當(dāng)usb.claimInterface 調(diào)用成功以后,再調(diào)用該接口。
  4. usb.bulkTransfer(devicepipe, endpoint, buffer).then((ret) => {
  5. console.log(`bulkTransfer = ${ret}`);
  6. });

usb.closePipe

closePipe(pipe: USBDevicePipe): number

關(guān)閉設(shè)備消息控制通道。

需要調(diào)用usb.getDevices獲取設(shè)備列表;調(diào)用usb.requestRight獲取設(shè)備請(qǐng)求權(quán)限;調(diào)用usb.connectDevice得到devicepipe作為參數(shù)。

系統(tǒng)能力: SystemCapability.USB.USBManager

參數(shù):

參數(shù)名

類型

必填

說明

pipe

USBDevicePipe

用于確定USB設(shè)備消息控制通道。

返回值:

類型

說明

number

關(guān)閉設(shè)備消息控制通道成功返回0;關(guān)閉設(shè)備消息控制通道失敗返回其他錯(cuò)誤碼。

示例:

  1. let ret = usb.closePipe(devicepipe);
  2. console.log(`closePipe = ${ret}`);

USBEndpoint

通過USB發(fā)送和接收數(shù)據(jù)的端口。通過USBInterface獲取。

系統(tǒng)能力: SystemCapability.USB.USBManager

名稱

類型

必填

說明

address

number

端點(diǎn)地址。

attributes

number

端點(diǎn)屬性。

interval

number

端點(diǎn)間隔。

maxPacketSize

number

端點(diǎn)最大數(shù)據(jù)包大小。

direction

USBRequestDirection

端點(diǎn)的方向。

number

number

端點(diǎn)號(hào)。

type

number

端點(diǎn)類型。

interfaceId

number

端點(diǎn)所屬的接口的唯一標(biāo)識(shí)。

USBInterface

一個(gè)USBConfiguration中可以含有多個(gè)USBInterface,每個(gè)USBInterface提供一個(gè)功能。

系統(tǒng)能力: SystemCapability.USB.USBManager

名稱

類型

必填

說明

id

number

接口的唯一標(biāo)識(shí)。

protocol

number

接口的協(xié)議。

clazz

number

設(shè)備類型。

subClass

number

設(shè)備子類。

alternateSetting

number

在同一個(gè)接口中的多個(gè)描述符中進(jìn)行切換設(shè)置。

name

string

接口名稱。

endpoints

Array<USBEndpoint>

當(dāng)前接口所包含的端點(diǎn)。

USBConfiguration

USB配置,一個(gè)USBDevice中可以含有多個(gè)配置。

系統(tǒng)能力: SystemCapability.USB.USBManager

名稱

類型

必填

說明

id

number

配置的唯一標(biāo)識(shí)。

attributes

number

配置的屬性。

maxPower

number

最大功耗,以毫安為單位。

name

string

配置的名稱,可以為空。

isRemoteWakeup

boolean

檢查當(dāng)前配置是否支持遠(yuǎn)程喚醒。

isSelfPowered

boolean

檢查當(dāng)前配置是否支持獨(dú)立電源。

interfaces

Array <USBInterface>

配置支持的接口屬性。

USBDevice

USB設(shè)備信息。

系統(tǒng)能力: SystemCapability.USB.USBManager

名稱

類型

必填

說明

busNum

number

總線地址。

devAddress

number

設(shè)備地址。

serial

string

序列號(hào)。

name

string

設(shè)備名字。

manufacturerName

string

產(chǎn)商信息。

productName

string

產(chǎn)品信息。

version

string

版本。

vendorId

number

廠商ID。

productId

number

產(chǎn)品ID。

clazz

number

設(shè)備類。

subClass

number

設(shè)備子類。

protocol

number

設(shè)備協(xié)議碼。

configs

Array<USBConfiguration>

設(shè)備配置描述符信息。

USBDevicePipe

USB設(shè)備消息傳輸通道,用于確定設(shè)備。

系統(tǒng)能力: SystemCapability.USB.USBManager

名稱

類型

必填

說明

busNum

number

總線地址。

devAddress

number

設(shè)備地址。

USBControlParams

控制傳輸參數(shù)。

系統(tǒng)能力: SystemCapability.USB.USBManager

名稱

類型

必填

說明

request

number

請(qǐng)求類型。

target

USBRequestTargetType

請(qǐng)求目標(biāo)類型。

reqType

USBControlRequestType

請(qǐng)求控制類型。

value

number

請(qǐng)求參數(shù)。

index

number

請(qǐng)求參數(shù)value對(duì)應(yīng)的索引值。

data

Uint8Array

用于寫入或讀取的緩沖區(qū)。

USBRequestTargetType

請(qǐng)求目標(biāo)類型。

系統(tǒng)能力: SystemCapability.USB.USBManager

名稱

說明

USB_REQUEST_TARGET_DEVICE

0

設(shè)備。

USB_REQUEST_TARGET_INTERFACE

1

接口。

USB_REQUEST_TARGET_ENDPOINT

2

端點(diǎn)。

USB_REQUEST_TARGET_OTHER

3

其他。

USBControlRequestType

控制請(qǐng)求類型。

系統(tǒng)能力: SystemCapability.USB.USBManager

名稱

說明

USB_REQUEST_TYPE_STANDARD

0

標(biāo)準(zhǔn)。

USB_REQUEST_TYPE_CLASS

1

類。

USB_REQUEST_TYPE_VENDOR

2

廠商。

USBRequestDirection

請(qǐng)求方向。

系統(tǒng)能力: SystemCapability.USB.USBManager

名稱

說明

USB_REQUEST_DIR_TO_DEVICE

0

寫數(shù)據(jù),主設(shè)備往從設(shè)備。

USB_REQUEST_DIR_FROM_DEVICE

0x80

讀數(shù)據(jù),從設(shè)備往主設(shè)備。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)