Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NimBLEUUID: initializing from a 16-bit ID and toString are not consistent #871

Open
bmedici opened this issue Jan 20, 2025 · 1 comment
Open

Comments

@bmedici
Copy link

bmedici commented Jan 20, 2025

When you initialize a NimBLEUUID with a 16-bit id, like in:

  • NimBLECharacteristic("1234",...
  • NimBLEUUID("1234")
    Only the string without 0x prefix is accepted and working.

But when you try to get the string representation of the ID from a characteristic or a service UUID, it's build with sprintf("0x%04"... which is not consistent with the input described above.

And I can"t find any simple way to get a numeric version of this ID, for example to call to16String, which could return 0x1234 (the uint16_t number).

The consequence is that I'm not able, as the same time and from the same set of values (characteristics ID for example), to

  • select services and chars and subscribe to notifications with these IDs
  • match the received notifications from the set of values, because the keys should be without "0x" prefix (ex: "1234"), and the received notifications expose the characteristic ID with this prfiex within the generated string ("0x1234").

Any suggestion on how to be able to feed and get the same format for these IDs, preferably strings, in the same format ?

@h2zero
Copy link
Owner

h2zero commented Jan 20, 2025

You shouldn't need to use the string value for anything except printing to the console, you can simply use the NimBLEUUID class for anything that needs the UUID.

static NimBLEUUID myGlobalUUID("1234"); // even better to use myGlobalUUID((uint16_t)0x1234)

class CharacteristicCallbacks : public NimBLECharacteristicCallbacks {
    void onRead(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo) override {
        if (pCharacteristic->getUUID() == myGlobalUUID) {
            Serial.println("Characteristic with myGlobalUUID was read");
        }
    }
};

/***** other code *****/
    pService->createCharacteristic(myGlobalUUID);

Doing it this way is much more efficient.
If you really want the 16 bit value you could do this: uint16_t uuidVal = *(uint16_t*)someUUID.getValue(); or alternatively if you really want this you could do: uint16_t uuidVal = ((ble_uuid16_t*)(myGlobalUUID.getBase()))->value;, but I don't see much point in this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants