Skip to content

Instantly share code, notes, and snippets.

@beaufortfrancois
Created August 12, 2021 12:31
Show Gist options
  • Save beaufortfrancois/1323816074f7383cfa574811abd6ea9c to your computer and use it in GitHub Desktop.
Save beaufortfrancois/1323816074f7383cfa574811abd6ea9c to your computer and use it in GitHub Desktop.
Parse Bluetooth Assigned Numbers (Service, Characteristics, Descriptors)
const fs = require("fs");
const LEGACY_UUIDS = {
"0x180F": "battery_service",
"0x2900": "gatt.characteristic_extended_properties",
"0x2901": "gatt.characteristic_user_description",
"0x2902": "gatt.client_characteristic_configuration",
"0x2903": "gatt.server_characteristic_configuration",
"0x2904": "gatt.characteristic_presentation_format",
"0x2905": "gatt.characteristic_aggregate_format",
"0x290B": "es_configuration",
"0x290C": "es_measurement",
"0x290D": "es_trigger_setting",
"0x2A00": "gap.device_name",
"0x2A01": "gap.appearance",
"0x2A02": "gap.peripheral_privacy_flag",
"0x2A03": "gap.reconnection_address",
"0x2A04": "gap.peripheral_preferred_connection_parameters",
"0x2A05": "gatt.service_changed",
"0x2A2A": "ieee_11073-20601_regulatory_certification_data_list",
"0x2A95": "two_zone_heart_rate_limit",
"0x2AA0": "magnetic_flux_density_2D",
"0x2AA1": "magnetic_flux_density_3D",
"0x2AA6": "gap.central_address_resolution_support",
"0x2AB1": "local_east_coordinate.xml",
"0x2AC5": "object_action_control_point",
};
// Instructions:
// 1. Download https://btprodspecificationrefs.blob.core.windows.net/assigned-values/16-bit%20UUID%20Numbers%20Document.pdf
// 2. Convert it to txt.
// 3. Run `node read-assigned-numbers.js`
// 4. Copy/paste output in `third_party/blink/renderer/modules/bluetooth/bluetooth_uuid.cc`
fs.readFile("16-bit UUID Numbers Document.txt", "utf8", (err, data) => {
// Expect a txt file with this structure.
//
// GATT Service
//
// 0x1800
//
// Generic Access
//
// GATT Service
//
// 0x1801
//
// Generic Attribute
printAssignedNumbers(data, "GATT Service");
printAssignedNumbers(data, "GATT Characteristic");
printAssignedNumbers(data, "GATT Descriptor");
});
function printAssignedNumbers(data, filter) {
let uuids = [];
let names = [];
let saveUuid = false;
let saveName = false;
data
.split(/\r?\n/)
.filter((line) => line.length != 0)
.forEach((line) => {
if (saveName) {
names.push(line);
saveName = false;
}
if (saveUuid) {
uuids.push(line);
saveUuid = false;
saveName = true;
}
if (line.startsWith(filter)) {
saveUuid = true;
}
});
console.log(`${filter}:`);
for (let i = 0; i < uuids.length; i++) {
let name = names[i]
.toLowerCase()
.replace(/ - /g, "_")
.replace(/ /g, "_")
.replace(/-/g, "_");
let uuid = uuids[i].replace(/^0X/, "0x");
if (uuid in LEGACY_UUIDS) {
name = LEGACY_UUIDS[uuid];
}
console.log(` {"${name}", ${uuid}},`);
}
}
@ariccio
Copy link

ariccio commented Dec 19, 2021

It's kinda wild that nobody else maintains something like this, right? When devices don't have good documentation, it's super useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment