收藏
回答

小程序蓝牙搜索不到普通设备,是有bug?

小程序使用标准蓝牙接口搜索不到车机蓝牙设备,微信版本:7.0.16 手机:小米mix2 系统:android 9

initBT: function() {

    this.printLog("启动蓝牙适配器, 蓝牙初始化")

    var that = this;

    wx.openBluetoothAdapter({

      success: function(res) {

        console.log("initBT:",res);

        that.findBT();

      },

      fail: function(res) {

         console.log('请先打开蓝牙');

      }

    })

  },

  findBT: function() {

    this.printLog("打开蓝牙成功.")

    var that = this

    wx.startBluetoothDevicesDiscovery({

      allowDuplicatesKey: false,

      interval: 0,

      success: function(res) {

        wx.showLoading({

          title: '正在搜索设备',

        })

        console.log("startBluetoothDevicesDiscovery:",res);

        delayTimer = setInterval(function(){

          that.discoveryBT() //3.0 //这里的discovery需要多次调用

        }, 1000);

        console.log("delayTimer:"+delayTimer)

        setTimeout(function () {

          if (isFound) {

            return;

          } else {

            wx.hideLoading();

            console.log("搜索设备超时");

            wx.stopBluetoothDevicesDiscovery({

              success: function (res) {

                console.log('连接蓝牙成功之后关闭蓝牙搜索');

              }

            })

            clearInterval(delayTimer)

            wx.showModal({

              title: '搜索设备超时',

              content: '请检查蓝牙设备是否正常工作,Android手机请打开GPS定位.',

              showCancel: false

            })

            console.log("搜索设备超时,请打开GPS定位,再搜索")

            return

          }

        }, 15000);

      },

      fail: function(res) {

        that.printLog("蓝牙设备服务发现失败: " + res.errMsg);

      }

    })

  },

  discoveryBT: function() {

    var that = this

    wx.getBluetoothDevices({

      success: function(res) {

        var list = res.devices;

        console.log(list);

        if(list.length <= 0){

          return ;

        }

        var devices = [];

        for (var i = 0; i < list.length; i++) {   

          //that.data.inputValue:表示的是需要连接的蓝牙设备ID,

          //简单点来说就是我想要连接这个蓝牙设备,

          //所以我去遍历我搜索到的蓝牙设备中是否有这个ID

          var name = list[i].name || list[i].localName;

          console.log("name:",name+" RSSI:"+list[i].RSSI)

          // if(this.isEmpty(name)){

         if (name!="") {

           continue;

         }

          if(list[i].RSSI != 0){//name.indexOf('JL') >= 0 &&

            console.log(list[i]);

            devices.push(list[i]);

          }

        }

        console.log('总共有' + devices.length + "个设备需要设置")

        if (devices.length <= 0) {

          return;

        }

        that.connectBT(devices);

      },

      fail: function() {

        console.log('搜索蓝牙设备失败');

      }

    })

  },

最后一次编辑于  2020-07-14
回答关注问题邀请回答
收藏

1 个回答

  • Cjiang
    Cjiang
    2020-07-14

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

    2020-07-14
    有用
    回复 5
    • Kerwin
      Kerwin
      2020-07-14
      机型和系统版本已经给出,在帖子第一行,小程序使用标准蓝牙接口搜索不到车机蓝牙设备,微信版本:7.0.16 手机:小米mix2 系统:android 9
      2020-07-14
      回复
    • Kerwin
      Kerwin
      2020-07-14
      代码片段:https://developers.weixin.qq.com/s/4HM6hPmb7Eiu
      2020-07-14
      回复
    • Cjiang
      Cjiang
      2020-07-14回复Kerwin
      你好,小程序的蓝牙搜索只搜索低功耗蓝牙设备,表现并不是对齐系统设置页的
      2020-07-14
      回复
    • Kerwin
      Kerwin
      2020-07-15
      后期会支持标准蓝牙设备么
      2020-07-15
      回复
    • Yiu
      Yiu
      2021-07-21
      你好,遇上了类似的问题,手机设置里能搜到某个设备的蓝牙,但是应用api的搜索就搜索不到,苹果安卓都这样,了解原因嘛
      2021-07-21
      回复
登录 后发表内容
问题标签