---
title: "Heart rate central"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/third_party/nimble/central_hr/
last_updated: 2026-11-19
sdk_version: 4.44
---
# Heart rate central

Source: https://developer.repebble.com/pebbleos/docs/samples/third_party/nimble/central_hr/

This sample is the client of the [Heart rate peripheral](https://developer.repebble.com/pebbleos/docs/samples/third_party/nimble/peripheral_hr.md) sample, or of any heart rate sensor. It scans for a device listing the Heart Rate service in its advertisement, connects to it, finds the measurement characteristic and its CCCD, and subscribes. It prints ten measurements, reading the rate as 8 or 16 bits as the flags of each say, then disconnects.

Run:

`pbl samples samples/third_party/nimble/central_hr`

Boards:

every board

## Sources

src/main.c

```
#include \<stdint.h\>#include \<stdio.h\>#include \<pbl/kernel/sem.h\>#include \<pbl/kernel/types.h\>#include \<host/ble\_hs.h\>#include \<host/util/util.h\>#include \<nimble/nimble\_port.h\>#include \<nimble/nimble\_port\_pebble.h\>#define HRS\_UUID 0x180DU#define HRS\_MEASUREMENT\_UUID 0x2A37U#define CCCD\_UUID 0x2902U#define MEASUREMENTS 10static PBL\_SEM\_DEFINE(s\_synced, 0, 1);static PBL\_SEM\_DEFINE(s\_done, 0, 1);static uint8\_t s\_own\_addr\_type;/\* Only the host thread touches these. \*/static uint16\_t s\_svc\_end\_handle;static uint16\_t s\_measurement\_handle;static uint16\_t s\_cccd\_handle;static int s\_measurements;static int prv\_gap\_event(struct ble\_gap\_event \*event, void \*arg);static void prv\_scan(void) {/\* Active: some sensors only list their services in the scan response. \*/const struct ble\_gap\_disc\_params params = {.filter\_duplicates = 1,};int rc;rc = ble\_gap\_disc(s\_own\_addr\_type, BLE\_HS\_FOREVER, &params, prv\_gap\_event, nullptr);if (rc != 0) {printf("central\_hr: scanning failed (%d)\n", rc);return;}printf("central\_hr: scanning for a heart rate sensor\n");}static bool prv\_has\_hrs(const struct ble\_gap\_disc\_desc \*desc) {struct ble\_hs\_adv\_fields fields;if (ble\_hs\_adv\_parse\_fields(&fields, desc-\>data, desc-\>length\_data) != 0) {return false;}for (uint8\_t i = 0U; i \< fields.num\_uuids16; i++) {if (ble\_uuid\_u16(&fields.uuids16[i].u) == HRS\_UUID) {return true;}}return false;}static void prv\_fail(uint16\_t conn\_handle, const char \*what, int rc) {printf("central\_hr: %s failed (%d)\n", what, rc);(void)ble\_gap\_terminate(conn\_handle, BLE\_ERR\_REM\_USER\_CONN\_TERM);}static int prv\_on\_subscribed(uint16\_t conn\_handle, const struct ble\_gatt\_error \*error,struct ble\_gatt\_attr \*attr, void \*arg) {if (error-\>status != 0) {prv\_fail(conn\_handle, "subscribing", error-\>status);return 0;}printf("central\_hr: subscribed to the measurements\n");return 0;}/\* The CCCD is the descriptor after the value with the 0x2902 UUID. \*/static int prv\_on\_dsc(uint16\_t conn\_handle, const struct ble\_gatt\_error \*error,uint16\_t chr\_val\_handle, const struct ble\_gatt\_dsc \*dsc, void \*arg) {static const uint8\_t notify[] = {0x01U, 0x00U};int rc;if (error-\>status == 0) {if (ble\_uuid\_u16(&dsc-\>uuid.u) == CCCD\_UUID) {s\_cccd\_handle = dsc-\>handle;}return 0;}if (error-\>status != BLE\_HS\_EDONE || s\_cccd\_handle == 0U) {prv\_fail(conn\_handle, "finding the CCCD", error-\>status);return 0;}rc = ble\_gattc\_write\_flat(conn\_handle, s\_cccd\_handle, notify, sizeof(notify), prv\_on\_subscribed,nullptr);if (rc != 0) {prv\_fail(conn\_handle, "subscribing", rc);}return 0;}static int prv\_on\_chr(uint16\_t conn\_handle, const struct ble\_gatt\_error \*error,const struct ble\_gatt\_chr \*chr, void \*arg) {int rc;if (error-\>status == 0) {s\_measurement\_handle = chr-\>val\_handle;return 0;}if (error-\>status != BLE\_HS\_EDONE || s\_measurement\_handle == 0U) {prv\_fail(conn\_handle, "finding the measurement", error-\>status);return 0;}rc = ble\_gattc\_disc\_all\_dscs(conn\_handle, s\_measurement\_handle, s\_svc\_end\_handle, prv\_on\_dsc,nullptr);if (rc != 0) {prv\_fail(conn\_handle, "finding the CCCD", rc);}return 0;}static int prv\_on\_svc(uint16\_t conn\_handle, const struct ble\_gatt\_error \*error,const struct ble\_gatt\_svc \*service, void \*arg) {int rc;if (error-\>status == 0) {s\_svc\_end\_handle = service-\>end\_handle;rc = ble\_gattc\_disc\_chrs\_by\_uuid(conn\_handle, service-\>start\_handle, service-\>end\_handle,BLE\_UUID16\_DECLARE(HRS\_MEASUREMENT\_UUID), prv\_on\_chr, nullptr);if (rc != 0) {prv\_fail(conn\_handle, "finding the measurement", rc);}return 0;}if (error-\>status != BLE\_HS\_EDONE || s\_svc\_end\_handle == 0U) {prv\_fail(conn\_handle, "finding the service", error-\>status);}return 0;}static void prv\_on\_measurement(uint16\_t conn\_handle, struct os\_mbuf \*om) {uint8\_t value[3];uint16\_t len = 0U;unsigned int bpm;/\* Only the flags and the rate: what may follow is cut off. \*/(void)ble\_hs\_mbuf\_to\_flat(om, value, sizeof(value), &len);if (len \< 2U) {return;}/\* Bit 0 of the flags: the rate is 16-bit. \*/bpm = (value[0] & 0x01U) != 0U && len \>= 3U ? get\_le16(&value[1]) : value[1];printf("central\_hr: %u bpm\n", bpm);if (++s\_measurements == MEASUREMENTS) {(void)ble\_gap\_terminate(conn\_handle, BLE\_ERR\_REM\_USER\_CONN\_TERM);}}/\* Runs on the host thread. \*/static int prv\_gap\_event(struct ble\_gap\_event \*event, void \*arg) {int rc;switch (event-\>type) {case BLE\_GAP\_EVENT\_DISC:if (!prv\_has\_hrs(&event-\>disc)) {break;}(void)ble\_gap\_disc\_cancel();rc = ble\_gap\_connect(s\_own\_addr\_type, &event-\>disc.addr, 30000, nullptr, prv\_gap\_event,nullptr);if (rc != 0) {printf("central\_hr: connecting failed (%d)\n", rc);prv\_scan();}break;case BLE\_GAP\_EVENT\_CONNECT:if (event-\>connect.status != 0) {printf("central\_hr: connection failed (%d)\n", event-\>connect.status);prv\_scan();break;}printf("central\_hr: connected\n");s\_svc\_end\_handle = 0U;s\_measurement\_handle = 0U;s\_cccd\_handle = 0U;rc = ble\_gattc\_disc\_svc\_by\_uuid(event-\>connect.conn\_handle, BLE\_UUID16\_DECLARE(HRS\_UUID),prv\_on\_svc, nullptr);if (rc != 0) {prv\_fail(event-\>connect.conn\_handle, "finding the service", rc);}break;case BLE\_GAP\_EVENT\_NOTIFY\_RX:if (event-\>notify\_rx.attr\_handle == s\_measurement\_handle) {prv\_on\_measurement(event-\>notify\_rx.conn\_handle, event-\>notify\_rx.om);}break;case BLE\_GAP\_EVENT\_DISCONNECT:printf("central\_hr: disconnected, reason 0x%03x\n", event-\>disconnect.reason);if (s\_measurements \>= MEASUREMENTS) {pbl\_sem\_give(&s\_done);} else {prv\_scan();}break;default:break;}return 0;}static void prv\_on\_sync(void) {pbl\_sem\_give(&s\_synced);}static void prv\_on\_reset(int reason) {printf("central\_hr: host reset, reason %d\n", reason);}int main(void) {int rc;nimble\_port\_init();ble\_hs\_cfg.sync\_cb = prv\_on\_sync;ble\_hs\_cfg.reset\_cb = prv\_on\_reset;nimble\_port\_pebble\_init();pbl\_sem\_take(&s\_synced, PBL\_FOREVER);rc = ble\_hs\_util\_ensure\_addr(0);if (rc == 0) {rc = ble\_hs\_id\_infer\_auto(0, &s\_own\_addr\_type);}if (rc != 0) {printf("central\_hr: no address (%d)\n", rc);return rc;}prv\_scan();pbl\_sem\_take(&s\_done, PBL\_FOREVER);printf("Sample done\n");return 0;}
```

## Output

```
central_hr: scanning for a heart rate sensor
```

Generated from PebbleOS [ac5760587](https://github.com/coredevices/pebbleos/commit/ac576058737718d2de0783896789b836ea09757d). This page is maintained in the pebbleos repository: [docs/samples/third\_party/nimble/central\_hr.md](https://github.com/coredevices/pebbleos/blob/ac576058737718d2de0783896789b836ea09757d/docs/samples/third_party/nimble/central_hr.md).
