鍍金池/ 問答/HTML/ 小程序向藍牙發(fā)送命令,返回成功,藍牙怎么做出相應(yīng)的改變?

小程序向藍牙發(fā)送命令,返回成功,藍牙怎么做出相應(yīng)的改變?

問題描述

小程序向藍牙發(fā)送命令,返回成功,藍牙怎么做出相應(yīng)的改變

sticValue: function() { // 執(zhí)行

var _this = this;
let buf = _this.hexStringToArrayBuffer("[a|00001|0000000110](bFA 00 06 00 24 01)");
wx.writeBLECharacteristicValue({
  deviceId: _this.data.device_id,
  serviceId: _this.data.service_id,
  characteristicId: _this.data.characteristicsId,
  value: buf,
  success: function(res) {
    console.log( res, '成功');
    console.log(buf);
   _this.down();
  },
  fail(res) {
    console.log('錯誤',res)
    _this.sticValues()
    wx.showToast({
      title: '真在嘗試重連',
      icon: 'loading',
      duration: 2000
    })
  }
})

},

轉(zhuǎn)碼方式1:
var hex = 'a|00001|0000000110'

          var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {
           return parseInt(h, 16)
          }))
          console.log(typedArray)
          console.log([0xAA, 0x55, 0x04, 0xB1, 0x00, 0x00, 0xB5])
          var buffer1 = typedArray.buffer
          console.log(buffer1)

方法:2
hexStringToArrayBuffer: function(str) {

if (!str) {
  return new ArrayBuffer(0);
}
var buffer = new ArrayBuffer(str.length);
let dataView = new DataView(buffer)
let ind = 0;
for (var i = 0, len = str.length; i < len; i += 2) {
  let code = parseInt(str.substr(i, 2), 16)
  dataView.setUint8(ind, code)
  ind++
}
return buffer;

}

結(jié)果:
clipboard.png

轉(zhuǎn)碼結(jié)果對應(yīng) 方法1 和 2

clipboard.png

回答
編輯回答
巴扎嘿

以解決 先轉(zhuǎn)ArrayBuffer在轉(zhuǎn)16進制 .... 巨坑

2017年5月7日 15:26