Bluetooth: Difference between revisions

imported>Mdedetrich
m Minor syntax issues
m Enabling extra codecs: use non-obsolete services.pulseaudio
 
(31 intermediate revisions by 19 users not shown)
Line 1: Line 1:
==Enabling Bluetooth support==
==Setup==
To enable support for Bluetooth devices, add {{nixos:option|hardware.bluetooth.enable}} to <tt>/etc/nixos/configuration.nix</tt>:
To enable support for Bluetooth devices, amend your system configuration as follows and apply changes:
 
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
<syntaxhighlight lang="nix">{
hardware.bluetooth.enable = true;
  ...
</nowiki>}}
  hardware.bluetooth.enable = true;
==Usage==
  ...
In order to use Bluetooth devices, they must be paired with your NixOS machine. Heavier [[:Category:Desktop_environment|desktop environments]] will usually provide a Bluetooth management GUI which you can use to pair devices.
}</syntaxhighlight>
 
{{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.


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.
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">
<syntaxhighlight lang="nix">
services.blueman.enable = true;
services.blueman.enable = true;
</syntaxhighlight>
</syntaxhighlight>Another option for a GUI based Bluetooth management GUI can be [https://search.nixos.org/packages?channel=unstable&show=overskride&from=0&size=50&sort=relevance&type=packages&query=overskride overskirde]
 
Alternatively if you wish to use a TUI<ref>https://en.wikipedia.org/wiki/Text-based_user_interface</ref> then check out [https://search.nixos.org/packages?channel=unstable&show=bluetuith&from=0&size=50&sort=relevance&type=packages&query=bluetui bluetuith] or [https://github.com/pythops/bluetui bluetui]


===Pairing devices from the command line===
===Pairing devices from the command line===
Line 33: Line 28:
Bluetooth devices automatically connect with <tt>bluetoothctl</tt> as well:
Bluetooth devices automatically connect with <tt>bluetoothctl</tt> as well:


<syntaxhighlight lang="console">$ bluetoothctl
<syntaxhighlight lang="console">
[bluetooth] # trust [hex-address]</syntaxhighlight>
$ bluetoothctl
[bluetooth] # trust [hex-address]
</syntaxhighlight>


==Using Bluetooth headsets with PulseAudio==
== Tips and tricks ==


To allow Bluetooth audio devices to be used with PulseAudio, amend <tt>/etc/nixos/configuration.nix</tt> as follows:
=== 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.pulseaudio = {
    enable = true;
 
    # NixOS allows either a lightweight build (default) or full build of PulseAudio to be installed.
    # Only the full build has Bluetooth support, so it must be selected here.
    package = pkgs.pulseaudioFull;
  };
 
   hardware.bluetooth.enable = true;
   hardware.bluetooth.enable = true;
  ...
}</syntaxhighlight>
}</syntaxhighlight>


Line 60: Line 49:
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.
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 ==
=== Using Bluetooth headset buttons to control media player ===
 
Many bluetooth headsets have buttons for  pause/play or to skip to the next track.
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,
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.
one can use <code>mpris-proxy</code> that is part of bluez package.
Line 69: Line 57:
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
systemd.user.services.mpris-proxy = {
systemd.user.services.mpris-proxy = {
  Unit.Description = "Mpris proxy";
    description = "Mpris proxy";
  Unit.After = [ "network.target" "sound.target" ];
    after = [ "network.target" "sound.target" ];
  Service.ExecStart = "${pkgs.bluez}/bin/mpris-proxy";
    wantedBy = [ "default.target" ];
  Install.WantedBy = [ "default.target" ];
    serviceConfig.ExecStart = "${pkgs.bluez}/bin/mpris-proxy";
};
};
</syntaxHighlight>
</syntaxHighlight>


===System-Wide PulseAudio ===
Or, starting with Home Manager 21.05, enable the <code>mpris-proxy</code> service.
 
Some headsets (such as Sony's WH-1000XM series) also support AVRCP directly and as such <code>mpris-proxy</code> is not required for this functionality, as headset button presses will register as media key presses. If you are using a WM or a desktop environment which doesn't support media keys, you will have to set up keybindigs yourself; the keys are typically called <code>XF86AudioPlay</code>, <code>XF86AudioPause</code>, <code>XF86AudioNext</code>, and <code>XF86AudioPrev</code>. However, note that some functionality, such as "take off headphones to pause" on WH-1000XM4/5 requires two-way AVRCP communication to work properly, so `mpris-proxy` is still recommended if you wish to use it. It also may or may not help with Bluetooth multipoint.
 
==== 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.}}
{{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.}}


Line 92: Line 84:
</syntaxHighlight>
</syntaxHighlight>


===Enabling extra codecs===
==== Enabling extra codecs ====
 
'''WARNING''': The <tt>hardware.pulseaudio.extraModules</tt> option is only available in the 19.03 release or later.
 
