Files
scripts/ps3-controller-bluetooth-latency-fix.sh
T
2026-06-11 13:47:48 -04:00

96 lines
2.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# setup-ps3-bt-slave-poller.sh
# ============================
# Installs a systemd service + script to poll for your PS3 controllers
# and force slave/peripheral mode (PC master) when connected.
# No udev timing issues. Works around hcitool sr race.
set -euo pipefail
MACS=("00:1B:FB:F0:7C:85" "00:1B:FB:F3:D2:25" "00:1B:FB:6A:1E:E4")
SERVICE_FILE="/etc/systemd/system/ps3-bt-slave.service"
POLLER_SCRIPT="/usr/local/bin/ps3-bt-slave-poller.sh"
HCITOOL="$(which hcitool 2>/dev/null || echo "/usr/bin/hcitool")"
if [[ ${EUID:-} -ne 0 ]]; then
echo "Run with sudo."
exit 1
fi
if [[ ! -x "$HCITOOL" ]]; then
echo "hcitool missing. Install bluez-deprecated-tools:"
echo " sudo apt install bluez-deprecated-tools (Debian/Ubuntu/Mint)"
echo " or equivalent for your distro."
exit 1
fi
echo "Creating poller script: $POLLER_SCRIPT"
cat > "$POLLER_SCRIPT" << 'EOP'
#!/usr/bin/env bash
# ps3-bt-slave-poller.sh - background checker for PS3 BT role switch
MACS=("00:1B:FB:F0:7C:85" "00:1B:FB:F3:D2:25" "00:1B:FB:6A:1E:E4")
LOG="/tmp/ps3-bt-slave-poller.log"
while true; do
for mac in "${MACS[@]}"; do
if hcitool con | grep -q "$mac"; then
# Connected → force slave/peripheral
echo "$(date '+%Y-%m-%d %H:%M:%S') - $mac connected, forcing slave" >> "$LOG"
hcitool sr "$mac" slave >> "$LOG" 2>&1 || echo "$(date '+%Y-%m-%d %H:%M:%S') - Failed for $mac" >> "$LOG"
fi
done
sleep 10
done
EOP
chmod +x "$POLLER_SCRIPT"
echo "Poller script created."
echo "Creating systemd service: $SERVICE_FILE"
cat > "$SERVICE_FILE" << EOF
[Unit]
Description=PS3 Bluetooth Slave/Peripheral Mode Poller
After=bluetooth.service network.target
[Service]
ExecStart=$POLLER_SCRIPT
Restart=always
RestartSec=5
User=root
[Install]
WantedBy=multi-user.target
EOF
echo "Service file created."
echo "Enabling/starting service..."
systemctl daemon-reload
systemctl enable --now ps3-bt-slave.service
echo ""
echo "=== Setup Complete ==="
echo "The poller runs in background. After any controller connects:"
echo " - Wait 10-30 seconds"
echo " - Run: hcitool con"
echo " → Should show lm PERIPHERAL (good) instead of lm CENTRAL"
echo ""
echo "Check log for activity/errors:"
echo " tail -f /tmp/ps3-bt-slave-poller.log"
echo ""
echo "Test:"
echo " 1. Disconnect controller fully (hold PS ~10s, lights off)"
echo " 2. Reconnect (press PS button)"
echo " 3. Wait 20-30s"
echo " 4. hcitool con"
echo ""
echo "If still CENTRAL after 30s: Check log for 'Failed' → may need different adapter or accept manual 'sudo hcitool sr MAC slave' after connect."
echo "To stop/uninstall: sudo systemctl stop ps3-bt-slave.service && sudo systemctl disable ps3-bt-slave.service"
exit 0