Raspberry pi bluetooth звук

Connecting bluetooth audio device to Raspberry Pi

Raspberry Pi Bluetooth cover

Raspberry Pi Bluetooth cover

In this tutorial we are connecting bluetooth audio device to Raspberry Pi that is running Raspberry Pi OS and PulseAudio. This works with bluetooth speaker and headphones. What i have learned in the process that its important to do all the steps in the right order so that you don’t get any errors. This tutorial is tested with the Raspberry Pi 3/4 and Raspberry Pi OS Lite (32bit) that is released in 2020-12-02.

Connect Raspberry Pi to bluetooth speaker

1. Connect to your Raspberry Pi over SSH using terminal software like PuTTy, or if you are using desktop version of Raspberry Pi OS then you have to open Raspberry menu and go to Raspberry Menu > Accessories > Terminal.

2. Let’s start by updating package repositories by writing sudo apt-get update and hit enter.

3. Install pulseaudio and needed modules to Rasperry Pi OS. You have now two options to choose from. You can go with the option a that doesn’t install all the modules and takes 164 MB less space, or you can ago with option b, where you install all the extra modules. There has been cases where users had to install all the modules (Option b), because they couldn’t pinpoint what was the missing module.

Option a. sudo apt-get install pulseaudio pulseaudio-utils pulseaudio-module-bluetooth
(After this operation, 676 MB of additional disk space will be used.)

Option b. sudo apt-get install pulseaudio*
(After this operation, 840 MB of additional disk space will be used.)

4. Next we need to add user pi to group lp: sudo usermod -a -G lp pi . If you are not doing this, you will get «Failed to connect: org.bluez.Error.Failed» -error while connecting to device with bluetoothctl.

5. Reboot Raspberry Pi with this command: sudo reboot . You need to reboot so that the group change comes into effect. Closing and reconnecting with PuTTy wasn’t enough for me, but reboot worked.

Install Pulseaudio and utils using PuTTy

6. Next start pulseaudio with command: pulseaudio —start

7. Let’s access bluetooth using: sudo bluetoothctl

8. Power on bluetooth by writing: power on and then select default agent: default-agent .

9. Start your headphones/speaker and put it in the pairing mode. Then start scanning the devices with: scan on . Wait untill you see your device in the scan list, and then add that device to trusted list with command: trust xx:xx:xx:xx . After that, pair Raspberry Pi with: pair xx:xx:xx:xx , and then connect with: connect xx:xx:xx:xx .

10. If you did everything in right order you should be now connected to your bluetooth device. If thats the case, exit bluetoohctl with command: exit

Connect to bluetooth speaker using Pulseaudio and bluetoothctl

11. Check that your newly connected audio device is showing in the audio card list with: pactl list cards short . Sometimes there is small delay untill your bluetooth device is showing, so you might have to run the command multiple times. If you want more info about the available cards just write the command without short pactl list cards .

Читайте также:  Driver bluetooth lenovo all in one

12. When you see your audio device in the list, you can test that the device is working. Check the number of the card with the previous command and use that number with this command by replacing the —device id.
paplay -p —device=1 /usr/share/sounds/alsa/Front_Center.wav

13. If you are hearing sound, then everything is working like it should!

Play audio on your audio device using paplay

Make Raspberry Pi to connect bluetooth automatically on boot

There might be situations where you want Raspberry Pi to connect to your bluetooth device automatically on boot, so let’s also cover that up. We are going to use simple crontab approach. Here is the reasons why you shouldn’t run PulseAudio as system mode: What is wrong with system mode?

1. First we need to create bash script. I’m creating the file to /home/pi with command touch btconnect.sh

2. Next we need to change the file permission so that we can run it chmod +x btconnect.sh

3. Then we can open the file for editing. I’m using nano for editing. nano btconnect.sh . Copy&paste or write the lines below to the sh file. Replace the XX with the address of your bluetooth device, so that Raspberry Pi will attempt to connect to that specifig device on boot. Optionally you don’t have to add the bluetoohctl lines at all, then Raspberry Pi doesn’t connect to your device, but when you start your speaker/headset it will automatically connect to Raspberry.

