Discover custom UUID (services, characteristics and CCCDs) with Zephyr RTOS as a BLE Central

I’m developing a BLE Central device on a nRF52840 DK with Zephyr RTOS. After connecting, pairing and bonding, I want to discover and log every services (with their characteristics and CCCDs) the peripheral has.

I haven’t any problem to discover standard services (understand services that are defined by the Bluetooth Sig). Where the thing become more difficult is when I want to retrieve the custom services.

My central indeed discover the custom services but the values that are logged are the same for every services (I actually print 3 times the same UUID with different formats: short (16 bits), medium (32 bits) and long (128 bits) UUID).

This is the central discovery code:

static discover_phases_t discover_phase;
static struct bt_gatt_discover_params discover_params;

static void connected_cb(struct bt_conn *conn, uint8_t conn_err)
{
  const bt_addr_le_t * addr = bt_conn_get_dst(conn);

  if (conn_err) {
    bt_conn_unref(default_conn);
    default_conn = NULL;
    start_scan();
  }

  if (conn == default_conn)
  {
    int err = bt_conn_set_security(conn, BT_SECURITY_L3);
    if (err)
    {
      LOG_ERR("Error while pairing %d", err);
      return;
    }

    LOG_WRN("Connected: %02X:%02X:%02X:%02X:%02X:%02X", addr->a.val[5], addr->a.val[4], addr->a.val[3], addr->a.val[2], addr->a.val[1], addr->a.val[0]);

    discover_params.uuid = NULL;
    discover_params.func = discover_services_cb;
    discover_params.start_handle = BT_ATT_FIRST_ATTRIBUTE_HANDLE;
    discover_params.end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE;
    discover_params.type = BT_GATT_DISCOVER_PRIMARY;
    discover_phase = DISC_PHASE_SERVICE;

    err = bt_gatt_discover(conn, &discover_params);
    if (err)
    {
      LOG_ERR("Discovery failed (err %d)", err);
    }
  }
}

static uint8_t discover_services_cb(struct bt_conn *conn, const struct bt_gatt_attr *attr, struct bt_gatt_discover_params *params)
{
  static uint16_t last_handle;

  if (attr)
    LOG_INF("[ATTRIBUTE] handle: %u  UUID type: %d  val: %x", attr->handle, attr->uuid->type, BT_UUID_16(attr->uuid)->val);

  switch (discover_phase)
  {
    case DISC_PHASE_SERVICE:
      if (attr)
      {
        struct bt_gatt_service_val *svc_attr = attr->user_data;
        if (svc_attr->uuid->type == BT_UUID_TYPE_16)
          LOG_INF("Attr is primary service UUID type: %d  val: %04x", svc_attr->uuid->type, BT_UUID_16(svc_attr->uuid)->val);
        else if (svc_attr->uuid->type == BT_UUID_TYPE_32)
          LOG_INF("Attr is UUID 32bits");
        else if (svc_attr->uuid->type == BT_UUID_TYPE_128)
        {
          LOG_INF("Attr is primary service UUID type: %d  val 16:  %04x", svc_attr->uuid->type, BT_UUID_16(svc_attr->uuid)->val);
          LOG_INF("Attr is primary service UUID type: %d  val 32:  %04x", svc_attr->uuid->type, BT_UUID_32(svc_attr->uuid)->val);
          LOG_INF("Attr is primary service UUID type: %d  val 128: %s", svc_attr->uuid->type, BT_UUID_128(svc_attr->uuid)->val);

          LOG_INF("Found Custom service");
        }
        else
        {
          LOG_ERR("discover_services_cb: UUID type check failed");
          return BT_GATT_ITER_STOP;
        }
        last_handle = attr->handle;
      }
      else
      {
        LOG_WRN("No more services, looking for characteristics");
        discover_params.uuid = NULL;
        discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;
        discover_params.start_handle = last_handle + 1;

        int err = bt_gatt_discover(conn, &discover_params);
        if (err)
          LOG_ERR("Discover failed - services (err %d)", err);
        discover_phase = DISC_PHASE_CHRC;
        return BT_GATT_ITER_STOP;
      }
      break;

    case DISC_PHASE_CHRC:
      if (attr)
      {
        struct bt_gatt_chrc *chrc_attr = attr->user_data;
        LOG_INF("attr is characteristic. UUID type: %d  val: %x", chrc_attr->uuid->type, BT_UUID_16(chrc_attr->uuid)->val);

        last_handle = attr->handle;
      }
      else
      {
        LOG_WRN("No more characteristics, looking for CCCDs");
        discover_params.uuid = NULL;
        discover_params.type = BT_GATT_DISCOVER_DESCRIPTOR;
        discover_params.start_handle = last_handle + 1;

        int err = bt_gatt_discover(conn, &discover_params);
        if (err)
          LOG_ERR("Discover failed - characteristics (err %d)", err);
        discover_phase = DISC_PHASE_CCCD;
        return BT_GATT_ITER_STOP;
      }
      break;

    case DISC_PHASE_CCCD:
      if (attr)
      {
        LOG_INF("attr is CCCD. UUID type: %d  val: %x", attr->uuid->type, BT_UUID_16(attr->uuid)->val);
        if (attr->user_data == NULL)
          LOG_INF("user_data is NULL");
        else
        {
          struct bt_gatt_ccc *dope = attr->user_data;
          LOG_INF("user_data is %x", dope->flags);
        }
        last_handle = attr->handle;
      }
      else
      {
        LOG_WRN("Discover complete");
      }
      break;

    default:
      break;
  }

  return BT_GATT_ITER_CONTINUE;
}

