收藏
回答

小程序写入失败,会断开蓝牙吗?


蓝牙写入报错后,会直接断开蓝牙连接吗?写入数据包格式是24字节没办法避免。


鄙人做的是一个共享项目,需要通过蓝牙开锁关锁。


在开启 notify 之后 监听特征值变化,代码如下:

openNotify(services_id) {
    let _this = this
    wx.notifyBLECharacteristicValueChange({
      state: true, // 启用 notify 功能
      deviceId: this.data.tooth_deviceId, //蓝牙设备id
      serviceId: services_id, //服务id
      characteristicId: this.data.indicate_id, //服务特征值indicate
      success: function(res) {
        console.log('开启notify', res.errMsg)
        //监听低功耗蓝牙设备的特征值变化
        wx.onBLECharacteristicValueChange(function(res) {
          // _this.setData({
          //   tooth_deviceId: res.deviceId,
          //   indicate_id: res.characteristicId
          // })
          // services_id = res.serviceId
          console.log('特征值变化', res);
          let value = _this.ab2hex(res.value)
          console.log(value)
          if(value.indexOf('01') > 0 && value.length === 10){
            // 告诉后台已开锁
            console.log('我的锁已经开了')
            // 监听关锁
            // let buffer = _this.hexStringToArrayBuffer('FF05A3011F');
            // _this.data.timer = setInterval(function(){
            //   wx.writeBLECharacteristicValue({
            //     deviceId: _this.data.tooth_deviceId,
            //     serviceId: services_id,
            //     characteristicId: _this.data.read_id,
            //     value: buffer,
            //     success: function(res) {
            //       console.log(res)
            //       // clearInterval(_this.data.timer)
            //     },
            //     fail:err=>{
            //       console.log(err)
            //     }
            //   })
            // },2000)
          }
        })
        //写入数据
        setTimeout(function(){
          _this.writeData(services_id, 'FF0CA20102030405065500EF')
        },1000)
      },
      fail: err => {
        console.log(err)
      }
    })
  },

在  onBLECharacteristicValueChange 中只获取到 开锁的设备回复码,没有获取到关锁的获取码。


猜想一:是不是因为在 write 的时候报错了,所以断开了蓝牙,导致无法收取到?

猜想二:onBLECharacteristicValueChange 在开锁的时候已经使用过了,需要重新开启一个?


希望大神帮助。急急急,老大的刀已经在我的脖子上了

回答关注问题邀请回答
收藏

2 个回答

  • 六
    2020-01-07

    你好,我最近也在做一个共享项目,需要实现蓝牙开锁的功能。能问一下你用的锁是哪款吗?方便的话可以留个联系方式吗?

    2020-01-07
    有用
    回复
  • 疯狂的小辣椒
    疯狂的小辣椒
    2019-05-09

    你好,麻烦提供出现问题的具体机型、微信版本号、系统版本号,以及能复现问题的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)

    2019-05-09
    有用
    回复
登录 后发表内容