Python script to generate random NimBLE BLE UUID

The following script generates a source line for a random BLE UUID in C++ format, suitable for use with NimBLE.

Example output

static const ble_uuid128_t custom_service = 
    BLE_UUID128_INIT(0xB5, 0xB6, 0xA7, 0xB8, 0xC9, 0x8D, 0x90, 0x42,
                     0x8E, 0x39, 0xA5, 0x62, 0x23, 0x90, 0x97, 0x54);

generate_nimble_uuid.py

#!/usr/bin/env python3
import argparse
import uuid

def main():
    parser = argparse.ArgumentParser(description="Generate C++ BLE UUID source line.")
    parser.add_argument("name", help="Variable name for the UUID")
    args = parser.parse_args()

    u = uuid.uuid4()
    bytes_le = u.bytes_le  # little-endian byte order

    # Format bytes as hex for C++ macro
    hex_bytes = ', '.join(f'0x{b:02X}' for b in bytes_le)
    # Split into two lines: 8 bytes per line
    hex_list = hex_bytes.split(', ')
    line1 = ', '.join(hex_list[:8])
    line2 = ', '.join(hex_list[8:])
    print(f"static const ble_uuid128_t {args.name} = ")
    print(f"    BLE_UUID128_INIT({line1},")
    print(f"                     {line2});")

if __name__ == "__main__":
    main()