m***@163.com
m***@163.com
  • 发布:2024-02-20 17:46
  • 更新:2024-02-20 17:46
  • 阅读:68

微信小程序蓝牙监听不到特征值变化

分类:HBuilderX
     getCharacteId (serviceId){   
           let that = this  
        uni.getBLEDeviceCharacteristics({  
            deviceId: that.deviceId,  
            serviceId: serviceId,   
            success(res) {  

                res.characteristics.forEach(item => {  
                    //读  
                    if(item.properties.read){  
                        uni.offBLECharacteristicValueChange();  
                        // that.startread(item.uuid,serviceId)  
                    }  
                    //监听  
                    if (item.properties.notify || item.properties.indicate) {  
                        console.log('可以监听的uuid',item.uuid)  
                        // that.startNotify(item.uuid,serviceId)  
                        uni.notifyBLECharacteristicValueChange({  
                            deviceId: that.deviceId,  
                            serviceId: serviceId,  
                            characteristicId: item.uuid,  
                            state: true,  
                        })  
                        that.onBLECharacteristicValueChange()  

                    }  
                    //写入  
                    if (item.properties.write) {  
                        // that.startWrite(item.uuid,serviceId)  
                    }  
                });  
            },  
            fail(err) {  
                console.log("获取蓝牙特征值失败", err)  
            }  
        })  
       },  
     //   //监听  
        // startNotify(uuid, serviceId) {  
        //   let that = this;  
        //   console.log('可以监听的uuid', uuid);  
        //   uni.notifyBLECharacteristicValueChange({  
        //  state: true,  
        //  deviceId: that.deviceId,  
        //  serviceId: serviceId,  
        //  characteristicId: uuid,  
        //  success(res) {  
        //    console.log('监听', res);  
        //    that.onBLECharacteristicValueChange();  
        //  }  
        //   });  
        // },  
        onBLECharacteristicValueChange() {  
          console.log("开始监听");  
          let that = this; // 将外部的 this 存储在变量 that 中  
          uni.onBLECharacteristicValueChange(function(res) {  
            console.log("特征变化", res);  
            console.log(that.ab2hex(res.value)); // 使用 that 替代 this  
            // that.resdata = that.ab2hex(res.value); // 使用 that 替代 this  
          });  
        },  

为什么我这段代码 当特征值变化的时候我监听不到变化?

2024-02-20 17:46 负责人:无 分享
已邀请:

要回复问题请先登录注册