微信小程序通过蓝牙广播中获取mac

微信小程序在使用搜索附近蓝牙获得已发现蓝牙列表中,有些蓝牙会出现广播对象叫advertisData

具体是用微信小程序的wx.onBluetoothDeviceFound(function callback) 来实现

获取广播中的mac的方法为:

// ArrayBuffer转16进度字符串示例
function ab2hex(buffer) {
  var hexArr = Array.prototype.map.call(
    new Uint8Array(buffer),
    function(bit) {
      return ('00' + bit.toString(16)).slice(-2)
    }
  )
  return hexArr.join('');
}
wx.onBluetoothDeviceFound(function(res) {
  var devices = res.devices;
  console.log('new device list has founded')
  console.dir(devices)
  console.log(ab2hex(devices[0].advertisData))

  var buff = devices.advertisData.slice(2, 8);
  var arrayBuff = Array.prototype.map.call(new Uint8Array(buff), x => ('00' + 
  x.toString(16)).slice(-2)).join(':');
  var arrayMac = arrayBuff.toUpperCase();
  console.dir(arrayMac)
})

官方介绍:https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth/wx.onBluetoothDeviceFound.html

你可能感兴趣的:(微信小程序通过蓝牙广播中获取mac)