Parameters
Callback Parameters
Code Example
Related APIs
my.offBLEConnectionStateChanged
Stop listening
my.connectBLEDevice
Connect to a BLE device
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Listen for BLE connection state changes.
| Property | Type | Required | Description |
|---|---|---|---|
| callback | Function | Yes | Callback when the connection state changes |
| Property | Type | Description |
|---|---|---|
| deviceId | String | The Bluetooth device ID |
| connected | Boolean | The current connection state |
Page({
onLoad() {
my.onBLEConnectionStateChanged(this.handleConnectionStateChange);
},
handleConnectionStateChange(res) {
if (!res.connected) {
console.log('Device disconnected:', res.deviceId);
}
},
onUnload() {
my.offBLEConnectionStateChanged(this.handleConnectionStateChange);
}
});
my.offBLEConnectionStateChanged to remove any existing listener before calling my.onBLEConnectionStateChanged, to avoid registering duplicate listeners that each fire on the same event. IDE simulation is not supported for this API — test on a real device.