Monday, August 20, 2018

Raspberry Pi Bluetooth speakers

Hello everyone,

I recently uploaded a video to share more detail on how I turned a Raspberry Pi into a custom Bluetooth speaker for my carpc project. I thought it would be cool to share a step-by-step description of the entire project. Enjoy!

In this post I'll assume you have a Raspberry Pi 2B with a working installation of Raspbian Stretch, a USB Bluetooth dongle, and a speaker. Start by updating the Pi and running some preparatory commands in a console.

sudo apt-get update
sudo apt-get dist-upgrade -y
sudo rpi-update
sudo poweroff

Restart the Pi and install all needed packages for Bluez and PulseAudio.

sudo apt-get install bluez pulseaudio-module-bluetooth python-gobject python-gobject-2

Add the active user to the lp group.

sudo usermod -a -G lp pi

Edit the Pulseaudio daemon configuration file and change the resampling method to trivial. In my file, the relevant configuration line was present but commented out.

sudo vim /etc/pulse/daemon.conf
    >> resample-method = trivial

Enable required functionality of the Bluetooth module by adding the following line to the General section of the Bluetooth main configuration file.

sudo vim /etc/bluetooth/main.conf
    >> Enable=Source,Sink,Media,Socket

Now reboot.

sudo reboot

Activate the Bluetooth service and verify that the adapter is recognized as hci0 by hciconfig.

sudo service bluetooth start
sudo service bluetooth status
sudo hciconfig -a

Activate the Bluetooth on your phone, and run a device scan on the Pi. Take note of the Bluetooth address of your phone (XX:XX:XX:XX:XX:XX). Then, add the piscan option to hciconfig, and verify that a line saying "UP RUNNING PSCAN ISCAN" in all caps is present in the output of hciconfig -a. When you are set, run the bluetoothctl agent.

sudo hcitool scan
sudo hciconfig hci0 piscan
sudo hciconfig -a
bluetoothctl

Now go to your phone and pair it with the Pi. You may be asked to provide a PIN of your choice. The bluetoothctl agent will ask you to provide the same PIN to greenlight the operation. If successful, authorize the two services proposed by bluetoothctl (multimedia and audio calls), and run the following commands in the bluetoothctl prompt to tell Bluez you trust your phone.

agent on
default-agent
trust XX:XX:XX:XX:XX:XX
quit

Excellent. now we need to identify possible data sources, data sinks, and connect the Bluetooth source to the audio sink. Take note of the Bluez source and of the complete name of the default sink as returned by pactl. Use the two strings as "source" and "sink" arguments to "pactl load-module module-loopback".

pactl list sources short
pactl list sinks short
pactl load-module module-loopback source=bluez_source.XX_XX_XX_XX_XX_XX.a2dp_source sink=alsa_output.platform-soc_audio.analog-stereo
# For source and sink, use the relevant output of the two relevant pactl list commands, and your phone address.

The last step is to redirect all HDMI audio to the jack, and to pump the volume up.

amixer cset numid=3 1
amixer set Master 100%
pacmd set-sink-volume 0 65537

From this moment on, all audio produced by your phone, including music, ringtones, alarms, and voice calls, is played by the speaker connected to the Raspberry Pi. The audio source can be any Bluetooth-capable device. The pin-based authentication step is done only once. After that, the device is trusted and pairing does not require any human action on the Pi.

So, this is how I turned a Raspberry Pi into a Bluetooth speaker for my carpc project. It was good fun, and I encourage you to play around with Bluetooth communication on your devices. Bluetooth is so widely spread and supported, and it is a good option for low-bandwidth wireless communication between DIY projects and commercial devices.

Have you ever worked with Bluetooth? Have you ever made something cool with it? Share your experience!

Have fun,
Kradion.

