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

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

This sample scans for five seconds and lists every device it hears advertising once, with its address, signal strength and name, when the advertisement has one. The scan is passive: it never sends a scan request, so it only sees what devices broadcast on their own.

The scan events come on the host thread; the sample waits on a semaphore for the one that ends it. On the emulator, the fake controller never reports a device.

Run:

`pbl samples samples/third_party/nimble/observer`

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 SCAN\_MS 5000#define MAX\_DEVICES 32#define NAME\_LEN 32static PBL\_SEM\_DEFINE(s\_synced, 0, 1);static PBL\_SEM\_DEFINE(s\_done, 0, 1);static ble\_addr\_t s\_devices[MAX\_DEVICES];static size\_t s\_num\_devices;static void prv\_on\_sync(void) {pbl\_sem\_give(&s\_synced);}static void prv\_on\_reset(int reason) {printf("observer: host reset, reason %d\n", reason);}static bool prv\_seen(const ble\_addr\_t \*addr) {for (size\_t i = 0U; i \< s\_num\_devices; i++) {if (ble\_addr\_cmp(&s\_devices[i], addr) == 0) {return true;}}return false;}static void prv\_report(const struct ble\_gap\_disc\_desc \*desc) {struct ble\_hs\_adv\_fields fields;char name[NAME\_LEN] = "";const uint8\_t \*a = desc-\>addr.val;if (ble\_hs\_adv\_parse\_fields(&fields, desc-\>data, desc-\>length\_data) == 0 &&fields.name != nullptr) {size\_t len = fields.name\_len \< sizeof(name) - 1U ? fields.name\_len : sizeof(name) - 1U;memcpy(name, fields.name, len);name[len] = '\0';}printf("observer: %02x:%02x:%02x:%02x:%02x:%02x (%s), %d dBm%s%s\n", a[5], a[4], a[3], a[2], a[1],a[0], desc-\>addr.type == BLE\_ADDR\_PUBLIC ? "public" : "random", desc-\>rssi,name[0] != '\0' ? ", " : "", 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\_DISC:if (s\_num\_devices \< MAX\_DEVICES && !prv\_seen(&event-\>disc.addr)) {s\_devices[s\_num\_devices++] = event-\>disc.addr;prv\_report(&event-\>disc);}break;case BLE\_GAP\_EVENT\_DISC\_COMPLETE:pbl\_sem\_give(&s\_done);break;default:break;}return 0;}int main(void) {uint8\_t own\_addr\_type;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, &own\_addr\_type);}if (rc != 0) {printf("observer: no address (%d)\n", rc);return rc;}/\* Passive: listen to advertisements without asking for scan responses. The controller drops \* the duplicates, as far as its own list of them goes. \*/const struct ble\_gap\_disc\_params params = {.passive = 1,.filter\_duplicates = 1,};rc = ble\_gap\_disc(own\_addr\_type, SCAN\_MS, &params, prv\_gap\_event, nullptr);if (rc != 0) {printf("observer: scanning failed (%d)\n", rc);return rc;}printf("observer: scanning for %d seconds\n", SCAN\_MS / 1000);pbl\_sem\_take(&s\_done, PBL\_FOREVER);printf("observer: %u device%s found\n", (unsigned int)s\_num\_devices,s\_num\_devices == 1U ? "" : "s");printf("Sample done\n");return 0;}
```

## Output

```
observer: scanning for 5 seconds
observer: \d+ devices? found
Sample done
```

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