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