1# @ohos.usbManager (USB管理) 2 3本模块主要提供管理USB设备的相关功能,包括主设备上查询USB设备列表、批量数据传输、控制命令传输、权限控制等;从设备上端口管理、功能切换及查询等。 4 5> **说明:** 6> 7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9## 导入模块 10 11```ts 12import usb from "@ohos.usbManager"; 13``` 14 15## usb.getDevices 16 17getDevices(): Array<Readonly<USBDevice>> 18 19获取接入主设备的USB设备列表。如果没有设备接入,那么将会返回一个空的列表。 20 21**系统能力:** SystemCapability.USB.USBManager 22 23**返回值:** 24 25| 类型 | 说明 | 26| ---------------------------------------------------- | ------- | 27| Array<Readonly<[USBDevice](#usbdevice)>> | 设备信息列表。 | 28 29**示例:** 30 31```ts 32let devicesList: Array<usb.USBDevice> = usb.getDevices(); 33console.log(`devicesList = ${devicesList}`); 34/* 35devicesList 返回的数据结构,此处提供一个简单的示例,如下 36[ 37 { 38 name: "1-1", 39 serial: "", 40 manufacturerName: "", 41 productName: "", 42 version: "", 43 vendorId: 7531, 44 productId: 2, 45 clazz: 9, 46 subClass: 0, 47 protocol: 1, 48 devAddress: 1, 49 busNum: 1, 50 configs: [ 51 { 52 id: 1, 53 attributes: 224, 54 isRemoteWakeup: true, 55 isSelfPowered: true, 56 maxPower: 0, 57 name: "1-1", 58 interfaces: [ 59 { 60 id: 0, 61 protocol: 0, 62 clazz: 9, 63 subClass: 0, 64 alternateSetting: 0, 65 name: "1-1", 66 endpoints: [ 67 { 68 address: 129, 69 attributes: 3, 70 interval: 12, 71 maxPacketSize: 4, 72 direction: 128, 73 number: 1, 74 type: 3, 75 interfaceId: 0, 76 }, 77 ], 78 }, 79 ], 80 }, 81 ], 82 }, 83] 84*/ 85``` 86 87## usb.connectDevice 88 89connectDevice(device: USBDevice): Readonly<USBDevicePipe> 90 91根据getDevices()返回的设备信息打开USB设备。 92 93需要调用[usb.getDevices](#usbgetdevices)获取设备信息以及device,再调用[usb.requestRight](#usbrequestright)请求使用该设备的权限。 94 95**系统能力:** SystemCapability.USB.USBManager 96 97**参数:** 98 99| 参数名 | 类型 | 必填 | 说明 | 100| -------- | -------- | -------- | -------- | 101| device | [USBDevice](#usbdevice) | 是 | USB设备信息。 | 102 103**返回值:** 104 105| 类型 | 说明 | 106| -------- | -------- | 107| Readonly<[USBDevicePipe](#usbdevicepipe)> | 指定的传输通道对象。 | 108 109**错误码:** 110 111以下错误码的详细介绍参见[USB错误码](../errorcodes/errorcode-usb.md)。 112 113| 错误码ID | 错误信息 | 114| -------- | -------- | 115| 14400001 |Permission denied. Need call requestRight to get permission. | 116 117**示例:** 118 119```ts 120let devicesList: Array<usb.USBDevice> = usb.getDevices(); 121if (devicesList.length == 0) { 122 console.log(`device list is empty`); 123} 124 125let device: usb.USBDevice = devicesList[0]; 126usb.requestRight(device.name); 127let devicepipe: usb.USBDevicePipe = usb.connectDevice(device); 128console.log(`devicepipe = ${devicepipe}`); 129``` 130 131## usb.hasRight 132 133hasRight(deviceName: string): boolean 134 135判断是否有权访问该设备。 136 137如果“使用者”(如各种App或系统)有权访问设备则返回true;无权访问设备则返回false。 138 139**系统能力:** SystemCapability.USB.USBManager 140 141**参数:** 142 143| 参数名 | 类型 | 必填 | 说明 | 144| -------- | -------- | -------- | -------- | 145| deviceName | string | 是 | 设备名称。 | 146 147**返回值:** 148 149| 类型 | 说明 | 150| -------- | -------- | 151| boolean | true表示有访问设备的权限,false表示没有访问设备的权限。 | 152 153**示例:** 154 155```ts 156let devicesName: string = "1-1"; 157let right: boolean = usb.hasRight(devicesName); 158console.log(`${right}`); 159``` 160 161## usb.requestRight 162 163requestRight(deviceName: string): Promise<boolean> 164 165请求软件包的临时权限以访问设备。使用Promise异步回调。系统应用默认拥有访问设备权限,无需调用此接口申请。 166 167**系统能力:** SystemCapability.USB.USBManager 168 169**参数:** 170 171| 参数名 | 类型 | 必填 | 说明 | 172| -------- | -------- | -------- | -------- | 173| deviceName | string | 是 | 设备名称。 | 174 175**返回值:** 176 177| 类型 | 说明 | 178| -------- | -------- | 179| Promise<boolean> | Promise对象,返回临时权限的申请结果。返回true表示临时权限申请成功;返回false则表示临时权限申请失败。 | 180 181**示例:** 182 183```ts 184let devicesName: string = "1-1"; 185usb.requestRight(devicesName).then(ret => { 186 console.log(`requestRight = ${ret}`); 187}); 188``` 189 190## usb.removeRight 191 192removeRight(deviceName: string): boolean 193 194移除软件包访问设备的权限。系统应用默认拥有访问设备权限,调用此接口不会产生影响。 195 196**系统能力:** SystemCapability.USB.USBManager 197 198**参数:** 199 200| 参数名 | 类型 | 必填 | 说明 | 201| -------- | -------- | -------- | -------- | 202| deviceName | string | 是 | 设备名称。 | 203 204**返回值:** 205 206| 类型 | 说明 | 207| -------- | -------- | 208| boolean | 返回权限移除结果。返回true表示权限移除成功;返回false则表示权限移除失败。 | 209 210**示例:** 211 212```ts 213let devicesName: string = "1-1"; 214if (usb.removeRight(devicesName)) { 215 console.log(`Succeed in removing right`); 216} 217``` 218 219## usb.addRight 220 221addRight(bundleName: string, deviceName: string): boolean 222 223添加软件包访问设备的权限。系统应用默认拥有访问设备权限,调用此接口不会产生影响。 224 225[requestRight](#usbrequestright)会触发弹框请求用户授权;addRight不会触发弹框,而是直接添加软件包访问设备的权限。 226 227**系统接口:** 此接口为系统接口。 228 229**系统能力:** SystemCapability.USB.USBManager 230 231**参数:** 232 233| 参数名 | 类型 | 必填 | 说明 | 234| -------- | -------- | -------- | -------- | 235| deviceName | string | 是 | 设备名称。 | 236| bundleName | string | 是 | 软件包名称。| 237 238**返回值:** 239 240| 类型 | 说明 | 241| -------- | -------- | 242| boolean | 返回权限添加结果。返回true表示权限添加成功;返回false则表示权限添加失败。 | 243 244**示例:** 245 246```ts 247let devicesName: string = "1-1"; 248let bundleName: string = "com.example.hello"; 249if (usb.addRight(bundleName, devicesName)) { 250 console.log(`Succeed in adding right`); 251} 252``` 253 254## usb.claimInterface 255 256claimInterface(pipe: USBDevicePipe, iface: USBInterface, force ?: boolean): number 257 258注册通信接口。 259 260需要调用[usb.getDevices](#usbgetdevices)获取设备信息以及interfaces;调用[usb.requestRight](#usbrequestright)获取设备请求权限;调用[usb.connectDevice](#usbconnectdevice)接口得到devicepipe作为参数。 261 262**系统能力:** SystemCapability.USB.USBManager 263 264**参数:** 265 266| 参数名 | 类型 | 必填 | 说明 | 267| -------- | -------- | -------- | -------- | 268| pipe | [USBDevicePipe](#usbdevicepipe) | 是 | 用于确定总线号和设备地址。 | 269| iface | [USBInterface](#usbinterface) | 是 | 用于确定需要获取接口的索引。 | 270| force | boolean | 否 | 可选参数,是否强制获取。默认值为false ,表示不强制获取。 | 271 272**返回值:** 273 274| 类型 | 说明 | 275| -------- | -------- | 276| number | 注册通信接口成功返回0;注册通信接口失败返回其他错误码。 | 277 278**示例:** 279 280```ts 281let devicesList: Array<usb.USBDevice> = usb.getDevices(); 282if (devicesList.length == 0) { 283 console.log(`device list is empty`); 284} 285 286let device: usb.USBDevice = devicesList[0]; 287usb.requestRight(device.name); 288let devicepipe: usb.USBDevicePipe = usb.connectDevice(device); 289let interfaces: usb.USBInterface = device.configs[0].interfaces[0]; 290let ret: number= usb.claimInterface(devicepipe, interfaces); 291console.log(`claimInterface = ${ret}`); 292``` 293 294## usb.releaseInterface 295 296releaseInterface(pipe: USBDevicePipe, iface: USBInterface): number 297 298释放注册过的通信接口。 299 300需要调用[usb.claimInterface](#usbclaiminterface)先获取接口,才能使用此方法释放接口。 301 302**系统能力:** SystemCapability.USB.USBManager 303 304**参数:** 305 306| 参数名 | 类型 | 必填 | 说明 | 307| -------- | -------- | -------- | -------- | 308| pipe | [USBDevicePipe](#usbdevicepipe) | 是 | 用于确定总线号和设备地址。 | 309| iface | [USBInterface](#usbinterface) | 是 | 用于确定需要释放接口的索引。 | 310 311**返回值:** 312 313| 类型 | 说明 | 314| -------- | -------- | 315| number | 释放接口成功返回0;释放接口失败返回其他错误码。 | 316 317**示例:** 318 319```ts 320let devicesList: Array<usb.USBDevice> = usb.getDevices(); 321if (devicesList.length == 0) { 322 console.log(`device list is empty`); 323} 324 325let device: usb.USBDevice = devicesList[0]; 326usb.requestRight(device.name); 327let devicepipe: usb.USBDevicePipe = usb.connectDevice(device); 328let interfaces: usb.USBInterface = device.configs[0].interfaces[0]; 329let ret: number = usb.claimInterface(devicepipe, interfaces); 330ret = usb.releaseInterface(devicepipe, interfaces); 331console.log(`releaseInterface = ${ret}`); 332``` 333 334## usb.setConfiguration 335 336setConfiguration(pipe: USBDevicePipe, config: USBConfiguration): number 337 338设置设备配置。 339 340需要调用[usb.getDevices](#usbgetdevices)获取设备信息以及config;调用[usb.requestRight](#usbrequestright)获取设备请求权限;调用[usb.connectDevice](#usbconnectdevice)得到devicepipe作为参数。 341 342**系统能力:** SystemCapability.USB.USBManager 343 344**参数:** 345 346| 参数名 | 类型 | 必填 | 说明 | 347| -------- | -------- | -------- | -------- | 348| pipe | [USBDevicePipe](#usbdevicepipe) | 是 | 用于确定总线号和设备地址。 | 349| config | [USBConfiguration](#usbconfiguration) | 是 | 用于确定需要设置的配置。 | 350 351**返回值:** 352 353| 类型 | 说明 | 354| -------- | -------- | 355| number | 设置设备配置成功返回0;设置设备配置失败返回其他错误码。 | 356 357**示例:** 358 359```ts 360let devicesList: Array<usb.USBDevice> = usb.getDevices(); 361if (devicesList.length == 0) { 362 console.log(`device list is empty`); 363} 364 365let device: usb.USBDevice = devicesList[0]; 366usb.requestRight(device.name); 367let devicepipe: usb.USBDevicePipe = usb.connectDevice(device); 368let config: usb.USBConfiguration = device.configs[0]; 369let ret: number= usb.setConfiguration(devicepipe, config); 370console.log(`setConfiguration = ${ret}`); 371``` 372 373## usb.setInterface 374 375setInterface(pipe: USBDevicePipe, iface: USBInterface): number 376 377设置设备接口。 378 379需要调用[usb.getDevices](#usbgetdevices)获取设备列表以及interfaces;调用[usb.requestRight](#usbrequestright)获取设备请求权限;调用[usb.connectDevice](#usbconnectdevice)得到devicepipe作为参数;调用[usb.claimInterface](#usbclaiminterface)注册通信接口。 380 381**系统能力:** SystemCapability.USB.USBManager 382 383**参数:** 384 385| 参数名 | 类型 | 必填 | 说明 | 386| ----- | ------------------------------- | --- | ------------- | 387| pipe | [USBDevicePipe](#usbdevicepipe) | 是 | 用于确定总线号和设备地址。 | 388| iface | [USBInterface](#usbinterface) | 是 | 用于确定需要设置的接口。 | 389 390**返回值:** 391 392| 类型 | 说明 | 393| -------- | -------- | 394| number | 设置设备接口成功返回0;设置设备接口失败返回其他错误码。 | 395 396**示例:** 397 398```ts 399let devicesList: Array<usb.USBDevice> = usb.getDevices(); 400if (devicesList.length == 0) { 401 console.log(`device list is empty`); 402} 403 404let device: usb.USBDevice = devicesList[0]; 405usb.requestRight(device.name); 406let devicepipe: usb.USBDevicePipe = usb.connectDevice(device); 407let interfaces: usb.USBInterface = device.configs[0].interfaces[0]; 408let ret: number = usb.claimInterface(devicepipe, interfaces); 409ret = usb.setInterface(devicepipe, interfaces); 410console.log(`setInterface = ${ret}`); 411``` 412 413## usb.getRawDescriptor 414 415getRawDescriptor(pipe: USBDevicePipe): Uint8Array 416 417获取原始的USB描述符。 418 419需要调用[usb.getDevices](#usbgetdevices)获取设备列表;调用[usb.requestRight](#usbrequestright)获取设备请求权限;调用[usb.connectDevice](#usbconnectdevice)接口得到devicepipe作为参数。 420 421**系统能力:** SystemCapability.USB.USBManager 422 423**参数:** 424 425| 参数名 | 类型 | 必填 | 说明 | 426| -------- | -------- | -------- | -------- | 427| pipe | [USBDevicePipe](#usbdevicepipe) | 是 | 用于确定总线号和设备地址。 | 428 429**返回值:** 430 431| 类型 | 说明 | 432| -------- | -------- | 433| Uint8Array | 返回获取的原始数据;失败返回undefined。 | 434 435**示例:** 436 437```ts 438let devicesList: Array<usb.USBDevice> = usb.getDevices(); 439if (devicesList.length == 0) { 440 console.log(`device list is empty`); 441} 442 443usb.requestRight(devicesList[0].name); 444let devicepipe: usb.USBDevicePipe = usb.connectDevice(devicesList[0]); 445let ret: Uint8Array = usb.getRawDescriptor(devicepipe); 446``` 447 448## usb.getFileDescriptor 449 450getFileDescriptor(pipe: USBDevicePipe): number 451 452获取文件描述符。 453 454需要调用[usb.getDevices](#usbgetdevices)获取设备列表;调用[usb.requestRight](#usbrequestright)获取设备请求权限;调用[usb.connectDevice](#usbconnectdevice)接口得到devicepipe作为参数。 455 456**系统能力:** SystemCapability.USB.USBManager 457 458**参数:** 459 460| 参数名 | 类型 | 必填 | 说明 | 461| -------- | -------- | -------- | -------- | 462| pipe | [USBDevicePipe](#usbdevicepipe) | 是 | 用于确定总线号和设备地址。 | 463 464**返回值:** 465 466| 类型 | 说明 | 467| ------ | -------------------- | 468| number | 返回设备对应的文件描述符;失败返回-1。 | 469 470**示例:** 471 472```ts 473let devicesList: Array<usb.USBDevice> = usb.getDevices(); 474if (devicesList.length == 0) { 475 console.log(`device list is empty`); 476} 477 478usb.requestRight(devicesList[0].name); 479let devicepipe: usb.USBDevicePipe = usb.connectDevice(devicesList[0]); 480let ret: number = usb.getFileDescriptor(devicepipe); 481``` 482 483## usb.controlTransfer 484 485controlTransfer(pipe: USBDevicePipe, controlparam: USBControlParams, timeout ?: number): Promise<number> 486 487控制传输。 488 489需要调用[usb.getDevices](#usbgetdevices)获取设备列表;调用[usb.requestRight](#usbrequestright)获取设备请求权限;调用[usb.connectDevice](#usbconnectdevice)接口得到devicepipe作为参数。 490 491**系统能力:** SystemCapability.USB.USBManager 492 493**参数:** 494 495| 参数名 | 类型 | 必填 | 说明 | 496| -------- | -------- | -------- | -------- | 497| pipe | [USBDevicePipe](#usbdevicepipe) | 是 | 用于确定设备。 | 498| controlparam | [USBControlParams](#usbcontrolparams) | 是 | 控制传输参数。 | 499| timeout | number | 否 | 超时时间(单位:ms),可选参数,默认为0不超时。 | 500 501**返回值:** 502 503| 类型 | 说明 | 504| -------- | -------- | 505| Promise<number> | Promise对象,获取传输或接收到的数据块大小。失败返回-1。 | 506 507**示例:** 508 509```ts 510class PARA { 511 request: number = 0 512 reqType: usb.USBControlRequestType = 0 513 target: usb.USBRequestTargetType = 0 514 value: number = 0 515 index: number = 0 516 data: Uint8Array = new Uint8Array() 517} 518 519let param: PARA = { 520 request: 0, 521 reqType: 0, 522 target:0, 523 value: 0, 524 index: 0, 525 data: new Uint8Array() 526}; 527 528let devicesList: Array<usb.USBDevice> = usb.getDevices(); 529if (devicesList.length == 0) { 530 console.log(`device list is empty`); 531} 532 533usb.requestRight(devicesList[0].name); 534let devicepipe: usb.USBDevicePipe = usb.connectDevice(devicesList[0]); 535usb.controlTransfer(devicepipe, param).then((ret: number) => { 536 console.log(`controlTransfer = ${ret}`); 537}) 538``` 539 540## usb.bulkTransfer 541 542bulkTransfer(pipe: USBDevicePipe, endpoint: USBEndpoint, buffer: Uint8Array, timeout ?: number): Promise<number> 543 544批量传输。 545 546需要调用[usb.getDevices](#usbgetdevices)获取设备信息列表以及endpoint;再调用[usb.requestRight](#usbrequestright)获取设备请求权限;然后调用[usb.connectDevice](#usbconnectdevice)接口得到返回数据devicepipe之后,再次获取接口[usb.claimInterface](#usbclaiminterface);再调用usb.bulkTransfer接口。 547 548**系统能力:** SystemCapability.USB.USBManager 549 550**参数:** 551 552| 参数名 | 类型 | 必填 | 说明 | 553| -------- | -------- | -------- | -------- | 554| pipe | [USBDevicePipe](#usbdevicepipe) | 是 | 用于确定设备。 | 555| endpoint | [USBEndpoint](#usbendpoint) | 是 | 用于确定传输的端口。 | 556| buffer | Uint8Array | 是 | 用于写入或读取数据的缓冲区。 | 557| timeout | number | 否 | 超时时间(单位:ms),可选参数,默认为0不超时。| 558 559**返回值:** 560 561| 类型 | 说明 | 562| -------- | -------- | 563| Promise<number> | Promise对象,获取传输或接收到的数据块大小。失败返回-1。 | 564 565**示例:** 566 567```ts 568//usb.getDevices 接口返回数据集合,取其中一个设备对象,并获取权限 。 569//把获取到的设备对象作为参数传入usb.connectDevice;当usb.connectDevice接口成功返回之后; 570//才可以调用第三个接口usb.claimInterface.当usb.claimInterface 调用成功以后,再调用该接口。 571let devicesList: Array<usb.USBDevice> = usb.getDevices(); 572if (devicesList.length == 0) { 573 console.log(`device list is empty`); 574} 575 576let device: usb.USBDevice = devicesList[0]; 577usb.requestRight(device.name); 578 579let devicepipe: usb.USBDevicePipe = usb.connectDevice(device); 580let interfaces: usb.USBInterface = device.configs[0].interfaces[0]; 581let endpoint: usb.USBEndpoint = device.configs[0].interfaces[0].endpoints[0]; 582let ret: number = usb.claimInterface(devicepipe, interfaces); 583let buffer = new Uint8Array(128); 584usb.bulkTransfer(devicepipe, endpoint, buffer).then((ret: number) => { 585 console.log(`bulkTransfer = ${ret}`); 586}); 587``` 588 589## usb.closePipe 590 591closePipe(pipe: USBDevicePipe): number 592 593关闭设备消息控制通道。 594 595需要调用[usb.getDevices](#usbgetdevices)获取设备列表;调用[usb.requestRight](#usbrequestright)获取设备请求权限;调用[usb.connectDevice](#usbconnectdevice)得到devicepipe作为参数。 596 597**系统能力:** SystemCapability.USB.USBManager 598 599**参数:** 600 601| 参数名 | 类型 | 必填 | 说明 | 602| -------- | -------- | -------- | -------- | 603| pipe | [USBDevicePipe](#usbdevicepipe) | 是 | 用于确定USB设备消息控制通道。 | 604 605**返回值:** 606 607| 类型 | 说明 | 608| -------- | -------- | 609| number | 关闭设备消息控制通道成功返回0;关闭设备消息控制通道失败返回其他错误码。 | 610 611**示例:** 612 613```ts 614let devicesList: Array<usb.USBDevice> = usb.getDevices(); 615if (devicesList.length == 0) { 616 console.log(`device list is empty`); 617} 618 619usb.requestRight(devicesList[0].name); 620let devicepipe: usb.USBDevicePipe = usb.connectDevice(devicesList[0]); 621let ret: number = usb.closePipe(devicepipe); 622console.log(`closePipe = ${ret}`); 623``` 624 625## usb.usbFunctionsFromString 626 627usbFunctionsFromString(funcs: string): number 628 629在设备模式下,将字符串形式的USB功能列表转化为数字掩码。 630 631**系统接口:** 此接口为系统接口。 632 633**系统能力:** SystemCapability.USB.USBManager 634 635**参数:** 636 637| 参数名 | 类型 | 必填 | 说明 | 638| ------ | ------ | ---- | ---------------------- | 639| funcs | string | 是 | 字符串形式的功能列表。 | 640 641**返回值:** 642 643| 类型 | 说明 | 644| ------ | ------------------ | 645| number | 转化后的数字掩码。 | 646 647**示例:** 648 649```ts 650let funcs: string = "acm"; 651let ret: number = usb.usbFunctionsFromString(funcs); 652``` 653 654## usb.usbFunctionsToString 655 656usbFunctionsToString(funcs: FunctionType): string 657 658在设备模式下,将数字掩码形式的USB功能列表转化为字符串。 659 660**系统接口:** 此接口为系统接口。 661 662**系统能力:** SystemCapability.USB.USBManager 663 664**参数:** 665 666| 参数名 | 类型 | 必填 | 说明 | 667| ------ | ------------------------------ | ---- | ----------------- | 668| funcs | [FunctionType](#functiontype) | 是 | USB功能数字掩码。 | 669 670**返回值:** 671 672| 类型 | 说明 | 673| ------ | ------------------------------ | 674| string | 转化后的字符串形式的功能列表。 | 675 676**示例:** 677 678```ts 679let funcs: number = usb.FunctionType.ACM | usb.FunctionType.ECM; 680let ret: string = usb.usbFunctionsToString(funcs); 681``` 682 683## usb.setCurrentFunctions 684 685setCurrentFunctions(funcs: FunctionType): Promise\<void\> 686 687在设备模式下,设置当前的USB功能列表。 688 689**系统接口:** 此接口为系统接口。 690 691**系统能力:** SystemCapability.USB.USBManager 692 693**参数:** 694 695| 参数名 | 类型 | 必填 | 说明 | 696| ------ | ------------------------------ | ---- | ----------------- | 697| funcs | [FunctionType](#functiontype) | 是 | USB功能数字掩码。 | 698 699**错误码:** 700 701以下错误码的详细介绍请参见[USB错误码](../errorcodes/errorcode-usb.md)。 702 703| 错误码ID | 错误信息 | 704| -------- | ---------------------------------------------------- | 705| 14400002 | Permission denied.The HDC is disabled by the system. | 706 707**返回值:** 708 709| 类型 | 说明 | 710| --------------- | ------------- | 711| Promise\<void\> | Promise对象。 | 712 713**示例:** 714 715```ts 716import {BusinessError} from '@ohos.base'; 717let funcs: string = usb.FunctionType.HDC; 718usb.setCurrentFunctions(funcs).then(() => { 719 console.info('usb setCurrentFunctions successfully.'); 720}).catch((err: BusinessError) => { 721 console.error('usb setCurrentFunctions failed: ' + err.code + ' message: ' + err.message); 722}); 723``` 724 725## usb.getCurrentFunctions 726 727getCurrentFunctions(): FunctionType 728 729在设备模式下,获取当前的USB功能列表的数字组合掩码。 730 731**系统接口:** 此接口为系统接口。 732 733**系统能力:** SystemCapability.USB.USBManager 734 735**返回值:** 736 737| 类型 | 说明 | 738| ------------------------------ | --------------------------------- | 739| [FunctionType](#functiontype) | 当前的USB功能列表的数字组合掩码。 | 740 741**示例:** 742 743```ts 744let ret: number = usb.getCurrentFunctions(); 745``` 746 747## usb.getPorts 748 749getPorts(): Array\<USBPort\> 750 751获取所有物理USB端口描述信息。 752 753**系统接口:** 此接口为系统接口。 754 755**系统能力:** SystemCapability.USB.USBManager 756 757**返回值:** 758 759| 类型 | 说明 | 760| ----------------------------- | --------------------- | 761| [Array\<USBPort\>](#usbport) | USB端口描述信息列表。 | 762 763**示例:** 764 765```ts 766let ret: Array<USBPort> = usb.getPorts(); 767``` 768 769## usb.getSupportedModes 770 771getSupportedModes(portId: number): PortModeType 772 773获取指定的端口支持的模式列表的组合掩码。 774 775**系统接口:** 此接口为系统接口。 776 777**系统能力:** SystemCapability.USB.USBManager 778 779**参数:** 780 781| 参数名 | 类型 | 必填 | 说明 | 782| ------ | ------ | ---- | -------- | 783| portId | number | 是 | 端口号。 | 784 785**返回值:** 786 787| 类型 | 说明 | 788| ------------------------------ | -------------------------- | 789| [PortModeType](#portmodetype) | 支持的模式列表的组合掩码。 | 790 791**示例:** 792 793```ts 794let ret: number = usb.getSupportedModes(0); 795``` 796 797## usb.setPortRoles 798 799setPortRoles(portId: number, powerRole: PowerRoleType, dataRole: DataRoleType): Promise\<void\> 800 801设置指定的端口支持的角色模式,包含充电角色、数据传输角色。 802 803**系统接口:** 此接口为系统接口。 804 805**系统能力:** SystemCapability.USB.USBManager 806 807**参数:** 808 809| 参数名 | 类型 | 必填 | 说明 | 810| --------- | -------------------------------- | ---- | ---------------- | 811| portId | number | 是 | 端口号。 | 812| powerRole | [PowerRoleType](#powerroletype) | 是 | 充电的角色。 | 813| dataRole | [DataRoleType](#dataroletype) | 是 | 数据传输的角色。 | 814 815**返回值:** 816 817| 类型 | 说明 | 818| --------------- | ------------- | 819| Promise\<void\> | Promise对象。 | 820 821**示例:** 822 823```ts 824import {BusinessError} from '@ohos.base'; 825let portId: number = 1; 826usb.setPortRoles(portId, usb.PowerRoleType.SOURCE, usb.DataRoleType.HOST).then(() => { 827 console.info('usb setPortRoles successfully.'); 828}).catch((err: BusinessError) => { 829 console.error('usb setPortRoles failed: ' + err.code + ' message: ' + err.message); 830}); 831``` 832 833## USBEndpoint 834 835通过USB发送和接收数据的端口。通过[USBInterface](#usbinterface)获取。 836 837**系统能力:** SystemCapability.USB.USBManager 838 839| 名称 | 类型 | 必填 |说明 | 840| ------------- | ------------------------------------------- | ------------- |------------- | 841| address | number | 是 |端点地址。 | 842| attributes | number | 是 |端点属性。 | 843| interval | number | 是 |端点间隔。 | 844| maxPacketSize | number | 是 |端点最大数据包大小。 | 845| direction | [USBRequestDirection](#usbrequestdirection) | 是 |端点的方向。 | 846| number | number | 是 |端点号。 | 847| type | number | 是 |端点类型。 | 848| interfaceId | number | 是 |端点所属的接口的唯一标识。 | 849 850## USBInterface 851 852一个[USBConfiguration](#usbconfiguration)中可以含有多个USBInterface,每个USBInterface提供一个功能。 853 854**系统能力:** SystemCapability.USB.USBManager 855 856| 名称 | 类型 | 必填 |说明 | 857| ---------------- | ---------------------------------------- | ------------- |--------------------- | 858| id | number | 是 |接口的唯一标识。 | 859| protocol | number | 是 |接口的协议。 | 860| clazz | number | 是 |设备类型。 | 861| subClass | number | 是 |设备子类。 | 862| alternateSetting | number | 是 |在同一个接口中的多个描述符中进行切换设置。 | 863| name | string | 是 |接口名称。 | 864| endpoints | Array<[USBEndpoint](#usbendpoint)> | 是 |当前接口所包含的端点。 | 865 866## USBConfiguration 867 868USB配置,一个[USBDevice](#usbdevice)中可以含有多个配置。 869 870**系统能力:** SystemCapability.USB.USBManager 871 872| 名称 | 类型 | 必填 |说明 | 873| -------------- | ------------------------------------------------ | --------------- |--------------- | 874| id | number | 是 |配置的唯一标识。 | 875| attributes | number | 是 |配置的属性。 | 876| maxPower | number | 是 |最大功耗,以毫安为单位。 | 877| name | string | 是 |配置的名称,可以为空。 | 878| isRemoteWakeup | boolean | 是 |检查当前配置是否支持远程唤醒。 | 879| isSelfPowered | boolean | 是 | 检查当前配置是否支持独立电源。 | 880| interfaces | Array <[USBInterface](#usbinterface)> | 是 |配置支持的接口属性。 | 881 882## USBDevice 883 884USB设备信息。 885 886**系统能力:** SystemCapability.USB.USBManager 887 888| 名称 | 类型 | 必填 |说明 | 889| ---------------- | ------------------------------------ | ---------- |---------- | 890| busNum | number | 是 |总线地址。 | 891| devAddress | number | 是 |设备地址。 | 892| serial | string | 是 |序列号。 | 893| name | string | 是 |设备名字。 | 894| manufacturerName | string | 是 | 产商信息。 | 895| productName | string | 是 |产品信息。 | 896| version | string | 是 |版本。 | 897| vendorId | number | 是 |厂商ID。 | 898| productId | number | 是 |产品ID。 | 899| clazz | number | 是 |设备类。 | 900| subClass | number | 是 |设备子类。 | 901| protocol | number | 是 |设备协议码。 | 902| configs | Array<[USBConfiguration](#usbconfiguration)> | 是 |设备配置描述符信息。 | 903 904## USBDevicePipe 905 906USB设备消息传输通道,用于确定设备。 907 908**系统能力:** SystemCapability.USB.USBManager 909 910| 名称 | 类型 | 必填 |说明 | 911| ---------- | ------ | ----- |----- | 912| busNum | number |是 | 总线地址。 | 913| devAddress | number |是 | 设备地址。 | 914 915## USBControlParams 916 917控制传输参数。 918 919**系统能力:** SystemCapability.USB.USBManager 920 921| 名称 | 类型 | 必填 |说明 | 922| ------- | ----------------------------------------------- | ---------------- |---------------- | 923| request | number | 是 |请求类型。 | 924| target | [USBRequestTargetType](#usbrequesttargettype) | 是 |请求目标类型。 | 925| reqType | [USBControlRequestType](#usbcontrolrequesttype) | 是 |请求控制类型。 | 926| value | number | 是 |请求参数。 | 927| index | number | 是 |请求参数value对应的索引值。 | 928| data | Uint8Array | 是 |用于写入或读取的缓冲区。 | 929 930## USBPort 931 932USB设备端口。 933 934**系统接口:** 此接口为系统接口。 935 936**系统能力:** SystemCapability.USB.USBManager 937 938| 名称 | 类型 | 必填 |说明 | 939| -------------- | ------------------------------- | ------------------- |------------------------ | 940| id | number | 是 |USB端口唯一标识。 | 941| supportedModes | [PortModeType](#portmodetype) | 是 |USB端口所支持的模式的数字组合掩码。 | 942| status | [USBPortStatus](#usbportstatus) | 是 |USB端口角色。 | 943 944## USBPortStatus 945 946USB设备端口角色信息。 947 948**系统接口:** 此接口为系统接口。 949 950**系统能力:** SystemCapability.USB.USBManager 951 952| 名称 | 类型 | 必填 |说明 | 953| ---------------- | -------- | ---------------- |---------------------- | 954| currentMode | number | 是 |当前的USB模式。 | 955| currentPowerRole | number | 是 |当前设备充电模式。 | 956| currentDataRole | number | 是 |当前设备数据传输模式。 | 957 958## USBRequestTargetType 959 960请求目标类型。 961 962**系统能力:** SystemCapability.USB.USBManager 963 964| 名称 | 值 | 说明 | 965| ---------------------------- | ---- | ------ | 966| USB_REQUEST_TARGET_DEVICE | 0 | 设备。 | 967| USB_REQUEST_TARGET_INTERFACE | 1 | 接口。 | 968| USB_REQUEST_TARGET_ENDPOINT | 2 | 端点。 | 969| USB_REQUEST_TARGET_OTHER | 3 | 其他。 | 970 971## USBControlRequestType 972 973控制请求类型。 974 975**系统能力:** SystemCapability.USB.USBManager 976 977| 名称 | 值 | 说明 | 978| ------------------------- | ---- | ------ | 979| USB_REQUEST_TYPE_STANDARD | 0 | 标准。 | 980| USB_REQUEST_TYPE_CLASS | 1 | 类。 | 981| USB_REQUEST_TYPE_VENDOR | 2 | 厂商。 | 982 983## USBRequestDirection 984 985请求方向。 986 987**系统能力:** SystemCapability.USB.USBManager 988 989| 名称 | 值 | 说明 | 990| --------------------------- | ---- | ------------------------ | 991| USB_REQUEST_DIR_TO_DEVICE | 0 | 写数据,主设备往从设备。 | 992| USB_REQUEST_DIR_FROM_DEVICE | 0x80 | 读数据,从设备往主设备。 | 993 994## FunctionType 995 996USB设备侧功能。 997 998**系统接口:** 此接口为系统接口。 999 1000**系统能力:** SystemCapability.USB.USBManager 1001 1002| 名称 | 值 | 说明 | 1003| ------------ | ---- | ---------- | 1004| NONE | 0 | 没有功能。 | 1005| ACM | 1 | acm功能。 | 1006| ECM | 2 | ecm功能。 | 1007| HDC | 4 | hdc功能。 | 1008| MTP | 8 | 暂不支持。 | 1009| PTP | 16 | 暂不支持。 | 1010| RNDIS | 32 | 暂不支持。 | 1011| MIDI | 64 | 暂不支持。 | 1012| AUDIO_SOURCE | 128 | 暂不支持。 | 1013| NCM | 256 | 暂不支持。 | 1014 1015## PortModeType 1016 1017USB端口模式类型。 1018 1019**系统接口:** 此接口为系统接口。 1020 1021**系统能力:** SystemCapability.USB.USBManager 1022 1023| 名称 | 值 | 说明 | 1024| --------- | ---- | ---------------------------------------------------- | 1025| NONE | 0 | 无。 | 1026| UFP | 1 | 数据上行,需要外部供电。 | 1027| DFP | 2 | 数据下行,对外提供电源。 | 1028| DRP | 3 | 既可以做DFP(Host),也可以做UFP(Device),当前不支持。 | 1029| NUM_MODES | 4 | 当前不支持。 | 1030 1031## PowerRoleType 1032 1033电源角色类型。 1034 1035**系统接口:** 此接口为系统接口。 1036 1037**系统能力:** SystemCapability.USB.USBManager 1038 1039| 名称 | 值 | 说明 | 1040| ------ | ---- | ---------- | 1041| NONE | 0 | 无。 | 1042| SOURCE | 1 | 外部供电。 | 1043| SINK | 2 | 内部供电。 | 1044 1045## DataRoleType 1046 1047数据角色类型。 1048 1049**系统接口:** 此接口为系统接口。 1050 1051**系统能力:** SystemCapability.USB.USBManager 1052 1053| 名称 | 值 | 说明 | 1054| ------ | ---- | ------------ | 1055| NONE | 0 | 无。 | 1056| HOST | 1 | 主设备角色。 | 1057| DEVICE | 2 | 从设备角色。 | 1058 1059