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

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

This sample is the other end of the [Peripheral](https://developer.repebble.com/pebbleos/docs/samples/third_party/nimble/peripheral.md) sample. It scans until it hears a device advertising that sample’s service UUID, connects to it, and works through its GATT service as a client:

- It discovers the service by UUID, then its characteristics.

- It reads the message, writes its own, and reads it back.

- It finds the CCCD of the counter among the descriptors that follow it, and writes it to subscribe, then prints three notifications and disconnects.

Each step starts the next from its completion callback, on the host thread; the main thread only waits for the end. On the emulator, nothing is ever found, so the sample keeps scanning.

Run:

`pbl samples samples/third_party/nimble/central`

Boards:

every board

## Sources

src/main.c

```
#include \<stdint.h\>#include \<stdio.h\>#include \<string.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 CCCD\_UUID 0x2902U#define MESSAGE "Hello from central"#define MESSAGE\_MAX 20U#define NOTIFICATIONS 3/\* The service of the peripheral sample, and its characteristics. \*/#define SAMPLE\_UUID(n) \ BLE\_UUID128\_INIT(0x37, 0x0a, 0x4e, 0x1f, 0x8c, 0x6b, 0x2a, 0x9d, 0x0e, 0x4f, 0x3b, 0x7c, n, \ 0x00, 0x1a, 0x5e)static const ble\_uuid128\_t s\_svc\_uuid = SAMPLE\_UUID(0x00);static const ble\_uuid128\_t s\_message\_uuid = SAMPLE\_UUID(0x01);static const ble\_uuid128\_t s\_counter\_uuid = SAMPLE\_UUID(0x02);static 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\_message\_handle;static uint16\_t s\_counter\_handle;static uint16\_t s\_cccd\_handle;static bool s\_written;static int s\_notifications;static int prv\_gap\_event(struct ble\_gap\_event \*event, void \*arg);static void prv\_scan(void) {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: scanning failed (%d)\n", rc);return;}printf("central: scanning for the peripheral sample\n");}static bool prv\_is\_sample(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\_uuids128; i++) {if (ble\_uuid\_cmp(&fields.uuids128[i].u, &s\_svc\_uuid.u) == 0) {return true;}}return false;}static void prv\_fail(uint16\_t conn\_handle, const char \*what, int rc) {printf("central: %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: subscribed to the counter\n");return 0;}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 (s\_cccd\_handle == 0U && 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\_written(uint16\_t conn\_handle, const struct ble\_gatt\_error \*error,struct ble\_gatt\_attr \*attr, void \*arg);/\* Read before and after writing it; then on to the counter. \*/static int prv\_on\_read(uint16\_t conn\_handle, const struct ble\_gatt\_error \*error,struct ble\_gatt\_attr \*attr, void \*arg) {char message[MESSAGE\_MAX + 1U];uint16\_t len = 0U;int rc;if (error-\>status != 0) {prv\_fail(conn\_handle, "reading the message", error-\>status);return 0;}(void)ble\_hs\_mbuf\_to\_flat(attr-\>om, message, MESSAGE\_MAX, &len);message[len] = '\0';printf("central: the message is \"%s\"\n", message);if (!s\_written) {s\_written = true;rc = ble\_gattc\_write\_flat(conn\_handle, s\_message\_handle, MESSAGE, strlen(MESSAGE),prv\_on\_written, nullptr);if (rc != 0) {prv\_fail(conn\_handle, "writing the message", rc);}return 0;}/\* The CCCD is among the descriptors after the counter's value. \*/rc =ble\_gattc\_disc\_all\_dscs(conn\_handle, s\_counter\_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\_written(uint16\_t conn\_handle, const struct ble\_gatt\_error \*error,struct ble\_gatt\_attr \*attr, void \*arg) {int rc;if (error-\>status != 0) {prv\_fail(conn\_handle, "writing the message", error-\>status);return 0;}rc = ble\_gattc\_read(conn\_handle, s\_message\_handle, prv\_on\_read, nullptr);if (rc != 0) {prv\_fail(conn\_handle, "reading the message", 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) {if (ble\_uuid\_cmp(&chr-\>uuid.u, &s\_message\_uuid.u) == 0) {s\_message\_handle = chr-\>val\_handle;} else if (ble\_uuid\_cmp(&chr-\>uuid.u, &s\_counter\_uuid.u) == 0) {s\_counter\_handle = chr-\>val\_handle;}return 0;}if (error-\>status != BLE\_HS\_EDONE || s\_message\_handle == 0U || s\_counter\_handle == 0U) {prv\_fail(conn\_handle, "finding the characteristics", error-\>status);return 0;}rc = ble\_gattc\_read(conn\_handle, s\_message\_handle, prv\_on\_read, nullptr);if (rc != 0) {prv\_fail(conn\_handle, "reading the message", 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\_all\_chrs(conn\_handle, service-\>start\_handle, service-\>end\_handle,prv\_on\_chr, nullptr);if (rc != 0) {prv\_fail(conn\_handle, "finding the characteristics", 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\_counter(uint16\_t conn\_handle, struct os\_mbuf \*om) {uint8\_t value[4];uint16\_t len = 0U;(void)ble\_hs\_mbuf\_to\_flat(om, value, sizeof(value), &len);if (len != sizeof(value)) {return;}printf("central: counter %u\n", (unsigned int)get\_le32(value));if (++s\_notifications == NOTIFICATIONS) {(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\_is\_sample(&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: connecting failed (%d)\n", rc);prv\_scan();}break;case BLE\_GAP\_EVENT\_CONNECT:if (event-\>connect.status != 0) {printf("central: connection failed (%d)\n", event-\>connect.status);prv\_scan();break;}printf("central: connected\n");s\_svc\_end\_handle = 0U;s\_message\_handle = 0U;s\_counter\_handle = 0U;s\_cccd\_handle = 0U;s\_written = false;rc = ble\_gattc\_disc\_svc\_by\_uuid(event-\>connect.conn\_handle, &s\_svc\_uuid.u, 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\_counter\_handle) {prv\_on\_counter(event-\>notify\_rx.conn\_handle, event-\>notify\_rx.om);}break;case BLE\_GAP\_EVENT\_DISCONNECT:printf("central: disconnected, reason 0x%03x\n", event-\>disconnect.reason);if (s\_notifications \>= NOTIFICATIONS) {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: 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: no address (%d)\n", rc);return rc;}prv\_scan();pbl\_sem\_take(&s\_done, PBL\_FOREVER);printf("Sample done\n");return 0;}
```

## Output

```
central: scanning for the peripheral sample
```

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.md](https://github.com/coredevices/pebbleos/blob/ac576058737718d2de0783896789b836ea09757d/docs/samples/third_party/nimble/central.md).
