33 lines
1.3 KiB
Bash
33 lines
1.3 KiB
Bash
#!/usr/bin/env bash
|
|
# Scans for an Apple BLE HID device (Siri Remote in pairing mode) and pairs,
|
|
# trusts and connects it as soon as it shows up.
|
|
# usage: autopair.sh [seconds]
|
|
LIMIT="${1:-180}"
|
|
strip() { sed 's/\x1b\[[0-9;]*m//g'; }
|
|
|
|
bluetoothctl pairable on >/dev/null
|
|
# Background scan for the whole window.
|
|
timeout "$LIMIT" bluetoothctl --timeout "$LIMIT" scan le >/dev/null 2>&1 &
|
|
SCAN=$!
|
|
trap 'kill $SCAN 2>/dev/null' EXIT
|
|
|
|
end=$((SECONDS + LIMIT))
|
|
while [ $SECONDS -lt $end ]; do
|
|
for m in $(bluetoothctl devices | strip | awk '{print $2}'); do
|
|
info=$(bluetoothctl info "$m" | strip)
|
|
if grep -q "00001812" <<<"$info" && grep -q "Key: 0x004c" <<<"$info" && grep -q "Paired: no" <<<"$info"; then
|
|
echo "found Apple HID $m, pairing..."
|
|
kill $SCAN 2>/dev/null
|
|
{ echo "agent NoInputNoOutput"; echo "default-agent"; sleep 1
|
|
echo "pair $m"; sleep 15; echo "trust $m"; sleep 1
|
|
echo "connect $m"; sleep 6; echo quit; } | bluetoothctl 2>&1 | strip \
|
|
| grep -E "Pairing|Paired|Bonded|trust|Connected|Failed|Error|ServicesResolved" | sort -u
|
|
bluetoothctl info "$m" | strip | grep -E "Name|Alias|Paired|Bonded|Trusted|Connected|Modalias"
|
|
exit 0
|
|
fi
|
|
done
|
|
sleep 1
|
|
done
|
|
echo "TIMEOUT: no Apple HID device in pairing mode"
|
|
exit 1
|