[Python] How can you set up a Raspberry Pi to automatically boot and connect to Bluetooth...

Discussão em 'Python' iniciado por Stack, Setembro 27, 2024.

  1. Stack

    Stack Membro Participativo

    How can you set up a Raspberry Pi to automatically boot and connect to Bluetooth headphones for audio output, instead of using wired headphones?

    1. Enable Auto Boot and Autostart a Script


    • Autoboot is enabled by default on Raspberry Pi. If you want a Python script or application to run automatically on boot, follow these steps:
      1. Open the terminal.
      2. Edit the rc.local file:
        sudo nano /etc/rc.local
      3. Add your script command before the exit 0 line (e.g., sudo python3 /home/pi/my_script.py &).
      4. Save the file and exit.

    • Alternatively, for desktop environments, create an autostart entry in /etc/xdg/autostart/.
    2. Install Bluetooth Packages

    • Install the required Bluetooth packages:
      sudo apt-get update
      sudo apt-get install pi-bluetooth pulseaudio pulseaudio-module-bluetooth
    3. Pair Bluetooth Headphones


    1. Put your Bluetooth headphones in


    2. Start Bluetooth pairing using bluetoothctl:

      sudo bluetoothctl

    3. In the Bluetooth shell:
      • Power on Bluetooth:
        power on
      • Set the agent for pairing:
        agent on
        default-agent
      • Scan for devices:
        scan on
      • When your Bluetooth headphones appear, note their MAC address.
      • Pair and connect:
        pair XX:XX:XX:XX:XX:XX
        connect XX:XX:XX:XX:XX:XX
        trust XX:XX:XX:XX:XX:XX
        4. **Configure Bluetooth Audio**

    4. Open PulseAudio configuration:

      sudo nano /etc/pulse/default.pa

    5. Ensure the line load-module module-bluetooth-discover is


    6. Restart Bluetooth and PulseAudio:

      sudo systemctl restart bluetooth
      pulseaudio -k
      pulseaudio --start

    7. Set Bluetooth Headphones as Default Audio Output


    8. Edit the ALSA sound configuration:

      sudo nano /etc/asound.conf

    9. Add the following to set PulseAudio as the default:

      defaults.pcm.card 1
      defaults.ctl.card 1

    10. Reboot the Raspberry Pi to apply changes:

      sudo reboo

    Test Bluetooth Connection on Boot

    • After rebooting, ensure that your Bluetooth headphones connect automatically and that audio plays through them.

    Continue reading...

Compartilhe esta Página