Parameters
Code Example
Basic Usage
Stop When Device Found
Scan with Timeout
Related APIs
my.startBluetoothDevicesDiscovery
Start discovery
my.getBluetoothDevices
Get discovered devices
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Stop discovering Bluetooth devices.
| Property | Type | Required | Description |
|---|---|---|---|
| success | Function | No | Callback on success |
| fail | Function | No | Callback on failure |
| complete | Function | No | Callback that always executes |
my.stopBluetoothDevicesDiscovery({
success() {
console.log('Stopped scanning');
}
});
Page({
targetDeviceName: 'MyDevice',
startScan() {
my.onBluetoothDeviceFound((res) => {
const targetDevice = res.devices.find(
d => d.name === this.targetDeviceName
);
if (targetDevice) {
// Found target, stop scanning
my.stopBluetoothDevicesDiscovery();
this.connectToDevice(targetDevice);
}
});
my.startBluetoothDevicesDiscovery();
},
connectToDevice(device) {
console.log('Connecting to:', device.name);
// Connect logic here
}
});
Page({
scanTimeout: null,
startTimedScan() {
my.startBluetoothDevicesDiscovery({
success: () => {
// Auto-stop after 15 seconds
this.scanTimeout = setTimeout(() => {
my.stopBluetoothDevicesDiscovery();
my.showToast({ content: 'Scan complete' });
}, 15000);
}
});
},
stopScanEarly() {
if (this.scanTimeout) {
clearTimeout(this.scanTimeout);
}
my.stopBluetoothDevicesDiscovery();
}
});