15 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hello,

    I am currently trying to do this small project.
    I have Raspberry pi-3 model B+.

    Will the same code can be used for pi-3 ?

    If not then please kindly guide me.


    Your quick response will be appreciated.

    Thank you

    Regards

    ReplyDelete
    Replies
    1. please help me. by accident I deleted the folder "press" and not recover it or in the recycling bin will help :'c

      Delete
    2. Sorry, it's the folder "pulse"

      Delete
  3. Hi, i want to rename the Bluetooth name, i do i do that?

    ReplyDelete
  4. Hi, i want to rename the Bluetooth name, how do i do that?

    ReplyDelete
  5. Hello,

    This blog was very helpful. I am actually working on smart helmet for cycle.
    How can I activate phone calling function to the audio speaker using raspberry pi 3+ ??
    Kindly guide me .

    Thank you

    Regards

    Regards

    ReplyDelete
  6. How can I make bluethoot turns on automatically on every turn on?

    ReplyDelete
  7. After installing blue when i check for service it is showing
    blue: unrecognized service

    ReplyDelete
  8. Is it Possible to create a little GUI which display the songname and a button to skip or pause/play songs

    ReplyDelete
  9. Play Free Games on Rollercoin, Earn Bitcoin, doge and Etherium:
    So why not join rollercoin.com to play with.
    #AFFILIATE

    ReplyDelete
  10. WhatsApp. http://wa.me/17077013848
    Get (IELTS, OET, PTE , GRE,NCLEX And TOEFL)
    -we update and modify your poorly performed scores directly from the system
    -we can control and change your scores from the the system while you attend the exam
    -we also provide leak questions and answers for upcoming exams
    -Provide certificate with online verification on the official website.

    We are a network of English Language Professors with years of Examination experience. During these years, we have been able to derive backdoor means of registering IELTS certificates without Students taking the Test. With our help, you can be able to get real registered and original IELTS Certificates without facing the stress and trauma of the Exam. The IELTS Certificates we issue carries a score of your choosing and you will be able to verify it online and collect the original TRF or Test report card from local district Examination Center or we send it directly to you contact us using the details below and know more about the process

    WhatsApp:+1(707)701-3848
    Email: buyrealdocumentshome@gmail.com

    BUY READY BLANK ATM CARD, READY TO HIT ANY ATM MACHINE WITHOUT ANY PIN CODE, WITH A MAXIMUM WITHDRAWAL OF $5.000.00 PER DAY AND A LIFE SPAN OF 4WEEKS(28days) OPERATIONAL.
    Credit Card Hack Software

    Need a Debt Loan To Pay Off Bills?
    Take control of your debt today
    Available Now Business Expansion Loan Offer?
    Do you need a loan to pay off Bills?
    Do you need a loan?
    Do you need Personal Loan?
    Business Expansion Loan?
    Business Start-up, Education,
    Debt Consolidation Loan
    Hard Money Loans
    Loan for any thing ?
    We offer loan at low interest rate of 3%

    WhatsApp:+1(707)701-3848
    Email: buyrealdocumentshome@gmail.com

    -Undetected Fake bank statement for loan
    -fake bank statement / Real Verify Bank Statement
    -Clear your criminal record from any Country database system
    -Hack any phone from a distance without physical contact.
    -Increase your credit score to your desired amount
    -Buy fake drivers license, I’d, ssn, Passport
    -Buy clone credit card ready to hit any atm machine, gift card amazon card, etc Fake bank statement for loan
    -Hack any online banking account, PayPal account, bitcoin, flight tickets
    -Buy any school certificate, diploma, degree all registered under the database system
    -Fake bank account/verified bank account with login details for a loan
    — Our bank articulation is utilised to demonstrate your loan specialists or customers you possess the accurate offset on the announcement with a 6months exchange notable that can be checked through any strategy for their decision, yet you can't move or pull back any cash from this record, for more details visit our website of Fake bank statement for loan

    WhatsApp:+1(707)701-3848
    Email: buyrealdocumentshome@gmail.com


    Texas drivers license replacement, lost drivers license texas, lost texas drivers license, replace texas drivers license, how to get a drivers license in texas, texas drivers license template, oklahoma drivers license, colorado drivers license, kansas drivers license, drivers license utah, fake drivers license, ny drivers license document number, idaho drivers license renewal, florida birth certificate, oklahoma birth certificate, illinois birth certificate, oregon birth certificate, maryland birth certificate, birth certificate michigan, arizona birth certificate, new jersey birth certificate, idaho birth certificate,Fake bank statement for loan, los angeles county birth certificate, birth certificate virginia, wisconsin birth certificate, mississippi birth certificate, fake birth certificate, alameda county birth certificate, utah birth certificate, nj birth certificate, nebraska birth certificate, blank birth certificate, birth certificate oregon, minnesota birth certificate, certificate of live birth, last 4 digits of ssn, australian ssn, fake ssn card, ssn background checks, adoption certificate, certificate of adoption, cat adoption certificate, pre adoption birth certificate, puppy adoption certificate, certificate of adoption

    WhatsApp:+1(707)701-3848
    Email: buyrealdocumentshome@gmail.com

    ReplyDelete

Featured posts

Raspberry Pi Bluetooth speakers