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

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

This sample only lets a peer in with LE Secure Connections and a passkey: security mode 1, level 4. Its `prj.conf` turns off legacy pairing, asks for bonding and protection against a man in the middle, and declares that the watch can display but not type, so the peer types the passkey the sample prints on the console.

Its one characteristic is only readable over a link encrypted with an authenticated key. The sample also asks a peer to secure the link as soon as it connects. A bonded peer that has lost its keys is forgotten, and pairs again.

Bonds are kept by NimBLE’s config store, which PebbleOS saves in a settings file: the sample mounts the filesystem first, and lists the bonded peers at boot. The board overlays give each board a filesystem partition.

Run:

`pbl samples samples/third_party/nimble/peripheral_sc_only`

Boards:

every board

## Sources

src/main.c

```
#include \<stdint.h\>#include \<stdio.h\>#include \<string.h\>#include \<pbl/fs/fs.h\>#include \<pbl/fs/pfs.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\>#include \<services/gap/ble\_svc\_gap.h\>#include \<services/gatt/ble\_svc\_gatt.h\>#define NAME "PebbleOS SC"#define MAX\_BONDS 4/\* 5e1a0100-7c3b-4f0e-9d2a-6b8c1f4e0a37, and its characteristic. \*/#define SAMPLE\_UUID(n) \ BLE\_UUID128\_INIT(0x37, 0x0a, 0x4e, 0x1f, 0x8c, 0x6b, 0x2a, 0x9d, 0x0e, 0x4f, 0x3b, 0x7c, n, \ 0x01, 0x1a, 0x5e)static const ble\_uuid128\_t s\_svc\_uuid = SAMPLE\_UUID(0x00);static const ble\_uuid128\_t s\_secret\_uuid = SAMPLE\_UUID(0x01);static struct pbl\_fs\_mount s\_mount = {.mnt\_point = "/", .ops = &pbl\_fs\_pfs\_ops};static PBL\_SEM\_DEFINE(s\_synced, 0, 1);static uint8\_t s\_own\_addr\_type;static void prv\_print\_addr(const char \*what, const ble\_addr\_t \*addr) {const uint8\_t \*a = addr-\>val;printf("peripheral\_sc\_only: %s %02x:%02x:%02x:%02x:%02x:%02x (%s)\n", what, a[5], a[4], a[3],a[2], a[1], a[0], addr-\>type == BLE\_ADDR\_PUBLIC ? "public" : "random");}/\* Only readable over a link encrypted with an authenticated key: reading it starts pairing. \*/static int prv\_secret\_access(uint16\_t conn\_handle, uint16\_t attr\_handle,struct ble\_gatt\_access\_ctxt \*ctxt, void \*arg) {static const char secret[] = "Only for bonded peers";if (os\_mbuf\_append(ctxt-\>om, secret, strlen(secret)) != 0) {return BLE\_ATT\_ERR\_INSUFFICIENT\_RES;}printf("peripheral\_sc\_only: secret read\n");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\_secret\_uuid.u,.access\_cb = prv\_secret\_access,.flags = BLE\_GATT\_CHR\_F\_READ | BLE\_GATT\_CHR\_F\_READ\_ENC | BLE\_GATT\_CHR\_F\_READ\_AUTHEN,},{},},},{},};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();/\* The 128-bit UUID leaves no room for the name: it goes in the scan response. \*/const struct ble\_hs\_adv\_fields fields = {.flags = BLE\_HS\_ADV\_F\_DISC\_GEN | BLE\_HS\_ADV\_F\_BREDR\_UNSUP,.uuids128 = &s\_svc\_uuid,.num\_uuids128 = 1,.uuids128\_is\_complete = 1,};const struct ble\_hs\_adv\_fields rsp\_fields = {.name = (const uint8\_t \*)name,.name\_len = strlen(name),.name\_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) {rc = ble\_gap\_adv\_rsp\_set\_fields(&rsp\_fields);}if (rc == 0) {rc = ble\_gap\_adv\_start(s\_own\_addr\_type, nullptr, BLE\_HS\_FOREVER, &params, prv\_gap\_event,nullptr);}if (rc != 0) {printf("peripheral\_sc\_only: advertising failed (%d)\n", rc);return;}printf("peripheral\_sc\_only: advertising as %s\n", name);}/\* The peer types in the passkey shown here. \*/static void prv\_show\_passkey(uint16\_t conn\_handle) {struct ble\_sm\_io io = {.action = BLE\_SM\_IOACT\_DISP};uint32\_t value;int rc;rc = ble\_hs\_hci\_rand(&value, sizeof(value));if (rc != 0) {printf("peripheral\_sc\_only: no random number (%d)\n", rc);return;}io.passkey = value % 1000000U;printf("peripheral\_sc\_only: passkey %06u\n", (unsigned int)io.passkey);rc = ble\_sm\_inject\_io(conn\_handle, &io);if (rc != 0) {printf("peripheral\_sc\_only: passkey refused (%d)\n", rc);}}static void prv\_print\_security(uint16\_t conn\_handle) {struct ble\_gap\_conn\_desc desc;if (ble\_gap\_conn\_find(conn\_handle, &desc) != 0) {return;}printf("peripheral\_sc\_only: encrypted, %s, %s\n",desc.sec\_state.authenticated ? "authenticated" : "unauthenticated",desc.sec\_state.bonded ? "bonded" : "not bonded");}/\* Runs on the host thread. \*/static int prv\_gap\_event(struct ble\_gap\_event \*event, void \*arg) {struct ble\_gap\_conn\_desc desc;switch (event-\>type) {case BLE\_GAP\_EVENT\_CONNECT:if (event-\>connect.status != 0) {printf("peripheral\_sc\_only: connection failed (%d)\n", event-\>connect.status);prv\_advertise();break;}printf("peripheral\_sc\_only: connected\n");/\* Ask the peer to pair, or to encrypt with the keys of a bond. \*/(void)ble\_gap\_security\_initiate(event-\>connect.conn\_handle);break;case BLE\_GAP\_EVENT\_DISCONNECT:printf("peripheral\_sc\_only: disconnected, reason 0x%03x\n", event-\>disconnect.reason);prv\_advertise();break;case BLE\_GAP\_EVENT\_PASSKEY\_ACTION:if (event-\>passkey.params.action == BLE\_SM\_IOACT\_DISP) {prv\_show\_passkey(event-\>passkey.conn\_handle);}break;case BLE\_GAP\_EVENT\_ENC\_CHANGE:if (event-\>enc\_change.status != 0) {printf("peripheral\_sc\_only: encryption failed (%d)\n", event-\>enc\_change.status);break;}prv\_print\_security(event-\>enc\_change.conn\_handle);break;case BLE\_GAP\_EVENT\_REPEAT\_PAIRING:/\* A bonded peer that lost its keys: forget ours, and pair again. \*/if (ble\_gap\_conn\_find(event-\>repeat\_pairing.conn\_handle, &desc) == 0) {prv\_print\_addr("pairing again with", &desc.peer\_id\_addr);(void)ble\_store\_util\_delete\_peer(&desc.peer\_id\_addr);}return BLE\_GAP\_REPEAT\_PAIRING\_RETRY;default:break;}return 0;}static void prv\_on\_sync(void) {pbl\_sem\_give(&s\_synced);}static void prv\_on\_reset(int reason) {printf("peripheral\_sc\_only: host reset, reason %d\n", reason);}int main(void) {ble\_addr\_t bonds[MAX\_BONDS];int num\_bonds;int rc;/\* The store keeps the bonds in a settings file. \*/rc = pbl\_fs\_mount(&s\_mount);if (rc != 0) {printf("peripheral\_sc\_only: mount failed (%d)\n", rc);return rc;}nimble\_port\_init();ble\_hs\_cfg.sync\_cb = prv\_on\_sync;ble\_hs\_cfg.reset\_cb = prv\_on\_reset;/\* A new bond replaces the oldest one when the store is full. \*/ble\_hs\_cfg.store\_status\_cb = ble\_store\_util\_status\_rr;ble\_store\_config\_init();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\_sc\_only: adding the services failed (%d)\n", rc);return rc;}rc = ble\_store\_util\_bonded\_peers(bonds, &num\_bonds, MAX\_BONDS);if (rc != 0) {printf("peripheral\_sc\_only: reading the bonds failed (%d)\n", rc);return rc;}printf("peripheral\_sc\_only: %d bonded peer%s\n", num\_bonds, num\_bonds == 1 ? "" : "s");for (int i = 0; i \< num\_bonds; i++) {prv\_print\_addr("bonded with", &bonds[i]);}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("peripheral\_sc\_only: no address (%d)\n", rc);return rc;}prv\_advertise();return 0;}
```

prj.conf

```
CONFIG_CONSOLE=y
CONFIG_CONSOLE_STDOUT=y
CONFIG_FLASH=y
CONFIG_FS=y
CONFIG_SETTINGS=y
CONFIG_NIMBLE=y
CONFIG_NIMBLE_STORE_CONFIG=y
CONFIG_NIMBLE_BLE_SM_LEGACY=n
CONFIG_NIMBLE_BLE_SM_SC=y
CONFIG_NIMBLE_BLE_SM_SC_ONLY=y
CONFIG_NIMBLE_BLE_SM_BONDING=y
CONFIG_NIMBLE_BLE_SM_MITM=y
CONFIG_NIMBLE_BLE_SM_IO_CAP_DISPLAY_ONLY=y
CONFIG_NIMBLE_BLE_SM_OUR_KEY_DIST=3
CONFIG_NIMBLE_BLE_SM_THEIR_KEY_DIST=3
```

## Output

```
peripheral_sc_only: \d+ bonded peers?
peripheral_sc_only: advertising as PebbleOS SC
```

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