openharmony 鸿蒙 js-apis-usb-deprecated-sys

2025-06-12 浏览 (1)

@ohos.usb (USB Manager) (System API)

The usb module provides USB device management functions, including USB device list query, bulk data transfer, control transfer, and permission control.

NOTE

The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

The APIs provided by this module are no longer maintained since API version 9. You are advised to use @ohos.usbManager.

This topic describes only system APIs provided by the module. For details about its public APIs, see @ohos.usb (USB Manager) (No Longer Maintained).

Modules to Import

import usb from "@ohos.usb";
import { BusinessError } from '@ohos.base';

usb.usbFunctionsFromString9+

usbFunctionsFromString(funcs: string): number

Converts the USB function list in the string format to a numeric mask in Device mode.

System API: This is a system API.

System capability: SystemCapability.USB.USBManager

Parameters

NameTypeMandatoryDescription
funcsstringYesFunction list in string format.

Return value

TypeDescription
numberFunction list in numeric mask format.

Example

let funcs = "acm";
let ret = usb.usbFunctionsFromString(funcs);

usb.usbFunctionsToString9+

usbFunctionsToString(funcs: FunctionType): string

Converts the USB function list in the numeric mask format to a string in Device mode.

System API: This is a system API.

System capability: SystemCapability.USB.USBManager

Parameters

NameTypeMandatoryDescription
funcsFunctionTypeYesUSB function list in numeric mask format.

Return value

TypeDescription
stringFunction list in string format.

Example

let funcs = usb.FunctionType.ACM|usb.FunctionType.ECM;
let ret = usb.usbFunctionsToString(funcs);

usb.setCurrentFunctions9+

setCurrentFunctions(funcs: FunctionType): Promise<boolean>

Sets the current USB function list in Device mode.

System API: This is a system API.

System capability: SystemCapability.USB.USBManager

Parameters

NameTypeMandatoryDescription
funcsFunctionTypeYesUSB function list in numeric mask format.

Return value

TypeDescription
Promise<boolean>Promise used to return the result. The value true indicates that the operation is successful, and the value false indicates the opposite.

Example

let funcs : number = usb.FunctionType.HDC;
usb.setCurrentFunctions(funcs).then(() => {
    console.info('usb setCurrentFunctions successfully.');
}).catch((err : BusinessError) => {
    console.error('usb setCurrentFunctions failed: ' + err.code + ' message: ' + err.message);
});

usb.getCurrentFunctions9+

getCurrentFunctions(): FunctionType

Obtains the numeric mask combination for the USB function list in Device mode.

System API: This is a system API.

System capability: SystemCapability.USB.USBManager

Return value

TypeDescription
FunctionTypeNumeric mask combination for the USB function list.

Example

let ret = usb.getCurrentFunctions();

usb.getPorts9+

getPorts(): Array<USBPort>

Obtains the list of all physical USB ports.

System API: This is a system API.

System capability: SystemCapability.USB.USBManager

Return value

TypeDescription
Array<USBPort>List of physical USB ports.

Example

let ret = usb.getPorts();

usb.getSupportedModes9+

getSupportedModes(portId: number): PortModeType

Obtains the mask combination for the supported mode list of a given USB port.

System API: This is a system API.

System capability: SystemCapability.USB.USBManager

Parameters

NameTypeMandatoryDescription
portIdnumberYesPort number.

Return value

TypeDescription
PortModeTypeMask combination for the supported mode list.

Example

let ret = usb.getSupportedModes(0);

usb.setPortRoles9+

setPortRoles(portId: number, powerRole: PowerRoleType, dataRole: DataRoleType): Promise<boolean>

Sets the role types supported by a specified port, which can be powerRole (for charging) and dataRole (for data transfer).

System API: This is a system API.

System capability: SystemCapability.USB.USBManager

Parameters

NameTypeMandatoryDescription
portIdnumberYesPort number.
powerRolePowerRoleTypeYesRole for charging.
dataRoleDataRoleTypeYesRole for data transfer.

Return value

TypeDescription
Promise<boolean>Promise used to return the result. The value true indicates that the operation is successful, and the value false indicates the opposite.

Example

let portId = 1;
usb.setPortRoles(portId, usb.PowerRoleType.SOURCE, usb.DataRoleType.HOST).then(() => {
    console.info('usb setPortRoles successfully.');
}).catch((err : BusinessError) => {
    console.error('usb setPortRoles failed: ' + err.code + ' message: ' + err.message);
});

USBPort9+

Represents a USB port.

System API: This is a system API.

System capability: SystemCapability.USB.USBManager

NameTypeMandatoryDescription
idnumberYesUnique identifier of a USB port.
supportedModesPortModeTypeYesNumeric mask combination for the supported mode list.
statusUSBPortStatusYesUSB port role.

USBPortStatus9+

Enumerates USB port roles.

System API: This is a system API.

System capability: SystemCapability.USB.USBManager

NameTypeMandatoryDescription
currentModenumberYesCurrent USB mode.
currentPowerRolenumberYesCurrent power role.
currentDataRolenumberYesCurrent data role.

FunctionType9+

Enumerates USB device function types.

System API: This is a system API.

System capability: SystemCapability.USB.USBManager

NameValueDescription
NONE0No function.
ACM1ACM function.
ECM2ECM function.
HDC4HDC function.
MTP8Media transmission.
PTP16Image transmission.
RNDIS32Network sharing.
MIDI64MIDI function.
AUDIO_SOURCE128Audio function.
NCM256NCM transmission.

PortModeType9+

Enumerates USB port mode types.

System API: This is a system API.

System capability: SystemCapability.USB.USBManager

NameValueDescription
NONE0None
UFP1Upstream facing port, which functions as the sink of power supply.
DFP2Downstream facing port, which functions as the source of power supply.
DRP3Dynamic reconfiguration port (DRP), which can function as the DFP (host) or UFP (device). It is not supported currently.
NUM_MODES4Not supported currently.

PowerRoleType9+

Enumerates power role types.

System API: This is a system API.

System capability: SystemCapability.USB.USBManager

NameValueDescription
NONE0None
SOURCE1External power supply.
SINK2Internal power supply.

DataRoleType9+

Enumerates data role types.

System API: This is a system API.

System capability: SystemCapability.USB.USBManager

NameValueDescription
NONE0None
HOST1USB host.
DEVICE2USB device.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Basic Services Kit

harmony 鸿蒙DeviceInfo

harmony 鸿蒙InitSync

harmony 鸿蒙OH_Print

harmony 鸿蒙OsAccount

harmony 鸿蒙Pasteboard

harmony 鸿蒙Print_Margin

harmony 鸿蒙Print_PageSize

harmony 鸿蒙Print_PrintAttributes

harmony 鸿蒙Print_PrintDocCallback

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/n8oPBK