Python : Script pour générer un UUID BLE NimBLE aléatoire

Le script suivant génère une ligne de code source pour un UUID BLE aléatoire au format C++, utilisable avec NimBLE.

Exemple de sortie

random_uuid_example.cpp
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

generate_nimble_uuid.py
#!/usr/bin/env python3
import argparse
import uuid

def main():
    parser = argparse.ArgumentParser(description="Générer une ligne de code source pour un UUID BLE C++.")
    parser.add_argument("name", help="Nom de variable pour l'UUID")
    args = parser.parse_args()

    u = uuid.uuid4()
    bytes_le = u.bytes_le  # ordre des octets petit-boutiste

    # Formater les octets en hexadécimal pour la macro C++
    hex_bytes = ', '.join(f'0x{b:02X}' for b in bytes_le)
    # Diviser en deux lignes : 8 octets par ligne
    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()

Check out similar posts by category: Bluetooth, Python