Parameters
Success Callback Parameters
Device Object Properties
Code Example
Basic Usage
Build Device List
Find Specific Device
Related APIs
my.offBluetoothDeviceFound
Stop listening
my.startBluetoothDevicesDiscovery
Start discovery
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Listen for newly discovered Bluetooth devices.
| Property | Type | Required | Description |
|---|---|---|---|
| success | Function | No | Callback when devices are found |
| fail | Function | No | Callback on failure |
| Property | Type | Description |
|---|---|---|
| devices | Array | List of all newly discovered devices |
| Property | Type | Description |
|---|---|---|
| deviceId | String | Device ID |
| name | String | Name of the Bluetooth device (some devices may not have a name) |
| deviceName | String | Name of the Bluetooth device |
| localName | String | Name of the local device |
| RSSI | Number | Received Signal Strength Indicator |
| advertisData | Hex String | Advertisement data of the device |
my.onBluetoothDeviceFound({
success: (res) => {
res.devices.forEach(device => {
console.log('Found:', device.name, device.deviceId);
});
}
});
Page({
data: {
devices: []
},
onLoad() {
my.openBluetoothAdapter({
success: () => {
this.setupDeviceListener();
this.startScan();
}
});
},
setupDeviceListener() {
my.onBluetoothDeviceFound({
success: (res) => {
const { devices } = this.data;
res.devices.forEach(newDevice => {
const exists = devices.find(d => d.deviceId === newDevice.deviceId);
if (!exists && newDevice.name) {
devices.push(newDevice);
}
});
this.setData({ devices });
}
});
},
startScan() {
my.startBluetoothDevicesDiscovery();
},
onUnload() {
my.offBluetoothDeviceFound();
my.closeBluetoothAdapter();
}
});
Page({
findDevice(targetName) {
my.onBluetoothDeviceFound({
success: (res) => {
const target = res.devices.find(d => d.name === targetName);
if (target) {
my.stopBluetoothDevicesDiscovery();
my.offBluetoothDeviceFound();
this.connectToDevice(target);
}
}
});
my.startBluetoothDevicesDiscovery();
},
connectToDevice(device) {
console.log('Connecting to:', device.name);
}
});
my.offBluetoothDeviceFound when done to stop receiving device updates.