html52.com
我爱小程序-开发者交流社区

为什么小程序的蓝牙开启后一直报10008,discovering 为false

ectedBluetoothDevices({     success: function (res) {       console.log(res)       //      }   }) },
ectedBluetoothDevices({     success: function (res) {       console.log(res)       //      }   }) },

/*开启蓝牙设备*/
 openBluetoothAdapter: function () {
 
   var that = this;
   wx.showLoading({
     title: '开启蓝牙适配'
   });
   wx.openBluetoothAdapter({
     success: function (res) {
       console.log("初始化蓝牙适配器");
       console.log(res);
       that.getBluetoothAdapterState();
     },
     fail: function (err) {
       console.log(err);
       wx.showToast({
         title: '蓝牙初始化失败',
         icon: 'success',
         duration: 2000
       })
       setTimeout(function () {
         wx.hideToast()
       }, 2000)
     }
   });
   wx.onBluetoothAdapterStateChange(function (res) {
     var available = res.available;
     if (available) {
       that.getBluetoothAdapterState();
     }
   })
 },
 /*获取本机蓝牙适配器状态*/
 getBluetoothAdapterState: function () {
   var that = this;
   wx.getBluetoothAdapterState({
     success: function (res) {
       var available = res.available,
         discovering = res.discovering;
         console.log(res)
       if (!available) {
         wx.showToast({
           title: '设备无法开启蓝牙连接',
           icon: 'success',
           duration: 2000
         })
         setTimeout(function () {
           wx.hideToast()
         }, 2000)
       }
       else {
         if (!discovering) {
           that.startBluetoothDevicesDiscovery();
           that.getConnectedBluetoothDevices();
         }
       }
     }
   })
 },
 startBluetoothDevicesDiscovery: function () {
   var that = this;
   wx.showLoading({
     title: '蓝牙搜索'
   });
   wx.startBluetoothDevicesDiscovery({
     services: [],
     allowDuplicatesKey: false,
     success: function (res) {
       if (!res.isDiscovering) {
         that.getBluetoothAdapterState();
       }
       else {
         that.onBluetoothDeviceFound();
       }
     },
     fail: function (err) {
       console.log(err);
     }
   });
 
 
 
 
 },
 /**获取在小程序蓝牙模块生效期间所有已发现的蓝牙设备,包括已经和本机处于连接状态的设备。*/
 getBluetoothDevices: function () {
   var that = this;
   wx.getBluetoothDevices({
     success: function (res) {
       console.log(res)
     }
   })
 
 },
 /**监听寻找到新设备的事件*/
 onBluetoothDeviceFound: function () {
   var that = this;
   wx.onBluetoothDeviceFound(function (res) {
     console.log('new device list has founded')
     console.log(res);
     if (res.devices[0]) {
       var name = res.devices[0]['name'];
     
 
       var deviceId = res.devices[0]['deviceId'];
      that.deviceId = deviceId;
        console.log(that.deviceId);
        that.startConnectDevices();
             
     }
   })
 
 
 },
 /**根据 uuid 获取处于已连接状态的设备*/
 getConnectedBluetoothDevices: function () {
   var that = this;
   that.createBLEConnection()
   wx.getConnectedBluetoothDevices({
     success: function (res) {
       console.log(res)
       //
 
     }
   })
 },


网友回复

寇凯成:

解决了吗哥们

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

真正的个人免签约支付接口

云免签H5支付