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). 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 access policy through the specified device administrator application. 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 | Device administrator application.| 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 used 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'; 57let wantTemp: Want = { 58 bundleName: 'bundleName', 59 abilityName: 'abilityName', 60}; 61let policy: usbManager.UsbPolicy = usbManager.UsbPolicy.READ_WRITE 62 63usbManager.setUsbPolicy(wantTemp, policy, (err) => { 64 if (err) { 65 console.error(`Failed to set usb policy. Code is ${err.code}, message is ${err.message}`); 66 return; 67 } 68 console.info('Succeeded in setting usb policy'); 69}) 70``` 71 72## usbManager.setUsbPolicy 73 74setUsbPolicy(admin: Want, usbPolicy: UsbPolicy): Promise\<void> 75 76Sets the USB access policy through the specified device administrator application. This API uses a promise to return the result. 77 78Required permissions: ohos.permission.ENTERPRISE_MANAGE_USB 79 80**System capability**: SystemCapability.Customization.EnterpriseDeviceManager 81 82 83 84**Parameters** 85 86| Name | Type | Mandatory | Description | 87| ----- | ----------------------------------- | ---- | ------- | 88| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes | Device administrator application.| 89| usbPolicy | [UsbPolicy](js-apis-enterprise-usbManager.md#usbpolicy) | Yes| USB access policy. This API supports **READ_WRITE** and **READ_ONLY** only.| 90 91**Return value** 92 93| Type | Description | 94| ----- | ----------------------------------- | 95| Promise\<void> | Promise that returns no value. An error object will be thrown if the operation fails.| 96 97**Error codes** 98 99For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md). 100 101| ID| Error Message | 102| ------- | ---------------------------------------------------------------------------- | 103| 9200001 | The application is not an administrator application of the device. | 104| 9200002 | The administrator application does not have permission to manage the device. | 105| 201 | Permission verification failed. The application does not have the permission required to call the API. | 106| 202 | Permission verification failed. A non-system application calls a system API. | 107| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 108 109**Example** 110 111```ts 112import { Want } from '@kit.AbilityKit'; 113import { BusinessError } from '@kit.BasicServicesKit'; 114let wantTemp: Want = { 115 bundleName: 'bundleName', 116 abilityName: 'abilityName', 117}; 118let policy: usbManager.UsbPolicy = usbManager.UsbPolicy.READ_WRITE 119 120usbManager.setUsbPolicy(wantTemp, policy).then(() => { 121 console.info('Succeeded in setting usb policy'); 122}).catch((err: BusinessError) => { 123 console.error(`Failed to set usb policy. Code is ${err.code}, message is ${err.message}`); 124}) 125``` 126 127## usbManager.disableUsb<sup>11+</sup> 128 129disableUsb(admin: Want, disable: boolean): void 130 131Enables or disables USB through the specified device administrator application. 132 133Required permissions: ohos.permission.ENTERPRISE_MANAGE_USB 134 135**System capability**: SystemCapability.Customization.EnterpriseDeviceManager 136 137 138 139**Parameters** 140 141| Name | Type | Mandatory| Description | 142| ------- | ----------------------------------- | ---- | ------------------------------------------------ | 143| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes | Device administrator application. | 144| disable | boolean | Yes | Whether to disable USB. The value **true** means to disable USB; the value **false** means the opposite.| 145 146**Error codes** 147 148For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md). 149 150| ID| Error Message | 151| -------- | ------------------------------------------------------------ | 152| 9200001 | The application is not an administrator application of the device. | 153| 9200002 | The administrator application does not have permission to manage the device. | 154| 9200010 | A conflict policy has been configured. | 155| 201 | Permission verification failed. The application does not have the permission required to call the API. | 156| 202 | Permission verification failed. A non-system application calls a system API. | 157| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 158 159**Example** 160 161```ts 162import { Want } from '@kit.AbilityKit'; 163let wantTemp: Want = { 164 bundleName: 'com.example.myapplication', 165 abilityName: 'EntryAbility', 166}; 167try { 168 usbManager.disableUsb(wantTemp, true); 169 console.info(`Succeeded in disabling USB`); 170} catch (err) { 171 console.error(`Failed to disabling USB. Code: ${err.code}, message: ${err.message}`); 172} 173``` 174 175## usbManager.isUsbDisabled<sup>11+</sup> 176 177isUsbDisabled(admin: Want): boolean 178 179Checks whether USB is disabled through the specified device administrator application. 180 181Required permissions: ohos.permission.ENTERPRISE_MANAGE_USB 182 183**System capability**: SystemCapability.Customization.EnterpriseDeviceManager 184 185 186 187**Parameters** 188 189| Name| Type | Mandatory| Description | 190| ------ | ----------------------------------- | ---- | -------------- | 191| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes | Device administrator application.| 192 193**Return value** 194 195| Type | Description | 196| ------- | ------------------------------------------------------ | 197| boolean | Returns **true** if USB is disabled; returns **false** otherwise.| 198 199**Error codes** 200 201For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md). 202 203| ID| Error Message | 204| -------- | ------------------------------------------------------------ | 205| 9200001 | The application is not an administrator application of the device. | 206| 9200002 | The administrator application does not have permission to manage the device. | 207| 201 | Permission verification failed. The application does not have the permission required to call the API. | 208| 202 | Permission verification failed. A non-system application calls a system API. | 209| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 210 211**Example** 212 213```ts 214import { Want } from '@kit.AbilityKit'; 215let wantTemp: Want = { 216 bundleName: 'com.example.myapplication', 217 abilityName: 'EntryAbility', 218}; 219try { 220 let isDisabled = usbManager.isUsbDisabled(wantTemp); 221 console.info(`Succeeded in querying if USB is disabled: ${isDisabled}`); 222} catch (err) { 223 console.error(`Failed to query if USB is disabled. Code: ${err.code}, message: ${err.message}`); 224} 225``` 226