1# @ohos.enterprise.bluetoothManager (Bluetooth Management) 2 3The **bluetoothManager** module provides Bluetooth management capabilities, including setting and obtaining Bluetooth information. 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 { bluetoothManager } from '@kit.MDMKit'; 17``` 18 19## bluetoothManager.getBluetoothInfo 20 21getBluetoothInfo(admin: Want): BluetoothInfo 22 23Obtains device Bluetooth information through the specified device administrator application. This API returns the result synchronously. If the operation is successful, the Bluetooth information obtained is returned. If the operation fails, an exception will be thrown. 24 25**Required permissions**: ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH 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 37**Return value** 38 39| Type | Description | 40| ------------------------------- | ------------------------------------------------ | 41| [BluetoothInfo](#bluetoothinfo) | Bluetooth information, including the Bluetooth name, state, and profile connection state.| 42 43**Error codes** 44 45For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md). 46 47| ID| Error Message | 48| -------- | ------------------------------------------------------------ | 49| 9200001 | The application is not an administrator application of the device. | 50| 9200002 | The administrator application does not have permission to manage the device. | 51| 201 | Permission verification failed. The application does not have the permission required to call the API. | 52| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 53 54**Example** 55 56```ts 57import { Want } from '@kit.AbilityKit'; 58import { bluetoothManager } from '@kit.MDMKit'; 59let wantTemp: Want = { 60 bundleName: 'com.example.myapplication', 61 abilityName: 'EntryAbility', 62}; 63 64try { 65 let result: bluetoothManager.BluetoothInfo = bluetoothManager.getBluetoothInfo(wantTemp); 66 console.info(`Succeeded in getting bluetooth info: ${JSON.stringify(result)}`); 67} catch(err) { 68 console.error(`Failed to get bluetooth info. Code: ${err.code}, message: ${err.message}`); 69} 70``` 71 72## bluetoothManager.addAllowedBluetoothDevices 73 74addAllowedBluetoothDevices(admin: Want, deviceIds: Array\<string>): void 75 76Adds allowed Bluetooth devices through the specified device administrator application. 77 78**Required permissions**: ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH 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| deviceIds | Array\<string> | Yes | MAC addresses of the Bluetooth devices to add. This array can hold a maximum of 1000 MAC addresses.| 90 91**Error codes** 92 93For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md). 94 95| ID| Error Message | 96| -------- | ------------------------------------------------------------ | 97| 9200001 | The application is not an administrator application of the device. | 98| 9200002 | The administrator application does not have permission to manage the device. | 99| 9200010 | A conflict policy has been configured. | 100| 201 | Permission verification failed. The application does not have the permission required to call the API. | 101| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 102 103**Example** 104 105```ts 106import { Want } from '@kit.AbilityKit'; 107import { bluetoothManager } from '@kit.MDMKit'; 108let wantTemp: Want = { 109 bundleName: 'com.example.myapplication', 110 abilityName: 'EntryAbility', 111}; 112let deviceIds: Array<string> = ["00:1A:2B:3C:4D:5E","AA:BB:CC:DD:EE:FF"]; 113try { 114 bluetoothManager.addAllowedBluetoothDevices(wantTemp,deviceIds); 115 console.info(`Succeeded in adding allowed bluetooth devices.`); 116} catch(err) { 117 console.error(`Failed to add allowed bluetooth devices. Code: ${err.code}, message: ${err.message}`); 118} 119``` 120 121## bluetoothManager.removeAllowedBluetoothDevices 122 123removeAllowedBluetoothDevices(admin: Want, deviceIds: Array\<string>): void 124 125Removes allowed Bluetooth devices through the specified device administrator application. 126 127**Required permissions**: ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH 128 129**System capability**: SystemCapability.Customization.EnterpriseDeviceManager 130 131 132 133**Parameters** 134 135| Name | Type | Mandatory| Description | 136| --------- | ------------------------------------------------------- | ---- | ----------------------- | 137| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes | Device administrator application. | 138| deviceIds | Array\<string> | Yes | MAC addresses of the Bluetooth devices to remove.| 139 140**Error codes** 141 142For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md). 143 144| ID| Error Message | 145| -------- | ------------------------------------------------------------ | 146| 9200001 | The application is not an administrator application of the device. | 147| 9200002 | The administrator application does not have permission to manage the device. | 148| 201 | Permission verification failed. The application does not have the permission required to call the API. | 149| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 150 151**Example** 152 153```ts 154import { Want } from '@kit.AbilityKit'; 155import { bluetoothManager } from '@kit.MDMKit'; 156let wantTemp: Want = { 157 bundleName: 'com.example.myapplication', 158 abilityName: 'EntryAbility', 159}; 160let deviceIds: Array<string> = ["00:1A:2B:3C:4D:5E","AA:BB:CC:DD:EE:FF"]; 161try { 162 bluetoothManager.removeAllowedBluetoothDevices(wantTemp,deviceIds); 163 console.info(`Succeeded in removing allowed bluetooth devices.`); 164} catch(err) { 165 console.error(`Failed to remove allowed bluetooth devices. Code: ${err.code}, message: ${err.message}`); 166} 167``` 168 169## bluetoothManager.getAllowedBluetoothDevices 170 171getAllowedBluetoothDevices(admin: Want): Array\<string>; 172 173Obtains allowed Bluetooth devices through the specified device administrator application. 174 175**Required permissions**: ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH 176 177**System capability**: SystemCapability.Customization.EnterpriseDeviceManager 178 179 180 181**Parameters** 182 183| Name| Type | Mandatory| Description | 184| ------ | ------------------------------------------------------- | ---- | -------------- | 185| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes | Device administrator application.| 186 187**Return value** 188 189| Type | Description | 190| -------------- | ----------------------------------- | 191| Array\<string> | MAC addresses of allowed Bluetooth devices obtained.| 192 193**Error codes** 194 195For details about the error codes, see [Enterprise Device Management Error Codes](errorcode-enterpriseDeviceManager.md) and [Universal Error Codes](../errorcode-universal.md). 196 197| ID| Error Message | 198| -------- | ------------------------------------------------------------ | 199| 9200001 | The application is not an administrator application of the device. | 200| 9200002 | The administrator application does not have permission to manage the device. | 201| 201 | Permission verification failed. The application does not have the permission required to call the API. | 202| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 203 204**Example** 205 206```ts 207import { Want } from '@kit.AbilityKit'; 208import { bluetoothManager } from '@kit.MDMKit'; 209let wantTemp: Want = { 210 bundleName: 'com.example.myapplication', 211 abilityName: 'EntryAbility', 212}; 213try { 214 let result: Array<string> = bluetoothManager.getAllowedBluetoothDevices(wantTemp); 215 console.info(`Succeeded in getting allowed bluetooth devices. Result: ${JSON.stringify(result)}`); 216} catch(err) { 217 console.error(`Failed to get allowed bluetooth devices. Code: ${err.code}, message: ${err.message}`); 218} 219``` 220 221## BluetoothInfo 222 223Represents the device Bluetooth information. 224 225**System capability**: SystemCapability.Customization.EnterpriseDeviceManager 226 227 228 229**Model restriction**: This API can be used only in the stage model. 230 231| Name | Type | Mandatory| Description | 232| --------------- | ------------------------------------------------------------ | ---- | ------------------------ | 233| name | string | Yes | Bluetooth name of the device. | 234| state | [access.BluetoothState](../apis-connectivity-kit/js-apis-bluetooth-access.md#bluetoothstate) | Yes | Bluetooth state of the device. | 235| connectionState | [constant.ProfileConnectionState](../apis-connectivity-kit/js-apis-bluetooth-constant.md#profileconnectionstate) | Yes | Bluetooth profile connection state of the device.| 236