Driving blth without hardware #
blth is an HCI host, so it needs a controller to talk H4 to, and macOS gives
no raw HCI access to the Mac's built-in radio. This example runs a software
controller with Bumble instead. blth
reaches it over a TCP socket, and you can scan, connect, read GATT and pair
without a dongle.
virtual_controller.py puts two controllers on one simulated radio link:
- a connectable, pairable peripheral FakeSensor (
F0:F1:F2:F3:F4:F5) with a GATT service (0x1815) holding one readable/writable characteristic; - a bare controller exposed over TCP, which
blthdrives with--tcp.
Run it #
Start the controller. The venv keeps Bumble out of your system Python:
python3 -m venv .venv
.venv/bin/pip install -r requirements.txt
.venv/bin/python3 virtual_controller.py
Then, in another terminal:
blth scan --tcp 127.0.0.1:9000
blth gatt --tcp 127.0.0.1:9000 --peer F0:F1:F2:F3:F4:F5 --peer-random
blth pair --tcp 127.0.0.1:9000 --peer F0:F1:F2:F3:F4:F5 --peer-random \
--random C0:11:22:33:44:55
scan should list FakeSensor with its name and RSSI. gatt connects,
discovers the services and characteristics, and reads a value. pair connects,
pairs with LE Secure Connections in Just Works mode, and prints the bond.
To profile the data path of a connection, run gatt under obs:
obs run --out /tmp/blth_gatt -- blth gatt --tcp 127.0.0.1:9000 \
--peer F0:F1:F2:F3:F4:F5 --peer-random
obs report /tmp/blth_gatt
Address flags #
A BLE device uses either the public address its manufacturer assigned or a
random one. A static random address is 48 random bits whose two top bits are
11. The device picks it at power-up and keeps it until it next loses power.
Private random addresses change on a timer instead, every 15 minutes by the
specification's recommendation, so that the device cannot be tracked (Core
Specification, Vol 6, Part B, 1.3.2).
Bumble's LocalLink sends every LE packet from the sending controller's random
address, so a peer on that link can only be reached as a random-address device.
For that reason the peripheral advertises a static random address and every
command passes --peer-random.
pair also needs --random ADDR, to make our own side a random-address device
as well. In LE Secure Connections the two sides agree on a key by
elliptic-curve Diffie-Hellman over P-256. Each then sends a DHKey check, an
AES-CMAC over the derived key, the nonces they exchanged, the pairing
parameters and both device addresses with their type (the function f6, Vol 3,
Part H, 2.2.8). When one side feeds f6 an address different from the one the
link carries, the two checks disagree and pairing fails. A real controller
keeps one address throughout, so none of these flags are needed with one.
Real hardware #
To scan over the air from a Mac, plug in an HCI controller that speaks over a
USB serial port. An nRF52840 running Zephyr's hci_uart works, and shows up as
/dev/cu.usbmodem*. Replace --tcp with --device /dev/cu.usbmodemXXXX --baud 1000000. An ordinary USB Bluetooth dongle does not work, because macOS takes it
over as the system adapter.
On a Linux host (e.g. a Raspberry Pi CM5) #
Linux gives raw HCI access, so blth can drive the machine's own radio with
--hci N, which opens an HCI user channel on hci<N>. Take the controller down
first so that no other program manages it:
sudo hciconfig hci0 down # or: sudo btmgmt --index 0 power off
sudo blth serve --hci 0 --name my-cm5
serve advertises a demo GATT server and prints a line each time a client
connects, writes or subscribes. You can then scan and connect from a phone or a
Mac with any BLE app, or with blth scan on a Mac that has an external
controller. scan, gatt and pair accept --hci as well. On a CM5, the
external antenna kit,
once enabled in the firmware configuration, gives the advertisement a useful
range.