@ohos.enterprise.usbManager(USB管理)(系统接口)

本模块提供USB管理能力。

说明

本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

本模块接口仅可在Stage模型下使用。

本模块接口仅对设备管理应用开放,需将设备管理应用激活后调用,实现相应功能。

本模块接口均为系统接口。

导入模块

import usbManager from '@ohos.enterprise.usbManager';

usbManager.setUsbPolicy

setUsbPolicy(admin: Want, usbPolicy: UsbPolicy, callback: AsyncCallback<void>): void

指定设备管理应用设置USB的读写策略。使用callback异步回调。

需要权限: ohos.permission.ENTERPRISE_MANAGE_USB

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

参数:

参数名 类型 必填 说明
admin Want 设备管理应用。
usbPolicy UsbPolicy USB读写策略(此接口只支持READ_WRITE和READ_ONLY)。
callback AsyncCallback<void> 回调函数。当接口调用成功,err为null,否则为错误对象。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID 错误信息
9200001 the application is not an administrator of the device.
9200002 the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
let wantTemp: Want = {
  bundleName: 'bundleName',
  abilityName: 'abilityName',
};
let policy: usbManager.UsbPolicy = usbManager.UsbPolicy.READ_WRITE

usbManager.setUsbPolicy(wantTemp, policy, (err) => {
  if (err) {
    console.error(`Failed to set usb policy. Code is ${err.code}, message is ${err.message}`);
    return;
  }
  console.info('Succeeded in setting usb policy');
})

usbManager.setUsbPolicy

setUsbPolicy(admin: Want, usbPolicy: UsbPolicy): Promise<void>

指定设备管理应用设置USB的读写策略。使用Promise异步回调。

需要权限: ohos.permission.ENTERPRISE_MANAGE_USB

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

参数:

参数名 类型 必填 说明
admin Want 设备管理应用。
usbPolicy UsbPolicy USB读写策略(此接口只支持READ_WRITE和READ_ONLY)。

返回值:

类型 说明
Promise<void> 无返回结果的Promise对象。当指定设备管理应用设置USB策略失败时抛出错误对象。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID 错误信息
9200001 the application is not an administrator of the device.
9200002 the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
let wantTemp: Want = {
  bundleName: 'bundleName',
  abilityName: 'abilityName',
};
let policy: usbManager.UsbPolicy = usbManager.UsbPolicy.READ_WRITE

usbManager.setUsbPolicy(wantTemp, policy).then(() => {
  console.info('Succeeded in setting usb policy');
}).catch((err: BusinessError) => {
  console.error(`Failed to set usb policy. Code is ${err.code}, message is ${err.message}`);
})

UsbPolicy

USB读写策略的枚举。

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

名称 说明
READ_WRITE 0 可读可写。
READ_ONLY 1 只读。
DISABLED11+ 2 禁用。

UsbDeviceId11+

USB设备ID信息。

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

名称 类型 必填 说明
vendorId number 厂商ID。
productId number 产品ID。

usbManager.disableUsb11+

disableUsb(admin: Want, disable: boolean): void

指定设备管理应用设置禁用或启用USB。

需要权限: ohos.permission.ENTERPRISE_MANAGE_USB

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

参数:

参数名 类型 必填 说明
admin Want 设备管理应用。
disable boolean 是否禁用USB设备,true表示禁用,false表示不禁用。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID 错误信息
9200001 the application is not an administrator of the device.
9200002 the administrator application does not have permission to manage the device.
9200010 a conflicting policy has been configured.

示例:

import Want from '@ohos.app.ability.Want';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
try {
  usbManager.disableUsb(wantTemp, true);
  console.info(`Succeeded in disabling USB`);
} catch (err) {
  console.error(`Failed to disabling USB. Code: ${err.code}, message: ${err.message}`);
}

usbManager.isUsbDisabled11+

isUsbDisabled(admin: Want): boolean

指定设备管理应用查询USB是否禁用。

需要权限: ohos.permission.ENTERPRISE_MANAGE_USB

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

参数:

参数名 类型 必填 说明
admin Want 设备管理应用。

返回值:

类型 说明
boolean 返回true表示USB被禁用,
返回false表示USB未被禁用。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID 错误信息
9200001 the application is not an administrator of the device.
9200002 the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
try {
  let isDisabled = usbManager.isUsbDisabled(wantTemp);
  console.info(`Succeeded in querying if USB is disabled: ${isDisabled}`);
} catch (err) {
  console.error(`Failed to query if USB is disabled. Code: ${err.code}, message: ${err.message}`);
}

usbManager.addAllowedUsbDevices11+

addAllowedUsbDevices(admin: Want, usbDeviceIds: Array<UsbDeviceId>): void

