点击蓝字
![7b19e2aefeb3805b885aef9b2a9c7613.gif](https://img-blog.csdnimg.cn/img_convert/7b19e2aefeb3805b885aef9b2a9c7613.gif)
关注我们
微信小程序-蓝牙传输与接收数据(重命名)
![7b19e2aefeb3805b885aef9b2a9c7613.gif](https://img-blog.csdnimg.cn/img_convert/7b19e2aefeb3805b885aef9b2a9c7613.gif)
01
蓝牙接收数据步骤
![7b19e2aefeb3805b885aef9b2a9c7613.gif](https://img-blog.csdnimg.cn/img_convert/7b19e2aefeb3805b885aef9b2a9c7613.gif)
1.获取蓝牙设备所有service(getBLEDeviceServices)
2.获取所有特征值(getBLEDeviceCharacteristics)
3.启用特征值变化(notifyBLECharacteristicValueChange)
4.接收蓝牙返回消息(onBLECharacteristicValueChange)
![7b19e2aefeb3805b885aef9b2a9c7613.gif](https://img-blog.csdnimg.cn/img_convert/7b19e2aefeb3805b885aef9b2a9c7613.gif)
02
蓝牙接收数据案例及说明
![7b19e2aefeb3805b885aef9b2a9c7613.gif](https://img-blog.csdnimg.cn/img_convert/7b19e2aefeb3805b885aef9b2a9c7613.gif)
获取蓝牙设备所有service
![7b19e2aefeb3805b885aef9b2a9c7613.gif](https://img-blog.csdnimg.cn/img_convert/7b19e2aefeb3805b885aef9b2a9c7613.gif)
wx.getBLEDeviceServices({
deviceId: // 这里的 deviceId 需要在 getBluetoothDevices接口中获取,
success: function (res) {
console.log('services UUID:\n', JSON.stringify(res.services));
for (var i = 0; i < res.services.length; i++) {
console.log("第"+(i+1) + "个UUID:" + res.services[i].uuid+"\n")
}
that.setData({
services: res.services,
info: JSON.stringify(res.services),
})
}
})
获取所有特征值
![7b19e2aefeb3805b885aef9b2a9c7613.gif](https://img-blog.csdnimg.cn/img_convert/7b19e2aefeb3805b885aef9b2a9c7613.gif)
wx.getBLEDeviceCharacteristics({
deviceId:// 这里的 deviceId 需要在上面的getBluetoothDevices接口中获取
serviceId: // 这里的 serviceId 需要在上面的getBLEDeviceServices接口中获取,
success: function (res) {
console.log("%c getBLEDeviceCharacteristics", "color:red;");
for (var i = 0; i < res.characteristics.length; i++) {
console.log('特征值:' + res.characteristics[i].uuid)
if (res.characteristics[i].properties.notify) {
console.log("notifyServicweId:", myUUID);
console.log("notifyCharacteristicsId:", res.characteristics[i].uuid);
that.setData({
notifyServicweId: myUUID,
notifyCharacteristicsId: "0000FF01-0000-1000-8000-00805F9B34FB",//手动设置notifyCharacteristicsId为这个UUID,为了方便写死在这里
})
}
if (res.characteristics[i].properties.write) {
console.log("writeServicweId:", myUUID);
console.log("writeCharacteristicsId:", res.characteristics[i].uuid);
that.setData({
writeServicweId: myUUID,
//writeCharacteristicsId: res.characteristics[i].uuid,
writeCharacteristicsId: "0000FF02-0000-1000-8000-00805F9B34FB",//手动设置writeCharacteristicsId为这个UUID,为了方便写死在这里
})
}
}
console.log('device getBLEDeviceCharacteristics:', res.characteristics);
that.setData({
msg: JSON.stringify(res.characteristics),
})
},
fail: function (res) {
console.log(res);
},
})
启用特征值变化
![7b19e2aefeb3805b885aef9b2a9c7613.gif](https://img-blog.csdnimg.cn/img_convert/7b19e2aefeb3805b885aef9b2a9c7613.gif)
必须先启用notifyCharacteristicsId才能监听characteristicValueChange事件
wx.notifyBLECharacteristicValueChange({
state: true, // 启用 notify 功能
deviceId: // 这里的 deviceId 需要在上面的getBluetoothDevices接口中获取
serviceId: // 蓝牙特征值对应服务的 uuid
characteristicId: // 蓝牙读取特征值的uuid
success: function (res) {
//console.log('notifyBLECharacteristicValueChange success', res.errMsg)
//var msg = '启动notify:' + res.errMsg
that.setData({
info: "开启notify成功"
})
},
fail: function (res) {
console.log('启动notify:' + res.errMsg);
},
})
接收蓝牙返回消息
![7b19e2aefeb3805b885aef9b2a9c7613.gif](https://img-blog.csdnimg.cn/img_convert/7b19e2aefeb3805b885aef9b2a9c7613.gif)
必须先启用 notifyBLECharacteristicValueChange 接口才能接收到设备推送的 notification。
wx.onBLECharacteristicValueChange(function (res) {
console.log("characteristicId:" + res.characteristicId)
console.log("serviceId:" + res.serviceId)
console.log("deviceId" + res.deviceId)
console.log("Length:" + res.value.byteLength)
console.log("hexvalue:" + ab2hex(res.value))
that.setData({
info: that.data.info + ab2hex(res.value)
})
})
ArrayBuffer转16进度字符串示例
![7b19e2aefeb3805b885aef9b2a9c7613.gif](https://img-blog.csdnimg.cn/img_convert/7b19e2aefeb3805b885aef9b2a9c7613.gif)
function ab2hex(buffer) {
var hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function (bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join(',');
}
03
蓝牙传输数据(重命名)步骤
![7b19e2aefeb3805b885aef9b2a9c7613.gif](https://img-blog.csdnimg.cn/img_convert/7b19e2aefeb3805b885aef9b2a9c7613.gif)
1.获取蓝牙设备所有service(getBLEDeviceServices)
2.获取所有特征值(getBLEDeviceCharacteristics)
3.启用特征值变化(notifyBLECharacteristicValueChange)
4.发送信息到蓝牙(writeBLECharacteristicValue)
![7b19e2aefeb3805b885aef9b2a9c7613.gif](https://img-blog.csdnimg.cn/img_convert/7b19e2aefeb3805b885aef9b2a9c7613.gif)
04
ModBus通讯协议
![7b19e2aefeb3805b885aef9b2a9c7613.gif](https://img-blog.csdnimg.cn/img_convert/7b19e2aefeb3805b885aef9b2a9c7613.gif)
Modbus是一种串行通信协议,是Modicon公司(现在的施耐德电气 Schneider Electric)于1979年为使用可编程逻辑控制器(PLC)通信而发表。Modbus已经成为工业领域通信协议的业界标准(De facto),并且现在是工业电子设备之间常用的连接方式。
![7b19e2aefeb3805b885aef9b2a9c7613.gif](https://img-blog.csdnimg.cn/img_convert/7b19e2aefeb3805b885aef9b2a9c7613.gif)
Modbus通信协议优点:
1. 公开发表并且无版权要求
2. 易于部署和维护
3. 对供应商来说,修改移动本地的比特或字节没有很多限制
![7b19e2aefeb3805b885aef9b2a9c7613.gif](https://img-blog.csdnimg.cn/img_convert/7b19e2aefeb3805b885aef9b2a9c7613.gif)
![d97fdf565990ade1d058ab3d36855b57.png](https://img-blog.csdnimg.cn/img_convert/d97fdf565990ade1d058ab3d36855b57.png)
图|写寄存器字节规范
05
蓝牙发送Modbus协议案例(重命名)
![7b19e2aefeb3805b885aef9b2a9c7613.gif](https://img-blog.csdnimg.cn/img_convert/7b19e2aefeb3805b885aef9b2a9c7613.gif)
发送信息到蓝牙
![7b19e2aefeb3805b885aef9b2a9c7613.gif](https://img-blog.csdnimg.cn/img_convert/7b19e2aefeb3805b885aef9b2a9c7613.gif)
var bluRename = this.data.bluRename //输入的蓝牙名称
console.log(`bluRename${bluRename}`);
if (bluRename.length < 16) { //如果输入的蓝牙不够15位在后面补空格
var index = bluRename.length
for (var i = 0; i < 16 - index; i++) {
bluRename = bluRename + ' '
}
}
bluRename = strToHexCharCode(bluRename) //转换为16进制数字
console.log(`bluRename${bluRename}`);
var hex = `021000780008${bluRename}`//Modbus发送数据格式
var crcTir = $CRC(hex, true) //生成CRC校验码(Modbus约定)
var modBus = hex + crcTir //把传输的数据与CRC校验拼接起来一起发送给单片机
console.log(`modBus:${modBus}`);
var typedArray = new Uint8Array(modBus.match(/[\da-f]{2}/gi).map(function (h) {
return parseInt(h, 16)
})) //16进制字符串转化为ArrayBuffer
var buffer1 = typedArray.buffer
var that = this
wx.writeBLECharacteristicValue({
deviceId: // 这里的 deviceId 需要在上面的getBluetoothDevices接口中获取,
serviceId: // 蓝牙特征值对应服务的 uuid
characteristicId: // 蓝牙写入特征值的 uuid
value: buffer1,
success: () => {
console.log('写入成功');
},
fail: () => {
$Message({
content: '修改重命名错误',
duration: 5,
type: 'error'
});
}
});