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