feat: Bluetooth MIDI setup script (discoverable, pairable, systemd service)

This commit is contained in:
2026-06-09 01:02:23 -04:00
parent ac16d211de
commit c73b1ffa6c
+158
View File
@@ -0,0 +1,158 @@
#!/usr/bin/env bash
# ── Pi Multi-FX Pedal — Bluetooth MIDI Setup ───────────────────────
# Enables Bluetooth MIDI (BLE-MIDI) so wireless controllers,
# expression pedals, and phone apps can send MIDI to the pedal.
#
# Usage:
# sudo bash scripts/setup-bt-midi.sh # Interactive setup
# sudo bash scripts/setup-bt-midi.sh --disable # Remove BT MIDI service
# sudo bash scripts/setup-bt-midi.sh --pair-only # Just make it discoverable
#
# Requirements:
# Built-in RPi Bluetooth, or USB BT dongle
# bluez, bluez-utils (installed by this script)
# ────────────────────────────────────────────────────────────────────
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
DISABLE=false
PAIR_ONLY=false
while [[ $# -gt 0 ]]; do
case "$1" in
--disable) DISABLE=true; shift ;;
--pair-only) PAIR_ONLY=true; shift ;;
--help|-h) echo "Usage: $0 [--disable] [--pair-only]"; exit 0 ;;
*) echo "Unknown: $1"; exit 1 ;;
esac
done
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; CYAN='\033[0;36m'; NC='\033[0m'
info() { echo -e "${CYAN}[INFO]${NC} $1"; }
ok() { echo -e "${GREEN}[OK]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
fail() { echo -e "${RED}[FAIL]${NC} $1"; }
if [ "$DISABLE" = true ]; then
echo ""
info "Disabling Bluetooth MIDI..."
sudo systemctl stop bluetooth-midi 2>/dev/null || true
sudo systemctl disable bluetooth-midi 2>/dev/null || true
sudo rm -f /etc/systemd/system/bluetooth-midi.service
# Restart bluetooth in standard mode
sudo systemctl restart bluetooth
ok "Bluetooth MIDI disabled"
exit 0
fi
echo ""
echo "╔═══════════════════════════════════════════════╗"
echo "║ Pi Multi-FX Pedal — Bluetooth MIDI Setup ║"
echo "╚═══════════════════════════════════════════════╝"
echo ""
# 1. Install bluez
info "Installing Bluetooth packages..."
sudo apt-get install -y -qq bluez bluez-utils bluez-tools 2>/dev/null || true
ok "Bluetooth packages installed"
# 2. Ensure BT hardware is on
info "Enabling Bluetooth hardware..."
sudo raspi-gpio set 28 a0 2>/dev/null || true # Enable BT on RPi
sudo hciconfig hci0 up 2>/dev/null || true
ok "Bluetooth hardware enabled"
# 3. Enable BT MIDI plugin in bluez
info "Configuring BlueZ for MIDI..."
BLUEZ_CONF="/etc/bluetooth/main.conf"
if [ -f "$BLUEZ_CONF" ]; then
sudo sed -i 's/^#Class *= *0x000100/Class = 0x002300/' "$BLUEZ_CONF" 2>/dev/null || true
sudo sed -i 's/^#DiscoverableTimeout *= *0/DiscoverableTimeout = 0/' "$BLUEZ_CONF" 2>/dev/null || true
sudo sed -i 's/^#PairableTimeout *= *0/PairableTimeout = 0/' "$BLUEZ_CONF" 2>/dev/null || true
fi
ok "BlueZ configured for discoverable + pairable MIDI"
# 4. Make device discoverable
info "Making pedal discoverable as 'Pi-Pedal-MIDI'..."
cat <<EOF | sudo bluetoothctl
power on
discoverable on
pairable on
scan on
name Pi-Pedal-MIDI
EOF
sleep 1
ok "Pedal is discoverable as 'Pi-Pedal-MIDI'"
if [ "$PAIR_ONLY" = true ]; then
echo ""
echo "╔═══════════════════════════════════════════════╗"
echo "║ Pairing Mode Active ║"
echo "║ ║"
echo "║ Scan for 'Pi-Pedal-MIDI' from your device ║"
echo "║ and pair. The pedal stays discoverable ║"
echo "║ for 5 minutes, then returns to normal. ║"
echo "╚═══════════════════════════════════════════════╝"
# Keep discoverable for 5 min then settle
(sleep 300 && echo "settle" | sudo bluetoothctl) &
exit 0
fi
# 5. Create systemd service for BT MIDI
info "Creating bluetooth-midi systemd service..."
cat <<'SERVICE' | sudo tee /etc/systemd/system/bluetooth-midi.service > /dev/null
[Unit]
Description=Pi Multi-FX Pedal — Bluetooth MIDI Service
After=bluetooth.service
Requires=bluetooth.service
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/bluetoothctl discoverable on
ExecStartPost=/usr/bin/bluetoothctl pairable on
ExecStartPost=/usr/bin/bluetoothctl name Pi-Pedal-MIDI
ExecStop=/usr/bin/bluetoothctl discoverable off
Restart=no
[Install]
WantedBy=multi-user.target
SERVICE
# 6. Register MIDI over BLE with ALSA (a2dp-midi bridge)
# On modern BlueZ (5.50+), BLE-MIDI shows up as a sequencer port automatically
# Verify with: aconnect -o -l | grep -i midi
sudo systemctl daemon-reload
sudo systemctl enable bluetooth-midi
sudo systemctl start bluetooth-midi
# 7. Also start the pedal's MIDI handler to accept BT MIDI
# The MIDI handler already listens on ALSA sequencer ports
info "MIDI handler will auto-detect BT MIDI sequencer ports on next pedal start"
ok "Bluetooth MIDI service started"
echo ""
echo "╔═══════════════════════════════════════════════╗"
echo "║ Bluetooth MIDI Setup Complete 🎸 ║"
echo "╚═══════════════════════════════════════════════╝"
echo ""
echo " 📡 Name: Pi-Pedal-MIDI"
echo " 🎛 Connect from: Phone, tablet, wireless footswitch"
echo ""
echo " From your phone:"
echo " 1. Enable Bluetooth"
echo " 2. Scan for 'Pi-Pedal-MIDI'"
echo " 3. Pair"
echo " 4. Open any MIDI controller app"
echo ""
echo " To disable later:"
echo " sudo bash $0 --disable"
echo ""