#!/bin/sh # Keep one interactive bluetoothctl client alive for a full discovery window. # BlueZ removes unpaired Device1 objects when the discovery client exits, and # non-interactive "scan on" does not reliably print device events to a pipe. # This helper drives bluetoothctl through a PTY via script(1), asks the same # client for "devices" while discovery is active, then stops discovery cleanly. REAL="${BANANARCHY_REAL_BLUETOOTHCTL:-/usr/bin/bluetoothctl}" DURATION="${BANANARCHY_BT_SCAN_SECONDS:-13}" case "$DURATION" in ''|*[!0-9]*) DURATION=13 ;; esac if [ "$DURATION" -lt 3 ]; then DURATION=3 elif [ "$DURATION" -gt 13 ]; then DURATION=13 fi "$REAL" power on >/dev/null 2>&1 || true doas -n /usr/bin/bananarchy-bt-le-enable.sh >/dev/null 2>&1 || true q1=$((DURATION / 3)) q2=$((DURATION * 2 / 3)) q3=$((DURATION - 1)) if [ "$q1" -lt 1 ]; then q1=1 fi if [ "$q2" -le "$q1" ]; then q2=$((q1 + 1)) fi if [ "$q3" -le "$q2" ]; then q3=$((q2 + 1)) fi { printf '%s\n' \ 'menu scan' \ 'transport auto' \ 'duplicate-data off' \ 'back' \ 'power on' sleep 1 printf '%s\n' 'scan on' sleep "$q1" printf '%s\n' 'devices' sleep "$((q2 - q1))" printf '%s\n' 'devices' sleep "$((q3 - q2))" printf '%s\n' 'devices' 'scan off' sleep 1 printf '%s\n' 'quit' } | timeout "$((DURATION + 2))" \ script -q -c "$REAL" /dev/null exit 0