Files
btle-proximity/README.md
T
Bastian Mäuser c25ccde58e Probe for 10s and log its outcome before locking
The phone's beacon app pauses advertising for stretches while in range,
which is what the 3s probe could not bridge. Give the controller a 10s
window to find the phone, and log probe outcomes at INFO/WARN so the
journal explains every lock decision.
2026-11-05 14:57:48 +01:00

145 lines
6.3 KiB
Markdown

# btle-proximity
A Linux daemon that locks your desktop session when your phone walks away from
Bluetooth range — and barely spends any power doing it.
Written in Rust, talks to BlueZ over D-Bus, locks via logind. No unlock: the
phone returning only rearms monitoring.
## How it works
BLE scanning runs in the Bluetooth controller firmware, so the CPU sleeps;
the real power lever is the radio duty cycle (`scan_window / scan_interval`).
The daemon adapts it to a small state machine:
```
fresh + strong sighting
Bootstrapping ───────────────► Present low duty (~2%: 100ms/5s)
│ never locks │
│ │ stale or weak signal
▼ ▼
(idle) ◄── sighting ──── Suspect burst (200ms/1s)
│
│ still no good evidence
│ for missing_after_secs
▼
Locked ───────► lock via logind,
(waits for the phone at 100ms/30s)
```
- **Present**: fresh advertisement updates with RSSI above `rssi_lock`, or an
active connection to the device. If updates go quiet — a stationary phone
produces identical RSSI values, which BlueZ dedupes — the radio steps up to
the Suspect duty after half of `presence_timeout_secs`, without leaving
Present.
- **Suspect**: high-duty re-check so a few dropped beacons don't lock you out.
- **Bootstrapping**: the daemon never locks before it has seen the phone at
least once, so you can boot with the phone in another room.
- **Locked**: no unlock; the next sighting just returns to Present.
Locking goes through `loginctl lock-session` (works with GNOME, KDE, Sway,
...), configurable via `lock_command`. Before locking, the daemon attempts a
short (10s) LE connection to the paired phone: a phone in range accepts it,
which cancels the lock — silence in the advertisement stream alone is not
proof of absence.
## Requirements
- Linux with BlueZ ≥ 5.50 (`bluetoothd` running, adapter powered)
- Rust ≥ 1.85 (`rust-version` is declared in Cargo.toml; the resolver
automatically selects dependency versions compatible with the oldest
supported toolchain)
- The phone must be **paired** with the desktop. Pairing gives the daemon a
stable identity address (BlueZ resolves the phone's rotating random address
against the bonding keys) and is the foundation for authenticated GATT in
the future.
- The phone must **advertise regularly**. A smartphone doesn't advertise on
its own — run a BLE beacon app (e.g. iBeacon/Eddystone beacons from
F-Droid/Play Store, ~1 Hz). This is the single most important reliability
factor; OS-generated advertisements (Fast Pair, Continuity) are too
intermittent to rely on.
## Build & install
```sh
cargo install --path . # installs to ~/.cargo/bin
mkdir -p ~/.config/btle-proximity
cp config.toml.example ~/.config/btle-proximity/config.toml
btle-proximity list-paired # pick your phone's MAC, edit config.toml
```
If you get `command not found`, `~/.cargo/bin` isn't on your PATH (distro
packages of Rust don't set it up):
```sh
echo 'export PATH="$HOME/.cargo/bin:$PATH"' >> ~/.zshrc # or ~/.bashrc
source ~/.zshrc
```
Test without locking anything, walk around with the phone and watch:
```sh
RUST_LOG=debug btle-proximity --dry-run
```
Tune `rssi_lock` / `presence_timeout_secs` / `missing_after_secs` to your
room. Then install the user service (runs inside your session, Type=notify):
```sh
cp systemd/btle-proximity.service ~/.config/systemd/user/
systemctl --user daemon-reload
systemctl --user enable --now btle-proximity
```
## Configuration
See `config.toml.example`; every knob is documented there. Highlights:
| key | default | meaning |
| ---------------------- | ------- | ------------------------------------------ |
| `device_mac` | — | identity MAC of the paired phone |
| `missing_after_secs` | 30 | absence confirmation before locking |
| `presence_timeout_secs`| 15 | how fresh an update must be to count |
| `rssi_floor` / `rssi_lock` | -90/-80 | discovery floor / out-of-range threshold |
| `duty.*` | see file| per-state scan window/interval |
## Power notes
- Discovery is duty-cycled by toggling BlueZ discovery: radio on ~2% of the
time while the phone is present, burst when absent is suspected, near-idle
while locked.
- Roadmap, in order of power payoff:
1. **AdvertisementMonitor** (BlueZ `org.bluez.AdvertisementMonitor1`,
kernel-offloaded on supported controllers): pattern-match the phone's
beacon payload in the controller, wake the host only on Found/Lost
transitions. Needs BlueZ ≥ 5.65 and `Experimental = true` in
`/etc/bluetooth/main.conf` on many distros.
2. mgmt-socket based scanning with exact controller scan parameters.
## Known trade-offs (v1)
- Presence is inferred from advertisement property updates. BlueZ dedupes
identical RSSI values (its property cache suppresses unchanged values), so
a stationary phone can go quiet in the event stream while in range. Two
countermeasures: the radio escalates duty while updates are stalled (see
Present above), and the lock decision is gated on a 10s LE connect probe.
`--dry-run` on your hardware remains the arbiter. The AdvertisementMonitor
roadmap removes this class of problem.
- While the screen is locked the daemon keeps low-duty scanning to notice
the phone's return (rearming only; it never unlocks).
## Roadmap: unlock with the phone
Planned v2 (companion app on the phone, GATT service on the desktop):
1. Daemon exposes an encrypted GATT service (LE Secure Connections bonding).
2. On approach, the desktop sends the phone a fresh nonce.
3. The phone asks for fingerprint (Android `BiometricPrompt`), signs the
nonce with a Keystore-bound key whose use is gated on biometric
authentication, and returns the signature.
4. The daemon verifies the enrolled public key and calls
`loginctl unlock-session`.
Replay-safe (nonce), no secrets over the air, and the unlock capability is
worth exactly as much as your phone's lock screen.