1# @ohos.bluetooth.access (Bluetooth Access Module)
2
3The **access** module provides APIs for enabling and disabling Bluetooth and obtaining the Bluetooth status.
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
10## Modules to Import
11
12```js
13import { access } from '@kit.ConnectivityKit';
14```
15
16
17## access.enableBluetooth
18
19enableBluetooth(): void
20
21Enables Bluetooth.
22
23**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
24
25**Atomic service API**: This API can be used in atomic services since API version 12.
26
27**System capability**: SystemCapability.Communication.Bluetooth.Core
28
29**Error codes**
30
31For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md).
32
33| ID| Error Message           |
34| -------- | ------------------ |
35|201 | Permission denied.                 |
36|801 | Capability not supported.          |
37|2900001   | Service stopped.   |
38|2900099   | Operation failed.  |
39
40**Example**
41
42```js
43import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
44try {
45    access.enableBluetooth();
46} catch (err) {
47    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
48}
49```
50
51
52## access.disableBluetooth
53
54disableBluetooth(): void
55
56Disables Bluetooth.
57
58**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
59
60**Atomic service API**: This API can be used in atomic services since API version 12.
61
62**System capability**: SystemCapability.Communication.Bluetooth.Core
63
64**Error codes**
65
66For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md).
67
68|ID  | Error Message          |
69| -------- | ------------------ |
70|201 | Permission denied.                 |
71|801 | Capability not supported.          |
72|2900001   | Service stopped.   |
73|2900099   | Operation failed.  |
74
75**Example**
76
77```js
78import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
79try {
80    access.disableBluetooth();
81} catch (err) {
82    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
83}
84```
85
86
87## access.getState
88
89getState(): BluetoothState
90
91Obtains the Bluetooth state.
92
93**Atomic service API**: This API can be used in atomic services since API version 11.
94
95**System capability**: SystemCapability.Communication.Bluetooth.Core
96
97**Return value**
98
99| Type                             | Description             |
100| --------------------------------- | ---------------- |
101| [BluetoothState](#bluetoothstate) | Bluetooth state obtained.|
102
103**Error codes**
104
105For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md).
106
107|ID  | Error Message          |
108| -------- | ------------------ |
109|801 | Capability not supported.          |
110|2900001   | Service stopped.   |
111|2900099   | Operation failed.  |
112
113**Example**
114
115```js
116import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
117try {
118    let state = access.getState();
119} catch (err) {
120    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
121}
122```
123
124## access.on('stateChange')<a name="stateChange"></a>
125
126on(type: 'stateChange', callback: Callback&lt;BluetoothState&gt;): void
127
128Subscribes to Bluetooth state changes. This API uses an asynchronous callback to return the result.
129
130**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
131
132**Atomic service API**: This API can be used in atomic services since API version 12.
133
134**System capability**: SystemCapability.Communication.Bluetooth.Core
135
136**Parameters**
137
138| Name  | Type                                              | Mandatory | Description                                                      |
139| -------- | ------------------------------------------------- | ----- | ---------------------------------------------------------- |
140| type     | string                                            | Yes   | Event type. The value is **stateChange**, which indicates Bluetooth state changes.              |
141| callback | Callback&lt;[BluetoothState](#bluetoothstate)&gt; | Yes   | Callback used to return the Bluetooth state. You need to implement this callback.|
142
143**Error codes**
144
145For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md).
146
147|ID  | Error Message          |
148| -------- | ------------------ |
149|201 | Permission denied.                 |
150|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
151|801 | Capability not supported.          |
152|2900099   | Operation failed.  |
153
154**Example**
155
156```js
157import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
158function onReceiveEvent(data: access.BluetoothState) {
159    console.info('bluetooth state = '+ JSON.stringify(data));
160}
161try {
162    access.on('stateChange', onReceiveEvent);
163} catch (err) {
164    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
165}
166```
167
168
169## access.off('stateChange')
170
171off(type: 'stateChange', callback?: Callback&lt;BluetoothState&gt;): void
172
173Unsubscribes from Bluetooth state changes.
174
175**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
176
177**Atomic service API**: This API can be used in atomic services since API version 12.
178
179**System capability**: SystemCapability.Communication.Bluetooth.Core
180
181**Parameters**
182
183| Name     | Type                                      | Mandatory  | Description                                      |
184| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
185| type     | string                                   | Yes   | Event type. The value is **stateChange**, which indicates Bluetooth state changes.          |
186| callback | Callback&lt;[BluetoothState](#bluetoothstate)&gt; | No   | Callback to unregister. If this parameter is not set, this API unregisters all callbacks for Bluetooth state changes.|
187
188**Error codes**
189
190For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md).
191
192| ID| Error Message|
193| -------- | ---------------------------- |
194|201 | Permission denied.                 |
195|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.                 |
196|801 | Capability not supported.          |
197|2900099 | Operation failed.                        |
198
199**Example**
200
201```js
202import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
203function onReceiveEvent(data: access.BluetoothState) {
204    console.info('bluetooth state = '+ JSON.stringify(data));
205}
206try {
207    access.on('stateChange', onReceiveEvent);
208    access.off('stateChange', onReceiveEvent);
209} catch (err) {
210    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
211}
212```
213
214
215## BluetoothState
216
217Enumerates the Bluetooth states.
218
219**Atomic service API**: This API can be used in atomic services since API version 11.
220
221**System capability**: SystemCapability.Communication.Bluetooth.Core
222
223| Name                   | Value | Description                |
224| --------------------- | ---- | ------------------ |
225| STATE_OFF             | 0    | Bluetooth is turned off.         |
226| STATE_TURNING_ON      | 1    | Bluetooth is being turned on.         |
227| STATE_ON              | 2    | Bluetooth is turned on.          |
228| STATE_TURNING_OFF     | 3    | Bluetooth is being turned off.         |
229| STATE_BLE_TURNING_ON  | 4    | The LE-only mode is being turned on for Bluetooth.|
230| STATE_BLE_ON          | 5    | Bluetooth is in LE-only mode. |
231| STATE_BLE_TURNING_OFF | 6    | The LE-only mode is being turned off for Bluetooth.|
232