Use this API to initialize the Bluetooth adapter. This must be called before using any other Bluetooth APIs.
Parameters
| Property | Type | Required | Description |
|---|
| success | Function | No | Callback on success |
| fail | Function | No | Callback on failure |
| complete | Function | No | Callback that always executes |
Code Example
Basic Usage
my.openBluetoothAdapter({
success() {
console.log('Bluetooth adapter initialized');
},
fail(err) {
console.error('Failed to initialize Bluetooth:', err);
}
});
Check Bluetooth Before Operations
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();
}
});
Error Codes
| Code | Description |
|---|
| 10001 | Bluetooth is not enabled on the device |
| 10000 | Bluetooth adapter not initialized |
Always call my.closeBluetoothAdapter when you’re done using Bluetooth to release system resources.
my.closeBluetoothAdapter
Close Bluetooth adapter
my.startBluetoothDevicesDiscovery
Start device discovery