Parameters
Success Callback
Code Example
Basic Usage
Check Bluetooth Before Operations
Error Codes
Related APIs
my.closeBluetoothAdapter
Close Bluetooth adapter
my.startBluetoothDevicesDiscovery
Start device discovery
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Initialize the Bluetooth adapter.
| Property | Type | Required | Description |
|---|---|---|---|
| autoClose | Boolean | No | Whether to automatically disconnect Bluetooth when leaving the current page. Default: true. Android only. |
| success | Function | No | Callback on success |
| fail | Function | No | Callback on failure |
| complete | Function | No | Callback that always executes |
| Property | Type | Description |
|---|---|---|
| isSupportBLE | Boolean | Whether BLE is supported |
my.openBluetoothAdapter({
success(res) {
if (!res.isSupportBLE) {
console.log('Bluetooth is unavailable temporarily');
return;
}
console.log('Bluetooth adapter initialized');
},
fail(err) {
console.error('Failed to initialize Bluetooth:', err);
}
});
Page({
onLoad() {
this.initBluetooth();
},
initBluetooth() {
my.openBluetoothAdapter({
success: () => {
console.log('Bluetooth ready');
this.setData({ bluetoothReady: true });
},
fail: (err) => {
if (err.error === 10001) {
my.alert({
title: 'Bluetooth Required',
content: 'Please enable Bluetooth to use this feature.'
});
}
}
});
},
onUnload() {
my.closeBluetoothAdapter();
}
});
| Code | Description | Solution |
|---|---|---|
| 12 | Bluetooth is not turned on. | Try again to turn on Bluetooth. |
| 13 | Connection to the system service is temporarily lost. | Try again to reconnect. |
| 14 | Not authorized to use Bluetooth. | Authorize app to use Bluetooth. |
| 15 | Unknown error. | - |
my.closeBluetoothAdapter when you’re done using Bluetooth to release system resources.