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