Bluetooth: Difference between revisions

From NixOS Wiki
imported>Samueldr
m (Adds a troubleshooting section for bad bluetooth usb dongles.)
imported>HLandau
No edit summary
Line 1: Line 1:
== Enabling ==
==Enabling Bluetooth support==
 
To enable support for Bluetooth devices, add {{nixos:option|hardware.bluetooth.enable}} to <tt>/etc/nixos/configuration.nix</tt>:
Enabling bluetooth support is as simple as adding {{nixos:option|hardware.bluetooth.enable}} to configuration.nix


<syntaxhighlight lang="nix">{
<syntaxhighlight lang="nix">{
  ...
   hardware.bluetooth.enable = true;
   hardware.bluetooth.enable = true;
  ...
}</syntaxhighlight>
}</syntaxhighlight>


{{evaluate}}
{{evaluate}}


== Pairing ==
==Pairing Bluetooth devices==
 
Bigger desktop environments will usually provide a bluetooth management system.
 
{{expansion}}
{{expansion}}


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>.
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 without GUI tools ===
If your desktop environment does not provide such a GUI, you can install the <tt>blueman</tt> package. This provides a tray icon process (<tt>blueman-applet</tt>) and a GUI management application (<tt>blueman-manager</tt>).


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#Configuration_via_the_CLI ArchWiki instructions], as follows.
===Pairing devices from the command line===
Alternatively, Bluetooth devices can be paired from the command line using <tt>bluetoothctl</tt>.


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


== Bluetooth headsets with PulseAudio ==
==Using Bluetooth headsets with PulseAudio==


The package used for PulseAudio has to be <code>pkgs.pulseaudioFull</code>, which includes bluetooth support.
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 = {
   hardware.pulseaudio = {
     enable = true;
     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;
     package = pkgs.pulseaudioFull;
   };
   };
   hardware.bluetooth.enable = true;
   hardware.bluetooth.enable = true;
  ...
}</syntaxhighlight>
}</syntaxhighlight>


{{evaluate}}
{{evaluate}}


Either restart the system for PulseAudio to load the bluetooth module, or restart your local instance of PulseAudio by running <code>pkill ^pulseaudio$</code>.
You will need to restart PulseAudio; try <tt>pkill '^pulseaudio$'</tt>.
 
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.
 
=== Managing the audio interface ===
 
Using <code>pavucontrol</code>, the headset profile can be changed to ''"High Fidelity Playback (A2DP Sink)"'' in the ''Configuration'' tab.
 
The default sink can be changed in the ''Output Devices'' tab. clicking on the right most button, which it title ''Set as fallback''.
 
==== Managing the audio interface using <code>pacmd</code> ====
 
Setting the card to A2DP


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


Setting the bluetooth sink as default
===Managing audio devices===


<syntaxhighlight lang="console">
<tt>pavucontrol</tt> can be used to reconfigure the device:
$ pacmd set-default-sink "$(pactl list sinks short | egrep -o bluez_sink[[:alnum:]._]+)"
* To enable A2DP, change the profile to &#8220;High Fidelity Playback (A2DP Sink)&#8221; on the &#8220;Configuration&#8221; tab.
</syntaxhighlight>
* To set the device as the default audio output, select &#8220;set as fallback&#8221; on the &#8220;Output Devices&#8221; tab.


== Troubleshooting ==
Alternatively, the device can be configured via the command line:


=== USB device needs to be unplugged/re-plugged after suspend ===
* 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>


Some USB devices/hosts combinations don't play well with the suspend/resume cycle, and needs to be unplugged and then re-plugged to work again.
==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 <code>/sys/</code> filesystem.
It is possible to simulate a unplug/re-plug cycle using the <tt>/sys</tt> filesystem.


[https://gist.github.com/samueldr/356e65374d452e4fd45314f818ae3545 This gist] provides a script and instructions to set-up a workaround for these devices.
[https://gist.github.com/samueldr/356e65374d452e4fd45314f818ae3545 This gist] provides a script and instructions to set-up a workaround for these devices.


== External resources ==
==See also==
* [http://anderspapitto.com/posts/2016-11-07-scripting_pulseaudio_bluetooth_jack.html Scripting PulseAudio, Bluetooth, JACK]
* [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:Installation]][[Category:Configuration]][[Category:Hardware]]

Revision as of 17:16, 24 October 2017

Enabling Bluetooth support

To enable support for Bluetooth devices, add hardware.bluetooth.enable to /etc/nixos/configuration.nix:

{
  ...
  hardware.bluetooth.enable = true;
  ...
}

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 install the blueman package. This provides a tray icon process (blueman-applet) and a GUI management application (blueman-manager).

Pairing devices from the command line

Alternatively, Bluetooth devices can be paired from the command line using bluetoothctl.

$ bluetoothctl
[bluetooth] # power on
[bluetooth] # agent on
[bluetooth] # default-agent
[bluetooth] # scan on
...put device in pairing mode and wait [hex-address] to appear here...
[bluetooth] # pair [hex-address]
[bluetooth] # connect [hex-address]

Using Bluetooth headsets with PulseAudio

To allow Bluetooth audio devices to be used with PulseAudio, amend /etc/nixos/configuration.nix as follows:

{
  ...
  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;
  ...
}

You will need to restart PulseAudio; try pkill '^pulseaudio$'.

You can verify that PulseAudio has loaded the Bluetooth module by running pactl list | grep -i 'Name.*module.*blue'; Bluetooth modules should be present in the list.

Managing audio devices

pavucontrol can be used to reconfigure the device:

  • To enable A2DP, change the profile to “High Fidelity Playback (A2DP Sink)” on the “Configuration” tab.
  • To set the device as the default audio output, select “set as fallback” on the “Output Devices” tab.

Alternatively, the device can be configured via the command line:

  • To enable A2DP, run:
    $ pacmd set-card-profile "$(pactl list cards short | egrep -o bluez_card[[:alnum:]._]+)" a2dp_sink
    
  • To set the device as the default audio output, run:
    $ pacmd set-default-sink "$(pactl list sinks short | egrep -o bluez_sink[[:alnum:]._]+)"
    

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 /sys filesystem.

This gist provides a script and instructions to set-up a workaround for these devices.

See also