This is the log associate:

[00:00:07.685,424] <wrn> main: Connected: 5F:5E:48:13:59:F4
[00:00:07.935,607] <inf> main: [ATTRIBUTE] handle: 1  UUID type: 0  val: 2800
[00:00:07.935,638] <inf> main: Attr is primary service UUID type: 0  val: 1800
[00:00:07.935,668] <inf> main: [ATTRIBUTE] handle: 10  UUID type: 0  val: 2800
[00:00:07.935,668] <inf> main: Attr is primary service UUID type: 0  val: 1801
[00:00:08.239,196] <inf> main: [ATTRIBUTE] handle: 14  UUID type: 0  val: 2800
[00:00:08.239,227] <inf> main: Attr is primary service UUID type: 2  val 16:  0246
[00:00:08.239,227] <inf> main: Attr is primary service UUID type: 2  val 32:  15e71db3
[00:00:08.239,288] <inf> main: Attr is primary service UUID type: 2  val 128: F����CD0
[00:00:08.239,288] <inf> main: Found Custom service
[00:00:08.335,418] <inf> main: [ATTRIBUTE] handle: 20  UUID type: 0  val: 2800
[00:00:08.335,418] <inf> main: Attr is primary service UUID type: 0  val: 180a
[00:00:08.435,577] <inf> main: [ATTRIBUTE] handle: 31  UUID type: 0  val: 2800
[00:00:08.435,577] <inf> main: Attr is primary service UUID type: 2  val 16:  0246
[00:00:08.435,607] <inf> main: Attr is primary service UUID type: 2  val 32:  15e71db3
[00:00:08.435,638] <inf> main: Attr is primary service UUID type: 2  val 128: F����CD0
[00:00:08.435,638] <inf> main: Found Custom service
[00:00:08.435,668] <inf> main: [ATTRIBUTE] handle: 39  UUID type: 0  val: 2800
[00:00:08.435,668] <inf> main: Attr is primary service UUID type: 2  val 16:  0246
[00:00:08.435,699] <inf> main: Attr is primary service UUID type: 2  val 32:  15e71db3
[00:00:08.435,729] <inf> main: Attr is primary service UUID type: 2  val 128: F����CD0
[00:00:08.435,760] <inf> main: Found Custom service
[00:00:08.435,760] <inf> main: [ATTRIBUTE] handle: 45  UUID type: 0  val: 2800
[00:00:08.435,760] <inf> main: Attr is primary service UUID type: 2  val 16:  0246
[00:00:08.435,791] <inf> main: Attr is primary service UUID type: 2  val 32:  15e71db3
[00:00:08.435,821] <inf> main: Attr is primary service UUID type: 2  val 128: F����CD0
[00:00:08.435,852] <inf> main: Found Custom service
[00:00:08.785,339] <inf> main: [ATTRIBUTE] handle: 51  UUID type: 0  val: 2800
[00:00:08.785,369] <inf> main: Attr is primary service UUID type: 2  val 16:  0246
[00:00:08.785,369] <inf> main: Attr is primary service UUID type: 2  val 32:  15e71db3
[00:00:08.785,430] <inf> main: Attr is primary service UUID type: 2  val 128: F����CD0
[00:00:08.785,430] <inf> main: Found Custom service
[00:00:08.785,461] <inf> main: [ATTRIBUTE] handle: 57  UUID type: 0  val: 2800
[00:00:08.785,461] <inf> main: Attr is primary service UUID type: 2  val 16:  0246
[00:00:08.785,491] <inf> main: Attr is primary service UUID type: 2  val 32:  15e71db3
[00:00:08.785,522] <inf> main: Attr is primary service UUID type: 2  val 128: F����CD0
[00:00:08.785,552] <inf> main: Found Custom service
[00:00:08.785,614] <inf> main: [ATTRIBUTE] handle: 63  UUID type: 0  val: 2800
[00:00:08.785,614] <inf> main: Attr is primary service UUID type: 2  val 16:  dcca
[00:00:08.785,614] <inf> main: Attr is primary service UUID type: 2  val 32:  a9e50e24
[00:00:08.785,675] <inf> main: Attr is primary service UUID type: 2  val 128: ���$������
[00:00:08.785,675] <inf> main: Found Custom service
[00:00:08.785,705] <wrn> main: No more services, looking for characteristics
[00:00:08.935,821] <inf> main: [ATTRIBUTE] handle: 64  UUID type: 0  val: 2803
[00:00:08.935,852] <inf> main: attr is characteristic. UUID type: 2  val: dcca
[00:00:08.935,852] <inf> main: [ATTRIBUTE] handle: 66  UUID type: 0  val: 2803
[00:00:08.935,882] <inf> main: attr is characteristic. UUID type: 2  val: dcca
[00:00:09.105,957] <wrn> main: No more characteristics, looking for CCCDs
[00:00:09.135,406] <inf> main: [ATTRIBUTE] handle: 67  UUID type: 2  val: dcca
[00:00:09.135,406] <inf> main: attr is CCCD. UUID type: 2  val: dcca
[00:00:09.135,406] <inf> main: user_data is NULL
[00:00:09.165,313] <inf> main: [ATTRIBUTE] handle: 68  UUID type: 0  val: 2902
[00:00:09.165,344] <inf> main: attr is CCCD. UUID type: 0  val: 2902
[00:00:09.165,344] <inf> main: user_data is NULL
[00:00:09.195,312] <wrn> main: Discover complete

I used my phone with the nRF Connect Mobile app to connect to the peripheral so I could see the different services, UUID, etc…
With the phone, I was able to read, write and subscribe to characteristics (So the peripheral work great and the issue must come from the central).

One of the custom service’s UUID is 8ab20b00-3044-4385-9315-e71db302460e. As I understand the BLE specs, the 16 bits UUID is the third and fourth bytes of the long UUID, where the 32 bits is the first to fourth bytes of the long UUID.
So in this case, it must be:

UUID format (number of bits) Result
16 0b00
32 8ab20b00
128 8ab20b00-3044-4385-9315-e71db302460e

But as we can see in the log above, it doesn’t print that at all.

However, as far as I understand, it seems that there is a endian conflict between the BT_UUID_XX macro and the value provide in the bt_gatt_attr *attr in the parameters. I think that one works with little-endian while the other works with big-endian. I thought that because the value printed in the log correspond to the end of the 128 bits UUID.

Is my thoughts about this endian conflict right ? If not, how can I resolve this ?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật