收藏
回答

纯血鸿蒙(onBLECharacteristicValueChange)这个不执行,IOS安卓正常?

纯血鸿蒙onBLECharacteristicValueChange不执行,不能监听,

notifyBLECharacteristicValueChange能写入,,

iOS和安卓可以全部正常

 listenNotify(deviceId, serviceId, characteristicId) {

  var _this = this;

// 1. 先销毁所有旧监听(鸿蒙必须)

wx.offBLECharacteristicValueChange();

// 2. 先注册监听!!!鸿蒙必须先注册,再开通知

wx.onBLECharacteristicValueChange((res) => {

  console.log("✅鸿蒙收到数据:", res); // 这句一定要打印出来

  _this.setData({

    buletext: "设备连接中......",

  });

});



  wx.notifyBLECharacteristicValueChange({

    state: true,

    deviceId: deviceId.trim(),

    serviceId: serviceId.trim(),

    characteristicId: characteristicId.trim(), // 重点:用通知特征值ID,不是writeId!

    type: 'notification', // 强制指定notification模式(解决鸿蒙默认走indication的问题)


    success: (res2) => {

      console.log('开1', deviceId.trim());

      console.log('开2', serviceId.trim());

      console.log('开3', characteristicId.trim());

      console.log('开启蓝牙通知成功', res2);


// setTimeout(() => {

  wx.writeBLECharacteristicValue({

    deviceId: _this.data.deviceId.trim(),

    serviceId: _this.data.serviceId.trim(),

    characteristicId: _this.data.writeId.trim(),

    value: encodedData,

    success: (res) =>{

      console.log("writeBLECharacteristicValue success ---- 发送成功", res);

      // clearInterval(errorCheckInterval.value);


      console.log('WiFi列表请求发送成功',res)

      // console.log(res.data.code)

      // if(res.data.code === 3){

      //   this.setData({ is4g: true })

      // }

      // if(res.data.code === 2){

      // this.setData({ is4g: false })

      // }

      // this.setData({ buletext: '设备71077'});

    },

    fail: (res) => {

      this.setData({

                    buletext:res.errCode+'写数据失败详情:'+this.data.deviceId+'lll'+this.data.serviceId+'lll'+this.data.writeId,

           });

           

      console.error('发送失败', res)}

  })

  // }, 1000);


      wx.onBLECharacteristicValueChange((res) => {

        console.log(res)

        this.setData({

          buletext:'设备连接中......',

        });

        const value = res.value;

        const text = simpleUtf8Decode(value);

        try {

          const data = JSON.parse(text);

          // console.log(data)

          // console.log(data.code)

          if(data.code === 3 ){

            this.setData({ is4g: true })

            wx.navigateTo({

              url: '/pages/ctlink/index?is4g=true',

               })

               console.log('4g')

      

             }

             if(data.code === 2 ){

              this.setData({ is4g: false })

              wx.navigateTo({

                url: '/pages/ctlink/index',

                 })

             }

             console.log(this.data.sn)

            //  const sn = wx.getStorageSync('sn') ? wx.getStorageSync('sn').substring(2, 14) : ''

             const sn = wx.getStorageSync('snbox');

             console.log(sn)

             

      

      

           if (Array.isArray(data)) {

            // console.log('333')

            this.setData({ wifiList: data }); 

            

            if(is4g==true){

              console.log(sn)

              wx.setStorageSync('changedc', sn);

              wx.redirectTo({

                url: '/pages/ctlink/index?is4g=true&sn='+sn,

                 })

            }else{

              console.log(sn)

              wx.redirectTo({

                url: '/pages/ctlink/index?sn='+sn,

                 })

            }

           

          } else if (data.code !== undefined) {

            // this.getWifiList();

            if (data.code === 0) {

              // const sn = wx.getStorageSync('sn') ? wx.getStorageSync('sn').substring(2, 14) : ''

              const sn = wx.getStorageSync('snbox');

             

      

              wx.request({

                url: 'https://axx.com?type=bindDevice&sn=' + sn,

                method: 'post',

                data: {

                  stype:'bind',

                  sn:sn,

                  openid:wx.getStorageSync('openid')

                },

                header: {

                  'content-type': 'application/x-www-form-urlencoded'

                },

                success(res) {

                  console.log(sn)

                  var resid =res.data;

                  console.log(res.data)

                  console.log(resid,sn)

                  wx.setStorageSync('deviceid', resid);

                  wx.setStorageSync('sn', sn);

                  //  wx.hideLoading();

                   _this.setData({

                    ssidtext: '绑定成功...',

                    ssidtextstate: 1

                  });

                  wx.setStorageSync('changedc', sn);

                  wx.showToast({ title: '连网成功' });

                  wx.redirectTo({

                    url: '/pages/spget/index?deviceid='+resid +'&isgo=1&sn=' + sn,

                  })

      

      

                 

      

                    },

                    fail: (err) => {

                      this.setData({

                        buletext:'设备连接中........',

                      });

                      console.error('鸿蒙端开启通知失败:', err);

                      // 鸿蒙常见错误处理

                      const errTips = {

                        10007: '设备未找到(重连设备)',

                        10012: '特征值不支持通知',

                        10013: '连接超时(重启蓝牙)'

                      };

                      wx.showToast({ title: errTips[err.errCode] || '开启通知失败', icon: 'none' });

                    }

                    

               });

      

      

             

              

            } 

            else {

              if (data.code === -1) {

                wx.showToast({ title: '连网失败,请重试', icon: 'error' });

                wx.setStorageSync('ssidtext', '连接Wi—Fi');

              }

      

              console.log(res)

              console.log(data)

      

            }

          }

        } catch (e) {}

      });

      

    },

    fail: (err) => {

      console.error('端开启通知失败:', err);

      // 鸿蒙常见错误处理

      const errTips = {

        10007: '设备未找到(重连设备)',

        10012: '特征值不支持通知',

        10013: '连接超时(重启蓝牙)'

      };

      wx.showToast({ title: errTips[err.errCode] || '开启通知失败', icon: 'none' });

      _this.setData({ 

        buletext: '开启通知失败....'

      });


    }

  });


const params = JSON.stringify({ action: "get_wifi_list" })

const encodedData = simpleUtf8Encode(params)

// setTimeout(() => {

  

this.setData({ 

  buletext: '设备连接中....'

});

wx.onBLECharacteristicValueChange((res) => {

  console.log(res)

  console.log('12312388888')


  this.setData({

    buletext:'设备连接中......',

  });

  const value = res.value;

  const text = simpleUtf8Decode(value);

  try {

    const data = JSON.parse(text);

    // console.log(data)

    // console.log(data.code)

    if(data.code === 3 ){

      this.setData({ is4g: true })

      wx.navigateTo({

        url: '/pages/btlink/index?is4g=true',

         })

         console.log('4g')


       }

       if(data.code === 2 ){

        this.setData({ is4g: false })

        wx.navigateTo({

          url: '/pages/btlink/index',

           })

       }

       console.log(this.data.sn)

       const sn = wx.getStorageSync('sn') ? wx.getStorageSync('sn').substring(2, 14) : ''

       console.log(sn)



     if (Array.isArray(data)) {

      // console.log('333')

      this.setData({ wifiList: data }); 

      

      if(is4g==true){

        console.log(this.data.sn)

        wx.setStorageSync('changedc', this.data.sn);

        wx.redirectTo({

          url: '/pages/btlink/index?is4g=true&sn='+this.data.sn,

           })

      }else{

        console.log(this.data.sn)

        wx.redirectTo({

          url: '/pages/btlink/index?sn='+this.data.sn,

           })

      }

     

    } else if (data.code !== undefined) {

      // this.getWifiList();

      if (data.code === 0) {

        const sn = wx.getStorageSync('sn') ? wx.getStorageSync('sn').substring(2, 14) : ''

       


        wx.request({

          url: 'https://xxxxx.php?type=bindDevice&sn=' + sn,

          method: 'post',

          data: {

            stype:'bind',

            sn:sn,

            openid:wx.getStorageSync('openid')

          },

          header: {

            'content-type': 'application/x-www-form-urlencoded'

          },

          success(res) {

            console.log(sn)

            var resid =res.data;

            console.log(res.data)

            console.log(resid,sn)

            wx.setStorageSync('deviceid', resid);

            wx.setStorageSync('sn', sn);

            //  wx.hideLoading();

             _this.setData({

              ssidtext: '绑定成功...',

              ssidtextstate: 1

            });

            wx.setStorageSync('changedc', sn);

            wx.showToast({ title: '连网成功' });

            wx.redirectTo({

              url: '/pages/spget/index?deviceid='+resid +'&isgo=1&sn=' + sn,

            })



           


              },

              fail: (err) => {

                this.setData({

                  buletext:'设备连接中........',

                });

                console.error('鸿蒙端开启通知失败:', err);

                // 鸿蒙常见错误处理

                const errTips = {

                  10007: '设备未找到(重连设备)',

                  10012: '特征值不支持通知',

                  10013: '连接超时(重启蓝牙)'

                };

                wx.showToast({ title: errTips[err.errCode] || '开启通知失败', icon: 'none' });

              }

              

         });



       

        

      } 

      else {

        if (data.code === -1) {

          wx.showToast({ title: '连网失败,请重试', icon: 'error' });

          wx.setStorageSync('ssidtext', '连接Wi—Fi');

        }


        console.log(res)

        console.log(data)


      }

    }

  } catch (e) {}

});

// }, 1000);

},






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

3 个回答

  • 社区技术运营专员--Asher
    社区技术运营专员--Asher
    03-06

    你好麻烦提供请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。

    03-06
    有用
    回复
  • 秋龍
    秋龍
    03-06

    console.log('特征值全量数据:', res.characteristics); 打印出来 notify是false , indicatetrue, readfalse , writetrue

    03-06
    有用
    回复
  • 智能回答 智能回答 本次回答由AI生成
    03-06
    有用
    回复 1
    • 秋龍
      秋龍
      03-06
      没有任何提示错误,其他都正常,就是不提执行onBLECharacteristicValueChange
      03-06
      回复
登录 后发表内容