#!/bin/bash pulseaudio --start bluetoothctl power on bluetoothctl connect XX:XX:XX:XX 

You can paste text to shell by right clicking your mouse in PuTTy. You can also copy text in shell just by highlighting text with left click.

4. Then you can save the file by pressing [Ctrl] + X then y and then [Enter].

5. Now we can test that the script works by writing ./btconnect.sh . If you don’t get any errors, then you can add the script to crontab.

6. Write crontab -e and add line @reboot /home/pi/btconnect.sh . If you are using nano for editing then you can save and exit by pressing [Ctrl] + X then y and then [Enter].

7. Reboot your Raspberry Pi to see if it works. You can reboot by writing sudo reboot . If you did everything right, Rasperry should automatically connect to your speaker/headphones, and if you power off your audio device and start it again, it should automatically connect to Raspberry, so it works both ways.

Troubleshooting

pa_context_connect() failed: Connection refused

You might get «Connection failure: Connection refused, pa_context_connect() failed: Connection refused» -messages when entering pactl list cards command. You can try to fix that by first killing pulseaudio process by writing pulseaudio -k and then starting it again by writing pulseaudio —start .

Failed to connect: org.bluez.Error.Failed

This is the most commonly seen error, and the problem is that it doesn’t tell you anything specific. You will get this error if: You don’t have all necessary modules, or if pulseaudio is not started, or if your user doesn’t have permission to use bluetooth.

1. Run this command pulseaudio —dump-modules to see all the available modules for PulseAudio. Make sure that you find these modules from the list: module-bluetooth-policy, module-bluetooth-discover, module-bluez5-discover. If you don’t have these modules installed, then you can just run sudo apt-get install pulseaudio* to install all the available modules.

2. Run this command pactl list short modules to see all the currently loaded modules for PulseAudio. Make sure that you find the modules mentioned on step 1. If you don’t find them, then check PulseAudio config with sudo nano /etc/pulse/default.pa and see if there is commented lines that contains these modules. If you don’t find this modules at all from this file, then you can try to manually add them to file.

Читайте также:  Iphone 12 pro codec bluetooth

3. Make sure that you added your user to lp group and rebooted Raspberry like mentioned in step 4.

4. You can check errors from systemlog by writing sudo cat /var/log/syslog | grep -i protocol and bluetooth errors by writing systemctl status bluetooth .

1 Comments

Hello internet. I have tried to install this using the second option shown. I am having trouble with this, because every time I type «power on», it tells me that the whole org.bluez thing is blocked. I am unsure of how to fix this (if it even is fixable). Any advice would be of great help. Thank you.

I am not familiar with this error, but there is blog posts about that. And the list of possible causes is long: https://blog.timekit.io/google-oauth-i.

EA took over Origin and deleted it. Now patch does not work and cannot run Yuri2. any fix? Thanks.

hi. Nice video and web page. I tryed to play YR cnCnet but it says that I could be cheatring and the maps doesnt load so they kicked me out everytime .

Im kinda also having the last problem I have my ps4 controller connect via BT opened x360ce it directed my controller and i even remapped and saved th.

February 13, 2023 — Poopyswerls

Our website uses cookies to improve your experience. By choosing I Accept, you consent to our use of cookies. Read about the cookies we use.

Источник

Play audio on a Bluetooth speaker with Raspberry Pi

Make use of the Raspberry Pi’s Bluetooth capabilities to play audio and/or music over a wireless speaker, without using a dongle!

The MagPi issue 131 cover

The MagPi issue 131 out now

Discover Python Robots in this month’s edition of The MagPi magazine. Design, build and control articulated automatons with Raspberry Pi. Alternatively, set up camera traps in your garden to capture real-world critters. Grab your copy today!

Bluetooth is a wireless technology standard, and it has been a feature on the Raspberry Pi since the Raspberry Pi 3 and Pi Zero W. You can use it to connect Bluetooth keyboards, mice, and other accessories: like speakers and headphones.