While pulseaudio itself only has support for the SBC bluetooth codec there is out-of-tree support for AAC, APTX, APTX-HD and LDAC.
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>:
To enable extra codecs add the following to <tt>/etc/nixos/configuration.nix</tt>:
<syntaxHighlight lang="nix">
<syntaxhighlight lang="nix">
{
{
...
...
   hardware.pulseaudio = {
   services.pulseaudio = {
     enable = true;
     enable = true;
    extraModules = [ pkgs.pulseaudio-modules-bt ];
     package = pkgs.pulseaudioFull;
     package = pkgs.pulseaudioFull;
   };
   };
...
...
}
}
</syntaxHighlight>
</syntaxhighlight>


===Enabling A2DP Sink===
==== 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>
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">{
<syntaxhighlight lang="nix">{
...
  hardware.bluetooth.settings = {
hardware.bluetooth.extraConfig = "
    General = {
  [General]
      Enable = "Source,Sink,Media,Socket";
  Enable=Source,Sink,Media,Socket
    };
";
  };
...
}</syntaxhighlight>
}</syntaxhighlight>
This configuration may be unnecessary and does not work with bluez5 (<tt>Unknown key Enable for group General</tt> ).
This configuration may be unnecessary and does not work with bluez5 (<tt>Unknown key Enable for group General</tt> ).


===Managing audio devices===
==== Managing audio devices ====
 
<tt>pavucontrol</tt> can be used to reconfigure the device:
<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 enable A2DP, change the profile to &#8220;High Fidelity Playback (A2DP Sink)&#8221; on the &#8220;Configuration&#8221; tab.
Line 145: Line 131:
}</syntaxhighlight>
}</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 also have to unset and then reset the default audio device, see https://github.com/NixOS/nixpkgs/issues/86441 for more info
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>
Afterwards rebuild your system and then restart your bluetooth service by executing  <syntaxhighlight lang="console">$ systemctl restart bluetooth</syntaxhighlight>
 
=== Pairing hearing aids using the ASHA protocol ===
The upstream bluez project [https://github.com/thewierdnut/asha_pipewire_sink#alternatives-are-coming has not yet implemented audio support for the ASHA protocol]. As an alternative it is possible to enable audio streaming using the [https://github.com/thewierdnut/asha_pipewire_sink asha-pipewire-sink] project.
 
Add following to your system config and apply it:
 
<syntaxhighlight lang="nix">
boot.extraModprobeConfig = ''
  options bluetooth enable_ecred=1
'';
 
hardware = {
  bluetooth = {
    enable = true;
    settings = {
      LE = {
        MinConnectionInterval = 16;
        MaxConnectionInterval = 16;
        ConnectionLatency = 10;
        ConnectionSupervisionTimeout = 100;
      };
    };
  };
};
 
environment.systemPackages = [ pkgs.asha-pipewire-sink ];
</syntaxhighlight>Ensure that profiles <code>LE2MTX LE2MRX</code> are part of <code>Selected phys</code> when running <code>sudo btmgmt phy</code>, otherwise follow the instruction [https://github.com/thewierdnut/asha_pipewire_sink#enable-2m-phy-optional here].
 
Pair and connect to both of your hearing aids.
 
Run the command <code>asha_pipewire_sink</code> and choose your hearing aids as audio sink in your sound mixer application, for example <code>pavucontrol</code>.
 
=== File Transfer from/to Mobile Device ===
 
Use Case: When you're not using a desktop/window manager who supports accepting files the GUI (e.g. sway, etc)
 
<code>systemctl --user edit obex</code>
 
<syntaxhighlight lang="ini">{
### Editing /home/<youruser>/.config/systemd/user/obex.service.d/override.conf
### Anything between here and the comment below will become the contents of the drop-in file
 
[Service]
ExecStart=
ExecStart=/nix/store/...-bluez-5.78/libexec/bluetooth/obexd --root=./Downloads --auto-accept
 
### Edits below this comment will be discarded
 
...
}</syntaxhighlight>
 
With this config, after pairing (and - I suspect - trusting) your mobile device, you should be able to receive files from your phone.
 
Caveat: When sending files to the phone, take into account that not all file extensions are accepted. Renaming the file before transfer did the trick.
 
<code>sudo journalctl -f -t obexd</code> be your friend.


==Troubleshooting==
==Troubleshooting==
Line 185: Line 240:
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.
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>
<syntaxhighlight lang="console">
sudo systemctl restart display-manager.service
$ sudo systemctl restart display-manager.service
</syntaxhighlight>
</syntaxhighlight>


Line 197: Line 252:


==See also==
==See also==
* [http://anderspapitto.com/posts/2016-11-07-scripting_pulseaudio_bluetooth_jack.html Scripting PulseAudio, Bluetooth, JACK]
* [https://web.archive.org/web/20170609072208/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)]
* [https://wiki.archlinux.org/index.php/Bluetooth Bluetooth (Arch Linux Wiki)]


[[Category:Installation]][[Category:Configuration]][[Category:Hardware]]
[[Category:Audio]][[Category:Configuration]][[Category:Hardware]]