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