422. Bluetooth
This page is for connecting Bluetooth devices to a KDOS machine: headsets, speakers, keyboards, mice and phones. It covers pairing and connecting from the desktop and from a terminal, Bluetooth audio, the daemon behind it, and what to check when an adapter or a device does not appear. Once a headset is connected, its sound is handled by PipeWire like any other output; see Audio.
Installation
The bluez port provides the daemon, bluetoothd, installed in /usr/lib/bluetooth with a link in /usr/sbin, and the tools bluetoothctl and btmon:
install -d $PKG/usr/sbin
ln -svf ../lib/bluetooth/bluetoothd $PKG/usr/sbin/bluetoothd
ports/core/hardware/bluez/build.sh, lines 30–31
It also installs obexd, the object-exchange daemon for file transfer, beside bluetoothd in /usr/lib/bluetooth and not on the PATH. The session bus starts it when a program asks for org.bluez.obex (/usr/share/dbus-1/services/org.bluez.obex.service); no service script runs it.
The desktop’s side is bluedevil: the Bluetooth applet in the system tray, the Bluetooth page of System Settings with its pairing wizard, file sending and receiving, and the pairing agent the session registers with the daemon. The audio side, the Bluetooth plugin and its codecs, is pipewire’s, with wireplumber choosing the profile. Adapter firmware comes from the vendor’s port of linux-firmware: Intel’s, Realtek’s and MediaTek’s are on the installation medium, and sudo kdos firmware sync installs any other the adapter asks for.
The service script is part of the base filesystem, fs/etc/init.d/60_bluetooth.sh. The service is named bluetooth, and its supervised process bluetoothd. It is on by default on both the live medium and an installed system. The installer lists it among its service choices, on unless you turn it off:
const Service ki_services[] = {
{ "networkmanager", "NetworkManager", "wired, wifi and VPN", 1 },
{ "bluetooth", "Bluetooth", "bluez daemon", 1 },
{ "alsa", "ALSA state", "restore mixer levels at boot", 1 },
{ "cups", "CUPS", "printing daemon", 1 },
{ "sshd", "OpenSSH server", "accepts remote logins", 0 },
};
src/system/kinstall-core/conf.c, lines 29–35
Configuration
The service script
bluetoothd is started with flags rather than a configuration file of KDOS’s own. -n keeps it in the foreground for the supervisor; -E and the --kernel= feature turn on the experimental interfaces and the kernel’s ISO sockets that LE Audio needs:
. /etc/init.d/service_helper
NAME="bluetoothd"
DAEMON="/usr/lib/bluetooth/bluetoothd"
case "$1" in
start)
if [ ! -x "$DAEMON" ]; then
# Try sbin symlink
DAEMON="/usr/sbin/bluetoothd"
fi
if [ ! -x "$DAEMON" ]; then
echo "[SKIP] $NAME: daemon not found"
exit 0
fi
echo "[KDOS] Starting $NAME..."
# -n = don't daemonise (stay foreground for supervision)
#
# -E and the ISO-socket kernel feature are what LE Audio needs: bluez
# registers the LC3 (BAP) endpoints PipeWire offers only when both are
# on, and without them an LE Audio headset or hearing aid falls back
# to classic A2DP/HFP or does not connect. -K takes its argument only
# in the `=` form. Flags rather than a main.conf here, which would
# shadow the one the bluez package owns.
supervise "$NAME" "$DAEMON" -n -E \
--kernel=6fbaf188-05e0-496a-9885-d6ddfdb4e03e
;;
stop)
stop_service "$NAME"
;;
status)
check_status "$NAME"
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
;;
esac
Why LE Audio needs both is in Audio in the KDOS book.
/etc/bluetooth/
main.conf, input.conf and network.conf are upstream’s files as the bluez package installs them, with every setting commented out (only the section headers, [General], [Policy] and the rest, are live lines), so the daemon runs on its built-in defaults. Their keys are described in the comments of each file. After an edit:
sudo service restart bluetooth
Who may use Bluetooth
No group is needed. bluez’s D-Bus policy, /usr/share/dbus-1/system.d/bluetooth.conf, lets any account send to org.bluez; only root may own the name.
/dev/uhid
A Bluetooth keyboard or mouse reaches the kernel’s input layer through /dev/uhid, whose driver loads only when the node is opened. 01_udev creates that node, with the other on-demand nodes, before udevd starts:
# The nodes of modules that load when their node is OPENED: uhid,
# uinput, snd/seq, vhost-net, cuse and the rest of modules.devname.
# No uevent names them before the module loads and nothing loads the
# module until the node exists, so without this /dev/uhid never
# appears and every Bluetooth keyboard and mouse pairs and then types
# nothing. Made BEFORE udevd starts, because udevd applies the rules'
# static_node= ownership (snd/seq to audio, vhost-net to kvm) to the
# nodes it finds at start-up and to no others.
if [ -x /usr/bin/kmod ]; then
/usr/bin/kmod static-nodes --format=tmpfiles 2>/dev/null |
while read -r _t _p _m _u _g _a _dev; do
case "$_t" in c*|b*) ;; *) continue ;; esac
[ -e "$_p" ] && continue
mkdir -p "${_p%/*}"
mknod -m "$_m" "$_p" "${_t%%!*}" "${_dev%%:*}" "${_dev##*:}"
done
fi
fs/etc/init.d/01_udev.sh, lines 16–32
Audio codecs
PipeWire’s Bluetooth plugin is built with these codecs:
-Dbluez5=enabled \
-Dbluez5-codec-aptx=enabled \
-Dbluez5-codec-ldac=enabled \
-Dbluez5-codec-aac=enabled \
-Dbluez5-codec-lc3=enabled \
-Dbluez5-codec-opus=enabled \
-Dbluez5-codec-g722=enabled \
-Dbluez5-codec-lc3plus=disabled \
-Dbluez5-codec-ldac-dec=disabled \
-Dbluez5-plc-spandsp=disabled \
ports/core/audio-io/pipewire/build.sh, lines 30–39
LC3plus, LDAC decoding and spandsp’s packet-loss concealment are off because their libraries are not ports; see Features whose library has no port.
Usage
Pairing a device from the desktop
- Click the Bluetooth applet in the system tray and turn Bluetooth on if it is off.
- Put the device into pairing mode, then Pair Device… (or System Settings → Bluetooth → Pair a New Device). Devices appear as they are found.
- Select the device and Next. When the device asks for a passkey to be confirmed, the wizard shows it: confirm it. A keyboard, which has no screen to show a passkey on, gets a number instead: type it on that keyboard and press its
Enter. A device that wants a PIN typed on this machine asks for one in the wizard. - The device is paired, trusted and connected.
Remove on the device’s row in the Bluetooth page forgets it; Make this computer visible is the page’s switch for being found by others.
Connecting a headset
- Pair it once, as above.
- Click it in the Bluetooth applet to connect or disconnect.
- Choose it as the output in the volume applet, or on System Settings → Sound, where its profile, A2DP for music or HFP for a call, is chosen too.
Pairing from a terminal
bluetoothctl speaks to the same daemon and registers a pairing agent of its own:
bluetoothctl
Then, at its prompt:
power on
agent on
default-agent
scan on
pair AA:BB:CC:DD:EE:FF
trust AA:BB:CC:DD:EE:FF
connect AA:BB:CC:DD:EE:FF
scan off
quit
devices lists what the adapter knows, and info <address> one device’s state. Each command can also be given on the command line, as bluetoothctl connect AA:BB:CC:DD:EE:FF.
Turning Bluetooth off
For this boot only, block the radio or stop the daemon:
sudo rfkill block bluetooth
sudo service stop bluetooth
To keep the daemon from starting at boot, sudo service disable bluetooth. On an installed system the installer has already done this if Bluetooth was turned off there.
Tips
- An untrusted device that asks to use a service is put to you in a notification; a trusted one is allowed without asking. The wizard trusts a device it pairs.
- Which profile a headset uses, A2DP for music or HFP for a call, is WirePlumber’s choice unless you pick one on the Sound page.
wpctl statuslists the headset’s nodes once it is connected. sudo btmonprints every packet between the daemon and the adapter, which shows where a pairing stops.- TLP runs at every boot, from
55_tlp. WhenDEVICES_TO_DISABLE_ON_STARTUPnamesbluetooth, in/etc/tlp.confor a file under/etc/tlp.d/, the radio is blocked at boot, and so it is withRESTORE_DEVICE_STATE_ON_STARTUP=1when it was off at the last shutdown. As shipped neither is set. See Power management. - Pairing keys are kept under
/var/lib/bluetooth. On the live medium they are lost at power-off unless a persistence store exists (Live persistence).
Troubleshooting
The Bluetooth applet shows no adapter. bluetoothctl show then says No default controller available. Work down the chain:
Is the daemon running?
service status bluetooth ls /etc/service.disabledA running daemon reports
[ OK ] bluetoothd (pid N). A file namedbluetoothin/etc/service.disabledkeeps it from starting at boot;sudo service enable bluetoothandsudo service start bluetooth.Is the radio blocked?
rfkill list sudo rfkill unblock bluetoothrfkill listprints nothing at all when the kernel has no radio of any kind, which is the case in a virtual machine.Did the kernel find the adapter and load its firmware?
sudo dmesg | grep -i bluetoothThe kernel’s log is root’s to read, so
dmesgwithoutsudoprints only an error.The
Bluetooth: Core ver …,HCIandBNEPlines are the protocol stack loading and appear with no adapter too; an adapter adds lines naminghci0. A line naming a firmware file that could not be loaded is the adapter’s firmware:kdos firmware sync --dry-runnames the port that ships it, andsudo kdos firmware syncinstalls it.
The daemon keeps restarting. service status bluetooth then reports a [FAIL] bluetoothd line with the daemon’s last exit code. The daemon’s own messages are in the supervisor’s log and in syslog’s /var/log/messages, both of which only root can read:
sudo tail -n 40 /run/kdos-svc.bluetoothd.log
A clean start logs Bluetooth daemon <version>, Starting SDP server and Bluetooth management interface <version> initialized.
A keyboard will not pair. Pairing a keyboard needs an agent, a program that answers the daemon’s passkey questions, and the daemon refuses the pairing when none answers. The session’s agent is bluedevil’s kded module; from a console or SSH, pair from bluetoothctl after agent on and default-agent. Type the number the wizard shows on the keyboard itself.
A keyboard or mouse pairs and connects, and does nothing. Check that the HID node exists:
ls -l /dev/uhid
If it is missing, 01_udev did not create it at boot; read /run/kdos-init.udev.log.
A headset is connected and plays nothing. PipeWire runs only inside a desktop session, so a headset is a sound device only there. In the session, check that its sink exists and is the default (wpctl status), and see Audio.
An LE Audio headset or hearing aid falls back to classic audio, or does not connect. Check that the daemon has both LE Audio flags:
tr '\0' ' ' < /proc/$(pgrep -x bluetoothd)/cmdline; echo
The line must carry -E and --kernel=6fbaf188-05e0-496a-9885-d6ddfdb4e03e.
Known gaps
- Broad hardware support is not a goal: no range of adapters and devices has been tested. The procedures for pairing, connecting and playing to a headset are upstream’s and have not been run on KDOS with a real adapter; only the daemon with no adapter, in a virtual machine, has been checked
- A USB modem that first appears as a storage device is not switched into a modem: Bluetooth tethering is built and has not been connected on hardware
See also
- Bluetooth: the user guide’s section
- The system tray: the tray’s Bluetooth and audio applets
- Audio: PipeWire, the codecs and LE Audio
- Audio, udev and device groups, Power management, Microcode and firmware, Logs, Services