微信小程序之三:测试蓝牙连接新游手柄

2018-03-08  本文已影响0人  汀江秋雨

20180308 by 慕容秋

开发环境

微信开发者工具 v1.02.1802270

测试设备

先简单介绍下流程:

1、初始化小程序蓝牙模块 openBluetoothAdapter

小程序定义的这个方法名字很神奇,大家不要误解了,这里的open只是初始化小程序的蓝牙模块;手机系统蓝牙关闭或不支持蓝牙功能时,调用这个方法是会报错的。

  openBluetoothAdapter: function () {
    var that = this;
    wx.openBluetoothAdapter({
      success: function (res) {
        console.log(res)
        that.setData({
          debugMsg: res.errMsg
        })
        // 监听蓝牙适配器状态变化
        wx.onBluetoothAdapterStateChange(function (res) {
          console.log(res)
        })
        // 发现新设备
        wx.onBluetoothDeviceFound(function (devices) {
          console.log(devices)
        })
      },

      fail: function (res) {
        console.log(res)
        that.setData({
          debugMsg: res.errMsg
        })
      }
    })
  },
2、获取手机系统已连接的蓝牙设备 getConnectedBluetoothDevices

官方的参数说明里要求services必填,但是官方的示例代码里面是没有加入services,这点要注意下。我这边测试情况是:

  getConnectedBluetoothDevices: function () {
    var that = this;
    wx.getConnectedBluetoothDevices({
      services: [SERVICE_UUID],
      success: function (res) {
        console.log(res)
        // 连接第一个目标设备做测试
        if (res.devices[0]) {
          mDeviceID = res.devices[0].deviceId;
        }
      }
    })
  },
3、建立BLE连接 createBLEConnection
  createBLEConnection: function (e) {
    var that = this;
    wx.createBLEConnection({
      // 这里的 deviceId 由 getConnectedBluetoothDevices 获取
      deviceId: mDeviceID,
      success: function (res) {
        console.log(res)
        // 获取BLE服务
        that.getBLEDeviceServices()
        // 监听BLE连接状态变化
        wx.onBLEConnectionStateChange(function (res) {
          // 该方法回调中可以用于处理连接意外断开等异常情况
          console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)
        })
      },
      fail: function (res) {
        console.log(res)
        that.setData({
          debugMsg: res.errMsg
        })
      },
    })
  },
4、获取BLE服务的UUID及相关特征值Characteristics的UUID,设置设备数据变化监听
  /**
   * 获取蓝牙设备所有 service(服务)
   */
  getBLEDeviceServices: function () {
    var that = this;
    wx.getBLEDeviceServices({
      deviceId: mDeviceID,
      success: function (res) {
        console.log('device services:', res.services)

        // 获取BLE服务的特征值characteristic
        that.getBLEDeviceCharacteristics()
      },
      fail: function (res) {
        console.log(res)
        that.setData({
          debugMsg: res.errMsg
        })
      },
    })
  },

  /**
   * 获取蓝牙设备某个服务中的所有 characteristic(特征值)
   */
  getBLEDeviceCharacteristics: function () {
    var that = this;
    wx.getBLEDeviceCharacteristics({
      deviceId: mDeviceID,
      serviceId: SERVICE_UUID,
      success: function (res) {
        console.log('device getBLEDeviceCharacteristics:', res.characteristics)

        // 设置设备Notify
        that.notifyBLECharacteristicValueChange() 
      },
      fail: function (res) {
        console.log(res)
        that.setData({
          debugMsg: res.errMsg
        })
      },
    })
  },

  ab2hex: function (buffer) {
    var hexArr = Array.prototype.map.call(
      new Uint8Array(buffer),
      function (bit) {
        return ('00' + bit.toString(16)).slice(-2)
      }
    )
    return hexArr.join('');
  },

  /**
   * 启用低功耗蓝牙设备特征值变化时的 notify 功能,订阅特征值。注意:必须设备的特征值支持notify或者
   * indicate才可以成功调用,具体参照characteristic 的 properties 属性
   */
  notifyBLECharacteristicValueChange: function () {
    var that = this;
    wx.notifyBLECharacteristicValueChange({
      state: true, // 启用 notify 功能
      deviceId: mDeviceID,
      serviceId: SERVICE_UUID,
      characteristicId: NOTIFY_UUID,
      success: function (res) {
        console.log(res)
        // 监听低功耗蓝牙设备的特征值变化。必须先启用notify接口才能接收到设备推送的notification。
        wx.onBLECharacteristicValueChange(function (res) {
          console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`)
          console.log(that.ab2hex(res.value))
        })
      },
      fail: function (res) {
        console.log(res)
        that.setData({
          debugMsg: res.errMsg
        })
      },
    })
  },
5、向蓝牙设备发送测试数据 writeBLECharacteristicValue
  writeBLECharacteristicValue: function () {
    var that = this;
    // 向蓝牙设备发送一个0x00的16进制数据
    let buffer = new ArrayBuffer(1)
    let dataView = new DataView(buffer)
    dataView.setUint8(0, 0)

    wx.writeBLECharacteristicValue({
      deviceId: mDeviceID,
      serviceId: SERVICE_UUID,
      characteristicId: WRITE_UUID,
      // 这里的value是ArrayBuffer类型
      value: buffer,
      success: function (res) {
        console.log('writeBLECharacteristicValue success', res.errMsg)
      },
      fail: function (res) {
        console.log(res)
        that.setData({
          debugMsg: res.errMsg
        })
      },
    })
  },

小结

源码地址:

https://gitee.com/muroqiu/weixin_Bluetooth

上一篇下一篇

猜你喜欢

热点阅读