1e41f4b71Sopenharmony_ci# @ohos.enterprise.usbManager (USB Management) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **usbManager** module provides APIs for USB management. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> The APIs of this module can be used only in the stage model. 10e41f4b71Sopenharmony_ci> 11e41f4b71Sopenharmony_ci> The APIs of this module can be called only by a [device administrator application](../../mdm/mdm-kit-guide.md#introduction) that is enabled. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci## Modules to Import 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci```ts 16e41f4b71Sopenharmony_ciimport { usbManager } from '@kit.MDMKit'; 17e41f4b71Sopenharmony_ci``` 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci## usbManager.addAllowedUsbDevices 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ciaddAllowedUsbDevices(admin: Want, usbDeviceIds: Array\<UsbDeviceId>): void 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ciAdds allowed USB devices through the specified device administrator application. 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ciRequired permissions: ohos.permission.ENTERPRISE_MANAGE_USB 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Customization.EnterpriseDeviceManager 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci**Parameters** 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 34e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------------------- | ---- | ------------------------------------------- | 35e41f4b71Sopenharmony_ci| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes | Device administrator application. | 36e41f4b71Sopenharmony_ci| usbDeviceIds | Array<[UsbDeviceId](#usbdeviceid)> | Yes | IDs of the allowed USB devices to add. This array can hold a maximum of 1000 USB device IDs.| 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci**Error codes** 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ciFor details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md). 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci| ID| Error Message | 43e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | 44e41f4b71Sopenharmony_ci| 9200001 | The application is not an administrator application of the device. | 45e41f4b71Sopenharmony_ci| 9200002 | The administrator application does not have permission to manage the device. | 46e41f4b71Sopenharmony_ci| 9200010 | A conflict policy has been configured. | 47e41f4b71Sopenharmony_ci| 201 | Permission verification failed. The application does not have the permission required to call the API. | 48e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci**Example** 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci```ts 53e41f4b71Sopenharmony_ciimport { Want } from '@kit.AbilityKit'; 54e41f4b71Sopenharmony_cilet wantTemp: Want = { 55e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 56e41f4b71Sopenharmony_ci abilityName: 'EntryAbility', 57e41f4b71Sopenharmony_ci}; 58e41f4b71Sopenharmony_citry { 59e41f4b71Sopenharmony_ci let usbDeviceIds: Array<usbManager.UsbDeviceId> = [{ 60e41f4b71Sopenharmony_ci vendorId: 1, 61e41f4b71Sopenharmony_ci productId: 1 62e41f4b71Sopenharmony_ci }]; 63e41f4b71Sopenharmony_ci usbManager.addAllowedUsbDevices(wantTemp, usbDeviceIds); 64e41f4b71Sopenharmony_ci console.info(`Succeeded in adding allowed USB devices.`); 65e41f4b71Sopenharmony_ci} catch (err) { 66e41f4b71Sopenharmony_ci console.error(`Failed to adding allowed USB devices. Code: ${err.code}, message: ${err.message}`); 67e41f4b71Sopenharmony_ci} 68e41f4b71Sopenharmony_ci``` 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci## usbManager.removeAllowedUsbDevices 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ciremoveAllowedUsbDevices(admin: Want, usbDeviceIds: Array\<UsbDeviceId>): void 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ciRemoves allowed USB devices through the specified device administrator application. 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ciRequired permissions: ohos.permission.ENTERPRISE_MANAGE_USB 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Customization.EnterpriseDeviceManager 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci**Parameters** 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 85e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------------------- | ---- | --------------- | 86e41f4b71Sopenharmony_ci| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes | Device administrator application. | 87e41f4b71Sopenharmony_ci| usbDeviceIds | Array<[UsbDeviceId](#usbdeviceid)> | Yes | IDs of the allowed USB devices to remove.| 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci**Error codes** 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ciFor details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md). 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci| ID| Error Message | 94e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | 95e41f4b71Sopenharmony_ci| 9200001 | The application is not an administrator application of the device. | 96e41f4b71Sopenharmony_ci| 9200002 | The administrator application does not have permission to manage the device. | 97e41f4b71Sopenharmony_ci| 201 | Permission verification failed. The application does not have the permission required to call the API. | 98e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci**Example** 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci```ts 103e41f4b71Sopenharmony_ciimport { Want } from '@kit.AbilityKit'; 104e41f4b71Sopenharmony_cilet wantTemp: Want = { 105e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 106e41f4b71Sopenharmony_ci abilityName: 'EntryAbility', 107e41f4b71Sopenharmony_ci}; 108e41f4b71Sopenharmony_citry { 109e41f4b71Sopenharmony_ci let usbDeviceIds: Array<usbManager.UsbDeviceId> = [{ 110e41f4b71Sopenharmony_ci vendorId: 1, 111e41f4b71Sopenharmony_ci productId: 1 112e41f4b71Sopenharmony_ci }]; 113e41f4b71Sopenharmony_ci usbManager.removeAllowedUsbDevices(wantTemp, usbDeviceIds); 114e41f4b71Sopenharmony_ci console.info(`Succeeded in removing allowed USB devices.`); 115e41f4b71Sopenharmony_ci} catch (err) { 116e41f4b71Sopenharmony_ci console.error(`Failed to removing allowed USB devices. Code: ${err.code}, message: ${err.message}`); 117e41f4b71Sopenharmony_ci} 118e41f4b71Sopenharmony_ci``` 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci## usbManager.getAllowedUsbDevices 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_cigetAllowedUsbDevices(admin: Want): Array\<UsbDeviceId> 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ciObtains allowed USB devices through the specified device administrator application. 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ciRequired permissions: ohos.permission.ENTERPRISE_MANAGE_USB 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Customization.EnterpriseDeviceManager 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci**Parameters** 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 135e41f4b71Sopenharmony_ci| ------ | ------------------------------------------------------- | ---- | -------------- | 136e41f4b71Sopenharmony_ci| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes | Device administrator application.| 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ci**Return value** 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ci| Type | Description | 141e41f4b71Sopenharmony_ci| ---------------------------------- | ------------------------- | 142e41f4b71Sopenharmony_ci| Array<[UsbDeviceId](#usbdeviceid)> | Allowed USB devices obtained.| 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci**Error codes** 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ciFor details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md). 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci| ID| Error Message | 149e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | 150e41f4b71Sopenharmony_ci| 9200001 | The application is not an administrator application of the device. | 151e41f4b71Sopenharmony_ci| 9200002 | The administrator application does not have permission to manage the device. | 152e41f4b71Sopenharmony_ci| 201 | Permission verification failed. The application does not have the permission required to call the API. | 153e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci**Example** 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci```ts 158e41f4b71Sopenharmony_ciimport { Want } from '@kit.AbilityKit'; 159e41f4b71Sopenharmony_cilet wantTemp: Want = { 160e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 161e41f4b71Sopenharmony_ci abilityName: 'EntryAbility', 162e41f4b71Sopenharmony_ci}; 163e41f4b71Sopenharmony_citry { 164e41f4b71Sopenharmony_ci let result: Array<usbManager.UsbDeviceId> = usbManager.getAllowedUsbDevices(wantTemp); 165e41f4b71Sopenharmony_ci console.info(`Succeeded in removing allowed USB devices. Result: ${JSON.stringify(result)}`); 166e41f4b71Sopenharmony_ci} catch (err) { 167e41f4b71Sopenharmony_ci console.error(`Failed to removing allowed USB devices. Code: ${err.code}, message: ${err.message}`); 168e41f4b71Sopenharmony_ci} 169e41f4b71Sopenharmony_ci``` 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci## usbManager.setUsbStorageDeviceAccessPolicy 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_cisetUsbStorageDeviceAccessPolicy(admin: Want, usbPolicy: UsbPolicy): void 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ciSets the USB storage device access policy through the specified device administrator application. 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ciRequired permissions: ohos.permission.ENTERPRISE_MANAGE_USB 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Customization.EnterpriseDeviceManager 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci**Parameters** 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 186e41f4b71Sopenharmony_ci| --------- | ------------------------------------------------------- | ---- | --------------------- | 187e41f4b71Sopenharmony_ci| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes | Device administrator application. | 188e41f4b71Sopenharmony_ci| usbPolicy | [UsbPolicy](#usbpolicy) | Yes | USB storage device access policy.| 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ci**Error codes** 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ciFor details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md). 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ci| ID| Error Message | 195e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | 196e41f4b71Sopenharmony_ci| 9200001 | The application is not an administrator application of the device. | 197e41f4b71Sopenharmony_ci| 9200002 | The administrator application does not have permission to manage the device. | 198e41f4b71Sopenharmony_ci| 9200010 | A conflict policy has been configured. | 199e41f4b71Sopenharmony_ci| 201 | Permission verification failed. The application does not have the permission required to call the API. | 200e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ci**Example** 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci```ts 205e41f4b71Sopenharmony_ciimport { Want } from '@kit.AbilityKit'; 206e41f4b71Sopenharmony_cilet wantTemp: Want = { 207e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 208e41f4b71Sopenharmony_ci abilityName: 'EntryAbility', 209e41f4b71Sopenharmony_ci}; 210e41f4b71Sopenharmony_citry { 211e41f4b71Sopenharmony_ci let policy: usbManager.UsbPolicy = usbManager.UsbPolicy.DISABLED; 212e41f4b71Sopenharmony_ci usbManager.setUsbStorageDeviceAccessPolicy(wantTemp, policy); 213e41f4b71Sopenharmony_ci console.info(`Succeeded in setting USB storage device access policy.`); 214e41f4b71Sopenharmony_ci} catch (err) { 215e41f4b71Sopenharmony_ci console.error(`Failed to setting USB storage device access policy. Code: ${err.code}, message: ${err.message}`); 216e41f4b71Sopenharmony_ci} 217e41f4b71Sopenharmony_ci``` 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_ci## usbManager.getUsbStorageDeviceAccessPolicy 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_cigetUsbStorageDeviceAccessPolicy(admin: Want): UsbPolicy 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ciObtains the USB storage device access policy through the specified device administrator application. 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ciRequired permissions: ohos.permission.ENTERPRISE_MANAGE_USB 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Customization.EnterpriseDeviceManager 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_ci 230e41f4b71Sopenharmony_ci 231e41f4b71Sopenharmony_ci**Parameters** 232e41f4b71Sopenharmony_ci 233e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 234e41f4b71Sopenharmony_ci| ------ | ------------------------------------------------------- | ---- | -------------- | 235e41f4b71Sopenharmony_ci| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes | Device administrator application.| 236e41f4b71Sopenharmony_ci 237e41f4b71Sopenharmony_ci**Return value** 238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_ci| Type | Description | 240e41f4b71Sopenharmony_ci| ----------------------- | --------------------- | 241e41f4b71Sopenharmony_ci| [UsbPolicy](#usbpolicy) | USB storage device access policy obtained.| 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_ci**Error codes** 244e41f4b71Sopenharmony_ci 245e41f4b71Sopenharmony_ciFor details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md). 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ci| ID| Error Message | 248e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | 249e41f4b71Sopenharmony_ci| 9200001 | The application is not an administrator application of the device. | 250e41f4b71Sopenharmony_ci| 9200002 | The administrator application does not have permission to manage the device. | 251e41f4b71Sopenharmony_ci| 201 | Permission verification failed. The application does not have the permission required to call the API. | 252e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 253e41f4b71Sopenharmony_ci 254e41f4b71Sopenharmony_ci**Example** 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_ci```ts 257e41f4b71Sopenharmony_ciimport { Want } from '@kit.AbilityKit'; 258e41f4b71Sopenharmony_cilet wantTemp: Want = { 259e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 260e41f4b71Sopenharmony_ci abilityName: 'EntryAbility', 261e41f4b71Sopenharmony_ci}; 262e41f4b71Sopenharmony_citry { 263e41f4b71Sopenharmony_ci let result: usbManager.UsbPolicy = usbManager.getUsbStorageDeviceAccessPolicy(wantTemp); 264e41f4b71Sopenharmony_ci console.info(`Succeeded in getting USB storage device access policy. Result: ${JSON.stringify(result)}`); 265e41f4b71Sopenharmony_ci} catch (err) { 266e41f4b71Sopenharmony_ci console.error(`Failed togetting USB storage device access policy. Code: ${err.code}, message: ${err.message}`); 267e41f4b71Sopenharmony_ci} 268e41f4b71Sopenharmony_ci``` 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_ci## UsbDeviceId 271e41f4b71Sopenharmony_ci 272e41f4b71Sopenharmony_ciRepresents the USB device identity information. 273e41f4b71Sopenharmony_ci 274e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Customization.EnterpriseDeviceManager 275e41f4b71Sopenharmony_ci 276e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 277e41f4b71Sopenharmony_ci| --------- | ------ | ---- | -------- | 278e41f4b71Sopenharmony_ci| vendorId | number | Yes | Vendor ID.| 279e41f4b71Sopenharmony_ci| productId | number | Yes | Product ID.| 280e41f4b71Sopenharmony_ci 281e41f4b71Sopenharmony_ci## UsbPolicy 282e41f4b71Sopenharmony_ci 283e41f4b71Sopenharmony_ciEnumerates the USB access policies. 284e41f4b71Sopenharmony_ci 285e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Customization.EnterpriseDeviceManager 286e41f4b71Sopenharmony_ci 287e41f4b71Sopenharmony_ci| Name | Value | Description | 288e41f4b71Sopenharmony_ci| ---------- | ---- | ---------- | 289e41f4b71Sopenharmony_ci| READ_WRITE | 0 | Read and write.| 290e41f4b71Sopenharmony_ci| READ_ONLY | 1 | Read only. | 291e41f4b71Sopenharmony_ci| DISABLED | 2 | Disabled. | 292