1# @ohos.usbManager (USB管理)(系统接口) 2 3本模块主要提供管理USB设备的相关功能,包括主设备上查询USB设备列表、批量数据传输、控制命令传输、权限控制等;从设备上端口管理、功能切换及查询等。 4 5> **说明:** 6> 7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.usbManager (USB管理)](js-apis-usbManager.md)。 9 10## 导入模块 11 12```ts 13import { usbManager } from '@kit.BasicServicesKit'; 14``` 15 16## addRight <sup>(deprecated)</sup> 17 18addRight(bundleName: string, deviceName: string): boolean 19 20添加软件包访问设备的权限。系统应用默认拥有访问设备权限,调用此接口不会产生影响。 21 22usbManager.requestRight (#usbrequestright)会触发弹框请求用户授权;addRight不会触发弹框,而是直接添加软件包访问设备的权限。 23 24**说明:** 25 26> 从 API version 9开始支持,从API version 12开始废弃。建议使用 [addDeviceAccessRight](#adddeviceaccessright12) 替代。 27 28**系统接口:** 此接口为系统接口。 29 30**系统能力:** SystemCapability.USB.USBManager 31 32**参数:** 33 34| 参数名 | 类型 | 必填 | 说明 | 35| ---------- | ------ | ---- | ------------ | 36| deviceName | string | 是 | 设备名称。 | 37| bundleName | string | 是 | 软件包名称。 | 38 39**错误码:** 40 41以下错误码的详细介绍请参见[USB服务错误码](errorcode-usb.md)。 42 43| 错误码ID | 错误信息 | 44| -------- | ------------------------------------------------------------------------------------------------------- | 45| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 46| 202 | Permission denied. Normal application do not have permission to use system api. | 47 48**返回值:** 49 50| 类型 | 说明 | 51| ------- | ------------------------------------------------------------------------- | 52| boolean | 返回权限添加结果。返回true表示权限添加成功;返回false则表示权限添加失败。 | 53 54**示例:** 55 56```ts 57let devicesName: string = "1-1"; 58let bundleName: string = "com.example.hello"; 59if (usbManager.addRight(bundleName, devicesName)) { 60 console.log(`Succeed in adding right`); 61} 62``` 63 64## usbFunctionsFromString<sup>(deprecated)</sup> 65 66usbFunctionsFromString(funcs: string): number 67 68在设备模式下,将字符串形式的USB功能列表转化为数字掩码。 69 70**说明:** 71 72> 从 API version 9开始支持,从API version 12开始废弃。建议使用 [getFunctionsFromString](#getfunctionsfromstring12) 替代。 73 74**系统接口:** 此接口为系统接口。 75 76**系统能力:** SystemCapability.USB.USBManager 77 78**参数:** 79 80| 参数名 | 类型 | 必填 | 说明 | 81| ------ | ------ | ---- | ---------------------- | 82| funcs | string | 是 | 字符串形式的功能列表。 | 83 84**错误码:** 85 86以下错误码的详细介绍请参见[USB服务错误码](errorcode-usb.md)。 87 88| 错误码ID | 错误信息 | 89| -------- | ------------------------------------------------------------------------------------------------------- | 90| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 91| 202 | Permission denied. Normal application do not have permission to use system api. | 92 93**返回值:** 94 95| 类型 | 说明 | 96| ------ | ------------------ | 97| number | 转化后的数字掩码。 | 98 99**示例:** 100 101```ts 102let funcs: string = "acm"; 103let ret: number = usbManager.usbFunctionsFromString(funcs); 104``` 105 106## usbFunctionsToString<sup>(deprecated)</sup> 107 108usbFunctionsToString(funcs: FunctionType): string 109 110在设备模式下,将数字掩码形式的USB功能列表转化为字符串。 111 112**说明:** 113 114> 从 API version 9开始支持,从API version 12开始废弃。建议使用 [getStringFromFunctions](#getstringfromfunctions12) 替代。 115 116**系统接口:** 此接口为系统接口。 117 118**系统能力:** SystemCapability.USB.USBManager 119 120**参数:** 121 122| 参数名 | 类型 | 必填 | 说明 | 123| ------ | ----------------------------- | ---- | ----------------- | 124| funcs | [FunctionType](#functiontype) | 是 | USB功能数字掩码。 | 125 126**错误码:** 127 128以下错误码的详细介绍请参见[USB服务错误码](errorcode-usb.md)。 129 130| 错误码ID | 错误信息 | 131| -------- | ------------------------------------------------------------------------------------------------------- | 132| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 133| 202 | Permission denied. Normal application do not have permission to use system api. | 134 135**返回值:** 136 137| 类型 | 说明 | 138| ------ | ------------------------------ | 139| string | 转化后的字符串形式的功能列表。 | 140 141**示例:** 142 143```ts 144let funcs: number = usbManager.FunctionType.ACM | usb.FunctionType.ECM; 145let ret: string = usbManager.usbFunctionsToString(funcs); 146``` 147 148## setCurrentFunctions<sup>(deprecated)</sup> 149 150setCurrentFunctions(funcs: FunctionType): Promise\<void\> 151 152在设备模式下,设置当前的USB功能列表。 153 154**说明:** 155 156> 从 API version 9开始支持,从API version 12开始废弃。建议使用 [setDeviceFunctions](#setdevicefunctions12) 替代。 157 158**系统接口:** 此接口为系统接口。 159 160**系统能力:** SystemCapability.USB.USBManager 161 162**参数:** 163 164| 参数名 | 类型 | 必填 | 说明 | 165| ------ | ----------------------------- | ---- | ----------------- | 166| funcs | [FunctionType](#functiontype) | 是 | USB功能数字掩码。 | 167 168**错误码:** 169 170以下错误码的详细介绍请参见[USB服务错误码](errorcode-usb.md)。 171 172| 错误码ID | 错误信息 | 173| -------- | ------------------------------------------------------------------------------------------------------- | 174| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 175| 14400002 | Permission denied. The HDC is disabled by the system. | 176 177**返回值:** 178 179| 类型 | 说明 | 180| ------------------- | ------------- | 181| Promise\<**void**\> | Promise对象。 | 182 183**示例:** 184 185```ts 186import {BusinessError} from '@kit.BasicServicesKit'; 187let funcs: number = usbManager.FunctionType.HDC; 188usbManager.setCurrentFunctions(funcs).then(() => { 189 console.info('usb setCurrentFunctions successfully.'); 190}).catch((err: BusinessError) => { 191 console.error('usb setCurrentFunctions failed: ' + err.code + ' message: ' + err.message); 192}); 193``` 194 195## getCurrentFunctions<sup>(deprecated)</sup> 196 197getCurrentFunctions(): FunctionType 198 199在设备模式下,获取当前的USB功能列表的数字组合掩码。 200 201**说明:** 202 203> 从 API version 9开始支持,从API version 12开始废弃。建议使用 [getDeviceFunctions](#getdevicefunctions12) 替代。 204 205**系统接口:** 此接口为系统接口。 206 207**系统能力:** SystemCapability.USB.USBManager 208 209**错误码:** 210 211以下错误码的详细介绍请参见[USB服务错误码](errorcode-usb.md)。 212 213| 错误码ID | 错误信息 | 214| -------- | ------------------------------------------------------------------------------- | 215| 401 | Parameter error. No parameters are required. | 216| 202 | Permission denied. Normal application do not have permission to use system api. | 217 218**返回值:** 219 220| 类型 | 说明 | 221| ----------------------------- | --------------------------------- | 222| [FunctionType](#functiontype) | 当前的USB功能列表的数字组合掩码。 | 223 224**示例:** 225 226```ts 227let ret: number = usbManager.getCurrentFunctions(); 228``` 229 230## getPorts<sup>(deprecated)</sup> 231 232getPorts(): Array\<USBPort\> 233 234获取所有物理USB端口描述信息。 235 236**说明:** 237 238> 从 API version 9开始支持,从API version 12开始废弃。建议使用 [getPortList](#getportlist12) 替代。 239 240**系统接口:** 此接口为系统接口。 241 242**系统能力:** SystemCapability.USB.USBManager 243 244**错误码:** 245 246以下错误码的详细介绍请参见[USB服务错误码](errorcode-usb.md)。 247 248| 错误码ID | 错误信息 | 249| -------- | ------------------------------------------------------------------------------- | 250| 401 | Parameter error. No parameters are required. | 251| 202 | Permission denied. Normal application do not have permission to use system api. | 252 253**返回值:** 254 255| 类型 | 说明 | 256| -------------------------- | --------------------- | 257| Array<[USBPort](#usbport)> | USB端口描述信息列表。 | 258 259**示例:** 260 261```ts 262let ret: Array<usbManager.USBPort> = usbManager.getPorts(); 263``` 264 265## getSupportedModes(deprecated) 266 267getSupportedModes(portId: number): PortModeType 268 269获取指定的端口支持的模式列表的组合掩码。 270 271**说明:** 272 273> 从 API version 9开始支持,从API version 12开始废弃。建议使用 [getPortSupportModes](#getportlist12) 替代。 274 275**系统接口:** 此接口为系统接口。 276 277**系统能力:** SystemCapability.USB.USBManager 278 279**参数:** 280 281| 参数名 | 类型 | 必填 | 说明 | 282| ------ | ------ | ---- | -------- | 283| portId | number | 是 | 端口号。 | 284 285**错误码:** 286 287以下错误码的详细介绍请参见[USB服务错误码](errorcode-usb.md)。 288 289| 错误码ID | 错误信息 | 290| -------- | ------------------------------------------------------------------------------------------------------- | 291| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 292| 202 | Permission denied. Normal application do not have permission to use system api. | 293 294**返回值:** 295 296| 类型 | 说明 | 297| ----------------------------- | -------------------------- | 298| [PortModeType](#portmodetype) | 支持的模式列表的组合掩码。 | 299 300**示例:** 301 302```ts 303let ret: number = usbManager.getSupportedModes(0); 304``` 305 306## setPortRoles<sup>(deprecated)</sup> 307 308setPortRoles(portId: number, powerRole: PowerRoleType, dataRole: DataRoleType): Promise\<void\> 309 310设置指定的端口支持的角色模式,包含充电角色、数据传输角色。 311 312**说明:** 313 314> 从 API version 9开始支持,从API version 12开始废弃。建议使用 [setPortRoleTypes](#setportroletypes12) 替代。 315 316**系统接口:** 此接口为系统接口。 317 318**系统能力:** SystemCapability.USB.USBManager 319 320**参数:** 321 322| 参数名 | 类型 | 必填 | 说明 | 323| --------- | ------------------------------- | ---- | ---------------- | 324| portId | number | 是 | 端口号。 | 325| powerRole | [PowerRoleType](#powerroletype) | 是 | 充电的角色。 | 326| dataRole | [DataRoleType](#dataroletype) | 是 | 数据传输的角色。 | 327 328**错误码:** 329 330以下错误码的详细介绍请参见[USB服务错误码](errorcode-usb.md)。 331 332| 错误码ID | 错误信息 | 333| -------- | ------------------------------------------------------------------------------------------------------- | 334| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 335 336**返回值:** 337 338| 类型 | 说明 | 339| ------------------- | ------------- | 340| Promise\<**void**\> | Promise对象。 | 341 342**示例:** 343 344```ts 345import {BusinessError} from '@kit.BasicServicesKit'; 346let portId: number = 1; 347usbManager.setPortRoles(portId, usbManager.PowerRoleType.SOURCE, ususbManagerb.DataRoleType.HOST).then(() => { 348 console.info('usb setPortRoles successfully.'); 349}).catch((err: BusinessError) => { 350 console.error('usb setPortRoles failed: ' + err.code + ' message: ' + err.message); 351}); 352``` 353 354## addDeviceAccessRight<sup>12+</sup> 355 356addDeviceAccessRight(tokenId: string, deviceName: string): boolean 357 358添加软件包访问设备的权限。系统应用默认拥有访问设备权限,调用此接口不会产生影响。 359 360usbManager.requestRight (#usbrequestright)会触发弹框请求用户授权;addDeviceAccessRight不会触发弹框,而是直接添加软件包访问设备的权限。 361 362**说明:** 363 364> 从 API version 12开始支持。 365 366**系统接口:** 此接口为系统接口。 367 368**需要权限:** ohos.permission.MANAGE_USB_CONFIG 369 370**系统能力:** SystemCapability.USB.USBManager 371 372**参数:** 373 374| 参数名 | 类型 | 必填 | 说明 | 375| ---------- | ------ | ---- | --------------- | 376| deviceName | string | 是 | 设备名称。 | 377| tokenId | string | 是 | 软件包tokenId。 | 378 379**错误码:** 380 381以下错误码的详细介绍请参见[USB服务错误码](errorcode-usb.md)。 382 383| 错误码ID | 错误信息 | 384| -------- | ------------------------------------------------------------------------------------------------------- | 385| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 386| 202 | Permission denied. Normal application do not have permission to use system api. | 387 388**返回值:** 389 390| 类型 | 说明 | 391| ------- | ------------------------------------------------------------------------- | 392| boolean | 返回权限添加结果。返回true表示权限添加成功;返回false则表示权限添加失败。 | 393 394**示例:** 395 396```ts 397import { bundleManager } from '@kit.AbilityKit'; 398import { BusinessError } from '@kit.BasicServicesKit'; 399let devicesName: string = "1-1"; 400let tokenId: string = ""; 401 402 try { 403 let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT; 404 bundleManager.getBundleInfoForSelf(bundleFlags).then((bundleInfo) => { 405 console.info('testTag', 'getBundleInfoForSelf successfully. Data: %{public}s', JSON.stringify(bundleInfo)); 406 let token = bundleInfo.appInfo.accessTokenId; 407 tokenId = token.toString(); 408 if (usbManager.addDeviceAccessRight(tokenId, devicesName)) { 409 console.log(`Succeed in adding right`); 410 } 411 }).catch((err : BusinessError) => { 412 console.error('testTag getBundleInfoForSelf failed' ); 413 }); 414 } catch (err) { 415 console.error('testTag failed'); 416 } 417``` 418 419## getFunctionsFromString<sup>12+</sup> 420 421getFunctionsFromString(funcs: string): number 422 423在设备模式下,将字符串形式的USB功能列表转化为数字掩码。 424 425**说明:** 426 427> 从 API version 12开始支持。 428 429**系统接口:** 此接口为系统接口。 430 431**需要权限:** ohos.permission.MANAGE_USB_CONFIG 432 433**系统能力:** SystemCapability.USB.USBManager 434 435**参数:** 436 437| 参数名 | 类型 | 必填 | 说明 | 438| ------ | ------ | ---- | ---------------------- | 439| funcs | string | 是 | 字符串形式的功能列表。 | 440 441**错误码:** 442 443以下错误码的详细介绍请参见[USB服务错误码](errorcode-usb.md)。 444 445| 错误码ID | 错误信息 | 446| -------- | ------------------------------------------------------------------------------- | 447| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 448| 202 | Permission denied. Normal application do not have permission to use system api. | 449 450**返回值:** 451 452| 类型 | 说明 | 453| ------ | ------------------ | 454| number | 转化后的数字掩码。 | 455 456**示例:** 457 458```ts 459let funcs: string = "acm"; 460let ret: number = usbManager.getFunctionsFromString(funcs); 461``` 462 463## getStringFromFunctions<sup>12+</sup> 464 465getStringFromFunctions(funcs: FunctionType): string 466 467在设备模式下,将数字掩码形式的USB功能列表转化为字符串。 468 469**说明:** 470 471> 从 API version 12开始支持。 472 473**系统接口:** 此接口为系统接口。 474 475**需要权限:** ohos.permission.MANAGE_USB_CONFIG 476 477**系统能力:** SystemCapability.USB.USBManager 478 479**参数:** 480 481| 参数名 | 类型 | 必填 | 说明 | 482| ------ | ----------------------------- | ---- | ----------------- | 483| funcs | [FunctionType](#functiontype) | 是 | USB功能数字掩码。 | 484 485**错误码:** 486 487以下错误码的详细介绍请参见[USB服务错误码](errorcode-usb.md)。 488 489| 错误码ID | 错误信息 | 490| -------- | ------------------------------------------------------------------------------------------------------- | 491| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 492| 202 | Permission denied. Normal application do not have permission to use system api. | 493 494**返回值:** 495 496| 类型 | 说明 | 497| ------ | ------------------------------ | 498| string | 转化后的字符串形式的功能列表。 | 499 500**示例:** 501 502```ts 503let funcs: number = usbManager.FunctionType.ACM | usbManager.FunctionType.ECM; 504let ret: string = usbManager.getStringFromFunctions(funcs); 505``` 506 507## setDeviceFunctions<sup>12+</sup> 508 509setDeviceFunctions(funcs: FunctionType): Promise\<void\> 510 511在设备模式下,设置当前的USB功能列表。 512 513**说明:** 514 515> 从 API version 12开始支持。 516 517**系统接口:** 此接口为系统接口。 518 519**需要权限:** ohos.permission.MANAGE_USB_CONFIG 520 521**系统能力:** SystemCapability.USB.USBManager 522 523**参数:** 524 525| 参数名 | 类型 | 必填 | 说明 | 526| ------ | ----------------------------- | ---- | ----------------- | 527| funcs | [FunctionType](#functiontype) | 是 | USB功能数字掩码。 | 528 529**错误码:** 530 531以下错误码的详细介绍请参见[USB服务错误码](errorcode-usb.md)。 532 533| 错误码ID | 错误信息 | 534| -------- | ------------------------------------------------------------------------------------------------------- | 535| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 536| 202 | Permission denied. Normal application do not have permission to use system api. | 537| 14400002 | Permission denied. The HDC is disabled by the system. | 538| 14400006 | Unsupported operation. The function is not supported. | 539 540**返回值:** 541 542| 类型 | 说明 | 543| ------------------- | ------------- | 544| Promise\<**void**\> | Promise对象。 | 545 546**示例:** 547 548```ts 549import { BusinessError } from '@kit.BasicServicesKit'; 550let funcs: number = usbManager.FunctionType.HDC; 551usbManager.setDeviceFunctions(funcs).then(() => { 552 console.info('usb setDeviceFunctions successfully.'); 553}).catch((err : BusinessError) => { 554 console.error('usb setDeviceFunctions failed: ' + err.code + ' message: ' + err.message); 555}); 556``` 557 558## getDeviceFunctions<sup>12+</sup> 559 560getDeviceFunctions(): FunctionType 561 562在设备模式下,获取当前的USB功能列表的数字组合掩码。 563 564**说明:** 565 566> 从 API version 12开始支持。 567 568**系统接口:** 此接口为系统接口。 569 570**需要权限:** ohos.permission.MANAGE_USB_CONFIG 571 572**系统能力:** SystemCapability.USB.USBManager 573 574**错误码:** 575 576以下错误码的详细介绍请参见[USB服务错误码](errorcode-usb.md)。 577 578| 错误码ID | 错误信息 | 579| -------- | ------------------------------------------------------------------------------- | 580| 401 | Parameter error. No parameters are required. | 581| 202 | Permission denied. Normal application do not have permission to use system api. | 582 583**返回值:** 584 585| 类型 | 说明 | 586| ----------------------------- | --------------------------------- | 587| [FunctionType](#functiontype) | 当前的USB功能列表的数字组合掩码。 | 588 589**示例:** 590 591```ts 592let ret: number = usbManager.getDeviceFunctions(); 593``` 594 595## getPortList<sup>12+</sup> 596 597getPortList(): Array\<USBPort\> 598 599获取所有物理USB端口描述信息。 600 601**说明:** 602 603> 从 API version 12开始支持。 604 605**系统接口:** 此接口为系统接口。 606 607**需要权限:** ohos.permission.MANAGE_USB_CONFIG 608 609**系统能力:** SystemCapability.USB.USBManager 610 611**错误码:** 612 613以下错误码的详细介绍请参见[USB服务错误码](errorcode-usb.md)。 614 615| 错误码ID | 错误信息 | 616| -------- | ------------------------------------------------------------------------------------------------------- | 617| 202 | Permission denied. Normal application do not have permission to use system api. | 618 619**返回值:** 620 621| 类型 | 说明 | 622| -------------------------- | --------------------- | 623| Array<[USBPort](#usbport)> | USB端口描述信息列表。 | 624 625**示例:** 626 627```ts 628let ret: Array<usbManager.USBPort> = usbManager.getPortList(); 629``` 630 631## getPortSupportModes<sup>12+</sup> 632 633getPortSupportModes(portId: number): PortModeType 634 635获取指定的端口支持的模式列表的组合掩码。 636 637**系统接口:** 此接口为系统接口。 638 639**需要权限:** ohos.permission.MANAGE_USB_CONFIG 640 641**系统能力:** SystemCapability.USB.USBManager 642 643**参数:** 644 645| 参数名 | 类型 | 必填 | 说明 | 646| ------ | ------ | ---- | -------- | 647| portId | number | 是 | 端口号。 | 648 649**错误码:** 650 651以下错误码的详细介绍请参见[USB服务错误码](errorcode-usb.md)。 652 653| 错误码ID | 错误信息 | 654| -------- | ------------------------------------------------------------------------------------------------------- | 655| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 656| 202 | Permission denied. Normal application do not have permission to use system api. | 657 658**返回值:** 659 660| 类型 | 说明 | 661| ----------------------------- | -------------------------- | 662| [PortModeType](#portmodetype) | 支持的模式列表的组合掩码。 | 663 664**示例:** 665 666```ts 667let ret: number = usbManager.getSupportedModes(0); 668``` 669 670## setPortRoleTypes<sup>12+</sup> 671 672setPortRoleTypes(portId: number, powerRole: PowerRoleType, dataRole: DataRoleType): Promise\<void\> 673 674设置指定的端口支持的角色模式,包含充电角色、数据传输角色。 675 676**说明:** 677 678> 从 API version 12开始支持。 679 680**系统接口:** 此接口为系统接口。 681 682**需要权限:** ohos.permission.MANAGE_USB_CONFIG 683 684**系统能力:** SystemCapability.USB.USBManager 685 686**参数:** 687 688| 参数名 | 类型 | 必填 | 说明 | 689| --------- | ------------------------------- | ---- | ---------------- | 690| portId | number | 是 | 端口号。 | 691| powerRole | [PowerRoleType](#powerroletype) | 是 | 充电的角色。 | 692| dataRole | [DataRoleType](#dataroletype) | 是 | 数据传输的角色。 | 693 694**错误码:** 695 696以下错误码的详细介绍请参见[USB服务错误码](errorcode-usb.md)。 697 698| 错误码ID | 错误信息 | 699| -------- | ------------------------------------------------------------------------------------------------------- | 700| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 701| 202 | Permission denied. Normal application do not have permission to use system api. | 702| 14400003 | Unsupported operation. The current device does not support port role switching. | 703 704**返回值:** 705 706| 类型 | 说明 | 707| ------------------- | ------------- | 708| Promise\<**void**\> | Promise对象。 | 709 710**示例:** 711 712```ts 713import { BusinessError } from '@kit.BasicServicesKit'; 714let portId: number = 1; 715usbManager.setPortRoleTypes(portId, usbManager.PowerRoleType.SOURCE, usbManager.DataRoleType.HOST).then(() => { 716 console.info('usb setPortRoleTypes successfully.'); 717}).catch((err : BusinessError) => { 718 console.error('usb setPortRoleTypes failed: ' + err.code + ' message: ' + err.message); 719}); 720``` 721 722## addAccessoryRight<sup>14+<sup> 723 724addAccessoryRight(tokenId: number, accessory: USBAccessory): void; 725 726为应用程序添加访问USB配件权限。 727 728usbManager.requestAccessoryRight会触发弹窗请求用户授权;addAccessoryRight不会触发弹窗,而是直接添加应用程序访问设备的权限。 729 730**系统接口:** 此接口为系统接口 731 732**需要权限:** ohos.permission.MANAGE_USB_CONFIG 733 734**系统能力:** SystemCapability.USB.USBManager 735 736**参数:** 737 738| 参数名 | 类型 | 必填 | 说明 | 739| --------- | ------------ | ---- | ------------------------ | 740| tokenId | number | 是 | 应用程序tokenId。 | 741| accessory | USBAccessory | 是 | USB配件。 | 742 743**错误码:** 744 745以下错误码的详细介绍请参见[USB服务错误码](errorcode-usb.md)。 746 747| 错误码ID | 错误信息 | 748| -------- | ------------------------------------------------------------ | 749| 201 | The permission check failed. | 750| 202 | Permission denied. Normal application do not have permission to use system api. | 751| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 752| 14400004 | Service exception. Possible causes: 1. No accessory is plugged in. | 753| 14400005 | Database operation exception. | 754 755**示例:** 756 757```ts 758import { hilog } from '@kit.PerformanceAnalysisKit'; 759import { bundleManager } from '@kit.AbilityKit'; 760try { 761 let accList: usbManager.USBAccessory[] = usbManager.getAccessoryList() 762 let flags = bundleManager.BundleFlah.GET_BUNDLE_INFO_WITH_APPLICATION | bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY 763 let bundleInfo = await bundleManager.getBundleInfoForSelf(flags) 764 let tokenId: number = bundleInfo.appInfo.accessTokenId 765 usbManager.addAccessoryRight(tokenId, accList[0]) 766 hilog.info(0, 'testTag ui', `addAccessoryRight success`) 767} catch (error) { 768 hilog.info(0, 'testTag ui', `addAccessoryRight error ${error.code}, message is ${error.message}`) 769} 770``` 771 772## USBPort 773 774USB设备端口。 775 776**系统接口:** 此接口为系统接口。 777 778**系统能力:** SystemCapability.USB.USBManager 779 780| 名称 | 类型 | 必填 | 说明 | 781| -------------- | ------------------------------- | ---- | ----------------------------------- | 782| id | number | 是 | USB端口唯一标识。 | 783| supportedModes | [PortModeType](#portmodetype) | 是 | USB端口所支持的模式的数字组合掩码。 | 784| status | [USBPortStatus](#usbportstatus) | 是 | USB端口角色。 | 785 786## USBPortStatus 787 788USB设备端口角色信息。 789 790**系统接口:** 此接口为系统接口。 791 792**系统能力:** SystemCapability.USB.USBManager 793 794| 名称 | 类型 | 必填 | 说明 | 795| ---------------- | ------ | ---- | ---------------------- | 796| currentMode | number | 是 | 当前的USB模式。 | 797| currentPowerRole | number | 是 | 当前设备充电模式。 | 798| currentDataRole | number | 是 | 当前设备数据传输模式。 | 799 800## FunctionType 801 802USB设备侧功能。 803 804**系统接口:** 此接口为系统接口。 805 806**系统能力:** SystemCapability.USB.USBManager 807 808| 名称 | 值 | 说明 | 809| ------------ | --- | ---------- | 810| NONE | 0 | 没有功能。 | 811| ACM | 1 | acm功能。 | 812| ECM | 2 | ecm功能。 | 813| HDC | 4 | hdc功能。 | 814| MTP | 8 | 媒体传输(暂不支持)。 | 815| PTP | 16 | 图片传输(暂不支持)。 | 816| RNDIS | 32 | 网络共享(暂不支持)。 | 817| MIDI | 64 | midi功能(暂不支持)。 | 818| AUDIO_SOURCE | 128 | 音频功能(暂不支持)。 | 819| NCM | 256 | ncm传输(暂不支持)。 | 820 821## PortModeType 822 823USB端口模式类型。 824 825**系统接口:** 此接口为系统接口。 826 827**系统能力:** SystemCapability.USB.USBManager 828 829| 名称 | 值 | 说明 | 830| --------- | -- | ---------------------------------------------------- | 831| NONE | 0 | 无。 | 832| UFP | 1 | 数据上行,需要外部供电。 | 833| DFP | 2 | 数据下行,对外提供电源。 | 834| DRP | 3 | 既可以做DFP(Host),也可以做UFP(Device),当前不支持。 | 835| NUM_MODES | 4 | 当前不支持。 | 836 837## PowerRoleType 838 839电源角色类型。 840 841**系统接口:** 此接口为系统接口。 842 843**系统能力:** SystemCapability.USB.USBManager 844 845| 名称 | 值 | 说明 | 846| ------ | -- | ---------- | 847| NONE | 0 | 无。 | 848| SOURCE | 1 | 对外提供电源。 | 849| SINK | 2 | 需要外部供电。 | 850 851## DataRoleType 852 853数据角色类型。 854 855**系统接口:** 此接口为系统接口。 856 857**系统能力:** SystemCapability.USB.USBManager 858 859| 名称 | 值 | 说明 | 860| ------ | -- | ------------ | 861| NONE | 0 | 无。 | 862| HOST | 1 | 主设备角色。 | 863| DEVICE | 2 | 从设备角色。 | 864 865