微信小程序 连接 低功耗蓝牙 实例
废话不多说 直接上(刚刚完成的项目)
和 微信的 开发文档 一起参照
1. 初始化 蓝牙 设备
wx.openBluetoothAdapter({ // 调用 手机蓝牙 success: function (res) { console.log("初始化蓝牙适配器成功") wx.onBluetoothAdapterStateChange(function (res) { console.log("蓝牙适配器状态变化", res) }) wx.startBluetoothDevicesDiscovery({ success: function (res) { console.log("开始搜索附近蓝牙设备") console.log(res); } }) }, fail: function (res) { console.log("初始化蓝牙适配器失败") wx.showModal({ title: '提示', content: '请检查手机蓝牙是否打开', }) }, })}2. 进行蓝牙连接
wx.createBLEConnection({ deviceId: deviceId, // 搜索到的蓝牙 信息提供的(唯一性) success: function (res) { deviceId = deviceId; console.log(res); wx.hideLoading(); wx.showToast({ // 微信弹框 title: '连接中', icon: 'loading', duration: 5000, mask: true, success() { wx.stopBluetoothDevicesDiscovery({ // 连接成功后 停止搜索蓝牙 success: function (res) { console.log("停止蓝牙搜索") console.log(res); } }) wx.showToast({ title: '连接成功', icon: 'success', duration: 1000, success: function (res) { } }) } }) console.log("连接设备成功") }, fail: function (res) { wx.hideLoading() wx.showToast({ title: '连接设备失败', icon: 'fail', duration: 1000 }) console.log("连接设备失败") console.log(res) } })3. 获取蓝牙的列表
wx.getBLEDeviceServices({ deviceId: deviceId, success: function (res) { console.log(res); serviceId = res.services[0].uuid; // 蓝牙的服务列表 有多个 根据协议 看你需要那个 }, fail(res) { console.log(res) } })4. 获取蓝牙的特征值
wx.getBLEDeviceCharacteristics({ deviceId: deviceId, serviceId:serviceId, // 上一步 获取到的 服务id success(res) { console.log(res); writecharacteristics = res.characteristics[1].uuid; // 根据协议 判断那个 是 读 那个 是 写 readcharacteristics = res.characteristics[0].uuid; }, fail(res) { console.log(res); } })5. 开启 蓝牙的 特征值 变化
wx.notifyBLECharacteristicValueChange({ state: true, deviceId: deviceId, serviceId: serviceId, characteristicId: readcharacteristics, // 根据协议的 蓝牙 读 的 特征值id success: function (res) { console.log('notifyBLECharacteristicValueChange success', res); // 在这里可以 获取 蓝牙发来的 消息 }, fail(res) { console.log(res); } })6. 像 蓝牙 发送消息
wx.writeBLECharacteristicValue({ deviceId: deviceId, serviceId: serviceId, characteristicId: writecharacteristics, // 根据协议的 蓝牙 写 的 特征值id value: buffer, // 这个 发送的消息 是需要通过 转化的 success: function (res) { console.log(res) console.log('writeBLECharacteristicValue success', res.errMsg); }, fail: function (res) { console.log(res); } })7. 转化 像 蓝牙 发送 的 消息 (我这边是 转成16进制)
function stringToHex(str) { var strbuf = ""; for (var i = 0; i str.length; i++) { if (strbuf == "") strbuf = str.charCodeAt(i).toString(16); else strbuf += str.charCodeAt(i).toString(16); } var buffer = new ArrayBuffer(100); var hex = 'FAFF' + strbuf + '0D0A' // 前面 和 后面的 是 蓝牙 接收 消息的 格式(协议里面有) var typedArray = new Uint8Array(hex.match(/[da-f]{2}/gi).map(function (h) { return parseInt(h, 16) })) buffer = typedArray.buffer; // console.log(buffer); return buffer;}8. 接收到的蓝牙 消息 要进行转化
function ab2str(buffer) { // 16进制 转成 10进制 var hexArr = Array.prototype.map.call( new Uint8Array(buffer), function (bit) { return ('00' + bit.toString(16)).slice(-2) } ) return hexArr.join(''); // return String.fromCharCode.apply(null, new Uint8Array(buf));}总结
蓝牙连接 一定要看 蓝牙协议 这样 效率 会 提升很多.
小程序 安卓的 蓝牙连接 可能会出现 搜索不到 指定蓝牙 (ps: 1. 要检查 微信和安卓的版本; 2. 要开启下手机的定位 )
安卓可能会出现 退出 程序 但是 蓝牙还在连接着 所以 可以 强制 退出 微信 运行 并 关闭 蓝牙 在开启
想要连接指定 蓝牙
1 开启 蓝牙搜索功能
2 判断 是否 搜到 指定的 deviceid













