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