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