The new Raspberry Pi 3B+ even moves from Bluetooth 4.1 to 4.2, which brings faster and more reliable over-the-air data transmission.

In this tutorial we’ll take a look at hooking up a Raspberry Pi to a Bluetooth speaker. This enables you to play music wirelessly from your Raspberry Pi, turning it into an effective media centre.

You should be able to follow this Bluetooth audio tutorial with any Bluetooth-enabled Raspberry Pi.

And Bluetooth can easily be added to an older Raspberry Pi using a dongle, like this Nano USB to Bluetooth Dongle V2.0.

Play Bluetooth Audio on a Raspberry Pi: get some music

First, you’ll need some music files. You can stream audio online from a source such as YouTube, and many people have MP3 and other audio files. We downloaded Sampler EP Vol. 9 by Vulpiano Records Artists from Free Music Archive for this tutorial. Save the file to your Music folder. Use File Manager to locate the zip file, then right-click it and choose Extract Here.

It is possible to play the music files directly from Terminal using omxplayer. Open a Terminal window and enter omxplayer followed by the path to the music file, like this:

oxmplayer ~/Music/Anton_Rothschild_-_03_-_Its_Not_Vegas.mp3

But it’s not a great visual option. We’d rather double-click MP3 files in the File Manager using the Desktop interface. For this we need a media player, and we’re going to use a popular one called VLC. Enter these commands in Terminal:

sudo apt-get update sudo apt-get upgrade sudo apt-get install -y vlc

During the installation you’ll be asked if you want to allow metadata network access. This is used to add cover artwork and track listings to your files. Tick or untick the box and click Continue.

Читайте также:  Блютуз колонка samsung eo sg930

Double-clicking the MP3 files in File Manager will open VLC and play the file.

Connect to Bluetooth

OK, so we’ve got our music and a music player. Now it’s time to connect the Raspberry Pi to a Bluetooth speaker.

To do this you’ll need to put the speaker in pairing mode and pair the Raspberry Pi to the speaker. How you do this depends on the speaker itself. If your speaker has an interface, you’ll probably use the controls to put it into pairing mode. Many smaller speakers or headphones have a button that you hold down to put the device into pairing mode (typically you’ll see a flashing light).

Bluetooth Add Device 500

Connect a Raspberry Pi to Google Home via Bluetooth

  1. We’re using a Google Home speaker for this test. For this you simply say “Hey Google. Connect To Bluetooth” (you can do the same with Alexa).
  2. Now click on the Bluetooth icon in the top-right of the screen and choose Add Device. Look for your speaker in the ‘Add new device’ window. In most cases the speaker will be identified by an easily recognisable name, such as our Google Home.
  3. However, you may need to locate the device using its MAC address (the six octet hexadecimal code). These are often printed on the device itself – or you can cancel pairing mode, then re-enter it and see which device disappears and reappears. Hopefully you won’t have to resort to that. Locate the speaker in Add Devices and click Pair. After a short while, the window will display Pairing Successful. Click OK.
  4. Your Raspberry Pi is now paired with the speaker, but it won’t output audio to it yet. Right-click on the Volume Control applet in the top-right of the screen and choose your speaker from the list (it is below Analog and HDMI).

A pop-up will display ‘Connecting to Bluetooth audio device’. Now double-click on of the music files in Files Manager. The VLC app will open and your music will start playing through the Bluetooth speaker.

When you restart the Raspberry Pi, you will need to reconnect to the Bluetooth speaker (this isn’t the same as pairing – it is still paired). Click on the Bluetooth icon, choose your speaker, and select Connect from the menu.

You can use the Bluetooth menu to disconnect, if you don’t want to use a device for the time-being. Or remove it entirely if you don’t want to use it again.

Subscribe and never miss an issue

 Get a free Pi Zero W with every twelve-month print subscription

Get a a brand new Raspberry Pi Zero W, a case for it, and a selection of adapter cables with a twelve-month print subscription to The MagPi!

Russell runs Raspberry Pi Press, which includes The MagPi, Wireframe, and HackSpace magazine, and book projects. He’s a massive sci-fi bore.

Источник

Оцените статью
Adblock
detector