my.onBluetoothDeviceFound to receive discovered devices.
Parameters
Code Example
Basic Usage
Filter by Service UUID
Complete Discovery Flow
Related APIs
my.stopBluetoothDevicesDiscovery
Stop discovery
my.onBluetoothDeviceFound
Listen for devices
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Start discovering nearby Bluetooth devices.
my.onBluetoothDeviceFound to receive discovered devices.
| Property | Type | Required | Description |
|---|---|---|---|
| services | Array | No | Filter by service UUIDs |
| allowDuplicatesKey | Boolean | No | Allow duplicate device reports (default: false) |
| interval | Number | No | Reporting frequency. Default: 0, meaning a device is reported as soon as it’s found; otherwise devices are reported at this interval |
| success | Function | No | Callback on success |
| fail | Function | No | Callback on failure |
| complete | Function | No | Callback that always executes |
my.startBluetoothDevicesDiscovery({
success() {
console.log('Started scanning for devices');
}
});
my.startBluetoothDevicesDiscovery({
services: ['FEE7'], // Only discover devices with this service
success() {
console.log('Scanning for specific devices...');
}
});
Page({
data: {
devices: []
},
startScan() {
my.onBluetoothDeviceFound((res) => {
const newDevices = res.devices.filter(
d => !this.data.devices.find(existing => existing.deviceId === d.deviceId)
);
if (newDevices.length > 0) {
this.setData({
devices: [...this.data.devices, ...newDevices]
});
}
});
my.startBluetoothDevicesDiscovery({
allowDuplicatesKey: false,
success: () => {
this.setData({ scanning: true });
// Stop after 10 seconds
setTimeout(() => this.stopScan(), 10000);
}
});
},
stopScan() {
my.stopBluetoothDevicesDiscovery();
this.setData({ scanning: false });
}
});
my.stopBluetoothDevicesDiscovery when you’ve found the device you need.