Obtaining UUID of a primary service

Hi,

I'm trying to write a generic tool that lists the attribute table of a connected BLE Peripheral. For each discovered attribute I want to log to the console the attribute number, attribute type and its UUID.

I have a problem with primary service declarations. I can get the declaration UUID (2800) but I don't seem to be able to get the UUID of the service being declared. My understanding is that the service's own UUID is in the user_data field of the bt_gatt_attr.

My code in this area looks like this:

   if (bt_uuid_cmp(attr->uuid, BT_UUID_GATT_PRIMARY) == 0) {
        printk("Discovered Primary Service\n");
        // The service's own UUID is in the user_data field of the bt_gatt_attr
        // https://docs.zephyrproject.org/latest/doxygen/html/group__bt__gatt__client.html#:~:text=bt_gatt_discover_func_t,-typedef%20uint8_t(*&text=Discover%20attribute%20callback%20function.,-conn&text=Connection%20object.&text=Attribute%20found%2C%20or%20NULL%20if%20not%20found.&text=Discovery%20parameters%20given.,order%20to%20cache%20its%20information.&text=bt_gatt_attr%20is%20given%20as%20an,See%20below. 
        struct bt_gatt_service_val *svc = (struct bt_gatt_service_val *)attr->user_data;
        uint16_t uuid_val = BT_UUID_16(svc->uuid)->val;
        printk("Service UUID = %d\n",uuid_val);
    }

But I'm always getting the same value from the user_data  UUID field which as a uint16_t is 57926. Like this:

Creating connection with Coded PHY support
Connected: D1:00:2D:79:94:DD (random)
Discovered Primary Service
Service UUID = 57926
Discovered attribute, handle: 01 uuid: 2800 Primary Service
Discovered attribute, handle: 02 uuid: 2803   Characteristic
Discovered attribute, handle: 03 uuid: 2a05     Service Changed
Discovered attribute, handle: 04 uuid: 2902      Client Characteristic Configuration
Discovered attribute, handle: 05 uuid: 2803   Characteristic
Discovered attribute, handle: 06 uuid: 2b29     Client Supported Features
Discovered attribute, handle: 07 uuid: 2803   Characteristic
Discovered attribute, handle: 08 uuid: 2b2a     Database Hash
Discovered Primary Service
Service UUID = 57926
Discovered attribute, handle: 09 uuid: 2800 Primary Service
Discovered attribute, handle: 10 uuid: 2803   Characteristic
Discovered attribute, handle: 11 uuid: 2a00     Device Name
Discovered attribute, handle: 12 uuid: 2803   Characteristic
Discovered attribute, handle: 13 uuid: 2a01     Appearance
Discovered attribute, handle: 14 uuid: 2803   Characteristic
Discovered attribute, handle: 15 uuid: 2a04     Peripheral Preferred Connection Parameters
Discovered Primary Service
Service UUID = 57926
Discovered attribute, handle: 16 uuid: 2800 Primary Service
Discovered attribute, handle: 17 uuid: 2803   Characteristic
Discovered attribute, handle: 18 uuid: 2a19     Battery Level
Discovered attribute, handle: 19 uuid: 2902      Client Characteristic Configuration
Discovered attribute, handle: 20 uuid: 2904    Characteristic Presentation Format
Discovered Primary Service
Service UUID = 57926
Discovered attribute, handle: 21 uuid: 2800 Primary Service
Discovered attribute, handle: 22 uuid: 2803   Characteristic
Discovered attribute, handle: 23 uuid: 2a24     Model Number String
Discovered attribute, handle: 24 uuid: 2803   Characteristic
Discovered attribute, handle: 25 uuid: 2a29     Manufacturer Name String
Discovered Primary Service
Service UUID = 57926
Discovered attribute, handle: 26 uuid: 2800 Primary Service
Discovered attribute, handle: 27 uuid: 2803   Characteristic
Discovered attribute, handle: 28 uuid: 2a37     Heart Rate Measurement
Discovered attribute, handle: 29 uuid: 2902      Client Characteristic Configuration
Discovered attribute, handle: 30 uuid: 2803   Characteristic
Discovered attribute, handle: 31 uuid: 2a38     Body Sensor Location
Discovered attribute, handle: 32 uuid: 2803   Characteristic
Discovered attribute, handle: 33 uuid: 2a39     Heart Rate Control Point
Discovery complete

What am I doing wrong?

Thanks in anticipation

Parents Reply Children
  • Additional:

    The big difference between my attempt and the solution in the referenced post is that I attempted to discover using:

    discover_params.type = BT_GATT_DISCOVER_ATTRIBUTE

    which the documentation describes as

    // Discover Attributes of any type.

    whereas the other post performs discovery in several stages following the usual GATT hierarchy:

    discover_params.type = BT_GATT_DISCOVER_PRIMARY

    discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC and

    discover_params.type = BT_GATT_DISCOVER_DESCRIPTOR

    Using the staged approach produces different values of BT_UUID_16(svc_attr->uuid)->val from primary service declarations. e.g. the working code produces output such as:

    attr is a primary service. UUID type is 0, val = 1801

    whereas I always get

    attr is a primary service. UUID type is 13, val = e246

    Is it possible that when discover_params.type = BT_GATT_DISCOVER_ATTRIBUTE is used, there's a bug?

     could you please try to recreate this situation using discover_params.type = BT_GATT_DISCOVER_ATTRIBUTE and see what you think of the results?

    Thank you

  • Hi,

    I have not attempted to reproduce from scratch, but these APIs are used by the GATT Discovery Manager module, and I am not aware of any bugs there (that does not mean that non can exist).

    Perhaps it will be better to use the discovery manager instead? This is a module in the nRF Connect sDK exists precisely to make service discovery simpler than it is with only the Zephyr APIs. It is demonstrated in Peripheral GATT Discovery Manager (this is a peripheral sample, but the discovery part is the same regardless of GAP role). This sample performs a service discovery and prints all services and characteristics (including the service UUID).

  • Thanks, I wasn't aware of the GATT discovery manager. That sounds promising. I'll give it a try. 

Related