Parameters
Success Callback
Code Example
Basic Usage
Check Before Operations
Status Display
Related APIs
my.openBluetoothAdapter
Initialize Bluetooth
my.onBluetoothAdapterStateChange
Listen for state changes
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Get the current state of the Bluetooth adapter.
| Property | Type | Required | Description |
|---|---|---|---|
| success | Function | No | Callback on success |
| fail | Function | No | Callback on failure |
| complete | Function | No | Callback that always executes |
| Property | Type | Description |
|---|---|---|
| available | Boolean | Whether Bluetooth is available |
| discovering | Boolean | Whether currently discovering devices |
my.getBluetoothAdapterState({
success(res) {
console.log('Bluetooth available:', res.available);
console.log('Discovering:', res.discovering);
}
});
Page({
async checkBluetoothState() {
my.getBluetoothAdapterState({
success: (res) => {
if (!res.available) {
my.alert({
title: 'Bluetooth Unavailable',
content: 'Please enable Bluetooth to continue.'
});
return;
}
if (res.discovering) {
console.log('Already scanning...');
} else {
this.startDiscovery();
}
}
});
},
startDiscovery() {
my.startBluetoothDevicesDiscovery({
success: () => console.log('Started scanning')
});
}
});
Page({
data: {
bluetoothStatus: 'unknown'
},
updateStatus() {
my.getBluetoothAdapterState({
success: (res) => {
let status = 'Off';
if (res.available) {
status = res.discovering ? 'Scanning' : 'Ready';
}
this.setData({ bluetoothStatus: status });
}
});
}
});