读取蓝牙设备发出的数据问题
发布于 5 年前 作者 yang86 12186 次浏览 来自 问答

使用 wx.writeBLECharacteristicValue(OBJECT)  发送数据的时候,发送的数据在设备上可以接收到,并且正常,

但是使用 wx.readBLECharacteristicValue(OBJECT) 接收数据的时候,确一直接收不到,

我用了 使用了 wx.onBLECharacteristicValueChange(CALLBACK) 来监听数据,数据发送来的时候事件确实可以激活,但是

wx.onBLECharacteristicValueChange(function(res) {  

console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`)

})

上面代码的res.value确一直是空对象,


请问 是不是在底层这里的二进制转换没有做,导致字符串对象为空,还是说这里有什么其他方法可以处理的。

8 回复

请问如果接收到的是浮点数的二进制值,应该怎样将二进制值转化为需要的浮点数值呢?谢谢

sendtoequ: function (e) {

        var that = this

        console.log(this.data.services)

        console.log(“发送消息到:deviceId” + that.data.connectedDeviceId);

        console.log(“serviceId:” + that.data.services[0].uuid);

        console.log(“characteristicId:” + that.data.characteristicId);

        //这里是核心,自己编码协议

        let buffer = new ArrayBuffer(1)

        let dataView = new DataView(buffer)

        dataView.setUint8(0, 6)

         //这里是核心,自己编码协议

        wx.writeBLECharacteristicValue({

            // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取

            deviceId: that.data.connectedDeviceId,

            // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取

            serviceId: that.data.services[0].uuid,

            // 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取

            characteristicId: that.data.characteristicId,

            // 这里的value是ArrayBuffer类型

            value: buffer,

            success: function (res) {

                console.log(res)

                console.log(‘writeBLECharacteristicValue success’, res.errMsg)

            }

        })

    }

这段是给蓝牙设备发了一个字节

wx.onBLECharacteristicValueChange,在调用读或者写的时候再调用这个

转换为10进制

function arrayBufferToStr(buffer){

    return Array.prototype.map.call(new Uint8Array(buffer), x => (‘00’ + x.toString(10)).slice(-2)).join(’’);

}

你好!高手们,请教个问题,我是小程序上线了,转发别人打不开,必须点击主页才可以,搜索里也可以打开

请问wx.onBLECharacteristicValueChange这个方法是写在哪儿的?  我在onLoad/getBLEDeviceCharacteristics/notifyBLECharacteristicValueChange/writeBLECharacteristicValue方法里都谢了该方法,但是都没有回调。

方便大家 我自己回答了 收到的数据是二进制格式的,根据自己的协议来读吧。

举个例子

wx.onBLECharacteristicValueChange(function (characteristic) {

            console.log(‘characteristic value comed:’)

             let buffer = characteristic.value

             let dataView = new DataView(buffer)

             console.log(dataView.getUint8(1))

        })

请问wx.writeBLECharacteristicValue(OBJECT) 实例怎么写的,我写入成功了但是设备没有反应,求代码实例

回到顶部