html52.com
我爱小程序-开发者交流社区

读取蓝牙设备发出的数据问题

问题模块
API和组件

使用 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确一直是空对象,


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


网友回复

三佳网络丨监控丨电脑@18136986998:

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

飞:

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

无知少年:

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

张嘉禹:

转换为10进制

function arrayBufferToStr(buffer){

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

}


三零先生:

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

维明:

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

举个例子

wx.onBLECharacteristicValueChange(function (characteristic) {

            console.log('characteristic value comed:')

             let buffer = characteristic.value

             let dataView = new DataView(buffer)

             console.log(dataView.getUint8(1))

        })


维明:

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.writeBLECharacteristicValue(OBJECT) 实例怎么写的,我写入成功了但是设备没有反应,求代码实例

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

真正的个人免签约支付接口

云免签H5支付