小程序获取蓝牙设备,按照这个协议要求,怎么获取蓝牙设备的温度数据?

小程序获取蓝牙设备,按照这个协议要求,怎么获取蓝牙设备的温度数据?

img

我知道有以下步骤,但是就是不知道怎么去获取数据

img

写这个文档的哥们跟我说,要先发送连接校验指令,然后再发送体温检测开始指令,然后才能获取体温监测数据,他的描述,我写不好

将你的代码做了修改,你修改看看

  getBLEDeviceServices: function (deviceId) {
    var that = this; // 保存this关键字
    // 获取设备的服务列表
    wx.getBLEDeviceServices({
      deviceId: app.data.deviceId,
      success: function (res) {
        console.log('获取服务!!!');
        const services = res.services
        console.log('services', services);
        // 遍历服务列表,找到目标服务
        services.forEach(function (service) {
          if (service.uuid === '0000FEE0-0000-1000-8000-00805F9B34FB') {
            // 获取服务的特征值列表
            wx.getBLEDeviceCharacteristics({
              deviceId: app.data.deviceId,
              serviceId: service.uuid,
              success: function (res) {
                const characteristics = res.characteristics
                // 遍历特征值列表,找到目标特征值
                characteristics.forEach(function (characteristic) {
                  if (characteristic.uuid === '0000FEE1-0000-1000-8000-00805F9B34FB') {
                    // 启用设备特征值通知
                    wx.notifyBLECharacteristicValueChange({
                      deviceId: app.data.deviceId,
                      serviceId: service.uuid,
                      characteristicId: characteristic.uuid,
                      state: true,
                      success: function (res) {
                        console.log('开启设备数据通知成功');
                        // that.openCall();
                        that.getDatas();
                      },
                      fail: function (res) {
                        console.log('开启设备数据通知失败');
                      }
                    });

                  } else if (characteristic.uuid === '0000F445-0000-1000-8000-00805F9B34FB') {
                    // 进行连接校验
                    // that.verifyConnection(app.data.deviceId, service.uuid, characteristic.uuid); // 使用that调用verifyConnection方法
                    that._deviceId = app.data.deviceId;
                    that._serviceId = service.uuid;
                    that._characteristicId = characteristic.uuid;
                    // console.log('that._characteristicId', that._characteristicId);
                    // 发送体温监测开始指令
                    // that.startTemperatureMonitoring(app.data.deviceId, service.uuid, characteristic.uuid);
                  }
                })
              }
            })
          }
        })
      }
    });
  },
  /**
   * 发送指令列表
   */
  getDatas() {
    this.verifyConnection(app.data.deviceId, this._serviceId, this._characteristicId); // 使用that调用verifyConnection方法
  },
  // 进行连接校验
  verifyConnection: function (deviceId, serviceId, characteristicId) { // 添加参数serviceId和characteristicId
    console.log('serviceId', serviceId);
    console.log('characteristicId', characteristicId);
    // 连接校验指令
    var that = this; // 保存this关键字
    // 连接校验指令
    // var password = [0x01, 0x5A, 0x4B, 0x47, 0x36, 0x30];
    let buffer = new ArrayBuffer(6);
    let dataView = new DataView(buffer);
    dataView.setUint8(0, 1);
    dataView.setUint8(1, 90);
    dataView.setUint8(2, 75);
    dataView.setUint8(3, 71);
    dataView.setUint8(4, 54);
    dataView.setUint8(5, 48);
    // console.log('buffer', buffer);
    // 向蓝牙设备发送连接校验指令
    wx.writeBLECharacteristicValue({
      deviceId: deviceId,
      serviceId: serviceId,
      characteristicId: characteristicId,
      value: buffer,
      success: function (res) {
        console.log('res', res);
        console.log('连接校验指令发送成功');
        // 发送体温监测开始指令
        that.startTemperatureMonitoring(app.data.deviceId, serviceId, characteristicId);

      },
      fail: function (res) {
        console.log('连接校验指令发送失败')
      }
    });
  },
  // 发送体温监测开始指令
  startTemperatureMonitoring: function (deviceId, serviceId, characteristicId) {
    // 获取当前时间的32位Unix时间戳
    // var currentTimestamp = Math.floor(Date.now() / 1000);
    var that = this;
    console.log('deviceId', deviceId);
    console.log('serviceId', serviceId);
    console.log('characteristicId', characteristicId);
    // 构造体温监测开始指令
    var buffer = new ArrayBuffer(1);
    var dataView = new DataView(buffer);
    dataView.setUint8(0, 0x02);
    // dataView.setUint32(1, currentTimestamp);

    // 向蓝牙设备发送体温监测开始指令
    wx.writeBLECharacteristicValue({
      deviceId: deviceId,
      serviceId: serviceId,
      characteristicId: characteristicId,
      value: buffer,
      success: function (res) {
        console.log('体温监测开始指令发送成功');
        // 监听蓝牙设备的返回数据
        wx.onBLECharacteristicValueChange(function (res) {
          console.log('res', res);
          var deviceData = res.value;
          var arrayBuffer = deviceData.slice(15, 18); //获取到的ArrayBuffer对象
          var int8Array = new Int8Array(arrayBuffer); // 创建Int8Array视图
          that.temperature = int8Array[0] * 10 + int8Array[1] + int8Array[2] / 10;
          console.log('temperature', that.temperature);
        });
      },
      fail: function (res) {
        console.log('体温监测开始指令发送失败');
      }
    });
  },

  // 将ArrayBuffer转换为字符串
  arrayBufferToString: function (buffer) {
    var result = "";
    var uintArray = new Uint8Array(buffer);
    for (var i = 0; i < buffer.byteLength; i++) {
      result += String.fromCharCode(uintArray[i]);
    }
    return result;
  },

他都说了发数据你发到RX,收数据你通过TX
拿到RX的特征值后,你需要做的是

  1. 监听TX特征值
  2. 往RX特征值发登录报文

小程序的官网上有示例,有接口说明
https://developers.weixin.qq.com/miniprogram/dev/framework/device/ble.html

例如

//创建一个登录报文 Uint8Array
let u8 = Uint8Array.from([0x01,0x5A,0x4B,0x47,0x36,0x30]);
//通过wx的蓝牙API发送出去
wx.writeBLECharacteristicValue({
          deviceId,
          serviceId,
          characteristicId:uuid,
          value: u8.buffer,
})

根据蓝牙指令码,首先保证通讯成功,然后发送指令码,获取设备返回的数据,然后进行解析就行了

写这个文档的哥们?谁