1# @ohos.usbManager (USB Manager) (System API) 2 3The **usbManager** module provides USB device management functions, including USB device list query, bulk data transfer, control transfer, and permission control on the host side as well as port management, and function switch and query on the device side. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> This topic describes only the system APIs provided by the module. For details about its public APIs, see [@ohos.usbManager (USB Manager)](js-apis-usbManager.md). 9 10## Modules to Import 11 12```ts 13import { usbManager } from '@kit.BasicServicesKit'; 14``` 15 16## addRight <sup>(deprecated)</sup> 17 18addRight(bundleName: string, deviceName: string): boolean 19 20Adds the device access permission for the application. System applications are granted the device access permission by default, and calling this API will not revoke the permission. 21 22**usbManager.requestRight** triggers a dialog box to request for user authorization, whereas **addRight** adds the access permission directly without displaying a dialog box. 23 24**NOTE** 25 26> This API is supported since API version 9 and deprecated since API version 12. You are advised to use [addDeviceAccessRight](#adddeviceaccessright12). 27 28**System API**: This is a system API. 29 30**System capability**: SystemCapability.USB.USBManager 31 32**Parameters** 33 34| Name | Type | Mandatory| Description | 35| ---------- | ------ | ---- | ------------ | 36| deviceName | string | Yes | Device name. | 37| bundleName | string | Yes | Bundle name of the application.| 38 39**Error codes** 40 41For details about the error codes, see [USB Service Error Codes](errorcode-usb.md). 42 43| ID| Error Message | 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**Return value** 49 50| Type | Description | 51| ------- | ------------------------------------------------------------------------- | 52| boolean | Permission addition result. The value **true** indicates that the access permission is added successfully; and the value **false** indicates the opposite.| 53 54**Example** 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 68Converts the USB function list in the string format to a numeric mask in Device mode. 69 70**NOTE** 71 72> This API is supported since API version 9 and deprecated since API version 12. You are advised to use [getFunctionsFromString](#getfunctionsfromstring12). 73 74**System API**: This is a system API. 75 76**System capability**: SystemCapability.USB.USBManager 77 78**Parameters** 79 80| Name| Type | Mandatory| Description | 81| ------ | ------ | ---- | ---------------------- | 82| funcs | string | Yes | Function list in string format.| 83 84**Error codes** 85 86For details about the error codes, see [USB Service Error Codes](errorcode-usb.md). 87 88| ID| Error Message | 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**Return value** 94 95| Type | Description | 96| ------ | ------------------ | 97| number | Function list in numeric mask format.| 98 99**Example** 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 110Converts the USB function list in the numeric mask format to a string in Device mode. 111 112**NOTE** 113 114> This API is supported since API version 9 and deprecated since API version 12. You are advised to use [getStringFromFunctions](#getstringfromfunctions12). 115 116**System API**: This is a system API. 117 118**System capability**: SystemCapability.USB.USBManager 119 120**Parameters** 121 122| Name| Type | Mandatory| Description | 123| ------ | ----------------------------- | ---- | ----------------- | 124| funcs | [FunctionType](#functiontype) | Yes | USB function list in numeric mask format.| 125 126**Error codes** 127 128For details about the error codes, see [USB Service Error Codes](errorcode-usb.md). 129 130| ID| Error Message | 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**Return value** 136 137| Type | Description | 138| ------ | ------------------------------ | 139| string | Function list in string format.| 140 141**Example** 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 152Sets the current USB function list in Device mode. 153 154**NOTE** 155 156> This API is supported since API version 9 and deprecated since API version 12. You are advised to use [setDeviceFunctions](#setdevicefunctions12). 157 158**System API**: This is a system API. 159 160**System capability**: SystemCapability.USB.USBManager 161 162**Parameters** 163 164| Name| Type | Mandatory| Description | 165| ------ | ----------------------------- | ---- | ----------------- | 166| funcs | [FunctionType](#functiontype) | Yes | USB function list in numeric mask format.| 167 168**Error codes** 169 170For details about the error codes, see [USB Service Error Codes](errorcode-usb.md). 171 172| ID| Error Message | 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**Return value** 178 179| Type | Description | 180| ------------------- | ------------- | 181| Promise\<void\> | Promise used to return the result.| 182 183**Example** 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 199Obtains the numeric mask combination for the USB function list in Device mode. When the developer mode is disabled, **undefined** may be returned if no device is connected. Check whether the return value of the API is empty. 200 201**NOTE** 202 203> This API is supported since API version 9 and deprecated since API version 12. You are advised to use [getDeviceFunctions](#getdevicefunctions12). 204 205**System API**: This is a system API. 206 207**System capability**: SystemCapability.USB.USBManager 208 209**Error codes** 210 211For details about the error codes, see [USB Service Error Codes](errorcode-usb.md). 212 213| ID| Error Message | 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**Return value** 219 220| Type | Description | 221| ----------------------------- | --------------------------------- | 222| [FunctionType](#functiontype) | Numeric mask combination for the USB function list.| 223 224**Example** 225 226```ts 227let ret: number = usbManager.getCurrentFunctions(); 228``` 229 230## getPorts<sup>(deprecated)</sup> 231 232getPorts(): Array\<USBPort\> 233 234Obtains the list of all physical USB ports. When the developer mode is disabled, **undefined** may be returned if no device is connected. Check whether the return value of the API is empty. 235 236**NOTE** 237 238> This API is supported since API version 9 and deprecated since API version 12. You are advised to use [getPortList](#getportlist12). 239 240**System API**: This is a system API. 241 242**System capability**: SystemCapability.USB.USBManager 243 244**Error codes** 245 246For details about the error codes, see [USB Service Error Codes](errorcode-usb.md). 247 248| ID| Error Message | 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**Return value** 254 255| Type | Description | 256| -------------------------- | --------------------- | 257| Array<[USBPort](#usbport)> | List of physical USB ports.| 258 259**Example** 260 261```ts 262let ret: Array<usbManager.USBPort> = usbManager.getPorts(); 263``` 264 265## getSupportedModes(deprecated) 266 267getSupportedModes(portId: number): PortModeType 268 269Obtains the mask combination for the supported mode list of a given USB port. 270 271**NOTE** 272 273> This API is supported since API version 9 and deprecated since API version 12. You are advised to use [getPortSupportModes](#getportsupportmodes12) instead. 274 275**System API**: This is a system API. 276 277**System capability**: SystemCapability.USB.USBManager 278 279**Parameters** 280 281| Name| Type | Mandatory| Description | 282| ------ | ------ | ---- | -------- | 283| portId | number | Yes | Port number.| 284 285**Error codes** 286 287For details about the error codes, see [USB Service Error Codes](errorcode-usb.md). 288 289| ID| Error Message | 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**Return value** 295 296| Type | Description | 297| ----------------------------- | -------------------------- | 298| [PortModeType](#portmodetype) | Mask combination for the supported mode list.| 299 300**Example** 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 310Sets the role types supported by a specified port, which can be **powerRole** (for charging) and **dataRole** (for data transfer). 311 312**NOTE** 313 314> This API is supported since API version 9 and deprecated since API version 12. You are advised to use [setPortRoleTypes](#setportroletypes12). 315 316**System API**: This is a system API. 317 318**System capability**: SystemCapability.USB.USBManager 319 320**Parameters** 321 322| Name | Type | Mandatory| Description | 323| --------- | ------------------------------- | ---- | ---------------- | 324| portId | number | Yes | Port number. | 325| powerRole | [PowerRoleType](#powerroletype) | Yes | Role for charging. | 326| dataRole | [DataRoleType](#dataroletype) | Yes | Role for data transfer.| 327 328**Error codes** 329 330For details about the error codes, see [USB Service Error Codes](errorcode-usb.md). 331 332| ID| Error Message | 333| -------- | ------------------------------------------------------------------------------------------------------- | 334| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 335 336**Return value** 337 338| Type | Description | 339| ------------------- | ------------- | 340| Promise\<void\> | Promise used to return the result.| 341 342**Example** 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 358Adds the device access permission for the application. System applications are granted the device access permission by default, and calling this API will not revoke the permission. 359 360**usbManager.requestRight** triggers a dialog box to request for user authorization, whereas **addDeviceAccessRight** adds the access permission directly without displaying a dialog box. 361 362**NOTE** 363 364> This API is supported since API version 12. 365 366**System API**: This is a system API. 367 368**Required permissions**: ohos.permission.MANAGE_USB_CONFIG 369 370**System capability**: SystemCapability.USB.USBManager 371 372**Parameters** 373 374| Name | Type | Mandatory| Description | 375| ---------- | ------ | ---- | --------------- | 376| deviceName | string | Yes | Device name. | 377| tokenId | string | Yes | Token ID of the software package.| 378 379**Error codes** 380 381For details about the error codes, see [USB Service Error Codes](errorcode-usb.md). 382 383| ID| Error Message | 384| -------- | ------------------------------------------------------------------------------------------------------- | 385| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 386| 201 | Permission verification failed. The application does not have the permission required to call the API. | 387| 202 | Permission denied. Normal application do not have permission to use system api. | 388| 801 | Capability not supported. | 389 390**Return value** 391 392| Type | Description | 393| ------- | ------------------------------------------------------------------------- | 394| boolean | Permission addition result. The value **true** indicates that the access permission is added successfully; and the value **false** indicates the opposite.| 395 396**Example** 397 398```ts 399import { bundleManager } from '@kit.AbilityKit'; 400import { BusinessError } from '@kit.BasicServicesKit'; 401let devicesName: string = "1-1"; 402let tokenId: string = ""; 403 404 try { 405 let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT; 406 bundleManager.getBundleInfoForSelf(bundleFlags).then((bundleInfo) => { 407 console.info('testTag', 'getBundleInfoForSelf successfully. Data: %{public}s', JSON.stringify(bundleInfo)); 408 let token = bundleInfo.appInfo.accessTokenId; 409 tokenId = token.toString(); 410 if (usbManager.addDeviceAccessRight(tokenId, devicesName)) { 411 console.log(`Succeed in adding right`); 412 } 413 }).catch((err : BusinessError) => { 414 console.error('testTag getBundleInfoForSelf failed' ); 415 }); 416 } catch (err) { 417 console.error('testTag failed'); 418 } 419``` 420 421## getFunctionsFromString<sup>12+</sup> 422 423getFunctionsFromString(funcs: string): number 424 425Converts the USB function list in the string format to a numeric mask in Device mode. 426 427**NOTE** 428 429> This API is supported since API version 12. 430 431**System API**: This is a system API. 432 433**Required permissions**: ohos.permission.MANAGE_USB_CONFIG 434 435**System capability**: SystemCapability.USB.USBManager 436 437**Parameters** 438 439| Name| Type | Mandatory| Description | 440| ------ | ------ | ---- | ---------------------- | 441| funcs | string | Yes | Function list in string format.| 442 443**Error codes** 444 445For details about the error codes, see [USB Service Error Codes](errorcode-usb.md). 446 447| ID| Error Message | 448| -------- | ------------------------------------------------------------------------------- | 449| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 450| 201 | Permission verification failed. The application does not have the permission required to call the API. | 451| 202 | Permission denied. Normal application do not have permission to use system api. | 452| 801 | Capability not supported. | 453 454**Return value** 455 456| Type | Description | 457| ------ | ------------------ | 458| number | Function list in numeric mask format.| 459 460**Example** 461 462```ts 463let funcs: string = "acm"; 464let ret: number = usbManager.getFunctionsFromString(funcs); 465``` 466 467## getStringFromFunctions<sup>12+</sup> 468 469getStringFromFunctions(funcs: FunctionType): string 470 471Converts the USB function list in the numeric mask format to a string in Device mode. 472 473**NOTE** 474 475> This API is supported since API version 12. 476 477**System API**: This is a system API. 478 479**Required permissions**: ohos.permission.MANAGE_USB_CONFIG 480 481**System capability**: SystemCapability.USB.USBManager 482 483**Parameters** 484 485| Name| Type | Mandatory| Description | 486| ------ | ----------------------------- | ---- | ----------------- | 487| funcs | [FunctionType](#functiontype) | Yes | USB function list in numeric mask format.| 488 489**Error codes** 490 491For details about the error codes, see [USB Service Error Codes](errorcode-usb.md). 492 493| ID| Error Message | 494| -------- | ------------------------------------------------------------------------------------------------------- | 495| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 496| 201 | Permission verification failed. The application does not have the permission required to call the API. | 497| 202 | Permission denied. Normal application do not have permission to use system api. | 498| 801 | Capability not supported. | 499 500**Return value** 501 502| Type | Description | 503| ------ | ------------------------------ | 504| string | Function list in string format.| 505 506**Example** 507 508```ts 509let funcs: number = usbManager.FunctionType.ACM | usbManager.FunctionType.ECM; 510let ret: string = usbManager.getStringFromFunctions(funcs); 511``` 512 513## setDeviceFunctions<sup>12+</sup> 514 515setDeviceFunctions(funcs: FunctionType): Promise\<void\> 516 517Sets the current USB function list in Device mode. 518 519**NOTE** 520 521> This API is supported since API version 12. 522 523**System API**: This is a system API. 524 525**Required permissions**: ohos.permission.MANAGE_USB_CONFIG 526 527**System capability**: SystemCapability.USB.USBManager 528 529**Parameters** 530 531| Name| Type | Mandatory| Description | 532| ------ | ----------------------------- | ---- | ----------------- | 533| funcs | [FunctionType](#functiontype) | Yes | USB function list in numeric mask format.| 534 535**Error codes** 536 537For details about the error codes, see [USB Service Error Codes](errorcode-usb.md). 538 539| ID| Error Message | 540| -------- | ------------------------------------------------------------------------------------------------------- | 541| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 542| 201 | Permission verification failed. The application does not have the permission required to call the API. | 543| 202 | Permission denied. Normal application do not have permission to use system api. | 544| 801 | Capability not supported. | 545| 14400002 | Permission denied. The HDC is disabled by the system. | 546| 14400006 | Unsupported operation. The function is not supported. | 547 548**Return value** 549 550| Type | Description | 551| ------------------- | ------------- | 552| Promise\<void\> | Promise used to return the result.| 553 554**Example** 555 556```ts 557import { BusinessError } from '@kit.BasicServicesKit'; 558let funcs: number = usbManager.FunctionType.HDC; 559usbManager.setDeviceFunctions(funcs).then(() => { 560 console.info('usb setDeviceFunctions successfully.'); 561}).catch((err : BusinessError) => { 562 console.error('usb setDeviceFunctions failed: ' + err.code + ' message: ' + err.message); 563}); 564``` 565 566## getDeviceFunctions<sup>12+</sup> 567 568getDeviceFunctions(): FunctionType 569 570Obtains the numeric mask combination for the USB function list in Device mode. When the developer mode is disabled, **undefined** may be returned if no device is connected. Check whether the return value of the API is empty. 571 572**NOTE** 573 574> This API is supported since API version 12. 575 576**System API**: This is a system API. 577 578**Required permissions**: ohos.permission.MANAGE_USB_CONFIG 579 580**System capability**: SystemCapability.USB.USBManager 581 582**Error codes** 583 584For details about the error codes, see [USB Service Error Codes](errorcode-usb.md). 585 586| ID| Error Message | 587| -------- | ------------------------------------------------------------------------------- | 588| 401 | Parameter error. No parameters are required. | 589| 201 | Permission verification failed. The application does not have the permission required to call the API. | 590| 202 | Permission denied. Normal application do not have permission to use system api. | 591| 801 | Capability not supported. | 592 593**Return value** 594 595| Type | Description | 596| ----------------------------- | --------------------------------- | 597| [FunctionType](#functiontype) | Numeric mask combination for the USB function list.| 598 599**Example** 600 601```ts 602let ret: number = usbManager.getDeviceFunctions(); 603``` 604 605## getPortList<sup>12+</sup> 606 607getPortList(): Array\<USBPort\> 608 609Obtains the list of all physical USB ports. When the developer mode is disabled, **undefined** may be returned if no device is connected. Check whether the return value of the API is empty. 610 611**NOTE** 612 613> This API is supported since API version 12. 614 615**System API**: This is a system API. 616 617**Required permissions**: ohos.permission.MANAGE_USB_CONFIG 618 619**System capability**: SystemCapability.USB.USBManager 620 621**Error codes** 622 623For details about the error codes, see [USB Service Error Codes](errorcode-usb.md). 624 625| ID| Error Message | 626| -------- | ------------------------------------------------------------------------------------------------------- | 627| 201 | Permission verification failed. The application does not have the permission required to call the API. | 628| 202 | Permission denied. Normal application do not have permission to use system api. | 629| 801 | Capability not supported. | 630 631**Return value** 632 633| Type | Description | 634| -------------------------- | --------------------- | 635| Array<[USBPort](#usbport)> | List of physical USB ports.| 636 637**Example** 638 639```ts 640let ret: Array<usbManager.USBPort> = usbManager.getPortList(); 641``` 642 643## getPortSupportModes<sup>12+</sup> 644 645getPortSupportModes(portId: number): PortModeType 646 647Obtains the mask combination for the supported mode list of a given USB port. 648 649**System API**: This is a system API. 650 651**Required permissions**: ohos.permission.MANAGE_USB_CONFIG 652 653**System capability**: SystemCapability.USB.USBManager 654 655**Parameters** 656 657| Name| Type | Mandatory| Description | 658| ------ | ------ | ---- | -------- | 659| portId | number | Yes | Port number.| 660 661**Error codes** 662 663For details about the error codes, see [USB Service Error Codes](errorcode-usb.md). 664 665| ID| Error Message | 666| -------- | ------------------------------------------------------------------------------------------------------- | 667| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 668| 201 | Permission verification failed. The application does not have the permission required to call the API. | 669| 202 | Permission denied. Normal application do not have permission to use system api. | 670| 801 | Capability not supported. | 671 672**Return value** 673 674| Type | Description | 675| ----------------------------- | -------------------------- | 676| [PortModeType](#portmodetype) | Mask combination for the supported mode list.| 677 678**Example** 679 680```ts 681let ret: number = usbManager.getPortSupportModes(0); 682``` 683 684## setPortRoleTypes<sup>12+</sup> 685 686setPortRoleTypes(portId: number, powerRole: PowerRoleType, dataRole: DataRoleType): Promise\<void\> 687 688Sets the role types supported by a specified port, which can be **powerRole** (for charging) and **dataRole** (for data transfer). 689 690**NOTE** 691 692> This API is supported since API version 12. 693 694**System API**: This is a system API. 695 696**Required permissions**: ohos.permission.MANAGE_USB_CONFIG 697 698**System capability**: SystemCapability.USB.USBManager 699 700**Parameters** 701 702| Name | Type | Mandatory| Description | 703| --------- | ------------------------------- | ---- | ---------------- | 704| portId | number | Yes | Port number. | 705| powerRole | [PowerRoleType](#powerroletype) | Yes | Role for charging. | 706| dataRole | [DataRoleType](#dataroletype) | Yes | Role for data transfer.| 707 708**Error codes** 709 710For details about the error codes, see [USB Service Error Codes](errorcode-usb.md). 711 712| ID| Error Message | 713| -------- | ------------------------------------------------------------------------------------------------------- | 714| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 715| 201 | Permission verification failed. The application does not have the permission required to call the API. | 716| 202 | Permission denied. Normal application do not have permission to use system api. | 717| 801 | Capability not supported. | 718| 14400003 | Unsupported operation. The current device does not support port role switching. | 719 720**Return value** 721 722| Type | Description | 723| ------------------- | ------------- | 724| Promise\<void\> | Promise used to return the result.| 725 726**Example** 727 728```ts 729import { BusinessError } from '@kit.BasicServicesKit'; 730let portId: number = 1; 731usbManager.setPortRoleTypes(portId, usbManager.PowerRoleType.SOURCE, usbManager.DataRoleType.HOST).then(() => { 732 console.info('usb setPortRoleTypes successfully.'); 733}).catch((err : BusinessError) => { 734 console.error('usb setPortRoleTypes failed: ' + err.code + ' message: ' + err.message); 735}); 736``` 737 738## addAccessoryRight<sup>14+</sup> 739 740addAccessoryRight(tokenId: number, accessory: USBAccessory): void 741 742Adds the permission to applications for accessing USB accessories. 743 744**usbManager.requestAccessoryRight** triggers a dialog box to request user authorization. **addAccessoryRight** does not trigger a dialog box but directly adds the device access permission for the application. 745 746**System API**: This is a system API. 747 748**Required permissions**: ohos.permission.MANAGE_USB_CONFIG 749 750**System capability**: SystemCapability.USB.USBManager 751 752**Parameters** 753 754| Name | Type | Mandatory| Description | 755| --------- | ------------ | ---- | ------------------------ | 756| tokenId | number | Yes | Token ID of the application.| 757| accessory | USBAccessory | Yes | USB accessory. | 758 759**Error codes** 760 761For details about the error codes, see [USB Service Error Codes](errorcode-usb.md). 762 763| ID| Error Message | 764| -------- | ------------------------------------------------------------ | 765| 201 | The permission check failed. | 766| 202 | Permission denied. Normal application do not have permission to use system api. | 767| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 768| 801 | Capability not supported. | 769| 14400004 | Service exception. Possible causes: 1. No accessory is plugged in. | 770| 14400005 | Database operation exception. | 771 772**Example** 773 774```ts 775import { hilog } from '@kit.PerformanceAnalysisKit'; 776import { bundleManager } from '@kit.AbilityKit'; 777try { 778 let accList: usbManager.USBAccessory[] = usbManager.getAccessoryList() 779 let flags = bundleManager.BundleFlah.GET_BUNDLE_INFO_WITH_APPLICATION | bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY 780 let bundleInfo = await bundleManager.getBundleInfoForSelf(flags) 781 let tokenId: number = bundleInfo.appInfo.accessTokenId 782 usbManager.addAccessoryRight(tokenId, accList[0]) 783 hilog.info(0, 'testTag ui', `addAccessoryRight success`) 784} catch (error) { 785 hilog.info(0, 'testTag ui', `addAccessoryRight error ${error.code}, message is ${error.message}`) 786} 787``` 788 789## USBPort 790 791Represents a USB port. 792 793**System API**: This is a system API. 794 795**System capability**: SystemCapability.USB.USBManager 796 797| Name | Type | Mandatory| Description | 798| -------------- | ------------------------------- | ---- | ----------------------------------- | 799| id | number | Yes | Unique identifier of a USB port. | 800| supportedModes | [PortModeType](#portmodetype) | Yes | Numeric mask combination for the supported mode list.| 801| status | [USBPortStatus](#usbportstatus) | Yes | USB port role. | 802 803## USBPortStatus 804 805Enumerates USB port roles. 806 807**System API**: This is a system API. 808 809**System capability**: SystemCapability.USB.USBManager 810 811| Name | Type | Mandatory| Description | 812| ---------------- | ------ | ---- | ---------------------- | 813| currentMode | number | Yes | Current USB mode. | 814| currentPowerRole | number | Yes | Current power role. | 815| currentDataRole | number | Yes | Current data role.| 816 817## FunctionType 818 819Enumerates USB device function types. 820 821**System API**: This is a system API. 822 823**System capability**: SystemCapability.USB.USBManager 824 825| Name | Value | Description | 826| ------------ | --- | ---------- | 827| NONE | 0 | No function.| 828| ACM | 1 | ACM function. | 829| ECM | 2 | ECM function. | 830| HDC | 4 | HDC function. | 831| MTP | 8 | Media transmission.| 832| PTP | 16 | Image transmission.| 833| RNDIS | 32 | Network sharing (not supported).| 834| MIDI | 64 | MIDI function (not supported).| 835| AUDIO_SOURCE | 128 | Audio function (not supported).| 836| NCM | 256 | NCM transmission (not supported). | 837 838## PortModeType 839 840Enumerates USB port mode types. 841 842**System API**: This is a system API. 843 844**System capability**: SystemCapability.USB.USBManager 845 846| Name | Value| Description | 847| --------- | -- | ---------------------------------------------------- | 848| NONE | 0 | None | 849| UFP | 1 | Upstream facing port, which functions as the sink of power supply. | 850| DFP | 2 | Downstream facing port, which functions as the source of power supply. | 851| DRP | 3 | Dynamic reconfiguration port (DRP), which can function as the DFP (host) or UFP (device). It is not supported currently.| 852| NUM_MODES | 4 | Not supported currently. | 853 854## PowerRoleType 855 856Enumerates power role types. 857 858**System API**: This is a system API. 859 860**System capability**: SystemCapability.USB.USBManager 861 862| Name | Value| Description | 863| ------ | -- | ---------- | 864| NONE | 0 | None | 865| SOURCE | 1 | Power supply for external devices.| 866| SINK | 2 | External power supply.| 867 868## DataRoleType 869 870Enumerates data role types. 871 872**System API**: This is a system API. 873 874**System capability**: SystemCapability.USB.USBManager 875 876| Name | Value| Description | 877| ------ | -- | ------------ | 878| NONE | 0 | None | 879| HOST | 1 | USB host.| 880| DEVICE | 2 | USB device.| 881