Jump to content

Bluetooth: Difference between revisions

6,766 bytes added ,  Friday at 15:01
m
imported>Samueldr
m (re-worked the page; added non-gui and gui variants of pairing and for pulseaudio management.)
(39 intermediate revisions by 25 users not shown)
Line 1: Line 1:
==Enabling Bluetooth support==
To enable support for Bluetooth devices, amend your system configuration as follows:
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{
  ...
  hardware.bluetooth.enable = true; # enables support for Bluetooth
  hardware.bluetooth.powerOnBoot = true; # powers up the default Bluetooth controller on boot
  ...
}
</nowiki>}}
{{evaluate}}
==Pairing Bluetooth devices==
In order to use Bluetooth devices, they must be paired with your NixOS machine. Heavier desktop environments will usually provide a Bluetooth management GUI which you can use to pair devices.


== Pairing ==
If your desktop environment does not provide such a GUI, you can additionally enable the blueman service, which provides blueman-applet and blueman-manager with the snippet below.
 
<syntaxhighlight lang="nix">
Bigger desktop environments will usually provide a bluetooth management system.
services.blueman.enable = true;
</syntaxhighlight>


{{expansion}}
===Pairing devices from the command line===
 
Alternatively, Bluetooth devices can be paired from the command line using <tt>bluetoothctl</tt>.
Alternatively, using <code>pkgs.blueman</code>, it is possible to get a tray icon <code>blueman-applet</code> or use the manager directly using <code>blueman-manager</code>.
 
=== Pairing without GUI tools ===
 
