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