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 510let param: usb.USBControlParams = { 511 request: 0, 512 reqType: usb.USBControlRequestType.USB_REQUEST_TYPE_STANDARD, 513 target: usb.USBRequestTargetType.USB_REQUEST_TARGET_DEVICE, 514 value: 0, 515 index: 0, 516 data: new Uint8Array() 517}; 518 519let devicesList: Array<usb.USBDevice> = usb.getDevices(); 520if (devicesList.length == 0) { 521 console.log(`device list is empty`); 522} 523 524usb.requestRight(devicesList[0].name); 525let devicepipe: usb.USBDevicePipe = usb.connectDevice(devicesList[0]); 526usb.controlTransfer(devicepipe, param).then((ret: number) => { 527 console.log(`controlTransfer = ${ret}`); 528}) 529``` 530 531## usb.bulkTransfer 532 533bulkTransfer(pipe: USBDevicePipe, endpoint: USBEndpoint, buffer: Uint8Array, timeout ?: number): Promise<number> 534 535批量传输。 536 537需要调用[usb.getDevices](#usbgetdevices)获取设备信息列表以及endpoint;再调用[usb.requestRight](#usbrequestright)获取设备请求权限;然后调用[usb.connectDevice](#usbconnectdevice)接口得到返回数据devicepipe之后,再次获取接口[usb.claimInterface](#usbclaiminterface);再调用usb.bulkTransfer接口。 538 539**系统能力:** SystemCapability.USB.USBManager 540 541**参数:** 542 543| 参数名 | 类型 | 必填 | 说明 | 544| -------- | -------- | -------- | -------- | 545| pipe | [USBDevicePipe](#usbdevicepipe) | 是 | 用于确定设备。 | 546| endpoint | [USBEndpoint](#usbendpoint) | 是 | 用于确定传输的端口。 | 547| buffer | Uint8Array | 是 | 用于写入或读取数据的缓冲区。 | 548| timeout | number | 否 | 超时时间(单位:ms),可选参数,默认为0不超时。| 549 550**返回值:** 551 552| 类型 | 说明 | 553| -------- | -------- | 554| Promise<number> | Promise对象,获取传输或接收到的数据块大小。失败返回-1。 | 555 556**示例:** 557 558```ts 559//usb.getDevices 接口返回数据集合,取其中一个设备对象,并获取权限 。 560//把获取到的设备对象作为参数传入usb.connectDevice;当usb.connectDevice接口成功返回之后; 561//才可以调用第三个接口usb.claimInterface.当usb.claimInterface 调用成功以后,再调用该接口。 562let devicesList: Array<usb.USBDevice> = usb.getDevices(); 563if (devicesList.length == 0) { 564 console.log(`device list is empty`); 565} 566 567let device: usb.USBDevice = devicesList[0]; 568usb.requestRight(device.name); 569 570let devicepipe: usb.USBDevicePipe = usb.connectDevice(device); 571let interfaces: usb.USBInterface = device.configs[0].interfaces[0]; 572let endpoint: usb.USBEndpoint = device.configs[0].interfaces[0].endpoints[0]; 573let ret: number = usb.claimInterface(devicepipe, interfaces); 574let buffer = new Uint8Array(128); 575usb.bulkTransfer(devicepipe, endpoint, buffer).then((ret: number) => { 576 console.log(`bulkTransfer = ${ret}`); 577}); 578``` 579 580## usb.closePipe 581 582closePipe(pipe: USBDevicePipe): number 583 584关闭设备消息控制通道。 585 586需要调用[usb.getDevices](#usbgetdevices)获取设备列表;调用[usb.requestRight](#usbrequestright)获取设备请求权限;调用[usb.connectDevice](#usbconnectdevice)得到devicepipe作为参数。 587 588**系统能力:** SystemCapability.USB.USBManager 589 590**参数:** 591 592| 参数名 | 类型 | 必填 | 说明 | 593| -------- | -------- | -------- | -------- | 594| pipe | [USBDevicePipe](#usbdevicepipe) | 是 | 用于确定USB设备消息控制通道。 | 595 596**返回值:** 597 598| 类型 | 说明 | 599| -------- | -------- | 600| number | 关闭设备消息控制通道成功返回0;关闭设备消息控制通道失败返回其他错误码。 | 601 602**示例:** 603 604```ts 605let devicesList: Array<usb.USBDevice> = usb.getDevices(); 606if (devicesList.length == 0) { 607 console.log(`device list is empty`); 608} 609 610usb.requestRight(devicesList[0].name); 611let devicepipe: usb.USBDevicePipe = usb.connectDevice(devicesList[0]); 612let ret: number = usb.closePipe(devicepipe); 613console.log(`closePipe = ${ret}`); 614``` 615 616## usb.usbFunctionsFromString 617 618usbFunctionsFromString(funcs: string): number 619 620在设备模式下,将字符串形式的USB功能列表转化为数字掩码。 621 622**系统接口:** 此接口为系统接口。 623 624**系统能力:** SystemCapability.USB.USBManager 625 626**参数:** 627 628| 参数名 | 类型 | 必填 | 说明 | 629| ------ | ------ | ---- | ---------------------- | 630| funcs | string | 是 | 字符串形式的功能列表。 | 631 632**返回值:** 633 634| 类型 | 说明 | 635| ------ | ------------------ | 636| number | 转化后的数字掩码。 | 637 638**示例:** 639 640```ts 641let funcs: string = "acm"; 642let ret: number = usb.usbFunctionsFromString(funcs); 643``` 644 645## usb.usbFunctionsToString 646 647usbFunctionsToString(funcs: FunctionType): string 648 649在设备模式下,将数字掩码形式的USB功能列表转化为字符串。 650 651**系统接口:** 此接口为系统接口。 652 653**系统能力:** SystemCapability.USB.USBManager 654 655**参数:** 656 657| 参数名 | 类型 | 必填 | 说明 | 658| ------ | ------------------------------ | ---- | ----------------- | 659| funcs | [FunctionType](#functiontype) | 是 | USB功能数字掩码。 | 660 661**返回值:** 662 663| 类型 | 说明 | 664| ------ | ------------------------------ | 665| string | 转化后的字符串形式的功能列表。 | 666 667**示例:** 668 669```ts 670let funcs: number = usb.FunctionType.ACM | usb.FunctionType.ECM; 671let ret: string = usb.usbFunctionsToString(funcs); 672``` 673 674## usb.setCurrentFunctions 675 676setCurrentFunctions(funcs: FunctionType): Promise\<void\> 677 678在设备模式下,设置当前的USB功能列表。 679 680**系统接口:** 此接口为系统接口。 681 682**系统能力:** SystemCapability.USB.USBManager 683 684**参数:** 685 686| 参数名 | 类型 | 必填 | 说明 | 687| ------ | ------------------------------ | ---- | ----------------- | 688| funcs | [FunctionType](#functiontype) | 是 | USB功能数字掩码。 | 689 690**错误码:** 691 692以下错误码的详细介绍请参见[USB错误码](../errorcodes/errorcode-usb.md)。 693 694| 错误码ID | 错误信息 | 695| -------- | ---------------------------------------------------- | 696| 14400002 | Permission denied.The HDC is disabled by the system. | 697 698**返回值:** 699 700| 类型 | 说明 | 701| --------------- | ------------- | 702| Promise\<void\> | Promise对象。 | 703 704**示例:** 705 706```ts 707import {BusinessError} from '@ohos.base'; 708let funcs: number = usb.FunctionType.HDC; 709usb.setCurrentFunctions(funcs).then(() => { 710 console.info('usb setCurrentFunctions successfully.'); 711}).catch((err: BusinessError) => { 712 console.error('usb setCurrentFunctions failed: ' + err.code + ' message: ' + err.message); 713}); 714``` 715 716## usb.getCurrentFunctions 717 718getCurrentFunctions(): FunctionType 719 720在设备模式下,获取当前的USB功能列表的数字组合掩码。 721 722**系统接口:** 此接口为系统接口。 723 724**系统能力:** SystemCapability.USB.USBManager 725 726**返回值:** 727 728| 类型 | 说明 | 729| ------------------------------ | --------------------------------- | 730| [FunctionType](#functiontype) | 当前的USB功能列表的数字组合掩码。 | 731 732**示例:** 733 734```ts 735let ret: number = usb.getCurrentFunctions(); 736``` 737 738## usb.getPorts 739 740getPorts(): Array\<USBPort\> 741 742获取所有物理USB端口描述信息。 743 744**系统接口:** 此接口为系统接口。 745 746**系统能力:** SystemCapability.USB.USBManager 747 748**返回值:** 749 750| 类型 | 说明 | 751| ----------------------------- | --------------------- | 752| [Array\<USBPort\>](#usbport) | USB端口描述信息列表。 | 753 754**示例:** 755 756```ts 757let ret: Array<usb.USBPort> = usb.getPorts(); 758``` 759 760## usb.getSupportedModes 761 762getSupportedModes(portId: number): PortModeType 763 764获取指定的端口支持的模式列表的组合掩码。 765 766**系统接口:** 此接口为系统接口。 767 768**系统能力:** SystemCapability.USB.USBManager 769 770**参数:** 771 772| 参数名 | 类型 | 必填 | 说明 | 773| ------ | ------ | ---- | -------- | 774| portId | number | 是 | 端口号。 | 775 776**返回值:** 777 778| 类型 | 说明 | 779| ------------------------------ | -------------------------- | 780| [PortModeType](#portmodetype) | 支持的模式列表的组合掩码。 | 781 782**示例:** 783 784```ts 785let ret: number = usb.getSupportedModes(0); 786``` 787 788## usb.setPortRoles 789 790setPortRoles(portId: number, powerRole: PowerRoleType, dataRole: DataRoleType): Promise\<void\> 791 792设置指定的端口支持的角色模式,包含充电角色、数据传输角色。 793 794**系统接口:** 此接口为系统接口。 795 796**系统能力:** SystemCapability.USB.USBManager 797 798**参数:** 799 800| 参数名 | 类型 | 必填 | 说明 | 801| --------- | -------------------------------- | ---- | ---------------- | 802| portId | number | 是 | 端口号。 | 803| powerRole | [PowerRoleType](#powerroletype) | 是 | 充电的角色。 | 804| dataRole | [DataRoleType](#dataroletype) | 是 | 数据传输的角色。 | 805 806**返回值:** 807 808| 类型 | 说明 | 809| --------------- | ------------- | 810| Promise\<void\> | Promise对象。 | 811 812**示例:** 813 814```ts 815import {BusinessError} from '@ohos.base'; 816let portId: number = 1; 817usb.setPortRoles(portId, usb.PowerRoleType.SOURCE, usb.DataRoleType.HOST).then(() => { 818 console.info('usb setPortRoles successfully.'); 819}).catch((err: BusinessError) => { 820 console.error('usb setPortRoles failed: ' + err.code + ' message: ' + err.message); 821}); 822``` 823 824## USBEndpoint 825 826通过USB发送和接收数据的端口。通过[USBInterface](#usbinterface)获取。 827 828**系统能力:** SystemCapability.USB.USBManager 829 830| 名称 | 类型 | 必填 |说明 | 831| ------------- | ------------------------------------------- | ------------- |------------- | 832| address | number | 是 |端点地址。 | 833| attributes | number | 是 |端点属性。 | 834| interval | number | 是 |端点间隔。 | 835| maxPacketSize | number | 是 |端点最大数据包大小。 | 836| direction | [USBRequestDirection](#usbrequestdirection) | 是 |端点的方向。 | 837| number | number | 是 |端点号。 | 838| type | number | 是 |端点类型。 | 839| interfaceId | number | 是 |端点所属的接口的唯一标识。 | 840 841## USBInterface 842 843一个[USBConfiguration](#usbconfiguration)中可以含有多个USBInterface,每个USBInterface提供一个功能。 844 845**系统能力:** SystemCapability.USB.USBManager 846 847| 名称 | 类型 | 必填 |说明 | 848| ---------------- | ---------------------------------------- | ------------- |--------------------- | 849| id | number | 是 |接口的唯一标识。 | 850| protocol | number | 是 |接口的协议。 | 851| clazz | number | 是 |设备类型。 | 852| subClass | number | 是 |设备子类。 | 853| alternateSetting | number | 是 |在同一个接口中的多个描述符中进行切换设置。 | 854| name | string | 是 |接口名称。 | 855| endpoints | Array<[USBEndpoint](#usbendpoint)> | 是 |当前接口所包含的端点。 | 856 857## USBConfiguration 858 859USB配置,一个[USBDevice](#usbdevice)中可以含有多个配置。 860 861**系统能力:** SystemCapability.USB.USBManager 862 863| 名称 | 类型 | 必填 |说明 | 864| -------------- | ------------------------------------------------ | --------------- |--------------- | 865| id | number | 是 |配置的唯一标识。 | 866| attributes | number | 是 |配置的属性。 | 867| maxPower | number | 是 |最大功耗,以毫安为单位。 | 868| name | string | 是 |配置的名称,可以为空。 | 869| isRemoteWakeup | boolean | 是 |检查当前配置是否支持远程唤醒。 | 870| isSelfPowered | boolean | 是 | 检查当前配置是否支持独立电源。 | 871| interfaces | Array <[USBInterface](#usbinterface)> | 是 |配置支持的接口属性。 | 872 873## USBDevice 874 875USB设备信息。 876 877**系统能力:** SystemCapability.USB.USBManager 878 879| 名称 | 类型 | 必填 |说明 | 880| ---------------- | ------------------------------------ | ---------- |---------- | 881| busNum | number | 是 |总线地址。 | 882| devAddress | number | 是 |设备地址。 | 883| serial | string | 是 |序列号。 | 884| name | string | 是 |设备名字。 | 885| manufacturerName | string | 是 | 产商信息。 | 886| productName | string | 是 |产品信息。 | 887| version | string | 是 |版本。 | 888| vendorId | number | 是 |厂商ID。 | 889| productId | number | 是 |产品ID。 | 890| clazz | number | 是 |设备类。 | 891| subClass | number | 是 |设备子类。 | 892| protocol | number | 是 |设备协议码。 | 893| configs | Array<[USBConfiguration](#usbconfiguration)> | 是 |设备配置描述符信息。 | 894 895## USBDevicePipe 896 897USB设备消息传输通道,用于确定设备。 898 899**系统能力:** SystemCapability.USB.USBManager 900 901| 名称 | 类型 | 必填 |说明 | 902| ---------- | ------ | ----- |----- | 903| busNum | number |是 | 总线地址。 | 904| devAddress | number |是 | 设备地址。 | 905 906## USBControlParams 907 908控制传输参数。 909 910**系统能力:** SystemCapability.USB.USBManager 911 912| 名称 | 类型 | 必填 |说明 | 913| ------- | ----------------------------------------------- | ---------------- |---------------- | 914| request | number | 是 |请求类型。 | 915| target | [USBRequestTargetType](#usbrequesttargettype) | 是 |请求目标类型。 | 916| reqType | [USBControlRequestType](#usbcontrolrequesttype) | 是 |请求控制类型。 | 917| value | number | 是 |请求参数。 | 918| index | number | 是 |请求参数value对应的索引值。 | 919| data | Uint8Array | 是 |用于写入或读取的缓冲区。 | 920 921## USBPort 922 923USB设备端口。 924 925**系统接口:** 此接口为系统接口。 926 927**系统能力:** SystemCapability.USB.USBManager 928 929| 名称 | 类型 | 必填 |说明 | 930| -------------- | ------------------------------- | ------------------- |------------------------ | 931| id | number | 是 |USB端口唯一标识。 | 932| supportedModes | [PortModeType](#portmodetype) | 是 |USB端口所支持的模式的数字组合掩码。 | 933| status | [USBPortStatus](#usbportstatus) | 是 |USB端口角色。 | 934 935## USBPortStatus 936 937USB设备端口角色信息。 938 939**系统接口:** 此接口为系统接口。 940 941**系统能力:** SystemCapability.USB.USBManager 942 943| 名称 | 类型 | 必填 |说明 | 944| ---------------- | -------- | ---------------- |---------------------- | 945| currentMode | number | 是 |当前的USB模式。 | 946| currentPowerRole | number | 是 |当前设备充电模式。 | 947| currentDataRole | number | 是 |当前设备数据传输模式。 | 948 949## USBRequestTargetType 950 951请求目标类型。 952 953**系统能力:** SystemCapability.USB.USBManager 954 955| 名称 | 值 | 说明 | 956| ---------------------------- | ---- | ------ | 957| USB_REQUEST_TARGET_DEVICE | 0 | 设备。 | 958| USB_REQUEST_TARGET_INTERFACE | 1 | 接口。 | 959| USB_REQUEST_TARGET_ENDPOINT | 2 | 端点。 | 960| USB_REQUEST_TARGET_OTHER | 3 | 其他。 | 961 962## USBControlRequestType 963 964控制请求类型。 965 966**系统能力:** SystemCapability.USB.USBManager 967 968| 名称 | 值 | 说明 | 969| ------------------------- | ---- | ------ | 970| USB_REQUEST_TYPE_STANDARD | 0 | 标准。 | 971| USB_REQUEST_TYPE_CLASS | 1 | 类。 | 972| USB_REQUEST_TYPE_VENDOR | 2 | 厂商。 | 973 974## USBRequestDirection 975 976请求方向。 977 978**系统能力:** SystemCapability.USB.USBManager 979 980| 名称 | 值 | 说明 | 981| --------------------------- | ---- | ------------------------ | 982| USB_REQUEST_DIR_TO_DEVICE | 0 | 写数据,主设备往从设备。 | 983| USB_REQUEST_DIR_FROM_DEVICE | 0x80 | 读数据,从设备往主设备。 | 984 985## FunctionType 986 987USB设备侧功能。 988 989**系统接口:** 此接口为系统接口。 990 991**系统能力:** SystemCapability.USB.USBManager 992 993| 名称 | 值 | 说明 | 994| ------------ | ---- | ---------- | 995| NONE | 0 | 没有功能。 | 996| ACM | 1 | acm功能。 | 997| ECM | 2 | ecm功能。 | 998| HDC | 4 | hdc功能。 | 999| MTP | 8 | 暂不支持。 | 1000| PTP | 16 | 暂不支持。 | 1001| RNDIS | 32 | 暂不支持。 | 1002| MIDI | 64 | 暂不支持。 | 1003| AUDIO_SOURCE | 128 | 暂不支持。 | 1004| NCM | 256 | 暂不支持。 | 1005 1006## PortModeType 1007 1008USB端口模式类型。 1009 1010**系统接口:** 此接口为系统接口。 1011 1012**系统能力:** SystemCapability.USB.USBManager 1013 1014| 名称 | 值 | 说明 | 1015| --------- | ---- | ---------------------------------------------------- | 1016| NONE | 0 | 无。 | 1017| UFP | 1 | 数据上行,需要外部供电。 | 1018| DFP | 2 | 数据下行,对外提供电源。 | 1019| DRP | 3 | 既可以做DFP(Host),也可以做UFP(Device),当前不支持。 | 1020| NUM_MODES | 4 | 当前不支持。 | 1021 1022## PowerRoleType 1023 1024电源角色类型。 1025 1026**系统接口:** 此接口为系统接口。 1027 1028**系统能力:** SystemCapability.USB.USBManager 1029 1030| 名称 | 值 | 说明 | 1031| ------ | ---- | ---------- | 1032| NONE | 0 | 无。 | 1033| SOURCE | 1 | 外部供电。 | 1034| SINK | 2 | 内部供电。 | 1035 1036## DataRoleType 1037 1038数据角色类型。 1039 1040**系统接口:** 此接口为系统接口。 1041 1042**系统能力:** SystemCapability.USB.USBManager 1043 1044| 名称 | 值 | 说明 | 1045| ------ | ---- | ------------ | 1046| NONE | 0 | 无。 | 1047| HOST | 1 | 主设备角色。 | 1048| DEVICE | 2 | 从设备角色。 | 1049 1050