When not using a desktop-manager, configuring the device from the command line can be done following the [https://wiki.archlinux.org/index.php/Bluetooth_Headset ArchWiki instructions], as follows.


<syntaxhighlight lang="console">$ bluetoothctl
<syntaxhighlight lang="console">$ bluetoothctl
Line 21: Line 30:
[bluetooth] # connect [hex-address]</syntaxhighlight>
[bluetooth] # connect [hex-address]</syntaxhighlight>


== Bluetooth headsets with PulseAudio ==
Bluetooth devices automatically connect with <tt>bluetoothctl</tt> as well:


The package used for PulseAudio has to be <code>pkgs.pulseaudioFull</code>, which includes bluetooth support.
<syntaxhighlight lang="console">
$ bluetoothctl
[bluetooth] # trust [hex-address]
</syntaxhighlight>
 
==Using Bluetooth headsets with PulseAudio==
 
To allow Bluetooth audio devices to be used with PulseAudio, amend <tt>/etc/nixos/configuration.nix</tt> as follows:


<syntaxhighlight lang="nix">{
<syntaxhighlight lang="nix">{
  hardware.pulseaudio.enable = true;
  hardware.bluetooth.enable = true;
}</syntaxhighlight>
{{evaluate}}
You will need to restart PulseAudio; try <tt>systemctl --user daemon-reload; systemctl --user restart pulseaudio</tt>.
You can verify that PulseAudio has loaded the Bluetooth module by running <tt>pactl list | grep -i 'Name.*module.*blue'</tt>; Bluetooth modules should be present in the list.
== Using Bluetooth headset buttons to control media player ==
Some bluetooth headsets have buttons for  pause/play or to skip to the next track.
To make these buttons usable with media players supporting the dbus-based [https://specifications.freedesktop.org/mpris-spec/latest/ MPRIS] standard,
one can use <code>mpris-proxy</code> that is part of bluez package.
The following snippet can be used in [[Home Manager]] to start this program as a daemon:
<syntaxHighlight lang="nix">
systemd.user.services.mpris-proxy = {
    description = "Mpris proxy";
    after = [ "network.target" "sound.target" ];
    wantedBy = [ "default.target" ];
    serviceConfig.ExecStart = "${pkgs.bluez}/bin/mpris-proxy";
};
</syntaxHighlight>
Or, starting with Home Manager 21.05, enable the <code>mpris-proxy</code> service.
===System-Wide PulseAudio ===
{{Expansion|When setting up pulseaudio systemWide extra policies needs to be deployed for pulse to be able to connect to the bluetooth stack. The Info below is not enough.}}
When you are running PulseAudio system-wide then you will need to add the following modules to your <code>default.pa</code> configuration:
<syntaxHighlight lang="nix">
hardware.pulseaudio.configFile = pkgs.writeText "default.pa" ''
  load-module module-bluetooth-policy
  load-module module-bluetooth-discover
  ## module fails to load with
  ##  module-bluez5-device.c: Failed to get device path from module arguments
  ##  module.c: Failed to load module "module-bluez5-device" (argument: ""): initialization failed.
  # load-module module-bluez5-device
  # load-module module-bluez5-discover
'';
</syntaxHighlight>
===Enabling extra codecs===
While pulseaudio itself only has support for the SBC bluetooth codec there is out-of-tree support for AAC, APTX, APTX-HD and LDAC.
To enable extra codecs add the following to <tt>/etc/nixos/configuration.nix</tt>:
<syntaxHighlight lang="nix">
{
...
   hardware.pulseaudio = {
   hardware.pulseaudio = {
     enable = true;
     enable = true;
     package = pkgs.pulseaudioFull;
     package = pkgs.pulseaudioFull;
   };
   };
   hardware.bluetooth.enable = true;
...
}
</syntaxHighlight>
 
===Enabling A2DP Sink===
Modern headsets will generally try to connect using the A2DP profile. To enable this for your bluetooth connection, add the following to <tt>/etc/nixos/configuration.nix</tt>
 
<syntaxhighlight lang="nix">
{
  hardware.bluetooth.settings = {
    General = {
      Enable = "Source,Sink,Media,Socket";
    };
   };
}
</syntaxhighlight>
This configuration may be unnecessary and does not work with bluez5 (<tt>Unknown key Enable for group General</tt> ).
 
===Managing audio devices===
 
<tt>pavucontrol</tt> can be used to reconfigure the device:
* To enable A2DP, change the profile to &#8220;High Fidelity Playback (A2DP Sink)&#8221; on the &#8220;Configuration&#8221; tab.
* To set the device as the default audio output, select &#8220;set as fallback&#8221; on the &#8220;Output Devices&#8221; tab.
 
Alternatively, the device can be configured via the command line:
 
* To enable A2DP, run: <syntaxhighlight lang="console">$ pacmd set-card-profile "$(pactl list cards short | egrep -o bluez_card[[:alnum:]._]+)" a2dp_sink</syntaxhighlight>
* To set the device as the default audio output, run: <syntaxhighlight lang="console">$ pacmd set-default-sink "$(pactl list sinks short | egrep -o bluez_sink[[:alnum:]._]+)"</syntaxhighlight>
 
You can also set pulseaudio to automatically switch audio to the connected bluetooth device when it connects, in order to do this add the following entry into the pulseaudio config
 
<syntaxhighlight lang="nix">{
...
hardware.pulseaudio.extraConfig = "
  load-module module-switch-on-connect
";
...
}</syntaxhighlight>
 
Note that you may need to clear the pulseaudio config located at ~/.config/pulse to get this to work. Also you may have to unset and then set the default audio device to the bluetooth device, see https://github.com/NixOS/nixpkgs/issues/86441 for more info
 
==Showing battery charge of bluetooth devices==
If you want to see what charge your bluetooth devices have you have to enable experimental features, which might lead to bugs (according to [https://wiki.archlinux.org/title/Bluetooth_headset#Battery_level_reporting Arch Wiki). You can add the following to your config to enable experimental feature for bluetooth:
<syntaxhighlight lang="nix">{
...
hardware.bluetooth.settings = {
General = {
Experimental = true;
};
};
...
}</syntaxhighlight>
}</syntaxhighlight>
Afterwards rebuild your system and then restart your bluetooth service by executing  <syntaxhighlight lang="console">$ systemctl restart bluetooth</syntaxhighlight>.


{{evaluate}}
==Troubleshooting==
===USB device needs to be unplugged/re-plugged after suspend===
Some USB device/host combinations don't play well with the suspend/resume cycle, and need to be unplugged and then re-plugged to work again.
 
It is possible to simulate a unplug/re-plug cycle using the <tt>/sys</tt> filesystem.


Either restart the system for PulseAudio to load the bluetooth module, or restart your local instance of PulseAudio by running <code>pkill ^pulseaudio$</code>.
[https://gist.github.com/samueldr/356e65374d452e4fd45314f818ae3545 This gist] provides a script and instructions to set-up a workaround for these devices.


Ensure PulseAudio has loaded bluetooth modules by using <code>pactl list | grep -i Name.*module.*blue</code>. Bluetooth modules should be present in the list.
===When connecting to an audio device: Failed to connect: org.bluez.Error.Failed===


=== Managing the audio interface ===
You need to use pulseaudioFull, see [[#Using Bluetooth headsets with PulseAudio]].


Using <code>pavucontrol</code>, the headset profile can be changed to ''"High Fidelity Playback (A2DP Sink)"'' in the ''Configuration'' tab.
===Bluetooth fails to power on with Failed to set power on: org.bluez.Error.Blocked===


The default sink can be changed in the ''Output Devices'' tab. clicking on the right most button, which it title ''Set as fallback''.
If <tt>journalctl -eu bluetooth</tt> shows <tt>Failed to set mode: Blocked through rfkill (0x12)</tt>, rfkill might be blocking it:


==== Managing the audio interface using <code>pacmd</code> ====
<syntaxhighlight lang="console">
$ rfkill
ID TYPE      DEVICE      SOFT      HARD
1 wlan      phy0  unblocked unblocked
37 bluetooth hci0  blocked unblocked
</syntaxhighlight>


Setting the card to A2DP
Unblock it first:


<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
$ pacmd set-card-profile "$(pactl list cards short | egrep -o bluez_card[[:alnum:]._]+)" a2dp_sink
$ sudo rfkill unblock bluetooth
</syntaxhighlight>
</syntaxhighlight>


Setting the bluetooth sink as default
=== Cannot use bluetooth while it previously worked ===
 
Symptoms:
 
* When using <code>bluetoothctl</code>, getting "No agent is registered".
* When using <code>blueman</code> or anything using dbus to talk to bluez, getting <code>dbus.exceptions.DBusException: org.freedesktop.DBus.Error.AccessDenied: Rejected send message"</code>
 
This possibly can be fixed by restarting the display-manager session. The session management may have had an issue with registering your current session and doesn't allow you to control bluetooth.


<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
$ pacmd set-default-sink "$(pactl list sinks short | egrep -o bluez_sink[[:alnum:]._]+)"
$ sudo systemctl restart display-manager.service
</syntaxhighlight>
</syntaxhighlight>


== External resources ==
=== No audio when using headset in HSP/HFP mode ===
 
If the output of <code>dmesg | grep Bluetooth</code> shows a line similar to <code>Bluetooth: hci0: BCM: Patch brcm/BCM-0a5c-6410.hcd not found</code> then your machine uses a Broadcom chipset without the required firmware installed.
 
To fix this, add <code>hardware.enableAllFirmware = true;</code> to your <tt>/etc/nixos/configuration.nix</tt> then reboot.
 
 
==See also==
* [http://anderspapitto.com/posts/2016-11-07-scripting_pulseaudio_bluetooth_jack.html Scripting PulseAudio, Bluetooth, JACK]
* [https://wiki.gentoo.org/wiki/Bluetooth Bluetooth (Gentoo Wiki)]
* [https://wiki.archlinux.org/index.php/Bluetooth Bluetooth (Arch Linux Wiki)]


* [http://anderspapitto.com/posts/2016-11-07-scripting_pulseaudio_bluetooth_jack.html Scripting pulseaudio, bluetooth, jack]
[[Category:Audio]][[Category:Configuration]][[Category:Hardware]]