9、获取所有特征值(getBLEDeviceCharacteristics)

wx.getBLEDeviceCharacteristics(Object object)

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

获取蓝牙设备某个服务中所有特征值(characteristic)。

参数

Object object

属性类型默认值必填说明
deviceIdstring蓝牙设备 id
serviceIdstring蓝牙服务 uuid,需要使用 getBLEDeviceServices 获取
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.success 回调函数

参数

Object res

属性类型说明
characteristicsArray.<Object>设备特征值列表

res.characteristics 的结构

属性类型说明
uuidstring蓝牙设备特征值的 uuid
propertiesObject该特征值支持的操作类型

properties 的结构

属性类型说明
readboolean该特征值是否支持 read 操作
writeboolean该特征值是否支持 write 操作
notifyboolean该特征值是否支持 notify 操作
indicateboolean该特征值是否支持 indicate 操作

注意事项:

所有的UUID的值请务必大写,这是后来调试的时候发现的问题,IOS下小写的UUID值无法识别,前面教程的UUID值都是小写的,我懒得改过来了,但是13、断开蓝牙设备连接(closeBLEConnection)这个教程里面的都是经过调试的,安卓和IOS都没问题的

lanyatest.wxml代码:

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

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

  </view>

  <button type="primary" class="button" bindtap="lanyatest1">1初始化蓝牙</button>
  <button type="primary" class="button" bindtap="lanyatest2">2获取蓝牙状态</button>
  <button type="primary" class="button" bindtap="lanyatest3">3搜索周边设备</button>
  <button type="primary" class="button" bindtap="lanyatest4">4获取所有设备</button>
  <block wx:for="{{devices}}" wx:key="{{test}}">
    <button type="primary" class="button" id="{{item.deviceId}}" style='background-color:red' bindtap="lanyaconnect">5连接{{item.name}}         </button>
  </block>
  <button type="primary" class="button" bindtap="lanyatest6">6停止搜索周边蓝牙设备</button>
  <button type="primary" class="button" bindtap="lanyatest7">7获取所有service</button>
  <button type="primary" class="button" bindtap="lanyatest8">8获取所有特征值</button>
</view>

lanyatest.js代码:

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

  /**
   * 页面的初始数据
   */
  data: {
    info:"未初始化蓝牙适配器",
    connectedDeviceId:"",
    deviceId:"",
    services:"",
    servicesUUID:"0000FF00-0000-1000-8000-00805F9B34FB",
    serviceId:""
  },

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

  },



  lanyatest7(event){
    var that = this;
    wx.getBLEDeviceServices({
      // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
      deviceId: that.data.connectedDeviceId,
      success: function (res) {
        console.log('services UUID:\n', JSON.stringify(res.services));
        for (var i = 0; i < res.services.length; i++) {
          console.log("第"+(i+1) + "个UUID:" + res.services[i].uuid+"\n")
          

        }
        that.setData({
          services: res.services,
          info: JSON.stringify(res.services),
        })
      }
    })

  },



  lanyatest8(event){
    var that = this;
    var myUUID = that.data.servicesUUID;//具有读、写、通知、属性的服务uuid
    console.log('UUID' + myUUID)
    wx.getBLEDeviceCharacteristics({
      // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
      deviceId: that.data.connectedDeviceId,
      // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
      serviceId: myUUID,
      success: function (res) {
        console.log("%c getBLEDeviceCharacteristics", "color:red;");
        for (var i = 0; i < res.characteristics.length; i++) {
          console.log('特征值:' + res.characteristics[i].uuid)

          if (res.characteristics[i].properties.notify) {
            console.log("notifyServicweId:", myUUID);
            console.log("notifyCharacteristicsId:", res.characteristics[i].uuid);
            that.setData({
              notifyServicweId: myUUID,
              notifyCharacteristicsId: "0000FF01-0000-1000-8000-00805F9B34FB",//手动设置notifyCharacteristicsId为这个UUID,为了方便写死在这里

            })
            console.log("打印notifyCharacteristicsId")
            console.log(that.data.notifyCharacteristicsId)
          }
          if (res.characteristics[i].properties.write) {
            console.log("writeServicweId:", myUUID);
            console.log("writeCharacteristicsId:", res.characteristics[i].uuid);
            that.setData({
              writeServicweId: myUUID,
              //writeCharacteristicsId: res.characteristics[i].uuid,
              writeCharacteristicsId: "0000FF02-0000-1000-8000-00805F9B34FB",//手动设置writeCharacteristicsId为这个UUID,为了方便写死在这里
            })

          }

        }
        console.log('device getBLEDeviceCharacteristics:', res.characteristics);

        that.setData({
          msg: JSON.stringify(res.characteristics),
        })
      },
      fail: function () {
        console.log("fail");
      },

    })




  },



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

})

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;/* 自动换行 */
}

真机调试结果:

 

开发心得:

这一步是最坑的,很多人都卡在这一步上面,在开发微信之前,已经测试过安卓app连接蓝牙,可以正常通讯,安卓app使用的是数据透传的UUID是0xFF00。

研究了一下微信有个airsync协议,UUID是为 0xFEE7,以为小程序也需要使用这个0xFEE7的UUID,结果在这里卡了好久,怎样都无法通讯,后来发现微信小程序也是使用0xFF00的UUID进行通讯,而airsync则是用来做别的事的:详情看下方链接https://www.cnblogs.com/yueqian-scut/p/5359254.html

此处获取的特征值是0xFF00这个service的characteristics,总共有4个我们使用的是最上面的两个,就是写入和通知,基本数据透传通讯靠这两个就够了。这是我使用的蓝牙模块,别的蓝牙模块可能不一样,请仔细阅读数据手册

特别注意:UUID的值请大写,这是另一个坑,在安卓上,uuid的值小写也可以,但是在苹果微信客户端,小写的uuid会提示搜不到

评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值