Thursday, January 1, 2015

Audio Streaming to Bluetooth Speaker Using Raspberry Pi B+

Edit 2016Jan3: This guide was based on old Raspbian Wheezy distro, some packages were no longer included in latest Raspbian Jessie distro. I'll have a look on how to make the old setting works on Raspbian Jessie.

Edit 2017May14: Finally found a way to make it work on Raspbian Jessie distro. Please refer to this link for instructions to setup bluetooth speaker.

Hi~ Here is a small project for my new RPi B+. So i got some bluetooth peripherals lying around the house, so i thought i can make a small music box using RPi. Did some research and got it working!

So here are what you need for the hardware:
  1. RPi Model B+ with Raspbian Installed (Should work with model B or A)
  2. Bluetooth Speaker
  3. USB Bluetooth dongle
  4. USB Wifi dongle / LAN cable for internet connection
  5. 5V 1A/2A Power Supply
Figure6: Hardware Setup
 If you have everything ready for the hardware. You need to install some packages to your RPi before you can connect to your bluetooth speaker. Boot up your RPi, but do not plug in your bluetooth dongle yet. Open up terminal and enter commands below for installation of the required packages.
  1. "sudo apt-get update && sudo apt-get upgrade" - upgrade your Raspbian packages list to latest version. Wait for program to finish upgrade before next command.
  2. "sudo apt-get install bluetooth bluez bluez-utils bluez-alsa blueman" - install five packages for bluetooth dongle and bluetooth audio. Wait for installation to complete before next command.
  3. "sudo usermod -a -G lp pi" - this command will add "lp" group under account "pi". You account need to have access to this group for this to work. If you are using other account, just replace the "pi" in command with your account name.
  4. "sudo nano /etc/bluetooth/audio.conf" - you need to add some command in the audio.conf file. This command will open nano text editor and allow you to edit the file. You need to add in two line in the file. "Enable=Source,Sink,Socket" and "Disable=Media". Refer to Figure1 and Figure2 below. After edit file, press Ctrl+O and Enter to save the file.
    Figure1: Editing audio.conf file
    Figure2: Add in highlighted two lines in audio.conf file
  5. Once save the file, enter command "sudo shutdown -h -P now" to safe shutdown the Pi. Once you see green light blip for 10 times. You can turn off the power supply.
  6. Now plug-in your bluetooth dongle to the USB and start up your RPi.
  7. Wait for boot complete, go to terminal and enter command "lsusb". You should see your bluetooth dongle available as shown in Figure3. You can refer HERE for list of working bluetooth adapter for RPi in case your bluetooth dongle is not detectable.
    Figure3: Bluetooth Dongle Detected in USB list
  8. If you able to find your bluetooth dongle in USB list, you should see a Bluetooth icon at your RPi tray. If you did not boot up your Pi in GUI and you are in command line interface, enter "startx" to start your GUI session. Refer to Figure4 below to setup your bluetooth connection with your bluetooth speaker with 3 simple steps.
    Figure4: Setup Bluetooth Connection with Your Bluetooth Speaker
  9. Follow up on step 8. You can also click "Trusted" on the device as shown in Figure4 if you want to let the speaker auto connect to RPi in the future. Mine is showing "Untrust" because i've already "Trusted" this device.
  10. Open up terminal again to edit one last file. Enter "sudo nano ~/.asoundrc" and enter the script as shown in Figure5. For line start with "device 00:", that the place where you put your bluetooth speaker's MAC address. You can find you MAC address in Figure4. Copy and paste in to the script. Once done save the file and you are done setting up the bluetooth portion for RPi.
    Figure5: Script to Connect Bluetooth to Alsa
  11. If you completed the steps up to 10, you are almost ready. You just need to install one more package so that you RPi can play music. In this case i recommend Mplayer because it is a terminal player that can support wide range of audio format including m4a. Go to terminal and enter "sudo apt-get install mplayer" and wait for installation complete.
  12. Once you complete installation of mplayer. Navigate to your music folder using terminal and then enter command "mplayer -volume 10 -ao alsa:device=bluetooth youmusic.mp3" to play music.
  13. You should be able to listen to your music from bluetooth speaker from now. 
  14. Here is the video showing the working bluetooth speaker using RPi.

