版本要求
| 蓝牙类型 | 版本要求 | Android 或 iOS 版本要求 |
|---|---|---|
| BLE 低功耗蓝牙 | mPaaS 10.1.60 及以上版本。 |
|
| 传统蓝牙 | mPaaS 10.1.60 及以上版本。 | - |
基本流程
低功耗蓝牙流程图

传统蓝牙流程图

蓝牙 API
低功耗蓝牙
| 名称 | 功能说明 |
|---|---|
| my.connectBLEDevice | 连接低功耗蓝牙设备。 |
| my.disconnectBLEDevice | 断开与低功耗蓝牙设备的连接。 |
| my.getBLEDeviceCharacteristics | 获取蓝牙设备所有 characteristic(特征值)。 |
| my.getBLEDeviceServices | 获取所有已发现的蓝牙设备,包括已经和本机处于连接状态的设备。 |
| my.notifyBLECharacteristicValueChange | 启用低功耗蓝牙设备特征值变化时的 notify 功能。 |
| my.offBLECharacteristicValueChange | 取消监听低功耗蓝牙设备的特征值变化的事件。 |
| my.offBLEConnectionStateChanged | 取消低功耗蓝牙连接状态变化事件的监听。 |
| my.onBLECharacteristicValueChange | 监听低功耗蓝牙设备的特征值变化的事件。 |
| my.onBLEConnectionStateChanged | 监听低功耗蓝牙连接的错误事件,包括设备丢失,连接异常断开等。 |
| my.readBLECharacteristicValue | 读取低功耗蓝牙设备特征值中的数据。 |
| my.writeBLECharacteristicValue | 向低功耗蓝牙设备特征值中写入数据。 |
传统蓝牙
| 名称 | 功能说明 |
|---|---|
| my.closeBluetoothAdapter | 关闭本机蓝牙模块。 |
| my.getBluetoothAdapterState | 获取本机蓝牙模块状态。 |
| my.getBluetoothDevices | 获取所有已发现的蓝牙设备,包括已经和本机处于连接状态的设备。 |
| my.getConnectedBluetoothDevices | 获取处于已连接状态的设备。 |
| my.offBluetoothAdapterStateChange | 移除本机蓝牙状态变化的事件的监听。 |
| my.offBluetoothDeviceFound | 移除寻找到新的蓝牙设备事件的监听。 |
| my.onBluetoothDeviceFound | 搜索到新的蓝牙设备时触发此事件。 |
| my.onBluetoothAdapterStateChange | 监听本机蓝牙状态变化的事件。 |
| my.openBluetoothAdapter | 初始化小程序蓝牙适配器。 |
| my.startBluetoothDevicesDiscovery | 开始搜寻附近的蓝牙外围设备。 |
| my.stopBluetoothDevicesDiscovery | 停止搜寻附近的蓝牙外围设备。 |
调用示例
//初始化my.openBluetoothAdapter({success: (res) => {console.log(res);}});//注册发现事件my.onBluetoothDeviceFound({success: (res) => {let device = res.devices[0];//连接发现的设备my.connectBLEDevice({deviceId: deviceId,success: (res) => {console.log(res)},fail:(res) => {},complete: (res)=>{}});//停止搜索my.stopBluetoothDevicesDiscovery({success: (res) => {console.log(res)},fail:(res) => {},complete: (res)=>{}});}});//注册连接事件my.onBLEConnectionStateChanged({success: (res) => {console.log(res);if (res.connected) {//开始读写 notify 等操作my.notifyBLECharacteristicValueChange({deviceId: deviceId,serviceId: serviceId,characteristicId: characteristicId,success: (res) => {console.log(res)},fail:(res) => {},complete: (res)=>{}});}}});//注册接收 read 或 notify 的数据my.onBLECharacteristicValueChange({success: (res) => {console.log(res);}});//开始搜索my.startBluetoothDevicesDiscovery({services: ['fff0'],success: (res) => {console.log(res)},fail:(res) => {},complete: (res)=>{}});//断开连接my.disconnectBLEDevice({deviceId: deviceId,success: (res) => {console.log(res)},fail:(res) => {},complete: (res)=>{}});//注销事件my.offBluetoothDeviceFound();my.offBLEConnectionStateChanged();my.offBLECharacteristicValueChange();//退出蓝牙模块my.closeBluetoothAdapter({success: (res) => {},fail:(res) => {},complete: (res)=>{}});
该文章对您有帮助吗?