• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.enterprise.usbManager (USB Management)
2
3The **usbManager** module provides APIs for USB management.
4
5> **NOTE**
6>
7> - The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8>
9> - The APIs of this module can be used only in the stage model.
10>
11> - The APIs provided by this module can be called only by a [device administrator application](enterpriseDeviceManagement-overview.md#basic-concepts) that is [enabled](js-apis-enterprise-adminManager.md#adminmanagerenableadmin).
12
13## Modules to Import
14
15```ts
16import usbManager from '@ohos.enterprise.usbManager';
17```
18
19## usbManager.setUsbPolicy
20
21setUsbPolicy(admin: Want, usbPolicy: UsbPolicy, callback: AsyncCallback\<void>): void
22
23Sets the USB read/write policy through the specified device administrator application. This API uses an asynchronous callback to return the result.
24
25Required permissions: ohos.permission.ENTERPRISE_MANAGE_USB
26
27**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
28
29**System API**: This is a system API.
30
31**Parameters**
32
33| Name  | Type                                 | Mandatory  | Description     |
34| ----- | ----------------------------------- | ---- | ------- |
35| admin | [Want](js-apis-app-ability-want.md) | Yes   | Device administrator application.|
36| usbPolicy  | [UsbPolicy](#usbpolicy) | Yes| USB read/write policy to set.|
37| callback | AsyncCallback\<void> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **null**. Otherwise, **err** is an error object.|
38
39**Error codes**
40
41For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).
42
43| ID| Error Message                                                                     |
44| ------- | ---------------------------------------------------------------------------- |
45| 9200001 | the application is not an administrator of the device.                       |
46| 9200002 | the administrator application does not have permission to manage the device. |
47
48**Example**
49
50```ts
51import Want from '@ohos.app.ability.Want';
52let wantTemp: Want = {
53  bundleName: 'bundleName',
54  abilityName: 'abilityName',
55};
56let policy: usbManager.UsbPolicy = usbManager.UsbPolicy.READ_WRITE
57
58usbManager.setUsbPolicy(wantTemp, policy, (err) => {
59  if (err) {
60    console.error(`Failed to set usb policy. Code is ${err.code}, message is ${err.message}`);
61    return;
62  }
63  console.info('Succeeded in setting usb policy');
64})
65```
66
67## usbManager.setUsbPolicy
68
69setUsbPolicy(admin: Want, usbPolicy: UsbPolicy): Promise\<void>
70
71Sets the USB read/write policy through the specified device administrator application. This API uses a promise to return the result.
72
73Required permissions: ohos.permission.ENTERPRISE_MANAGE_USB
74
75**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
76
77**System API**: This is a system API.
78
79**Parameters**
80
81| Name  | Type                                 | Mandatory  | Description     |
82| ----- | ----------------------------------- | ---- | ------- |
83| admin | [Want](js-apis-app-ability-want.md) | Yes   | Device administrator application.|
84| usbPolicy  | [UsbPolicy](#usbpolicy) | Yes| USB read/write policy to set.|
85
86**Return value**
87
88| Type  | Description                                 |
89| ----- | ----------------------------------- |
90| Promise\<void> | Promise that returns no value. An error object will be thrown if the operation fails.|
91
92**Error codes**
93
94For details about the error codes, see [Enterprise Device Management Error Codes](../errorcodes/errorcode-enterpriseDeviceManager.md).
95
96| ID| Error Message                                                                     |
97| ------- | ---------------------------------------------------------------------------- |
98| 9200001 | the application is not an administrator of the device.                        |
99| 9200002 | the administrator application does not have permission to manage the device. |
100
101**Example**
102
103```ts
104import Want from '@ohos.app.ability.Want';
105import { BusinessError } from '@ohos.base';
106let wantTemp: Want = {
107  bundleName: 'bundleName',
108  abilityName: 'abilityName',
109};
110let policy: usbManager.UsbPolicy = usbManager.UsbPolicy.READ_WRITE
111
112usbManager.setUsbPolicy(wantTemp, policy).then(() => {
113  console.info('Succeeded in setting usb policy');
114}).catch((err: BusinessError) => {
115  console.error(`Failed to set usb policy. Code is ${err.code}, message is ${err.message}`);
116})
117```
118
119## UsbPolicy
120
121Enumerates the USB read/write policies.
122
123**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
124
125**System API**: This is a system API.
126
127| Name| Value| Description|
128| -------- | -------- | -------- |
129| READ_WRITE | 0 | The USB is readable and writable.|
130| READ_ONLY | 1 | The USB is read-only.|
131