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

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

This sample is a connectable peripheral with a GATT service of its own, under a 128-bit UUID, that it advertises with its name. The service has two characteristics:

- A message of up to 20 bytes, to read and write. The sample prints it when a peer writes it.

- A counter the main thread increments every second, to read or subscribe to. While a peer is subscribed, the main thread calls `ble_gatts_chr_updated()` every second, and the host reads the new value through the characteristic’s access callback to notify it.

The services are registered before the host starts, as NimBLE requires. The sample prints the connections, subscriptions and MTU exchanges it sees, and advertises again after a disconnection. Any GATT client, such as a phone app for Bluetooth Low Energy development, can connect to it.

Run:

`pbl samples samples/third_party/nimble/peripheral`

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/gap/ble\_svc\_gap.h\>#include \<services/gatt/ble\_svc\_gatt.h\>#define NAME "PebbleOS"#define MESSAGE\_MAX 20U/\* 5e1a0000-7c3b-4f0e-9d2a-6b8c1f4e0a37, and its characteristics after it. \*/#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 uint8\_t s\_own\_addr\_type;/\* Only the host thread touches the message. \*/static char s\_message[MESSAGE\_MAX + 1U] = "Hello";static uint16\_t s\_counter\_handle;static atomic\_uint s\_counter;static atomic\_bool s\_subscribed;static int prv\_message\_access(uint16\_t conn\_handle, uint16\_t attr\_handle,struct ble\_gatt\_access\_ctxt \*ctxt, void \*arg) {uint16\_t len;switch (ctxt-\>op) {case BLE\_GATT\_ACCESS\_OP\_READ\_CHR:if (os\_mbuf\_append(ctxt-\>om, s\_message, strlen(s\_message)) != 0) {return BLE\_ATT\_ERR\_INSUFFICIENT\_RES;}return 0;case BLE\_GATT\_ACCESS\_OP\_WRITE\_CHR:if (OS\_MBUF\_PKTLEN(ctxt-\>om) \> MESSAGE\_MAX) {return BLE\_ATT\_ERR\_INVALID\_ATTR\_VALUE\_LEN;}if (ble\_hs\_mbuf\_to\_flat(ctxt-\>om, s\_message, MESSAGE\_MAX, &len) != 0) {return BLE\_ATT\_ERR\_UNLIKELY;}s\_message[len] = '\0';printf("peripheral: message is now \"%s\"\n", s\_message);return 0;default:return BLE\_ATT\_ERR\_UNLIKELY;}}/\* Also called when notifying, for the value to send. \*/static int prv\_counter\_access(uint16\_t conn\_handle, uint16\_t attr\_handle,struct ble\_gatt\_access\_ctxt \*ctxt, void \*arg) {uint8\_t value[4];put\_le32(value, atomic\_load(&s\_counter));if (os\_mbuf\_append(ctxt-\>om, value, sizeof(value)) != 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 = &s\_svc\_uuid.u,.characteristics =(struct ble\_gatt\_chr\_def[]){{.uuid = &s\_message\_uuid.u,.access\_cb = prv\_message\_access,.flags = BLE\_GATT\_CHR\_F\_READ | BLE\_GATT\_CHR\_F\_WRITE,},{.uuid = &s\_counter\_uuid.u,.access\_cb = prv\_counter\_access,.flags = BLE\_GATT\_CHR\_F\_READ | BLE\_GATT\_CHR\_F\_NOTIFY,.val\_handle = &s\_counter\_handle,},{},},},{},};static int prv\_gap\_event(struct ble\_gap\_event \*event, void \*arg);static void prv\_advertise(void) {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,.uuids128 = &s\_svc\_uuid,.num\_uuids128 = 1,.uuids128\_is\_complete = 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: 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: advertising failed (%d)\n", rc);return;}printf("peripheral: 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: connection failed (%d)\n", event-\>connect.status);prv\_advertise();break;}printf("peripheral: connected, handle %u\n", event-\>connect.conn\_handle);break;case BLE\_GAP\_EVENT\_DISCONNECT:printf("peripheral: disconnected, reason 0x%03x\n", event-\>disconnect.reason);atomic\_store(&s\_subscribed, false);prv\_advertise();break;case BLE\_GAP\_EVENT\_SUBSCRIBE:if (event-\>subscribe.attr\_handle == s\_counter\_handle) {atomic\_store(&s\_subscribed, event-\>subscribe.cur\_notify != 0U);printf("peripheral: counter notifications %s\n",event-\>subscribe.cur\_notify != 0U ? "on" : "off");}break;case BLE\_GAP\_EVENT\_MTU:printf("peripheral: MTU %u\n", event-\>mtu.value);break;default:break;}return 0;}static void prv\_on\_sync(void) {pbl\_sem\_give(&s\_synced);}static void prv\_on\_reset(int reason) {printf("peripheral: host reset, reason %d\n", reason);}int main(void) {uint8\_t addr[6];int rc;nimble\_port\_init();ble\_hs\_cfg.sync\_cb = prv\_on\_sync;ble\_hs\_cfg.reset\_cb = prv\_on\_reset;/\* The services go in before the host starts. \*/ble\_svc\_gap\_init();ble\_svc\_gatt\_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) {printf("peripheral: 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: no address (%d)\n", rc);return rc;}printf("peripheral: 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");prv\_advertise();/\* Count the seconds, and notify a subscribed peer of each one. \*/while (true) {pbl\_thread\_sleep(PBL\_SEC(1));atomic\_fetch\_add(&s\_counter, 1U);if (atomic\_load(&s\_subscribed)) {ble\_gatts\_chr\_updated(s\_counter\_handle);}}return 0;}
```

## Output

```
peripheral: address ([0-9a-f]{2}:){5}[0-9a-f]{2} \((public|random)\)
peripheral: advertising as PebbleOS
```

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