Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add Bluetooth support #150

Closed
Tracked by #439
linknum23 opened this issue Jul 30, 2021 · 25 comments · Fixed by #435
Closed
Tracked by #439

Feature: Add Bluetooth support #150

linknum23 opened this issue Jul 30, 2021 · 25 comments · Fixed by #435
Projects
Milestone

Comments

@linknum23
Copy link
Contributor

How should Bluetooth support be added to AmpliPi?

@linknum23
Copy link
Contributor Author

linknum23 commented Jul 30, 2021

@vszander has started working on integrating Bluetooth audio into AmpliPi, here's his question (with minor edits):

I was able to stream Bluetooth audio from my smart phone to my AmpliPi dev image using this method which is compatible with ALSA. The Bluetooth stream is going to the default channel on the soundcard. How can we make the Bluetooth stream an AmpliPi input?

@linknum23
Copy link
Contributor Author

First of all thanks for looking into this, @vszander! Based on how streams currently work it looks like you will want to test this by launching bluealsa-aplay from the command line on your pi (assuming you have already installed it).
Looking at the docs for bluealsa-aplay it looks like you just need to specify the ALSA device using the -D flag, so you may want to test using bluealsa-aplay -D ch0 (and ch1, ch2...). Try it out and see how it works for you (and report back your findings).

@linknum23 linknum23 added this to To do in AmpliPi Aug 4, 2021
@kjk2010
Copy link
Contributor

kjk2010 commented Aug 13, 2021

I was playing around with this today and have it working on my Pi 2b with a USB bluetooth adapter, outputting through ALSA to ch0/ch1/ch2/ch3. I think it would be easy to create a simple AmpliPi stream for this, and could quickly get more complicated if someone wanted to control specific Bluetooth devices or run multiple Bluetooth streams at once. Here is the information I used and how I configured it:

Setup Guide: https://gist.github.com/Pindar/e259bec5c3ab862f4ff5f1fbcb11bfc1
Controlling Bluetooth and Metadata: https://scribles.net/controlling-bluetooth-audio-on-raspberry-pi/
Bluealsa - Available Features (aac, apt-x): https://github.com/Arkq/bluez-alsa/wiki/Installation-from-source

INSTALL:

sudo apt-get install bluealsa python-dbus libasound2-dev

SETUP (these services should only be running when the AmpliPi stream is active):

sudo systemctl disable bluetooth
sudo systemctl disable bluealsa

##Edit /etc/bluetooth/main.conf:
Name = AmpliPi Bluetooth
DiscoverableTimeout = 0
Class = 0x200400

##Set up A2DP Bluetooth Agent for headless bluetooth discovery
https://gist.github.com/Pindar/e259bec5c3ab862f4ff5f1fbcb11bfc1#install-the-a2dp-bluetooth-agent

START (when the AmpliPi stream is started, run these in this order):

sudo systemctl start bluetooth
sudo systemctl start bluealsa
sudo systemctl start bt-agent-a2dp.service # This service is used to automatically allow bluetooth devices to connect)
bluealsa-aplay -d ch0 00:00:00:00:00:00 # This is the process that takes the audio from bluetooth and sends to ALSA via -d. The 00:00... allows for any bluetooth device. Maybe this could be used to send different bluetooth devices out different AmpliPi outputs?

STOP (when AmpliPi stream is stopped, run these in this order):

kill bluealsa-aplay
sudo systemctl stop bt-agent-a2dp.service
sudo systemctl stop bluealsa
sudo systemctl stop bluetooth

@linknum23
Copy link
Contributor Author

Is there a specific reason all of those Bluetooth services need to be stopped and started every time a Bluetooth stream is connected? Is this just so that we can have control over the name to connect to? Or maybe the security issue of always allowing any Bluetooth device to connect?

Here's what I am hoping the workflow for Bluetooth devices would look like:

  • At AmpliPi startup
    • Discover Bluetooth device, enable/disable Bluetooth single stream in the loaded configuration based on availability (we will need to add this disable/enable streams field)
  • On stream connection
    • connect Bluetooth device to one of the outputs (ch0-3)

Also as a side note, I'm guessing those of you that are using Bluetooth will be using a module with a nice antenna to get extra range? I was just looking at this: https://www.amazon.com/dp/B08LVH5BCP, maybe we should buy one and test it out around Jason's house.,.

@linknum23 linknum23 moved this from To do to In progress in AmpliPi Aug 13, 2021
@kjk2010
Copy link
Contributor

kjk2010 commented Aug 13, 2021

My reasoning behind starting and stopping the services is that my phone will stay connected to the RPi's Bluetooth and continue streaming audio, even though bluealsa-aplay isn't running. At least in my use case, I would only want my phone to connect to Bluetooth on the AmpliPi when one of the sources has a Bluetooth stream running, then disconnect from it when the stream has been stopped.
It might be possible to script something with bluetoothctl to allow and drop connected devices when a stream is activated and deactivated, but I'm not sure.

I don't believe it's a security issue leaving the services running and automatically accepting connections, as long as the Class setting is set to 0x200400 which says only audio devices can connect (http://bluetooth-pentest.narod.ru/software/bluetooth_class_of_device-service_generator.html).

I like the look of that Bluetooth adapter. I do plan on using one like that instead of my current tiny dongle that only reaches 10ft.

@linknum23
Copy link
Contributor Author

I think that use case makes the most sense. Is there anyway to enable/disable a single Bluetooth device, instead of the whole service? That could potentially simplify things here and also provide a longer term route to support 2 or more Bluetooth devices/streams.

@kjk2010
Copy link
Contributor

kjk2010 commented Aug 16, 2021 via email

@linknum23
Copy link
Contributor Author

linknum23 commented Aug 16, 2021

Please forgive me if I messed something up here as I don't have a Bluetooth device connected to AmpliPi to test with.

  1. Doesn't the device name already exist in some paired devices list, since it needs to be paired with the Rpi's Bluetooth? We could expose this device list in the API to build a drop down, in addition to having some way indicate a wildcard device.
  2. If you gracefully signal the bluealsa-aplay process (e.g. via Ctrl-c), does it disconnect from your phone/Bluetooth-audio-source?

I'm still trying to reduce the complexity of starting and stopping the Bluetooth stack here...

@kjk2010
Copy link
Contributor

kjk2010 commented Aug 17, 2021 via email

@bs42
Copy link

bs42 commented Aug 23, 2021

A vote for supporting more than 1 Bluetooth stream.

Scenario: 2 kids each with a set of ceiling speakers in their rooms.

I was planning to get a pair of BT to RCA devices to handle this, but if it could be done directly on the device with a BT dongle instead of using up RCA inputs that would be fantastic!

@vszander
Copy link

Sorry I dropped off line recently....
I love kjk2010's suggested approach. Unfortunately, I was unable to implement - but I didn't take time to MAKE the suggested driver. I also like supporting multiple bluetooth. @kjk2010 , super writeup !

@kjk2010
Copy link
Contributor

kjk2010 commented Oct 4, 2021

I've set up basic Bluetooth support on my dev Pi, and the good news is it can support at least 2 active streams to different zones using a single Bluetooth dongle. I still have to figure out how to handle disconnecting devices when the Bluetooth stream isn't active, reconnecting when it is active, pairing new devices, and the possibility of accessing metadata and using remote controls (next,.prev, start, pause).

@linknum23
Copy link
Contributor Author

@kjk2010 We are getting a long range Bluetooth adapter tomorrow, Let us know if there's anything you would like help with.

@Lohrer Lohrer moved this from In progress to To do in AmpliPi Nov 17, 2021
@bs42
Copy link

bs42 commented Nov 22, 2021

I had a thought today, this would be a low priority item but it could be really useful to have bluetooth output as an option.

Use case: I have a yamaha receiver running a room with 7ch speakers and want to sync that up with the music playing in the rest of the house and bluetooth is something built into that receiver which could be a way to accomplish that link other than trying to run a set of RCA cables through the walls to get them over to the AmpliPi and take up one of it's outputs.

@linknum23
Copy link
Contributor Author

I haven't fully thought through how to handle extra outputs, one limitation of the current design of AmpliPi would only be able to support this for digital/streaming input sources. That said Bluetooth would be a pretty cool way to support this. Another way to integrate with smart receivers would be with DLNA.

@linknum23 linknum23 moved this from To do to In progress in AmpliPi Dec 28, 2021
@linknum23 linknum23 moved this from In progress to Up Next in AmpliPi May 4, 2022
@Lohrer Lohrer moved this from Up Next to Planned in AmpliPi Jun 2, 2022
@kjkoning
Copy link
Contributor

I've put together a basic, but fully-functioning Bluetooth stream example here: https://github.com/kjkoning/amplipi-bluetooth-example
It requires a USB Bluetooth dongle. I'm using this one on my AmpliPi: https://www.amazon.com/gp/product/B08LVH5BCP/, but it should work with any Linux-compatible adapter.

I'm curious for any feedback, both on if it works at all for others, and if the way it works makes sense and is user-friendly. If this is something worthy of a PR to include in a future version, there will need to be some work on making sure a Bluetooth adapter is installed and working before allowing the Bluetooth stream to be selected.

@linknum23
Copy link
Contributor Author

Awesome! We'll try integrating this and see where it goes. I think the combination of this and our initial start on bluetooth here will hopefully get us -pretty far.

@linknum23 linknum23 moved this from Planned to In progress in AmpliPi Dec 15, 2022
@jonahshader
Copy link
Contributor

@kjkoning I'm trying out your code and I can connect and play music to the Bluetooth stream successfully! Did you also get the media controls working? For me, bluetooth.py is crashing because the dbus module doesn't exist for some reason, which is needed by dbus_tools in bluezero. I have python-dbus/python3-dbus installed so the dbus module should exist.

@jonahshader
Copy link
Contributor

Ah I fixed it. The packages were only in the venv, so I had to change the subprocess launches to use the venv instead of python3.

@linknum23 linknum23 changed the title Feature Request - Add Bluetooth support Feature - Add Bluetooth support Feb 2, 2023
@linknum23 linknum23 changed the title Feature - Add Bluetooth support Feature: Add Bluetooth support Feb 2, 2023
@linknum23 linknum23 linked a pull request Mar 20, 2023 that will close this issue
3 tasks
@linknum23
Copy link
Contributor Author

We added a prerelease with Bluetooth support. Check it out at https://github.com/micro-nova/AmpliPi/releases/tag/0.2.1-bluetooth-beta.0

@linknum23
Copy link
Contributor Author

Has anyone had a chance to evaluate this? We haven't gotten any feedback yet...

@rwarner
Copy link

rwarner commented Aug 3, 2023

Was just looking into bluetooth to add onto the AmpliPi and stumbled across this feature thread.

I already ordered a bluetooth receiver and some RCA cables because the distance from the unit to my need of bluetooth might not cover it. But, a USB extension may assist in that regard and not have the need for either and not need to power another entity.

Would I lose my settings switching to the pre-release bluetooth support build and/or if I wanted to switch off of it? Not sure when you guys were looking to fold it into the future 0.2.2+

@linknum23
Copy link
Contributor Author

linknum23 commented Aug 3, 2023 via email

@kjkoning
Copy link
Contributor

kjkoning commented Sep 3, 2023

I've been using the prerelease, and it's working well. No issues to report yet.

@linknum23
Copy link
Contributor Author

linknum23 commented Sep 29, 2023 via email

@rtertiaer rtertiaer added this to the 0.3.0 milestone Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
AmpliPi
In progress
Development

Successfully merging a pull request may close this issue.

8 participants