• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
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.
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](#getportlist12).
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| 202      | Permission denied. Normal application do not have permission to use system api.                         |
387
388**Return value**
389
390| Type   | Description                                                                     |
391| ------- | ------------------------------------------------------------------------- |
392| boolean | Permission addition result. The value **true** indicates that the access permission is added successfully; and the value **false** indicates the opposite.|
393
394**Example**
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
423Converts the USB function list in the string format to a numeric mask in Device mode.
424
425**NOTE**
426
427> This API is supported since API version 12.
428
429**System API**: This is a system API.
430
431**Required permissions**: ohos.permission.MANAGE_USB_CONFIG
432
433**System capability**: SystemCapability.USB.USBManager
434
435**Parameters**
436
437| Name| Type  | Mandatory| Description                  |
438| ------ | ------ | ---- | ---------------------- |
439| funcs  | string | Yes  | Function list in string format.|
440
441**Error codes**
442
443For details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
444
445| ID| Error Message                                                                       |
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**Return value**
451
452| Type  | Description              |
453| ------ | ------------------ |
454| number | Function list in numeric mask format.|
455
456**Example**
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
467Converts the USB function list in the numeric mask format to a string in Device mode.
468
469**NOTE**
470
471> This API is supported since API version 12.
472
473**System API**: This is a system API.
474
475**Required permissions**: ohos.permission.MANAGE_USB_CONFIG
476
477**System capability**: SystemCapability.USB.USBManager
478
479**Parameters**
480
481| Name| Type                         | Mandatory| Description             |
482| ------ | ----------------------------- | ---- | ----------------- |
483| funcs  | [FunctionType](#functiontype) | Yes  | USB function list in numeric mask format.|
484
485**Error codes**
486
487For details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
488
489| ID| Error Message                                                                                               |
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**Return value**
495
496| Type  | Description                          |
497| ------ | ------------------------------ |
498| string | Function list in string format.|
499
500**Example**
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
511Sets the current USB function list in Device mode.
512
513**NOTE**
514
515> This API is supported since API version 12.
516
517**System API**: This is a system API.
518
519**Required permissions**: ohos.permission.MANAGE_USB_CONFIG
520
521**System capability**: SystemCapability.USB.USBManager
522
523**Parameters**
524
525| Name| Type                         | Mandatory| Description             |
526| ------ | ----------------------------- | ---- | ----------------- |
527| funcs  | [FunctionType](#functiontype) | Yes  | USB function list in numeric mask format.|
528
529**Error codes**
530
531For details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
532
533| ID| Error Message                                                                                               |
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**Return value**
541
542| Type               | Description         |
543| ------------------- | ------------- |
544| Promise\<**void**\> | Promise used to return the result.|
545
546**Example**
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
562Obtains the numeric mask combination for the USB function list in Device mode.
563
564**NOTE**
565
566> This API is supported since API version 12.
567
568**System API**: This is a system API.
569
570**Required permissions**: ohos.permission.MANAGE_USB_CONFIG
571
572**System capability**: SystemCapability.USB.USBManager
573
574**Error codes**
575
576For details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
577
578| ID| Error Message                                                                       |
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**Return value**
584
585| Type                         | Description                             |
586| ----------------------------- | --------------------------------- |
587| [FunctionType](#functiontype) | Numeric mask combination for the USB function list.|
588
589**Example**
590
591```ts
592let ret: number = usbManager.getDeviceFunctions();
593```
594
595## getPortList<sup>12+</sup>
596
597getPortList(): Array\<USBPort\>
598
599Obtains the list of all physical USB ports.
600
601**NOTE**
602
603> This API is supported since API version 12.
604
605**System API**: This is a system API.
606
607**Required permissions**: ohos.permission.MANAGE_USB_CONFIG
608
609**System capability**: SystemCapability.USB.USBManager
610
611**Error codes**
612
613For details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
614
615| ID| Error Message                                                                                               |
616| -------- | ------------------------------------------------------------------------------------------------------- |
617| 202      | Permission denied. Normal application do not have permission to use system api.                         |
618
619**Return value**
620
621| Type                      | Description                 |
622| -------------------------- | --------------------- |
623| Array<[USBPort](#usbport)> | List of physical USB ports.|
624
625**Example**
626
627```ts
628let ret: Array<usbManager.USBPort> = usbManager.getPortList();
629```
630
631## getPortSupportModes<sup>12+</sup>
632
633getPortSupportModes(portId: number): PortModeType
634
635Obtains the mask combination for the supported mode list of a given USB port.
636
637**System API**: This is a system API.
638
639**Required permissions**: ohos.permission.MANAGE_USB_CONFIG
640
641**System capability**: SystemCapability.USB.USBManager
642
643**Parameters**
644
645| Name| Type  | Mandatory| Description    |
646| ------ | ------ | ---- | -------- |
647| portId | number | Yes  | Port number.|
648
649**Error codes**
650
651For details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
652
653| ID| Error Message                                                                                               |
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**Return value**
659
660| Type                         | Description                      |
661| ----------------------------- | -------------------------- |
662| [PortModeType](#portmodetype) | Mask combination for the supported mode list.|
663
664**Example**
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
674Sets the role types supported by a specified port, which can be **powerRole** (for charging) and **dataRole** (for data transfer).
675
676**NOTE**
677
678> This API is supported since API version 12.
679
680**System API**: This is a system API.
681
682**Required permissions**: ohos.permission.MANAGE_USB_CONFIG
683
684**System capability**: SystemCapability.USB.USBManager
685
686**Parameters**
687
688| Name   | Type                           | Mandatory| Description            |
689| --------- | ------------------------------- | ---- | ---------------- |
690| portId    | number                          | Yes  | Port number.        |
691| powerRole | [PowerRoleType](#powerroletype) | Yes  | Role for charging.    |
692| dataRole  | [DataRoleType](#dataroletype)   | Yes  | Role for data transfer.|
693
694**Error codes**
695
696For details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
697
698| ID| Error Message                                                                                               |
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**Return value**
705
706| Type               | Description         |
707| ------------------- | ------------- |
708| Promise\<**void**\> | Promise used to return the result.|
709
710**Example**
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
726Adds the permission to applications for accessing USB accessories.
727
728**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.
729
730**System API**: This is a system API.
731
732**Required permissions**: ohos.permission.MANAGE_USB_CONFIG
733
734**System capability**: SystemCapability.USB.USBManager
735
736**Parameters**
737
738| Name   | Type        | Mandatory| Description                    |
739| --------- | ------------ | ---- | ------------------------ |
740| tokenId   | number       | Yes  | Token ID of the application.|
741| accessory | USBAccessory | Yes  | USB accessory.               |
742
743**Error codes**
744
745For details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
746
747| ID| Error Message                                                    |
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**Example**
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
774Represents a USB port.
775
776**System API**: This is a system API.
777
778**System capability**: SystemCapability.USB.USBManager
779
780| Name          | Type                           | Mandatory| Description                               |
781| -------------- | ------------------------------- | ---- | ----------------------------------- |
782| id             | number                          | Yes  | Unique identifier of a USB port.                  |
783| supportedModes | [PortModeType](#portmodetype)   | Yes  | Numeric mask combination for the supported mode list.|
784| status         | [USBPortStatus](#usbportstatus) | Yes  | USB port role.                      |
785
786## USBPortStatus
787
788Enumerates USB port roles.
789
790**System API**: This is a system API.
791
792**System capability**: SystemCapability.USB.USBManager
793
794| Name            | Type  | Mandatory| Description                  |
795| ---------------- | ------ | ---- | ---------------------- |
796| currentMode      | number | Yes  | Current USB mode.       |
797| currentPowerRole | number | Yes  | Current power role.    |
798| currentDataRole  | number | Yes  | Current data role.|
799
800## FunctionType
801
802Enumerates USB device function types.
803
804**System API**: This is a system API.
805
806**System capability**: SystemCapability.USB.USBManager
807
808| Name        | Value | Description      |
809| ------------ | --- | ---------- |
810| NONE         | 0   | No function.|
811| ACM          | 1   | ACM function. |
812| ECM          | 2   | ECM function. |
813| HDC          | 4   | HDC function. |
814| MTP          | 8   | Media transmission (not supported).|
815| PTP          | 16  | Image transmission (not supported).|
816| RNDIS        | 32  | Network sharing (not supported).|
817| MIDI         | 64  | MIDI function (not supported).|
818| AUDIO_SOURCE | 128 | Audio function (not supported).|
819| NCM          | 256 | NCM transmission (not supported). |
820
821## PortModeType
822
823Enumerates USB port mode types.
824
825**System API**: This is a system API.
826
827**System capability**: SystemCapability.USB.USBManager
828
829| Name     | Value| Description                                                |
830| --------- | -- | ---------------------------------------------------- |
831| NONE      | 0  | None                                                |
832| UFP       | 1  | Upstream facing port, which functions as the sink of power supply.                            |
833| DFP       | 2  | Downstream facing port, which functions as the source of power supply.                            |
834| DRP       | 3  | Dynamic reconfiguration port (DRP), which can function as the DFP (host) or UFP (device). It is not supported currently.|
835| NUM_MODES | 4  | Not supported currently.                                        |
836
837## PowerRoleType
838
839Enumerates power role types.
840
841**System API**: This is a system API.
842
843**System capability**: SystemCapability.USB.USBManager
844
845| Name  | Value| Description      |
846| ------ | -- | ---------- |
847| NONE   | 0  | None      |
848| SOURCE | 1  | Power supply for external devices.|
849| SINK   | 2  | External power supply.|
850
851## DataRoleType
852
853Enumerates data role types.
854
855**System API**: This is a system API.
856
857**System capability**: SystemCapability.USB.USBManager
858
859| Name  | Value| Description        |
860| ------ | -- | ------------ |
861| NONE   | 0  | None        |
862| HOST   | 1  | USB host.|
863| DEVICE | 2  | USB device.|
864