收藏
回答

线上发布版本无法初始化蓝牙功能?

打开小程序就提示蓝牙初始化失败,但是在开发版本和体验版本上没有这个问题


wx.getSetting({

      success:(res)=>{

        if(res.authSetting['scope.bluetooth'!= undefined && res.authSetting['scope.bluetooth'== false){

          wx.openSetting({

            success:(authData)=>{

              if(authData.authSetting['scope.bluetooth'== true){

                this.openBluetoothAdapter() ;

              }else{

                wx.showToast({

                  title: '未获得蓝牙使用权限!',

                  icon: "none"

                })

              }

            }

          })

        }


        if(res.authSetting['scope.bluetooth'== undefined){

          wx.authorize({

            scope: 'scope.bluetooth',

            success:(res)=>{

              this.openBluetoothAdapter() ;

            }

          })

        }


        if(res.authSetting['scope.bluetooth'== true){

          this.openBluetoothAdapter() ;

        }

      }


    })


openBluetoothAdapter(){


    if(this.globalData.blehaveInit == false){

      wx.showLoading({title: "正在初始化蓝牙..."});

    }


    wx.openBluetoothAdapter({

      success: (res) => {

        console.log('openBluetoothAdapter success', res)

        this.globalData.blehaveInit = true;

        wx.hideLoading();

      },

      fail: (res) => {

        if (res.errCode === 10001{

          wx.onBluetoothAdapterStateChange(function (res) {

            console.log('onBluetoothAdapterStateChange', res)

            if (res.available{

              this.globalData.blehaveInit = true;

              wx.hideLoading();

            }else{

              this.globalData.blehaveInit = false;

              wx.hideLoading();


              wx.showToast({

                title: '蓝牙初始化失败!',

                icon: "none"

              })

            }

          })

        }else{

          this.globalData.blehaveInit = false;

          wx.hideLoading();

          wx.showToast({

            title: '蓝牙初始化失败!',

            icon: "none"

          })

        }

      }

    })

  },

以上是蓝牙的授权和初始化代码,在编译器里面也有警告无效的 app.json permission["scope.bluetooth"],请问如何解决?

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

2 个回答

  • 一笑皆春
    一笑皆春
    10-09

    完善一下用户隐私协议,加上“访问你的蓝牙”选项,发版勾选“采集用户隐私”

    10-09
    有用
    回复
  • 智能回答 智能回答 本次回答由AI生成
    10-09
    有用
登录 后发表内容