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