The code default stays 30 s for configs that omit the key, but 30 s false-locks with the companion beacon (event gaps of 25-60 s are routine). Docs now say explicitly that shipped configs must set 60+.
228 lines
9.5 KiB
Markdown
228 lines
9.5 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.
|
|
|
|
## Install (Debian packages)
|
|
|
|
Prebuilt `.deb` packages for amd64 and arm64 are attached to the
|
|
[releases](https://git.netz.org/bmaeuser/btle-proximity/releases) and
|
|
available through the Gitea Debian package registry.
|
|
|
|
### Option A — apt repository (recommended, delivers updates)
|
|
|
|
```sh
|
|
sudo install -d /etc/apt/keyrings
|
|
curl -fsSL https://git.netz.org/api/packages/bmaeuser/debian/repository.key \
|
|
| sudo gpg --dearmor -o /etc/apt/keyrings/btle-proximity.gpg
|
|
echo "deb [signed-by=/etc/apt/keyrings/btle-proximity.gpg] https://git.netz.org/api/packages/bmaeuser/debian stable main" \
|
|
| sudo tee /etc/apt/sources.list.d/btle-proximity.list
|
|
sudo apt update
|
|
sudo apt install btle-proximity
|
|
```
|
|
|
|
Future releases land with the usual `apt upgrade`.
|
|
|
|
### Option B — manual .deb from a release
|
|
|
|
```sh
|
|
curl -fLO https://git.netz.org/bmaeuser/btle-proximity/releases/download/v0.1.0/btle-proximity_0.1.0-1_amd64.deb
|
|
sudo apt install ./btle-proximity_0.1.0-1_amd64.deb
|
|
# arm64: btle-proximity_0.1.0-1_arm64.deb (fully static musl binary)
|
|
```
|
|
|
|
Both options install the binary to `/usr/bin`, the user unit to
|
|
`/usr/lib/systemd/user/`, a man page and the config example to
|
|
`/usr/share/doc/btle-proximity/`.
|
|
|
|
### Per-user setup
|
|
|
|
```sh
|
|
mkdir -p ~/.config/btle-proximity
|
|
cp /usr/share/doc/btle-proximity/examples/config.toml ~/.config/btle-proximity/config.toml
|
|
btle-proximity list-paired # pick your phone's MAC, edit config.toml
|
|
systemctl --user enable --now btle-proximity
|
|
journalctl --user -u btle-proximity -f
|
|
```
|
|
|
|
Coming from the `cargo install` flow? Remove that copy first
|
|
(`cargo uninstall btle-proximity`) and the unit copied to
|
|
`~/.config/systemd/user/`, so neither shadows the package.
|
|
|
|
The Android app (`btle-proximity-beacon-release.apk`) is attached to the
|
|
same releases; Obtainium can track the repo for automatic updates — see
|
|
`android/README.md`.
|
|
|
|
## Build & install from source
|
|
|
|
```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.
|
|
|
|
### Building the Debian packages
|
|
|
|
```sh
|
|
cargo install cargo-deb --version 2.12.1 # 3.x needs rustc > 1.85
|
|
cargo deb # -> target/debian/btle-proximity_*.deb
|
|
```
|
|
|
|
#### arm64 (aarch64)
|
|
|
|
Cross-built as a **static musl binary**, so no cross-gcc/sysroot is needed
|
|
and the result runs on any arm64 Linux (Ubuntu 24.04/26.04 included). Uses
|
|
an isolated rustup under `/tmp` so the distro cargo is not touched:
|
|
|
|
```sh
|
|
export RUSTUP_HOME=/tmp/rustup CARGO_HOME=/tmp/rustup-cargo PATH=/tmp/rustup-cargo/bin:$PATH
|
|
curl -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain 1.85.0 --profile minimal --no-modify-path
|
|
rustup target add aarch64-unknown-linux-musl
|
|
mkdir -p /tmp/lld-bin && ln -sf "$(rustc --print sysroot)"/lib/rustlib/x86_64-unknown-linux-gnu/bin/rust-lld /tmp/lld-bin/ld.lld
|
|
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER=/tmp/lld-bin/ld.lld
|
|
~/.cargo/bin/cargo-deb --target aarch64-unknown-linux-musl --no-strip
|
|
# -> target/aarch64-unknown-linux-musl/debian/btle-proximity_*_arm64.deb
|
|
```
|
|
|
|
The `--no-strip` matters: the release profile already strips via the
|
|
linker, and the host `strip` cannot read aarch64 objects.
|
|
|
|
### Manual install (dev flow)
|
|
|
|
The unit in `systemd/` runs the cargo-installed binary
|
|
(`%h/.cargo/bin/btle-proximity`); the packaged variant under
|
|
`packaging/` points at `/usr/bin` instead.
|
|
|
|
```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 (use 60+) | absence confirmation; 30 is only the fallback, false-locks with the companion app |
|
|
| `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 |
|
|
| `unlock_notice` | true | desktop notification after unlock explaining the lock |
|
|
|
|
## 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.
|