As we're working on Bluetooth LE, we will sometimes need UUIDs for services or characteristics (or other things that need randomly generated UUIDs). This is a simple tool that generates 128 bit UUID v4 instances, returning them in a few handy formats.
This tool is built using Node and NPM. You'll need a recent Node; if you don't have one, I recommend installing Node Version Manager, then getting the latest LTS version (as of this writing, that's lts/carbon) like this:
nvm install lts/carbon
Once you've got Node, you can install the tool dependencies with:
npm install
It's pretty simple:
bin/ble-uuid
You'll get an output that looks like this:
New 128 Bit UUID v4 Generated:
UUID: 8b5b54d0-b595-4de0-8b31-f08b1aefb667
array:
[0x8B,0x5B,0x54,0xD0,0xB5,0x95,0x4D,0xE0,0x8B,0x31,0xF0,0x8B,0x1A,0xEF,0xB6,0x67]
array (reversed):
[0x67,0xB6,0xEF,0x1A,0x8B,0xF0,0x31,0x8B,0xE0,0x4D,0x95,0xB5,0xD0,0x54,0x5B,0x8B]
uint8_t hex array:
{0x8B,0x5B,0x54,0xD0,0xB5,0x95,0x4D,0xE0,0x8B,0x31,0xF0,0x8B,0x1A,0xEF,0xB6,0x67}
uint8_t hex array (reversed):
{0x67,0xB6,0xEF,0x1A,0x8B,0xF0,0x31,0x8B,0xE0,0x4D,0x95,0xB5,0xD0,0x54,0x5B,0x8B}
You the new UUID as a formatted string, as the bytes of the UUID as Javascript/Java-compatible and uint8_t arrays (both in-order and reversed).
The uint8_t array versions are useful for dropping directly into certain C/C++ based code. For example, we have used the reversed version of these arrays with the Adafruit Bluefruit library for the nRF52 Feather.
You can also supply a list of characteristic names, and each one will be mapped to a UUID based on the service UUID, with the last 4 bytes stripped to zeroes, then incremented for each characteristic:
$ bin/ble-uuid a b c
New 128 Bit UUID v4 Generated:
UUID: 65eed7b6-a704-4cf9-88a1-688d2a899d35
array:
[0x65,0xEE,0xD7,0xB6,0xA7,0x4,0x4C,0xF9,0x88,0xA1,0x68,0x8D,0x2A,0x89,0x9D,0x35]
array (reversed):
[0x35,0x9D,0x89,0x2A,0x8D,0x68,0xA1,0x88,0xF9,0x4C,0x4,0xA7,0xB6,0xD7,0xEE,0x65]
uint8_t hex array:
{0x65,0xEE,0xD7,0xB6,0xA7,0x4,0x4C,0xF9,0x88,0xA1,0x68,0x8D,0x2A,0x89,0x9D,0x35}
uint8_t hex array (reversed):
{0x35,0x9D,0x89,0x2A,0x8D,0x68,0xA1,0x88,0xF9,0x4C,0x4,0xA7,0xB6,0xD7,0xEE,0x65}
--------------------------
Characteristic name: a
array:
[0x65,0xEE,0xD7,0xB6,0xA7,0x4,0x4C,0xF9,0x88,0xA1,0x68,0x8D,0x0,0x0,0x0,0x1]
array (reversed):
[0x1,0x0,0x0,0x0,0x8D,0x68,0xA1,0x88,0xF9,0x4C,0x4,0xA7,0xB6,0xD7,0xEE,0x65]
uint8_t hex array:
{0x65,0xEE,0xD7,0xB6,0xA7,0x4,0x4C,0xF9,0x88,0xA1,0x68,0x8D,0x0,0x0,0x0,0x1}
uint8_t hex array (reversed):
{0x1,0x0,0x0,0x0,0x8D,0x68,0xA1,0x88,0xF9,0x4C,0x4,0xA7,0xB6,0xD7,0xEE,0x65}
Characteristic name: b
array:
[0x65,0xEE,0xD7,0xB6,0xA7,0x4,0x4C,0xF9,0x88,0xA1,0x68,0x8D,0x0,0x0,0x0,0x2]
array (reversed):
[0x2,0x0,0x0,0x0,0x8D,0x68,0xA1,0x88,0xF9,0x4C,0x4,0xA7,0xB6,0xD7,0xEE,0x65]
uint8_t hex array:
{0x65,0xEE,0xD7,0xB6,0xA7,0x4,0x4C,0xF9,0x88,0xA1,0x68,0x8D,0x0,0x0,0x0,0x2}
uint8_t hex array (reversed):
{0x2,0x0,0x0,0x0,0x8D,0x68,0xA1,0x88,0xF9,0x4C,0x4,0xA7,0xB6,0xD7,0xEE,0x65}
Characteristic name: c
array:
[0x65,0xEE,0xD7,0xB6,0xA7,0x4,0x4C,0xF9,0x88,0xA1,0x68,0x8D,0x0,0x0,0x0,0x3]
array (reversed):
[0x3,0x0,0x0,0x0,0x8D,0x68,0xA1,0x88,0xF9,0x4C,0x4,0xA7,0xB6,0xD7,0xEE,0x65]
uint8_t hex array:
{0x65,0xEE,0xD7,0xB6,0xA7,0x4,0x4C,0xF9,0x88,0xA1,0x68,0x8D,0x0,0x0,0x0,0x3}
uint8_t hex array (reversed):
{0x3,0x0,0x0,0x0,0x8D,0x68,0xA1,0x88,0xF9,0x4C,0x4,0xA7,0xB6,0xD7,0xEE,0x65}
This tool is a wrapper around the UUID NPM package, meant to make it easy to get what we need when working with BLE services and characteristics.
You also use the CLI tool from the UUID package this relies on directly with:
npx uuid
This tools has many more options for generating UUIDs, so it might be worth looking at beyond the domain-specific use we are making.
If you want to see the options for that command, run npx uuid --help
.
The binary for this utility is made using the core module plus the very handy yargs.
And we stay pure and functional with ramda. I had a bug in an earlier version thanks to in-place mutation of the buffer array — never again with ramda!