小程序 蓝牙连接(出现的问题和一些解决方法)
侧边栏壁纸
  • 累计撰写 35 篇文章
  • 累计收到 1 条评论

小程序 蓝牙连接(出现的问题和一些解决方法)

逸曦穆泽
2022-03-11 / 0 评论 / 304 阅读 / 正在检测是否收录...

序:最近做小程序,要用到蓝牙模块的,不知道如何下手,没关系,看文档,看案例!
看文档: 微信文档

看案例: 谷歌/百度/知乎等等,只要你觉得合适,都可以去尝试!

一、小程序 BLE 开发 API 简介

操作蓝牙适配器的共有 4 个:
wx.openBluetoothAdapter 初始化蓝牙适配器 1
wx.closeBluetoothAdapter 关闭蓝牙模块 2
wx.getBluetoothAdapterState 获取本机蓝牙适配器状态 3
wx.onBluetoothAdapterStateChange 监听蓝牙适配器状态变化事件 4
扫描和获取周围 BLE 设备的有4个(操作方式与普通蓝牙一样):
wx.startBluetoothDevicesDiscovery 开始搜寻附近的蓝牙外围设备 5
wx.stopBluetoothDevicesDiscovery 停止搜寻附近的蓝牙外围设备 6
wx.getBluetoothDevices 获取所有已发现的蓝牙设备 7
wx.onBluetoothDeviceFound 监听寻找到新设备的事件 8
连接BLE设备的2个:
wx.createBLEConnection 连接低功耗蓝牙设备 9
wx.closeBLEConnection 断开与低功耗蓝牙设备的连接 10
连接成功后,读写BLE对应特征对象的数据:
wx.getConnectedBluetoothDevices 根据 uuid 获取处于已连接状态的设备 11
wx.getBLEDeviceServices 获取蓝牙设备所有 service(服务) 12
wx.getBLEDeviceCharacteristics  获取蓝牙设备所有 characteristic(特征值)13
wx.readBLECharacteristicValue  读取低功耗蓝牙设备的特征值的二进制数据值 14
wx.writeBLECharacteristicValue 向低功耗蓝牙设备特征值中写入二进制数据 15
wx.notifyBLECharacteristicValueChange  启用低功耗蓝牙设备特征值变化时的 notify 功能 16
wx.onBLECharacteristicValueChange 监听低功耗蓝牙设备的特征值变化 17
wx.onBLEConnectionStateChange 监听低功耗蓝牙连接的错误事件 18

二、小程序蓝牙连接流程

我的连接流程是这样的(代码我就不多贴了,只要你有思路、有想法,你就是黑马):
初始化蓝牙设备之前,我做了一些的骚操作(不懂的查一下):

1、打开定位
/** 获取定位信息 */
getLocation: function () {
  let that = this;
  // 获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限
  wx.getSetting({
    success: function (res) {
      if(res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true){
        wx.showModal({
          title: '是否授权当前位置',
          content: '需要获取你的地理位置,请确认授权,否则无法获蓝牙',
          success: function(mres){
            if(mres.confirm){
              wx.openSetting({
                success (authData) {
                  if(authData.authSetting['scope.userLocation'] == true){
                    wx.showToast({
                      title: '授权成功',
                      icon: "success",
                      duration: 1000
                    })
                  }else{
                    wx.showToast({
                      title: '授权失败',
                      icon: "error",
                      duration: 1000
                    })
                  }
                }
              })
            }else if(mres.cancel){
                wx.showToast({
                  title: '授权失败',
                  icon: "error",
                  duration: 1000
                })
            }
          }
        })
      }else{
      // 当前位置 wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标
        wx.getLocation({
          type: 'wgs84', 
          success: function (res) {
            console.log('打开地理位置')
          },
        })
      }
    },
  })
},
2、这个放到一个初始化方法里面,,校验一下微信的蓝牙权限是否打开:
init(){
    let that = this;
    // 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.bluetooth" 这个 scope
    wx.getSetting({
      success(res) {
        // (1) 微信授权已关闭,弹窗打开
        if (res.authSetting['scope.bluetooth'] != undefined && res.authSetting['scope.bluetooth'] == false) {
          wx.openSetting({
            success (authData) {
              if(authData.authSetting['scope.bluetooth'] == true){
                wx.showToast({
                  title: '授权成功',
                  icon: "success",
                  duration: 1000
                })
                that.openBluetoothAdapter(); // 打开蓝牙适配器 
              }else{
                wx.showToast({
                  title: '授权失败',
                  icon: "success",
                  duration: 1000
                })
              }
            }
          })
        }
        // (2) 'scope.bluetooth'属性不存在,需要授权
        if(res.authSetting['scope.bluetooth'] == undefined){
          wx.authorize({
            scope: 'scope.bluetooth',
            success(res){
              that.openBluetoothAdapter(); // 打开蓝牙适配器 
            }
          })
        }
        // (3) 已经授权
        if(res.authSetting['scope.bluetooth'] == true){
          that.openBluetoothAdapter(); // 打开蓝牙适配器 
        }
      }
    })
}

初始化蓝牙设备 1=》 (1_success)成功:开始搜索附近的蓝牙外围设备 5(1_fail)失败:获取蓝牙适配器状态 4

=》1_success:发现蓝牙设备 8 (对结果进行过滤,符合条件的设备进行连接) =》 连接到目标设备 9 ,连接成功,监听蓝牙低功耗连接状态的改变事件 18关闭蓝牙搜索 6 =》 获取蓝牙低功耗设备所有服务 12 =》 获取某个服务下所有的特征值 13 (打印一下特征值有:notify,read,write特征的),监听蓝牙低功耗设备的特征值变化 17 =》 启用低功耗蓝牙设备特征值变化时的 notify 功能 16

=》1_fail​​​​​​​:available 为true时,开始搜索附近的蓝牙外围设备 5,否则提示检查蓝牙是否打开

你会发现,有好几个是一些流程中没用到的,但是,这是不可能发生的,存在必有其道理;

​​​​​​​(1)比如:你刷新时,你不可能再重新走一遍初始化蓝牙吧,那就要用到 获取蓝牙适配器状态 3 ,判断是 获取所有已发现的蓝牙设备 8 还是 开始搜索附近的蓝牙外围设备 5

​​​​​​​(2)比如:要你断开主动断开蓝牙连接,断开与低功耗蓝牙设备的连接 10

(3)比如:短时间内,频繁退出或多次扫描 重新进入已连接蓝牙的小程序,在 onLoad 要用的 根据 uuid 获取处于已连接状态的设备 11(连接后,缓存 services 连接的 uuid 值),不然你已连接了设备,还一直搜不到;

(4)比如:关闭蓝牙模块 2获取所有已发现的蓝牙设备 7​​​​​​​读取低功耗蓝牙设备的特征值的二进制数据值 14​​​​​​​向低功耗蓝牙设备特征值中写入二进制数据 15 ,这个几个不用我多说了吧。

三、总结:

iOS 的坑是比较少一些的,Android 机的确实有很多不完善的地方,毕竟是百花齐放的嘛

0

评论 (0)

取消