给设备发数据writeBLECharacteristicValue没反应
发布于 6 年前 作者 juan60 7692 次浏览 来自 问答

代码如下:

bindViewTap: function () {

    var that = this;

    

    wx.onBLECharacteristicValueChange(function (res) {

        console.log(‘特征值变化’, res);

        const CharacteristicVal = that.buf2hex(res.value);

        if (res.characteristicId == that.data.wifi_uuid) {

            that.setData({

                device_char: CharacteristicVal

            });

        }

    });

    

    // 向蓝牙设备发送一个0x00的16进制数据

    let buffer = new ArrayBuffer(1)

    let dataView = new DataView(buffer)

    dataView.setUint8(0, 0)

    wx.writeBLECharacteristicValue({

        deviceId: that.data.deviceId,

        serviceId: that.data.wifi_data.service.uuid,

        characteristicId: that.data.wifi_uuid,

        value: buffer,

        success: function (res) {

            // success

            console.log(“success  指令发送成功”);

            console.log(res);

        },

        fail: function (res) {

            // fail

            console.log(res);

        },

        complete: function (res) {

            // complete

            wx.showModal({

                title: ‘提示’,

                content: res,

                success: function (res) {

                    if (res.confirm) {

                        console.log(‘用户点击确定’)

                    } else if (res.cancel) {

                        console.log(‘用户点击取消’)

                    }

                }

            })

        }

    })

}

点击给设备发送一个16进制的数据,writeBLECharacteristicValue和onBLECharacteristicValueChange都没反应,咋回事??

3 回复

是否需要调用或者什么时机调用是看你需求的。

可以和你团队的客户端同学同步一下,小程序蓝牙接口基本是开放到系统蓝牙接口级别,调用的方式和客户端的没太多区别。

  1. writeBLECharacteristicValue 不是触发  onBLECharacteristicValueChange 的 必要条件, 只有对某个支持 notify 的特征值进行了 setNotify 操作,并且设备对该特征值的订阅者执行了 notify 操作,小程序才会收到 onBLECharacteristicValueChange 回调;

  2. 麻烦确定下是否已经走到 writeBLECharacteristicValue 这一步,执行write 的特征值支持 write 属性或者 writeWithoutResponse 属性, 是否走到了 fail 回调中。

给设备发送指令,onBLECharacteristicValueChange触发了有返回值,

readBLECharacteristicValue这个接口啥时候调用??

回到顶部