2、初始化蓝牙适配器(wx.openBluetoothAdapter)

官方文档连接:https://developers.weixin.qq.com/miniprogram/dev/api/wx.openBluetoothAdapter.html

wx.openBluetoothAdapter(Object object)

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

初始化蓝牙模块

参数

Object object

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

注意

  • 其他蓝牙相关 API 必须在 wx.openBluetoothAdapter 调用之后使用。否则 API 会返回错误(errCode=10000)。
  • 在用户蓝牙开关未开启或者手机不支持蓝牙功能的情况下,调用 wx.openBluetoothAdapter 会返回错误(errCode=10001),表示手机蓝牙功能不可用。此时小程序蓝牙模块已经初始化完成,可通过 wx.onBluetoothAdapterStateChange 监听手机蓝牙状态的改变,也可以调用蓝牙模块的所有API。

 

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>
  
</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: '请打开蓝牙和定位功能'
        })
      }
    })
  },
//我删除了自动生命的声明周期函数

})

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

 

调试:

1、先进行编译

2、预览到手机(确保微信运行在手机前台,有时候不灵,可以多点两下)

3、打开手机微信的调试功能(小程序会退出,重新编译预览就会打开了)

4、微信右下角出现调试功能

5、查看打印出来的信息

 

 

开发心得:

很多人被卡在第一步,连蓝牙适配器都打不开,其实是有以下原因:

1、忘记打开蓝牙

2、没有打开定位功能

3、微信没有定位权限(在手机的权限设置里打开)

定位功能主要负责后面的搜索蓝牙时需要用到,如果没有打开定位功能,后面会导致搜索不到蓝牙

 

 

 

修改这段代码,使第一个定时任务先执行,第一个定时任务执行完之后,开始执行第二个定时任务,并且第二个定时任务执行时,第一个定时任务不再开启。 findBlue(){ var _this = this; wx.startBluetoothDevicesDiscovery({ allowDuplicatesKey: false, interval: 0, success:(res) => { console.log("搜索蓝牙设备成功...") setTimeout(function(){ _this.getBlue() },500) }, fail: (res) => { console.log('搜索附近的蓝牙设备失败') console.log(res) } }) }, // 3.搜索蓝牙设备之后,需要获取搜索到的蓝牙设备信息 getBlue(){ var _this = this; wx.getBluetoothDevices({ success: (res) => { console.log(res) if(res.devices.length > 0){ for (var i = 0; i < res.devices.length; i++){ // 判断里面是否有我想要的蓝牙设备 if (res.devices[i].name === this.data.inputValue || res.devices[i].localName === this.data.inputValue){ console.log("name:"+res.devices[i].name) this.setData({ isFound: true, deviceId: res.devices[i].deviceId }) //扫描到目标设备之后关闭蓝牙搜索 wx.stopBluetoothDevicesDiscovery({ success: (res) => { console.log('扫描到目标设备之后关闭蓝牙搜索'); setTimeout(function(){ _this.connetBlue(_this.data.deviceId);//4.0 },500) } }) // this.connetBlue(res.devices[i].deviceId);//4.0 return; } } } console.log("重新找....") var time = setTimeout(function(){ if(_this.data.isFound == false && _this.data.isTui == false && _this.data.isTip == false){ console.log("执行任务1==================") _this.findBlue() } },500) setTimeout(function(){ console.log("执行任务2---------------------") clearTimeout(time) console.log("清除time定时任务") console.log("isFound:",_this.data.isFound) console.log("isTui:",_this.data.isTui) console.log("isTip:",_this.data.isTip) if(_this.data.isFound == false && _this.data.isTui == false && _this.data.isTip == false){ console.log("重新打开适配器") wx.openBluetoothAdapter({ success: (res) => { console.log('初始化蓝牙设备成功') //获取适配器状态 wx.getBluetoothAdapterState({ success(res) { setTimeout(function(){ _this.findBlue();//2.0 },800) } }) } }) } }, 3000) }, fail: () =>{ console.log("搜索蓝牙设备失败") } }) },
05-25
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值