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

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

This sample is a heart rate sensor, as phones and fitness apps expect one: it has the standard Heart Rate service of its own, and NimBLE’s Battery and Device Information services, enabled in its `prj.conf`. It advertises the three services, its name and the heart rate sensor appearance.

Once a peer subscribes to the heart rate measurement, the sample notifies it of a simulated rate every second, building the notification itself with `ble_gatts_notify_custom()`. Every ten seconds the battery level drops by one: NimBLE’s Battery service notifies subscribed peers of it.

Run:

`pbl samples samples/third_party/nimble/peripheral_hr`

Boards:

every board

## Sources

src/main.c

```
#include \<stdatomic.h\>#include \<stdint.h\>#include \<stdio.h\>#include \<string.h\>#include \<pbl/kernel/sem.h\>#include \<pbl/kernel/thread.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\>#include \<services/bas/ble\_svc\_bas.h\>#include \<services/dis/ble\_svc\_dis.h\>#include \<services/gap/ble\_svc\_gap.h\>#include \<services/gatt/ble\_svc\_gatt.h\>#define NAME "PebbleOS HR"#define HRS\_UUID 0x180DU#define HRS\_MEASUREMENT\_UUID 0x2A37U#define HRS\_BODY\_SENSOR\_LOC\_UUID 0x2A38U#define HRS\_BODY\_SENSOR\_LOC\_WRIST 0x02U#define BAS\_UUID 0x180FU#define DIS\_UUID 0x180AU#define APPEARANCE\_HR\_SENSOR 0x0340Ustatic PBL\_SEM\_DEFINE(s\_synced, 0, 1);static uint8\_t s\_own\_addr\_type;static uint16\_t s\_measurement\_handle;static atomic\_uint s\_conn\_handle = BLE\_HS\_CONN\_HANDLE\_NONE;static atomic\_bool s\_subscribed;/\* Only notified: there is nothing to read. \*/static int prv\_measurement\_access(uint16\_t conn\_handle, uint16\_t attr\_handle,struct ble\_gatt\_access\_ctxt \*ctxt, void \*arg) {return BLE\_ATT\_ERR\_READ\_NOT\_PERMITTED;}static int prv\_body\_sensor\_loc\_access(uint16\_t conn\_handle, uint16\_t attr\_handle,struct ble\_gatt\_access\_ctxt \*ctxt, void \*arg) {const uint8\_t loc = HRS\_BODY\_SENSOR\_LOC\_WRIST;if (os\_mbuf\_append(ctxt-\>om, &loc, sizeof(loc)) != 0) {return BLE\_ATT\_ERR\_INSUFFICIENT\_RES;}return 0;}static const struct ble\_gatt\_svc\_def s\_services[] = {{.type = BLE\_GATT\_SVC\_TYPE\_PRIMARY,.uuid = BLE\_UUID16\_DECLARE(HRS\_UUID),.characteristics =(struct ble\_gatt\_chr\_def[]){{.uuid = BLE\_UUID16\_DECLARE(HRS\_MEASUREMENT\_UUID),.access\_cb = prv\_measurement\_access,.flags = BLE\_GATT\_CHR\_F\_NOTIFY,.val\_handle = &s\_measurement\_handle,},{.uuid = BLE\_UUID16\_DECLARE(HRS\_BODY\_SENSOR\_LOC\_UUID),.access\_cb = prv\_body\_sensor\_loc\_access,.flags = BLE\_GATT\_CHR\_F\_READ,},{},},},{},};static int prv\_gap\_event(struct ble\_gap\_event \*event, void \*arg);static void prv\_advertise(void) {static const ble\_uuid16\_t uuids[] = {BLE\_UUID16\_INIT(HRS\_UUID),BLE\_UUID16\_INIT(BAS\_UUID),BLE\_UUID16\_INIT(DIS\_UUID),};const char \*name = ble\_svc\_gap\_device\_name();struct ble\_hs\_adv\_fields fields = {.flags = BLE\_HS\_ADV\_F\_DISC\_GEN | BLE\_HS\_ADV\_F\_BREDR\_UNSUP,.name = (const uint8\_t \*)name,.name\_len = strlen(name),.name\_is\_complete = 1,.uuids16 = uuids,.num\_uuids16 = ARRAY\_SIZE(uuids),.uuids16\_is\_complete = 1,.appearance = APPEARANCE\_HR\_SENSOR,.appearance\_is\_present = 1,};const struct ble\_gap\_adv\_params params = {.conn\_mode = BLE\_GAP\_CONN\_MODE\_UND,.disc\_mode = BLE\_GAP\_DISC\_MODE\_GEN,};int rc;rc = ble\_gap\_adv\_set\_fields(&fields);if (rc != 0) {printf("peripheral\_hr: setting the data failed (%d)\n", rc);return;}rc = ble\_gap\_adv\_start(s\_own\_addr\_type, nullptr, BLE\_HS\_FOREVER, &params, prv\_gap\_event, nullptr);if (rc != 0) {printf("peripheral\_hr: advertising failed (%d)\n", rc);return;}printf("peripheral\_hr: advertising as %s\n", name);}/\* Runs on the host thread. \*/static int prv\_gap\_event(struct ble\_gap\_event \*event, void \*arg) {switch (event-\>type) {case BLE\_GAP\_EVENT\_CONNECT:if (event-\>connect.status != 0) {printf("peripheral\_hr: connection failed (%d)\n", event-\>connect.status);prv\_advertise();break;}atomic\_store(&s\_conn\_handle, event-\>connect.conn\_handle);printf("peripheral\_hr: connected\n");break;case BLE\_GAP\_EVENT\_DISCONNECT:atomic\_store(&s\_subscribed, false);atomic\_store(&s\_conn\_handle, BLE\_HS\_CONN\_HANDLE\_NONE);printf("peripheral\_hr: disconnected, reason 0x%03x\n", event-\>disconnect.reason);prv\_advertise();break;case BLE\_GAP\_EVENT\_SUBSCRIBE:if (event-\>subscribe.attr\_handle == s\_measurement\_handle) {atomic\_store(&s\_subscribed, event-\>subscribe.cur\_notify != 0U);printf("peripheral\_hr: measurements %s\n",event-\>subscribe.cur\_notify != 0U ? "on" : "off");}break;default:break;}return 0;}static void prv\_notify\_measurement(uint8\_t bpm) {/\* Flags: an 8-bit value, no sensor contact, energy or RR-interval fields. \*/const uint8\_t value[] = {0x00U, bpm};struct os\_mbuf \*om;int rc;om = ble\_hs\_mbuf\_from\_flat(value, sizeof(value));if (om == nullptr) {return;}/\* Takes the buffer, even when it fails. \*/rc = ble\_gatts\_notify\_custom(atomic\_load(&s\_conn\_handle), s\_measurement\_handle, om);if (rc != 0) {printf("peripheral\_hr: notifying failed (%d)\n", rc);}}static void prv\_on\_sync(void) {pbl\_sem\_give(&s\_synced);}static void prv\_on\_reset(int reason) {printf("peripheral\_hr: host reset, reason %d\n", reason);}int main(void) {uint8\_t addr[6];uint8\_t battery = 100U;int rc;nimble\_port\_init();ble\_hs\_cfg.sync\_cb = prv\_on\_sync;ble\_hs\_cfg.reset\_cb = prv\_on\_reset;/\* NimBLE's own services, then the one of this sample. \*/ble\_svc\_gap\_init();ble\_svc\_gatt\_init();ble\_svc\_bas\_init();ble\_svc\_dis\_init();rc = ble\_gatts\_count\_cfg(s\_services);if (rc == 0) {rc = ble\_gatts\_add\_svcs(s\_services);}if (rc == 0) {rc = ble\_svc\_gap\_device\_name\_set(NAME);}if (rc == 0) {rc = ble\_svc\_dis\_manufacturer\_name\_set("PebbleOS");}if (rc == 0) {rc = ble\_svc\_dis\_model\_number\_set("Heart rate sample");}if (rc != 0) {printf("peripheral\_hr: adding the services failed (%d)\n", rc);return rc;}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) {rc = ble\_hs\_id\_copy\_addr(s\_own\_addr\_type, addr, nullptr);}if (rc != 0) {printf("peripheral\_hr: no address (%d)\n", rc);return rc;}printf("peripheral\_hr: address %02x:%02x:%02x:%02x:%02x:%02x (%s)\n", addr[5], addr[4], addr[3],addr[2], addr[1], addr[0], s\_own\_addr\_type == BLE\_OWN\_ADDR\_PUBLIC ? "public" : "random");(void)ble\_svc\_bas\_battery\_level\_set(battery);prv\_advertise();/\* A simulated rate every second, and a battery that drains every ten. \*/for (unsigned int i = 1U;; i++) {pbl\_thread\_sleep(PBL\_SEC(1));if (atomic\_load(&s\_subscribed)) {prv\_notify\_measurement(60U + (i % 20U));}if (i % 10U == 0U && battery \> 0U) {battery--;/\* Notifies a subscribed peer itself. \*/(void)ble\_svc\_bas\_battery\_level\_set(battery);printf("peripheral\_hr: battery at %u%%\n", battery);}}return 0;}
```

prj.conf

```
CONFIG_CONSOLE=y
CONFIG_CONSOLE_STDOUT=y
CONFIG_NIMBLE=y
CONFIG_NIMBLE_BLE_SVC_BAS=y
CONFIG_NIMBLE_BLE_SVC_DIS=y
CONFIG_NIMBLE_BLE_SVC_DIS_DEFAULT_READ_PERM=0
```

## Output

```
peripheral_hr: address ([0-9a-f]{2}:){5}[0-9a-f]{2} \((public|random)\)
peripheral_hr: advertising as PebbleOS HR
peripheral_hr: battery at 99%
```

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