指定设备管理应用添加USB设备可用白名单。

需要权限: ohos.permission.ENTERPRISE_MANAGE_USB

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

参数:

参数名 类型 必填 说明
admin Want 设备管理应用。
usbDeviceIds Array<UsbDeviceId> USB设备ID数组。添加后的数组长度上限为1000。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID 错误信息
9200001 the application is not an administrator of the device.
9200002 the administrator application does not have permission to manage the device.
9200010 a conflicting policy has been configured.

示例:

import Want from '@ohos.app.ability.Want';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
try {
  let usbDeviceIds: Array<usbManager.UsbDeviceId> = [{
      vendorId: 1,
      productId: 1
  }];
  usbManager.addAllowedUsbDevices(wantTemp, usbDeviceIds);
  console.info(`Succeeded in adding allowed USB devices`);
} catch (err) {
  console.error(`Failed to adding allowed USB devices. Code: ${err.code}, message: ${err.message}`);
}

usbManager.removeAllowedUsbDevices11+

removeAllowedUsbDevices(admin: Want, usbDeviceIds: Array<UsbDeviceId>): void

指定设备管理应用移除USB设备可用白名单。

需要权限: ohos.permission.ENTERPRISE_MANAGE_USB

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

参数:

参数名 类型 必填 说明
admin Want 设备管理应用。
usbDeviceIds Array<UsbDeviceId> USB设备ID数组。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID 错误信息
9200001 the application is not an administrator of the device.
9200002 the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
try {
  let usbDeviceIds: Array<usbManager.UsbDeviceId> = [{
      vendorId: 1,
      productId: 1
  }];
  usbManager.removeAllowedUsbDevices(wantTemp, usbDeviceIds);
  console.info(`Succeeded in removing allowed USB devices`);
} catch (err) {
  console.error(`Failed to removing allowed USB devices. Code: ${err.code}, message: ${err.message}`);
}

usbManager.getAllowedUsbDevices11+

getAllowedUsbDevices(admin: Want): Array<UsbDeviceId>

指定设备管理应用获取USB设备可用白名单。

需要权限: ohos.permission.ENTERPRISE_MANAGE_USB

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

参数:

参数名 类型 必填 说明
admin Want 设备管理应用。

返回值:

类型 说明
Array<UsbDeviceId> 可用USB白名单设备ID数组。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID 错误信息
9200001 the application is not an administrator of the device.
9200002 the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
try {
  let result: Array<usbManager.UsbDeviceId> = usbManager.getAllowedUsbDevices(wantTemp);
  console.info(`Succeeded in removing allowed USB devices. Result: ${JSON.stringify(result)}`);
} catch (err) {
  console.error(`Failed to removing allowed USB devices. Code: ${err.code}, message: ${err.message}`);
}

usbManager.setUsbStorageDeviceAccessPolicy11+

setUsbStorageDeviceAccessPolicy(admin: Want, usbPolicy: UsbPolicy): void

指定设备管理应用设置USB存储设备访问策略。

需要权限: ohos.permission.ENTERPRISE_MANAGE_USB

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

参数:

参数名 类型 必填 说明
admin Want 设备管理应用。
usbPolicy UsbPolicy USB存储设备访问策略。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID 错误信息
9200001 the application is not an administrator of the device.
9200002 the administrator application does not have permission to manage the device.
9200010 a conflicting policy has been configured.

示例:

import Want from '@ohos.app.ability.Want';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
try {
  let policy: usbManager.UsbPolicy = usbManager.UsbPolicy.DISABLED;
  usbManager.setUsbStorageDeviceAccessPolicy(wantTemp, policy);
  console.info(`Succeeded in setting USB storage device access policy`);
} catch (err) {
  console.error(`Failed to setting USB storage device access policy. Code: ${err.code}, message: ${err.message}`);
}

usbManager.getUsbStorageDeviceAccessPolicy11+

getUsbStorageDeviceAccessPolicy(admin: Want): UsbPolicy

指定设备管理应用获取USB存储设备访问策略。

需要权限: ohos.permission.ENTERPRISE_MANAGE_USB

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

参数:

参数名 类型 必填 说明
admin Want 设备管理应用。

返回值:

类型 说明
UsbPolicy USB存储设备访问策略。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID 错误信息
9200001 the application is not an administrator of the device.
9200002 the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
try {
  let result: usbManager.UsbPolicy = usbManager.getUsbStorageDeviceAccessPolicy(wantTemp);
  console.info(`Succeeded in getting USB storage device access policy. Result: ${JSON.stringify(result)}`);
} catch (err) {
  console.error(`Failed togetting USB storage device access policy. Code: ${err.code}, message: ${err.message}`);
}