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