• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.enterprise.usbManager (USB Management) (System API)
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 of this module can be called only by a [device administrator application](../../mdm/mdm-kit-guide.md#introduction) that is [enabled](js-apis-enterprise-adminManager-sys.md#adminmanagerenableadmin-2).
12>
13> - This topic describes only the system APIs provided by the module. For details about its public APIs, see [@ohos.enterprise.usbManager](js-apis-enterprise-usbManager.md).
14
15## Modules to Import
16
17```ts
18import { usbManager } from '@kit.MDMKit';
19```
20
21## usbManager.setUsbPolicy
22
23setUsbPolicy(admin: Want, usbPolicy: UsbPolicy, callback: AsyncCallback\<void>): void
24
25Sets the USB read/write policy. This API uses an asynchronous callback to return the result.
26
27Required permissions: ohos.permission.ENTERPRISE_MANAGE_USB
28
29**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
30
31
32
33**Parameters**
34
35| Name  | Type                                 | Mandatory  | Description     |
36| ----- | ----------------------------------- | ---- | ------- |
37| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes   | EnterpriseAdminExtensionAbility.|
38| usbPolicy  | [UsbPolicy](js-apis-enterprise-usbManager.md#usbpolicy) | Yes| USB access policy. This API supports **READ_WRITE** and **READ_ONLY** only.|
39| callback | AsyncCallback\<void> | Yes| Callback invoked to return the result. If the operation is successful, **err** is **null**. Otherwise, **err** is an error object.|
40
41**Error codes**
42
43For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md).
44
45| ID| Error Message                                                                     |
46| ------- | ---------------------------------------------------------------------------- |
47| 9200001 | The application is not an administrator application of the device.           |
48| 9200002 | The administrator application does not have permission to manage the device. |
49| 201 | Permission verification failed. The application does not have the permission required to call the API. |
50| 202 | Permission verification failed. A non-system application calls a system API. |
51| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
52
53**Example**
54
55```ts
56import { Want } from '@kit.AbilityKit';
57
58let wantTemp: Want = {
59  bundleName: 'com.example.myapplication',
60  abilityName: 'EntryAbility',
61};
62let policy: usbManager.UsbPolicy = usbManager.UsbPolicy.READ_WRITE;
63
64usbManager.setUsbPolicy(wantTemp, policy, (err) => {
65  if (err) {
66    console.error(`Failed to set usb policy. Code is ${err.code}, message is ${err.message}`);
67    return;
68  }
69  console.info('Succeeded in setting usb policy');
70})
71```
72
73## usbManager.setUsbPolicy
74
75setUsbPolicy(admin: Want, usbPolicy: UsbPolicy): Promise\<void>
76
77Sets the USB read/write policy. This API uses a promise to return the result.
78
79Required permissions: ohos.permission.ENTERPRISE_MANAGE_USB
80
81**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
82
83
84
85**Parameters**
86
87| Name  | Type                                 | Mandatory  | Description     |
88| ----- | ----------------------------------- | ---- | ------- |
89| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes   | EnterpriseAdminExtensionAbility.|
90| usbPolicy  | [UsbPolicy](js-apis-enterprise-usbManager.md#usbpolicy) | Yes| USB access policy. This API supports **READ_WRITE** and **READ_ONLY** only.|
91
92**Return value**
93
94| Type  | Description                                 |
95| ----- | ----------------------------------- |
96| Promise\<void> | Promise that returns no value. An error object is thrown when the USB policy fails to be set.|
97
98**Error codes**
99
100For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md).
101
102| ID| Error Message                                                                     |
103| ------- | ---------------------------------------------------------------------------- |
104| 9200001 | The application is not an administrator application of the device.            |
105| 9200002 | The administrator application does not have permission to manage the device. |
106| 201 | Permission verification failed. The application does not have the permission required to call the API. |
107| 202 | Permission verification failed. A non-system application calls a system API. |
108| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
109
110**Example**
111
112```ts
113import { Want } from '@kit.AbilityKit';
114
115import { BusinessError } from '@kit.BasicServicesKit';
116let wantTemp: Want = {
117  bundleName: 'com.example.myapplication',
118  abilityName: 'EntryAbility',
119};
120let policy: usbManager.UsbPolicy = usbManager.UsbPolicy.READ_WRITE
121
122usbManager.setUsbPolicy(wantTemp, policy).then(() => {
123  console.info('Succeeded in setting usb policy');
124}).catch((err: BusinessError) => {
125  console.error(`Failed to set usb policy. Code is ${err.code}, message is ${err.message}`);
126})
127```
128
129## usbManager.disableUsb<sup>11+</sup>
130
131disableUsb(admin: Want, disable: boolean): void
132
133Enables or disables USB.
134
135Required permissions: ohos.permission.ENTERPRISE_MANAGE_USB
136
137**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
138
139
140
141**Parameters**
142
143| Name | Type                                                   | Mandatory| Description                                            |
144| ------- | ------------------------------------------------------- | ---- | ------------------------------------------------ |
145| admin   | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes  | EnterpriseAdminExtensionAbility.          |
146| disable | boolean                                                 | Yes  | Whether to disable USB. The value **true** means to disable USB; the value **false** means the opposite.|
147
148**Error codes**
149
150For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md).
151
152| ID| Error Message                                                    |
153| -------- | ------------------------------------------------------------ |
154| 9200001  | The application is not an administrator application of the device. |
155| 9200002  | The administrator application does not have permission to manage the device. |
156| 9200010  | A conflict policy has been configured.                       |
157| 201      | Permission verification failed. The application does not have the permission required to call the API. |
158| 202      | Permission verification failed. A non-system application calls a system API. |
159| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
160
161**Example**
162
163```ts
164import { Want } from '@kit.AbilityKit';
165
166let wantTemp: Want = {
167  bundleName: 'com.example.myapplication',
168  abilityName: 'EntryAbility',
169};
170try {
171  usbManager.disableUsb(wantTemp, true);
172  console.info(`Succeeded in disabling USB`);
173} catch (err) {
174  console.error(`Failed to disabling USB. Code: ${err.code}, message: ${err.message}`);
175}
176```
177
178## usbManager.isUsbDisabled<sup>11+</sup>
179
180isUsbDisabled(admin: Want): boolean
181
182Queries whether the USB is disabled.
183
184Required permissions: ohos.permission.ENTERPRISE_MANAGE_USB
185
186**System capability**: SystemCapability.Customization.EnterpriseDeviceManager
187
188
189
190**Parameters**
191
192| Name| Type                                                   | Mandatory| Description                                  |
193| ------ | ------------------------------------------------------- | ---- | -------------------------------------- |
194| admin  | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes  | EnterpriseAdminExtensionAbility.|
195
196**Return value**
197
198| Type   | Description                                                  |
199| ------- | ------------------------------------------------------ |
200| boolean | Returns **true** if USB is disabled;<br>returns **false** otherwise.|
201
202**Error codes**
203
204For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md).
205
206| ID| Error Message                                                    |
207| -------- | ------------------------------------------------------------ |
208| 9200001  | The application is not an administrator application of the device. |
209| 9200002  | The administrator application does not have permission to manage the device. |
210| 201      | Permission verification failed. The application does not have the permission required to call the API. |
211| 202      | Permission verification failed. A non-system application calls a system API. |
212| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
213
214**Example**
215
216```ts
217import { Want } from '@kit.AbilityKit';
218
219let wantTemp: Want = {
220  bundleName: 'com.example.myapplication',
221  abilityName: 'EntryAbility',
222};
223try {
224  let isDisabled = usbManager.isUsbDisabled(wantTemp);
225  console.info(`Succeeded in querying if USB is disabled: ${isDisabled}`);
226} catch (err) {
227  console.error(`Failed to query if USB is disabled. Code: ${err.code}, message: ${err.message}`);
228}
229```
230