Version requirement
Bluetooth | mPaaS version | Android/iOS |
Bluetooth Low Energy (BLE) | mPaaS 10.1.60 or later versions |
|
Traditional Bluetooth | mPaaS 10.1.60 or later versions | - |
Process flow
BLE

Traditional Bluetooth

Bluetooth API
Bluetooth low energy
API | Description |
Connect to low energy bluetooth devices. | |
Disconnect to low energy bluetooth devices. | |
Get the characteristics of low energy Bluetooth devices. | |
Get all the low energy Bluetooth devices that are discovered, including the connected devices. | |
Enable the function to notify changes to the characteristic value. | |
Disable the function to notify changes to the characteristic value. | |
Disable the listener for connection status change events. | |
Enable the listener for characteristic value change events. | |
Enable the listener for connection status change events, such as device lost and device disconnected. | |
Read the characteristic value. | |
Write data to the characteristic value. |
Traditional Bluetooth
API | Description |
Close the Bluetooth module in the mini program. | |
Get the Bluetooth adapter status in the mini program. | |
Get all the low energy Bluetooth devices that are discovered, including the connected devices. | |
Get the Bluetooth devices that are connected. | |
Remove the listener for connection status change events. | |
Remove the listener for new Bluetooth device discovery events. | |
Trigger the event when a new Bluetooth device is found. | |
Monitor the Bluetooth status change events. | |
Initialize the Bluetooth module in the mini program. | |
Start discovering Bluetooth devices nearby. | |
Stop discovering Bluetooth devices nearby. |
Sample code
//Initialize
my.openBluetoothAdapter({
success: (res) => {
console.log(res);
}
});
//Register discovery event
my.onBluetoothDeviceFound({
success: (res) => {
let device = res.devices[0];
//Connect the discovered device
my.connectBLEDevice({
deviceId: deviceId,
success: (res) => {
console.log(res)
},
fail:(res) => {
},
complete: (res)=>{
}
});
//Stop discovering
my.stopBluetoothDevicesDiscovery({
success: (res) => {
console.log(res)
},
fail:(res) => {
},
complete: (res)=>{
}
});
}
});
//Register connection event
my.onBLEConnectionStateChanged({
success: (res) => {
console.log(res);
if (res.connected) {
//Start reading and writing notify and other operations
my.notifyBLECharacteristicValueChange({
deviceId: deviceId,
serviceId: serviceId,
characteristicId: characteristicId,
success: (res) => {
console.log(res)
},
fail:(res) => {
},
complete: (res)=>{
}
});
}
}
});
//Register the read or notify data reception events
my.onBLECharacteristicValueChange({
success: (res) => {
console.log(res);
}
});
//Start discovering
my.startBluetoothDevicesDiscovery({
services: ['fff0'],
success: (res) => {
console.log(res)
},
fail:(res) => {
},
complete: (res)=>{
}
});
//Disconnect
my.disconnectBLEDevice({
deviceId: deviceId,
success: (res) => {
console.log(res)
},
fail:(res) => {
},
complete: (res)=>{
}
});
// Unregister events
my.offBluetoothDeviceFound();
my.offBLEConnectionStateChanged();
my.offBLECharacteristicValueChange();
//Exit bluetooth module
my.closeBluetoothAdapter({
success: (res) => {
},
fail:(res) => {
},
complete: (res)=>{
}
});