This topic walks you through on how to implement basic Bluetooth settings, including enabling and disabling Bluetooth, obtaining Bluetooth status, and pairing.
The main scenarios include enabling and disabling Bluetooth, obtaining the Bluetooth switch status, and listening for Bluetooth switch status change events.
For details about the complete API description and sample code, see @ohos.bluetooth.access (Bluetooth Access Module).
The following table describes the APIs used in this topic.
| Name | Description |
|---|---|
| enableBluetooth | Enables Bluetooth. |
| disableBluetooth | Disables Bluetooth. |
| getState | Obtains the Bluetooth state. |
| on('stateChange') | Subscribes to Bluetooth state changes. |
| off('stateChange') | Unsubscribes from Bluetooth state changes. |
| pairDevice | Initiates Bluetooth pairing. |
| getRemoteDeviceName | Obtains the name of the peer Bluetooth device. |
| setRemoteDeviceName | Sets the name of a remote Bluetooth device. |
| getPairedDevices | Obtains the paired devices. |
| getPairState | Obtains the Bluetooth pairing state. |
| startBluetoothDiscovery | Starts Bluetooth scanning to discover remote devices. |
| stopBluetoothDiscovery | Stops the Bluetooth scan. |
| on('bluetoothDeviceFind') | Subscribes to the Bluetooth device discovery events. |
| off('bluetoothDeviceFind') | Unsubscribes from the Bluetooth device discovery events. |
Import the access module. Add the ohos.permission.ACCESS_BLUETOOTH permission to the module.json5 file and call requestPermissionsFromUser to obtain user authorization for the ohos.permission.ACCESS_BLUETOOTH permission.
import { access } from '@kit.ConnectivityKit';
import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
import {abilityAccessCtrl, Context, PermissionRequestResult } from '@kit.AbilityKit';
requestPermissionsFromUser() {
let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl. createAtManager();
try {
let context = this.getUIContext().getHostContext();
atManager.requestPermissionsFromUser(context, ['ohos.permission.ACCESS_BLUETOOTH']).then(() => {
})
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}
} Enable Bluetooth and listen for the Bluetooth switch status.
import { access } from '@kit.ConnectivityKit';
let onReceiveEvent = (data: access.BluetoothState) => {
console.info(`onstateChange: ${data}`);
}
try {
access.on('stateChange', onReceiveEvent);
access.enableBluetooth(); // Enable Bluetooth.
} catch (err) {
console.error(`errCode: ${err.code}, errMessage: ${err.message}`);
} Obtain the Bluetooth switch status, disable Bluetooth, and unsubscribe from Bluetooth device switch status events.
import { access } from '@kit.ConnectivityKit';
try {
let state = access.getState();
} catch (err) {
console.error(`errCode: ${err.code}, errMessage: ${err.message}`);
}
try {
access.disableBluetooth(); // Disable Bluetooth.
} catch (err) {
console.error(`errCode: ${err.code}, errMessage: ${err.message}`);
}
let onReceiveEvent = (data: access.BluetoothState) => {
console.info(`onstateChange: ${data}`);
}
try {
access.off('stateChange', onReceiveEvent);
} catch (err) {
console.error(`errCode: ${err.code}, errMessage: ${err.message}`);
} Start Bluetooth scanning to discover remote devices, obtain the name of a remote device, and pair the device.
import { connection } from '@kit.ConnectivityKit';
try {
let onReceiveEvent = (data: Array<string>) => {
let mac: string = data[0];
console.info(`mac: ${mac}`)
let deviceName = connection.getRemoteDeviceName(mac);
console.info(`mac: ${deviceName}`)
if (deviceName == "XXX") {
console.info('Succeeded in doing getRemoteDeviceName');
connection.pairDevice(mac);
}
}
try {
connection.on('bluetoothDeviceFind', onReceiveEvent);
connection.startBluetoothDiscovery();
} catch (err) {
console.error(`errCode: ${err.code}, errMessage: ${err.message}`);
}
} catch (err) {
console.error(`errCode: ${err.code}, errMessage: ${err.message}`);
}