Bastian Mäuser b93c0134dd Add Debian packaging and arm64 cross build
cargo-deb metadata produces a .deb with the binary, the packaged user
unit variant, man page and example config; arm64 cross-builds as a
static musl binary via an isolated rustup and rust-lld (recipe in
README). Add MIT LICENSE for the package copyright.
2026-11-05 16:34:47 +01:00
2026-11-05 14:17:51 +01:00
2026-11-05 14:17:51 +01:00

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

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):

echo 'export PATH="$HOME/.cargo/bin:$PATH"' >> ~/.zshrc   # or ~/.bashrc
source ~/.zshrc

Test without locking anything, walk around with the phone and watch:

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):

Debian package

cargo install cargo-deb --version 2.12.1   # 3.x needs rustc > 1.85
cargo deb                                  # -> target/debian/btle-proximity_*.deb
sudo apt install ./target/debian/btle-proximity_0.1.0-1_amd64.deb
systemctl --user enable --now btle-proximity

Installs 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 config still lives at ~/.config/btle-proximity/config.toml. If you previously installed via cargo install, remove that copy (cargo uninstall btle-proximity) to avoid PATH shadowing.

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:

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 rustlib-dir)"/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)

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
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.

S
Description
No description provided
Readme MIT
248 KiB
0 Stars 1 Watchers 0 Forks
v0.2.1
Latest
2026-11-12 02:06:34 +01:00
Languages
Rust 74.9%
Kotlin 22.7%
Roff 2.4%