Bluetooth Low Energy for boards: advertise, scan, a small GATT server with write events and notifications, and central connect, read and write, with a simulated radio on a computer.
import hw.ble
hw.ble is Bluetooth Low Energy for boards. A device can be a peripheral (advertise, serve a small GATT table, receive writes from a phone and notify it) or a central (scan, connect to a device, read and write its characteristics). Calls return plain values, so a failure is an answer you check.
Where it runs
What answers the calls
A computer
A deterministic fake radio in memory: SimAddBleDevice fills the air, SimInjectWrite plays a central writing to you and SimSetRemoteValue gives a remote device something to read.
An ESP32-C3 through ESP-IDF
The salam_rt_ble_* hooks implemented in idf_radio.c on the NimBLE host. See the ESP32-C3 section of docs/FREESTANDING.md. This path has not been run on a device.
A board without a radio
The runtime's default hooks answer "unsupported", so Init returns false and the program still links.
Things to know:
•
No hidden threads. Writes from a central are queued; check Poll in your own loop and fetch each one with Take.
•
Add services first. On a board the GATT table is frozen when the radio starts, so call AddService and AddCharacteristic before the first Advertise, Scan or Connect.
•
Bytes are Vector<u8>. Values, events and remote reads all use it, up to max_value bytes.
•
Properties are flags. Combine prop_read, prop_write and prop_notify with |.
•
Read fields with functions.DeviceName, DeviceAddress, DeviceRssi, ServiceOk, CharacteristicOk, EventUuid, EventValue and EventOk read the records.
A peripheral that receives writes
Add a service with one writable characteristic, advertise, then wait for a central to write. The example plays the central with SimInjectWrite.
Prepares the Bluetooth controller and sets the device name (1 to 29 bytes). Returns true when ready and false on a board with no radio or with a bad name. Add services before the first Advertise, Scan or Connect.
Listens for advertising devices for timeout_ms milliseconds and returns up to max_scan of them as a Vector of Device. A zero timeout or a missing Init gives an empty Vector.
The example for this item is shown with the ones above: hw-ble-Scan.salam
Adds a primary service with the given UUID to the GATT table. On a board this must happen before the first Advertise, Scan or Connect. A bad UUID or a missing Init returns a Service for which ServiceOk is false.
The example for this item is shown with the ones above: hw-ble-Gatt.salam
Adds a characteristic to a service with the prop_ flags and an initial value of up to max_value bytes. A bad UUID, zero or unknown properties or a failed service return a Characteristic for which CharacteristicOk is false.
The example for this item is shown with the ones above: hw-ble-Gatt.salam
Reads a characteristic of the connected device by UUID and returns its bytes. The result is empty when not connected, for a bad UUID or an unknown characteristic.
Pretends a central wrote bytes to a characteristic. Only characteristics with the WRITE property accept it. The value changes and an event is queued; returns whether that happened.
The example for this item is shown with the ones above: hw-ble-Gatt.salam