Hope you like this tutorial. Thanks for your time!

Reference:
http://elinux.org/RPi_USB_Bluetooth_adapters
http://www.correderajorge.es/bluetooth-on-raspberry-audio-streaming/

33 comments:

  1. do you know how to stream audio to wifi speakers (jongo s3) on the rpi? haven't been able to find much on the subject. it must be doable because i was able stream wifi (limited to a few apps) with my android.
    thanks John

    ReplyDelete
    Replies
    1. Hi John, unfortunately i haven't try this yet because i don't own a wifi speaker myself. You can start searching for packages available in Linux that can enable access to your wifi speaker. Good Luck. =)

      Delete
  2. Hi, nice tutorial!
    Is it possible to stream online radio instead of mp3? If yes, can you tell me how?

    ReplyDelete
  3. Look great...
    I will try to make this working for my Home Automation.. [ stream music to a Bt speaker... ]
    Is there a option to have acces from a Http comand/.
    Or acces from a lua/bash script...
    My Home automation can work with this scripts an can set the player then on or off...

    Like to hear [ i orderd my first pi] so i must wait ]

    ReplyDelete
  4. Actually there is a github project (pulseaudio-dlna) that streams wifi to dlna renderer speakers (such as I have). It works on Ubuntu, Linux Mint and LMDE 2 and I would imagine any linux box that runs some form of debian jessie. But it requires jessie which at the moment the RPI doesn't support (I haven't had any luck upgrading from wheezy to jessie on the pi yet). I did try running it on my goflex that runs debian jessie (armel) but it kept crashing pulseaudio. I couldn't figure out how to get pulseaudio to stay up and work correctly while pulseaudio-dlna was running.

    ReplyDelete
  5. Thanks for showing how to make bluetooth speaker work with RPi! Curious what wifi and bluetooth adapters did you use?

    ReplyDelete
    Replies
    1. I bought the wifi module from element14, from adafruit industries. Link below:

      http://my.element14.com/webapp/wcs/stores/servlet/ProductDisplay?catalogId=15001&langId=83&urlRequestType=Base&partNumber=2301652&storeId=10188

      For the bluetooth dongle I'm using the one comes with my Samsung hands free module. I believe it is the normal Cambridge Silicon bluetooth 2.0 dongle.

      Delete
  6. Just about any bluetooth dongle that supports a2dp should work. I would suggest ver 4.0 and up either Broadcom or Cambridge Silicon Radio as Linux supports both. You should be able to purchase it on ebay for $5 and up. The following bluetooth dongle should work.
    http://www.ebay.com/itm/USB-Wireless-Bluetooth-4-0-CSR-Dongle-Adapter-Audio-Transmitter-XP-vista-win7-8-/301582691619?hash=item4637baad23

    For WiFi once more any wifi dongle that supports N should work. However the dongles don't have a whole lot of range as the antenna is very short. If range and signal strength is a concern I would suggest something such as Edimax EW-7811UAC 11AC (Amazon) or similar that has a high-gain external antenna will work much better. They are a little pricey though.
    CAUTION: no matter what you do be careful to always use the extension cradle with it as the connections on the dongle itself are reversed and will blow-it-out if you plug it directly into the usb port without the cradle.

    The other problem with the Edimax is that you will have to build a driver for it. It wasn't easy so if you're not familiar with compiling you will probably have problems with getting it to compile without errors. Check out MrEngman's post at
    https://www.raspberrypi.org/forums/viewtopic.php?f=66&t=66499&p=779003#p779003
    on how to build it. He may be willing to help you with it.

    YMMV
    Good luck
    John

    ReplyDelete
  7. Thanks for the info and the time u took to share this project.
    My pi b+ runs only osmc (kodi) as a media player. Do you know if I can use a bluetooth dongle on the pi to play the audio from movies and series to a set of bluetooth headphones?

    ReplyDelete
  8. Thanks for this tutorial I got a RPi 2 connected to a bluetooth speaker. I followed all steps except that after step 7, I cannot see the Bluetooth icon within the GUI. Searching google lead me to an important step that appears to be missed on this guide:

    sudo apt-get install blueman

    After I installed blueman, everything else was good.

    ReplyDelete
    Replies
    1. Thanks for the feedback! Yes somehow i miss out blueman (bluetooth manager) in my post. Edited my post.

      Delete
  9. Really good tutorial. Is it possible to play ALL audio through the bluetooth speakers? Thanks

    ReplyDelete
  10. Thanks for the tutorial. I'm not able to install the Bluez apps though. It tells me "E: Package 'bluez-utils' has no installation candidate" and "Package bluez-utils is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source
    However the following packages replace it:
    bluez"

    I tried the Bluez package but it didn't work. Any suggestions?

    ReplyDelete
    Replies
    1. May i know which repo you use for your Pi? I was using wheezy Raspbian when i write this post. Not sure it will work for jessie Raspbian or not.

      Delete
    2. This comment has been removed by the author.

      Delete
    3. "sudo apt-get install bluez" is the command if you are using jessie version

      Delete
    4. Here is the full post on how to make it works on raspbian jessie. http://mygeeks014.blogspot.my/2017/05/audio-streaming-to-bluetooth-speaker.html

      Delete
  11. Hi can you tell me which blotooth dongle are you using?

    ReplyDelete
    Replies
    1. Its a random bluetooth dongle lying around my room. If i remember correctly it was a bluetooth dongle for my Samsung handsfree module. Based on the driver, the name is Cambridge Silicon bluetooth 2.0 dongle.

      Delete
  12. I have question does all formats can play in bluetooth speakers. Some dont have it. - Latestone.com where i bought a bluetooth speaker which has more options in speaker

    ReplyDelete
    Replies
    1. It should be able to play all audio supported by mplayer.

      Delete
  13. Have you been able to make it work under Jessie ? If so, how ?

    ReplyDelete
    Replies
    1. Hi Andre, It seems like the old methods are not working. I switched to PulseAudio to make it work under Jessie however, it require GUI to work. I'm still figuring out how to make it work under command line.

      Delete
    2. Here is the full post on how to make it work under Jessie.

      http://mygeeks014.blogspot.my/2017/05/audio-streaming-to-bluetooth-speaker.html

      Delete
  14. I do not run like you that music. You can help me

    ReplyDelete
  15. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  16. hi! really thorough tutorial, much appreciated.
    so i have managed to follow it through, but the song does not play,
    step 12 results in a bunch of code, where these were the points that i could find:
    BILITIES failed: invalid argument
    failed to initialise alsa:device=bluetooth
    could not open/initialise audio device-> no sound
    exiting... (end of file)

    Would really really appreciate any insights

    ReplyDelete
    Replies
    1. May i know which distro you are using? If you are using jessie, please follow instruction in post below to make it work.

      http://mygeeks014.blogspot.my/2017/05/audio-streaming-to-bluetooth-speaker.html

      Delete
  17. Hi,

    Bluetooth audio streaming is no more supported by ALSA, you have to do it with PulseAudio, find a tutorial here: http://youness.net/raspberry-pi/bluetooth-headset-raspberry-pi

    ReplyDelete
    Replies
    1. Great post. I was just about to create another post using PulseAudio and Bluez5. Will publish once complete =)

      Delete
    2. Here you go. http://mygeeks014.blogspot.my/2017/05/audio-streaming-to-bluetooth-speaker.html

      Delete