收藏
回答

蓝牙找不到设备也找不到哪里出了问题,代码看了千万遍,求教

蓝牙 早上调试 出现过一次设备  后面就一直出不来  搞了3天了   泪奔

快崩溃了,已经连上手机的设备也获取不到,到底是哪里出了问题了,搞API的兄弟能够帮忙看看

//index.js 
//获取应用实例 
var app = getApp();
Page({
  data: {
    status: "等待连接",
    sousuo:"暂未搜索",//--是否在搜索中
    devices:[]//--设备列表--
  },
  onUnload:function(){
    wx.stopBluetoothDevicesDiscovery({
      success: function (res) {
        console.log('结束搜索设备:',res)
      }
    })
    wx.closeBluetoothAdapter({
      success: function (res) {
        console.log('断开蓝牙连接:',res)
      }
    })
  },
  onLoad: function () {
    var that=this;
    if (wx.openBluetoothAdapter) {//--判断基库的版本是否支持操作蓝牙
      wx.openBluetoothAdapter({
        success: function (res) {
          console.log('1.初始化蓝牙适配器:',res);
          that.setData({ status: "1.初始化成功!"});
          //1-------------------------------------------------
          //--开始搜索蓝牙设备-------------------
          //wx.stopBluetoothDevicesDiscovery({success:function(){}});
          wx.startBluetoothDevicesDiscovery({
            services: [],
            allowDuplicatesKey: false,
            success: function (res) {
              console.log('3.已经启动搜索设备逻辑:', res);
              //--启动新设备发现-------
              console.log('0.去发现新设备!');
              wx.onBluetoothDeviceFound(function (res) {
                console.log('0.1 发现新设备:',res);
                if (res.devices.length){
                  var temp = that.data.devices;
                  for (var i = 0, n = res.devices.length;i<n;i++){//--如果设备不再就加一个
                    var id = res.devices[i].deviceId;
                    var isdo=!0;
                    for (var j = 0, m = temp.length;j<m;j++){
                      if (res.devices[i].deviceId == temp[i].deviceId){
                        isdo=!1;
                        break;
                      }
                    }
                    if (isdo) temp.push(res.devices[i]);
                  }
                  that.setData({devices: temp})
                  console.log('0.2.现存设备的情况:',temp);
                }
              });
            },
            fail: function (res) {
              console.log('-3.搜索失败:', res);
              that.setData({ status: "-3.搜索失败!" });
            },
          });
           
          //获取已发现的蓝牙设备-----
          wx.getBluetoothDevices({
            success: function (res) {
              console.log('3.1.成功获取设备:', res);
              console.log('3.1.0.设备:', res.devices);
              that.setData({
                status: "3.1.1.成功获取设备列表",
                devices: res.devices
              });
            },
            fail: function (res) {
              console.log('-3.1.设备获取失败:', res);
              that.setData({ status: "-3.1.设备获取失败!" });
            },
          });
 
          //--------------------------------------------------1
          //监听蓝牙适配器状态
          wx.onBluetoothAdapterStateChange(function (res) {
            console.log('2.1.状态发生变化:', res);
            that.setData({
              sousuo: res.discovering ? "搜索中.." : "搜索完成",
              status: res.available ? "可用" : "不可用",
            });
          });
        },
        fail:function(res){
          console.log('-1.蓝牙初始化失败:',res)
          that.setData({ status: "-1.初始化失败!" })
          wx.showModal({
            title: '提示',
            content: '请检查设备是否支持蓝牙功能。'
          });
        }
      });
    } else {
      // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示 
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
      });
    }
 
  },
   
})


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

2 个回答

  • 说不出再见
    说不出再见
    2017-09-02

    手机和设备都重启,可能连接没断开

    2017-09-02
    有用
    回复
  • 王翊夫if
    王翊夫if
    2017-09-01

    什么平台下的问题?

    如果是已经处于连接状态的蓝牙设备,手机是没办法搜索到的,可以调用 wx.getConnectedBluetoothDevices 看是否能获取到你想要的设备。

    2017-09-01
    有用
    回复
登录 后发表内容