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