1# Bluetooth Setting Development
2
3## Introduction
4This topic walks you through on how to implement basic Bluetooth settings, including enabling and disabling Bluetooth and obtaining Bluetooth status.
5
6## When to Use
7You can use the APIs provided by the **access** module to enable and disable Bluetooth.
8
9
10## Available APIs
11
12For details about the APIs and sample code, see [@ohos.bluetooth.access](../../reference/apis-connectivity-kit/js-apis-bluetooth-access.md).
13
14The following table describes the related APIs.
15
16| API                            | Description                                                                      |
17| ---------------------------------- | ------------------------------------------------------------------------------ |
18| enableBluetooth()                  | Enables Bluetooth.                                                                      |
19| disableBluetooth()                 | Disables Bluetooth.                                                                      |
20| getState()                         | Obtains the Bluetooth state.                                                               |
21| on(type: 'stateChange')            | Subscribes to Bluetooth state changes.                                                        |
22| off(type: 'stateChange')           | Unsubscribes from Bluetooth state changes.                                                    |
23
24
25## How to Develop
26
27### Enabling and Disabling Bluetooth
281. Import the **access** module.
292. Check that the SystemCapability.Communication.Bluetooth.Core capability is available.
303. Enable Bluetooth.
314. Disable Bluetooth.
32Example:
33
34    ```ts
35    import { access } from '@kit.ConnectivityKit';
36    import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
37
38    // Enable Bluetooth.
39    access.enableBluetooth();
40    access.on('stateChange', (data) => {
41      let btStateMessage = '';
42      switch (data) {
43        case 0:
44          btStateMessage += 'STATE_OFF';
45          break;
46        case 1:
47          btStateMessage += 'STATE_TURNING_ON';
48          break;
49        case 2:
50          btStateMessage += 'STATE_ON';
51          break;
52        case 3:
53          btStateMessage += 'STATE_TURNING_OFF';
54          break;
55        case 4:
56          btStateMessage += 'STATE_BLE_TURNING_ON';
57          break;
58        case 5:
59          btStateMessage += 'STATE_BLE_ON';
60          break;
61        case 6:
62          btStateMessage += 'STATE_BLE_TURNING_OFF';
63          break;
64        default:
65          btStateMessage += 'unknown status';
66          break;
67      }
68      if (btStateMessage == 'STATE_ON') {
69        access.off('stateChange');
70      }
71      console.info('bluetooth statues: ' + btStateMessage);
72    })
73
74    // Disable Bluetooth.
75    access.disableBluetooth();
76    access.on('stateChange', (data) => {
77      let btStateMessage = '';
78      switch (data) {
79        case 0:
80          btStateMessage += 'STATE_OFF';
81          break;
82        case 1:
83          btStateMessage += 'STATE_TURNING_ON';
84          break;
85        case 2:
86          btStateMessage += 'STATE_ON';
87          break;
88        case 3:
89          btStateMessage += 'STATE_TURNING_OFF';
90          break;
91        case 4:
92          btStateMessage += 'STATE_BLE_TURNING_ON';
93          break;
94        case 5:
95          btStateMessage += 'STATE_BLE_ON';
96          break;
97        case 6:
98          btStateMessage += 'STATE_BLE_TURNING_OFF';
99          break;
100        default:
101          btStateMessage += 'unknown status';
102          break;
103      }
104      if (btStateMessage == 'STATE_OFF') {
105        access.off('stateChange');
106      }
107      console.info("bluetooth statues: " + btStateMessage);
108    })
109    ```
110
111For details about the error codes, see [Bluetooth Error Codes](../../reference/apis-connectivity-kit/errorcode-bluetoothManager.md).
112**Verification**
1131. Execute the code for enabling Bluetooth.<br>If "bluetooth statues: STATE_ON" is logged, Bluetooth is enabled.
1142. Execute the code for disabling Bluetooth.<br>If "bluetooth statues: STATE_OFF" is logged, Bluetooth is disabled.