Skip to content

Instantly share code, notes, and snippets.

@aw3s0me
Created July 20, 2016 10:34
Show Gist options
  • Select an option

  • Save aw3s0me/d78a15ed4b15acc0dcd018dcc8802fd9 to your computer and use it in GitHub Desktop.

Select an option

Save aw3s0me/d78a15ed4b15acc0dcd018dcc8802fd9 to your computer and use it in GitHub Desktop.
Parsing advertisement js
function parseAdvertisingData(buffer) {
var length, type, data, i = 0, advertisementData = {};
var bytes = new Uint8Array(buffer);
while (length !== 0) {
length = bytes[i] & 0xFF;
i++;
// decode type constants from https://www.bluetooth.org/en-us/specification/assigned-numbers/generic-access-profile
type = bytes[i] & 0xFF;
i++;
data = bytes.slice(i, i + length - 1).buffer; // length includes type byte, but not length byte
i += length - 2; // move to end of data
i++;
advertisementData[asHexString(type)] = data;
}
return advertisementData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment