Bluetooth audio receiver linux

Can I use my computer as an A2DP receiver / bluetooth speaker?

To use your Ubuntu machine as an a2dp Bluetooth device, you must first configure it to register as an «a2dp sink» endpoint.

The bluez package in Ubuntu (10.04 and above) includes a utility called sdptool that can be used to check whether a Bluetooth device is configured as an a2dp sink or not. Here is the output from sdptool run against my Bluetooth headset (the address is taking from the «Bluetooth settings» dialog in the gnome-control-center):

$ sdptool search --bdaddr 00:18:16:3A:3B:D4 a2snk Searching for a2snk on 00:18:16:3A:3B:D4 . Service RecHandle: 0x10002 Service Class ID List: "Audio Sink" (0x110b) Protocol Descriptor List: "L2CAP" (0x0100) PSM: 25 "AVDTP" (0x0019) uint16: 0x102 Profile Descriptor List: "Advanced Audio" (0x110d) Version: 0x0102 $ 

and here is the output when run against my local Ubuntu machine:

$ sdptool search --bdaddr local a2snk Searching for a2snk on FF:FF:FF:00:00:00 . $ 

This shows that the Ubuntu machine is not advertising itself as an a2dp sink. It is advertising itself as an a2dp source however:

$ sdptool search --bdaddr local a2src Searching for a2src on FF:FF:FF:00:00:00 . Service Name: Audio Source Service RecHandle: 0x10003 Service Class ID List: "Audio Source" (0x110a) Protocol Descriptor List: "L2CAP" (0x0100) PSM: 25 "AVDTP" (0x0019) uint16: 0x102 Profile Descriptor List: "Advanced Audio" (0x110d) Version: 0x0102 

This enables the Ubuntu machine to pair correctly with the headset as an audio source, but it does not enable using the Ubuntu machine as an output (sink) for Bluetooth audio.

If you edit /etc/bluetooth/audio.conf , you can enable a2dp sink support by adding this line underneath the [General] section:

This is both counterintuitively named—since what we’re adding here is Bluetooth sink support, not source support—and in contradiction with the comment in this file that claims all implemented services are enabled by default. 🙁

After making this change, you will need to restart bluetoothd by running sudo service bluetooth restart .

If you have previously paired your Android device and Ubuntu computer while trying to get this work, you will need to delete the pairing on both sides and re-pair them in order to get Android to recognize Ubuntu as an available audio device.

Once you’ve done this, the Android device should show up as an input device under PulseAudio. If PulseAudio does not detect the new Bluetooth audio source, you may need to install and load the Bluetooth module, from the command line:

 sudo apt-get install pulseaudio-module-bluetooth pactl load-module module-bluetooth-discover 

Then you need to tell PulseAudio to route this audio input to your output/sink (such as your speakers, or a Bluetooth headset) using a loopback connection (a straight line from a source to a sink).

Читайте также:  Linux посмотреть мой ip

Later versions of PulseAudio may have module-bluetooth-policy included and it may already have set up a loopback device for you, but this does not seem to be the most common case.

A semi-automatic way to set up the loopback connection — if you also have the pavucontrol GUI program installed — is to simply load the loopback module and configure it using pavucontrol, since PulseAudio will remember the settings. Loading the module is done from the command using pactl:

 pactl load-module module-loopback 

Don’t fear if you don’t hear anything yet, or if you get strange feedback effects, we need to tell the newly created device which source to get input from and which sink to send the output to first.

Open pavucontrol and open its Configuration tab. Make sure your Bluetooth device shows up here (after pairing with it using blueman-manager or another Bluetooth tool) and the profile is set to A2DP. Switch to the Input devices tab and make sure your device shows up here as well and is not muted. Now switch to the Recording tab and make the newly created loopback connection use your device as a source with the select box next to the mute button. Switch to the Playback tab to select the sink the loopback connection should use in the same way. If the loopback device does not show in the tabs, make sure all streams are displayed using the selectbox at the bottom of each tab.

You should now hear the audio from your device, if it’s playing, in your speakers, or whichever output sink you selected. When your device is disconnected the loopback device will fallback to an available sink and source, which may not be desirable, so make sure you mute the loopback device until you need it. The loopback connection will restore the same sink/source the next time the same Bluetooth device is connected thanks to the module-*-restore modules.

If that doesn’t work, or you don’t have pavucontrol installed, you may instead set up the loopback using the following method:

 pactl load-module module-loopback source_dont_move=yes source=$BTSOURCE sink=$SINK 

(Replace $BTSOURCE with the source name for your Bluetooth device as seen by PulseAudio, e.g. bluez_source.14_DA_E9_2A_D7_57 ; and replace $SINK with the name of the PulseAudio output you want to send the audio stream to, e.g: alsa_output.pci-0000_00_1b.0.analog-stereo . You may leave out the sink argument entirely and have it fallback to an active sink, and change it later via pavucontrol.)

  • You can find $SINK with pactl list sinks , it’s shown after Name:
  • Similarly you can see the $BTSOURCE with pactl list sources
  • The source_dont_move argument prevents the loopback connection from falling back to another audio source when the Bluetooth device is disconnected. It is instead removed and you’ll have to set it up again next time.
Читайте также:  Системный администратор обучение linux

Here’s what an example one would look like (Remember to replace the : with _ in the Bluetooth address!):

pactl load-module module-loopback source_dont_move=yes source=bluez_source.14_DA_E9_2A_D7_57 sink=alsa_output.pci-0000_00_14.2.analog-stereo 

If loading the fallback module fails, try removing the source_dont_move=yes argument, it was made available first in version 1.0. Then be careful however to remove this loopback connection before you drop this audio, or if for instance your laptop microphone is unmuted, you may get some very bad feedback. To drop this loopback connection manually when you’re done, run:

pactl unload-module $(pactl list short modules | grep "loopback.*$BTSOURCE" | cut -f1) 

Again, replace $BTSOURCE with the name for the PulseAudio source that refers to your Bluetooth device. You may also unload the module using the id returned by the load-module command:

$ pactl load-module module-loopback source_dont_move=yes source=bluez_source.14_DA_E9_2A_D7_57 sink=alsa_output.pci-0000_00_14.2.analog-stereo 15 $ pactl unload-module 15 

Источник

Linux. Делаем Bluetooth колонку из ПК

Соглашусь, задача странная на первый взгляд. Но по крайне мере я искал информацию об этом и судя по сообщениям на форумах я не единственный и явно не последний. А поскольку такой простой инструкции на русском языке в интернете нет, я её напишу.

Я делал такое на Debian 9 x32, Lubuntu 18.04 x32, Debian 11 x64 и Ubuntu 21.04, на других дистрибутивах даже не основанных на Debian тоже будет работать если там можно установить приложения упомянутые в этой инструкции. Действия описаны на примере Debian, если у вас другой дистрибутив замените apt на ваш пакетный менеджер или ищите иной способ установки тех же приложений.

Требования

Подключённое и исправное устройство вывода звука (наушники/колонки/динамики) и рабочий сервер PulseAudio. Чтобы это проверить воспроизведите что-нибудь через mplayer с ключом -ao pulse , например мой рингтон.

sudo apt-get install mplayer -y mplayer -ao pulse https://iasivan.ru/ringtone_zemlyane.mp3

Ну и конечно же Bluetooth адаптер. Он может быть встроенный либо внешний, подключённый в USB. Не знаете есть ли он? Проверьте.

Читайте также:  Dell latitude 120l linux

Приступим

Устанавливаем необходимые 3 пакета.
BlueZ — стек основных протоколов Bluetooth
Bluetooth — служба и консольный интерфейс для работы с Bluetooth
pulseaudio-module-bluetooth — связующее звено между Bluetooth и PulseAudio

sudo apt-get install bluetooth bluez pulseaudio-module-bluetooth -y

Добавим 2 плагина в автозапуск PulseAudio. Для этого нужно в файл /etc/pulse/system.pa дописать две строки. Сделаем это через echo чтобы вы могли просто скопировать команды даже не читая этот текст.

sudo echo "load-module module-bluetooth-policy" >> /etc/pulse/system.pa sudo echo "load-module module-bluetooth-discover" >> /etc/pulse/system.pa

Ну и конечно же перезапустим службу bluetooth и сервер PulseAudio. Именно в таком порядке.

sudo systemctl restart bluetooth.service pulseaudio --kill sleep 1 pulseaudio --start

По сути уже можно использовать, но такое устройство будет отображаться в Bluetooth поиске как ПК, заменим class устройства в поиске оно считалось колонкой.
Для начала отключим плагин hostname в bluetoothd чтобы class не считывался с самого устройства. Открываем файл /etc/systemd/system/bluetooth.target.wants/bluetooth.service, находим в нём параметр ExecStart и в конце строки дописываем —noplugin=hostname .

sudo nano /etc/systemd/system/bluetooth.target.wants/bluetooth.service

Теперь собственно сменим class. Открываем файл /etc/bluetooth/main.conf, в нём находим параметр class , убираем # в начале строки если есть, стираем текущее значение и вводим то что соответствует колонке, а именно 0x1c0420 . Можно и не только колонкой его сделать, class другого устройства можете нагуглить.

Поскольку мы отключили плагин hostname у ПК не будет название в поиске Blutooth. Чтобы это исправить редактируем файл /var/lib/bluetooth/00:1A:7D:DA:71:13/settings, при чём вместо 00:1A:7D:DA:71:13 будет адрес вашего адаптера. Удобнее всего найти файл settings через консольный файломый менеджер mc. Папка в нём открывается на Enter, навигиция стрелочками на клавиатуре, редактировать выбранный файл — F4.

sudo apt-get install mc sudo mc /var/lib/bluetooth/

В этот файл пишем следующее. Вместо IAS-Speaker придуманное вами название.

После проделанного разумеется перезапускам службу bluetooth.service.

sudo systemctl restart bluetooth.service

Использование

Открываем bluetoothctl — это консольный интерфейс управления Bluetooth. Делаем устройство видимым и доступным.

discoverable on pairable on

Ищем устройство с телефона. Отправляем запрос на сопряжение и на телефоне соглашаемся.

В терминале появится строка вроде [CHG] Device A4:55:90:BF:4B:1F Connected: yes здесь A4:55:90:BF:4B:1F это адрес телефона, его нужно добавить в доверенные чтобы с телефона можно было подключаться.

Из bluetoothctl можно выйти командой exit. В таком случае ПК перестанет отображаться в поиске, но с телефона который мы добавили в доверенные можно будет подключиться в любое время. ПК будет работать как колонка, звук выводимый телефоном на Bluetooth колонку будет воспроизводиться в динамики/колонки/наушники ПК.

Всего вам хорошего, друзья!
Изучайте Linux. Используйте Linux. С ним ваши возможности почти безграничны.

Источник

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