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

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

This sample brings up the NimBLE host and advertises without accepting connections or scan requests: what a beacon does. The host runs on its own thread, started by `nimble_port_pebble_init()`, and the sample waits for the host to sync with the controller before it uses it.

The advertisement carries the device name and manufacturer data under company ID `0xFFFF`, which the Bluetooth SIG reserves for testing. Its last byte is a counter, updated every second while advertising, so a scanner such as the [Observer](https://developer.repebble.com/pebbleos/docs/samples/third_party/nimble/observer.md) sample sees it change.

On the emulator the controller is a fake one, which accepts every command: the host runs, but nothing goes on the air.

Run:

`pbl samples samples/third_party/nimble/broadcaster`

Boards:

every board

## Sources

src/main.c

```
#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\>/\* Reserved for testing by the Bluetooth SIG. \*/#define COMPANY\_ID 0xFFFFU#define NAME "PebbleOS"#define UPDATES 5static PBL\_SEM\_DEFINE(s\_synced, 0, 1);static void prv\_on\_sync(void) {pbl\_sem\_give(&s\_synced);}static void prv\_on\_reset(int reason) {printf("broadcaster: host reset, reason %d\n", reason);}static int prv\_set\_adv\_data(uint8\_t counter) {const uint8\_t mfg\_data[] = {COMPANY\_ID & 0xFFU, COMPANY\_ID \>\> 8, counter};struct ble\_hs\_adv\_fields fields = {.flags = BLE\_HS\_ADV\_F\_BREDR\_UNSUP,.name = (const uint8\_t \*)NAME,.name\_len = strlen(NAME),.name\_is\_complete = 1,.mfg\_data = mfg\_data,.mfg\_data\_len = sizeof(mfg\_data),};return ble\_gap\_adv\_set\_fields(&fields);}int main(void) {uint8\_t own\_addr\_type;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;nimble\_port\_pebble\_init();/\* The host starts on its own thread, and is ready once in sync with the controller. \*/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) {rc = ble\_hs\_id\_copy\_addr(own\_addr\_type, addr, nullptr);}if (rc != 0) {printf("broadcaster: no address (%d)\n", rc);return rc;}printf("broadcaster: address %02x:%02x:%02x:%02x:%02x:%02x (%s)\n", addr[5], addr[4], addr[3],addr[2], addr[1], addr[0], own\_addr\_type == BLE\_OWN\_ADDR\_PUBLIC ? "public" : "random");rc = prv\_set\_adv\_data(0U);if (rc != 0) {printf("broadcaster: setting the data failed (%d)\n", rc);return rc;}/\* Neither connectable nor discoverable: a scanner only gets to see the data. \*/const struct ble\_gap\_adv\_params params = {.conn\_mode = BLE\_GAP\_CONN\_MODE\_NON,.disc\_mode = BLE\_GAP\_DISC\_MODE\_NON,.itvl\_min = BLE\_GAP\_ADV\_ITVL\_MS(100),.itvl\_max = BLE\_GAP\_ADV\_ITVL\_MS(150),};rc = ble\_gap\_adv\_start(own\_addr\_type, nullptr, BLE\_HS\_FOREVER, &params, nullptr, nullptr);if (rc != 0) {printf("broadcaster: advertising failed (%d)\n", rc);return rc;}printf("broadcaster: advertising\n");/\* The data can change while advertising. \*/for (uint8\_t i = 1U; i \<= UPDATES; i++) {pbl\_thread\_sleep(PBL\_SEC(1));rc = prv\_set\_adv\_data(i);if (rc != 0) {printf("broadcaster: updating the data failed (%d)\n", rc);return rc;}printf("broadcaster: counter %u\n", i);}(void)ble\_gap\_adv\_stop();printf("Sample done\n");return 0;}
```

## Output

```
broadcaster: address ([0-9a-f]{2}:){5}[0-9a-f]{2} \((public|random)\)
broadcaster: advertising
broadcaster: counter 5
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/broadcaster.md](https://github.com/coredevices/pebbleos/blob/ac576058737718d2de0783896789b836ea09757d/docs/samples/third_party/nimble/broadcaster.md).
