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/