JSFiddle - React, Tailwind, and code Playground

by Spero Koulouras

HTML

<div id="original"></div>
<div id="result"></div>

JavaScript

const ADVTEXT = {
  0x01: "flags",
  0x02: "incomplete16bitServices",
  0x03: "complete16bitSerices",
  0x04: "incomplete32bitServices",
  0x05: "complete32bitServices",
  0x06: "incomplete128bitServices",
  0x07: "complete128bitServices",
  0x08: "shortenedLocalName",
  0x09: "completeLocalName",
  0x0A: "txPowerLevel",
  0x0D: "deviceClass",
  0x0E: "simplePairingHashC",
  0x0F: "simplePairingRanddomizer",
  0x10: "deviceID",
  0x10: "securityManagerTK",
  0x11: "securityManagerOOB",
  0x12: "slaveConnectionIntervalRange",
  0x14: "16bitServiceSolicitations",
  0x15: "128bitServiceSolicitations",
  0x16: "16bitServiceDataUUID",
  0x17: "publicTargerAddress",
  0x18: "randomTargetAddress",
  0x19: "appearance",
  0x1A: "advertisingInterval",
  0x1B: "bleDeviceAddress",
  0x1C: "leRole",
  0x1D: "simplePairingHashC256",
  0x1E: "simplePairingRandomizerR256",
  0x1F: "32bitServiceSoliciatations",
  0x20: "32bitServiceDataUUID",
  0x21: "128bitServiceDataUUID",
  0x22: "leSecureConnectionsConfirmation",
  0x23: "leSecureConnectionsRandom",
  0x24: "uri",
  0x25: "indoorPositioning",
  0x26: "transportDiscovery",
  0x27: "leSupportedFeatures",
  0x28: "channelMapUpdate",
  0x29: "meshPBADV",
  0x2A: "meshMessage",
  0x2B: "meshBeacon",
  0x3D: "3DInfo",
  0xFF: "manufacturerSpecific"
}

function advToken(i) {
	return ADVTEXT[i]
}

function decodeAdv(buf) {
	console.log("buf: " + buf)
  var i = 0
  var length = 0
  var adv = {}
  while ((i < buf.length) && (!(buf.charCodeAt(i) === 0))) {
    length = buf.charCodeAt(i)
    var b = new Uint8Array(buf, i + 2, i + 2 + length)
    adv[advToken(buf.charCodeAt(i+1))] = b
    console.log("Value: " + b.forEach( function (e, i, a) { return " " + e.getUint8(0)}))
    i += length + 1
  }
  return adv
}

function convert(device) {
	console.log("Device: " + JSON.stringify(device))
  if (device.advertisement.type === "ArrayBuffer") {
    var d = atob(device.advertisement.data)
    device.advertisement = decodeAdv(d)
  }
}

var a =...