微信小程序与蓝牙模组通信
根据蓝牙模组的协议进行编码,主要通过 new ArrayBuffer 和 new DataView进制数据操作
let subscript = 0
let data = [1,2,3,4,5,6]
let t_buffer = new ArrayBuffer(data.length)
let t_dataview = new DataView(t_buffer)
// 参数1:序号 ; 参数2:不分包填0x00(根据模组协议来)
t_dataview.setUint8(subscript++, 0x00)
data.forEach((item, i) => {t_dataview.setUint8(i + subscript, `0x${item}`)
})
console.log('最终发送的数据:', t_buffer)
// 发送数据
wx.writeBLECharacteristicValue({deviceId,serviceId,characteristicId,value: t_buffer,success: (res) => {console.log('writeBLECharacteristicValue success', res)},fail: (error) => {console.log('write error, error message is: ', error)}
})
完结!