3、获取蓝牙适配器状态(getBluetoothAdapterState)

wx.getBluetoothAdapterState(Object object)

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

获取本机蓝牙适配器状态。

参数

Object object

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

object.success 回调函数

参数

Object res

属性 类型 说明
discovering boolean 是否正在搜索设备
available boolean 蓝牙适配器是否可用

 

lanyatest.wxml代码:

<!--pages/lanyatest/lanyatest.wxml-->
<view class="contentview">

  <view  class='myview'>
    <text>
      {{info}}
    </text>

  </view>

  <button type="primary" class="button" bindtap="lanyatest1">1初始化蓝牙</button>
  <button type="primary" class="button" bindtap="lanyatest2">2获取蓝牙状态</button>
 
</view>

 

lanyatest.js代码:

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

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

  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
        })

      }
      
    })

  },


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

})

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;
}

真机调试结果:

3、获取蓝牙适配器状态(getBluetoothAdapterState)

3、获取蓝牙适配器状态(getBluetoothAdapterState)