7、停止搜索蓝牙设备(stopBluetoothDevicesDiscovery)

wx.stopBluetoothDevicesDiscovery(Object object)

基础库 1.1.0 开始支持,低版本需做兼容处理。

停止搜寻附近的蓝牙外围设备。若已经找到需要的蓝牙设备并不需要继续搜索时,建议调用该接口停止蓝牙搜索。

参数

Object object

属性 类型 默认值 必填 说明
success function   接口调用成功的回调函数
fail function   接口调用失败的回调函数
complete function   接口调用结束的回调函数(调用成功、失败都会执行)

lanyatest.wxml代码:




  
    
      {{info}}
    

  

  
  
  
  
  
    
  
  

 

lanyatest.js代码:

// pages/lanyatest/lanyatest.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info:"未初始化蓝牙适配器",
    connectedDeviceId:""
  },

  lanyatest1(event){
    var that = this;
    wx.openBluetoothAdapter({
      success: function (res) {
        console.log('初始化蓝牙适配器成功')
        //页面日志显示
        that.setData({
          info: '初始化蓝牙适配器成功'
        })
      },
      fail: function (res) {
        console.log('请打开蓝牙和定位功能')
        that.setData({
          info: '请打开蓝牙和定位功能'
        })
      }
    })
  },



  lanyatest2(event){
    var that = this;
    wx.getBluetoothAdapterState({

      success: function (res) {

        //打印相关信息
        console.log(JSON.stringify(res.errMsg) + "\n蓝牙是否可用:" + res.available);

        that.setData({
          info: JSON.stringify(res.errMsg) +"\n蓝牙是否可用:" + res.available
        })

      },
      fail: function (res) {

        //打印相关信息
        console.log(JSON.stringify(res.errMsg) + "\n蓝牙是否可用:" + res.available);

        that.setData({
          info: JSON.stringify(res.errMsg) + "\n蓝牙是否可用:" + res.available
        })

      }
      
    })

  },



  lanyatest3(event){
    var that = this;
    wx.startBluetoothDevicesDiscovery({
      services: ['FEE7'], //如果填写了此UUID,那么只会搜索出含有这个UUID的设备,建议一开始先不填写或者注释掉这一句
      success: function (res) {
        that.setData({
          info: "搜索设备" + JSON.stringify(res),
        })
        console.log('搜索设备返回' + JSON.stringify(res))

      }
    })

  },




  lanyatest4(event){
    var that = this;
    wx.getBluetoothDevices({
      success: function (res) {

        that.setData({
          info: "设备列表\n" + JSON.stringify(res.devices),
          devices: res.devices
        })
        console.log('搜设备数目:' + res.devices.length)
        console.log('设备信息:\n' + JSON.stringify(res.devices)+"\n")
      }
    })

  },



  lanyaconnect(event){
    var that = this;
    wx.createBLEConnection({
      deviceId: event.currentTarget.id,
      success: function (res) {
        console.log('调试信息:' + res.errMsg);
        that.setData({
          connectedDeviceId: event.currentTarget.id,
          info: "MAC地址:" + event.currentTarget.id  + '  调试信息:' + res.errMsg,
          
        })
      },
      fail: function () {
        console.log("连接失败");
      },

    })

  },


  lanyatest6(event){
    var that = this;
    wx.stopBluetoothDevicesDiscovery({
      success: function (res) {
        console.log("停止搜索" + JSON.stringify(res.errMsg));
        that.setData({
          info: "停止搜索"  + JSON.stringify(res.errMsg),
        })
      }
    })

  },



//我删除了自动生成的生命周期函数

})

lanyatest.wxss代码:

/* pages/lanyatest/lanyatest.wxss */
.vertical{
  display: flex;
  flex-direction: column;
}

/**index.wxss**/
.horizontal{
  display: flex;
  flex-direction: row;
}

.btinfo{
  height:100px;
}

.contentview {
margin: 0 10px;
}
 
.button {
margin: 5px;
}

.myview{
  height:200px;
  word-break:break-all;/* 自动换行 */
}

真机调试结果:

7、停止搜索蓝牙设备(stopBluetoothDevicesDiscovery)_第1张图片

7、停止搜索蓝牙设备(stopBluetoothDevicesDiscovery)_第2张图片

 

 

 

 

 

你可能感兴趣的:(微信小程序,js)