Using Bluetooth audio devices (speaker, headphones, earbuds) with FreeBSD

olli@

Daemon
Developer

Reaction score: 1,230
Messages: 1,120

Here’s just a small recommendation if you want to connect Bluetooth audio devices to a FreeBSD machine, such as wireless speakers or headphones.

Unfortunately, FreeBSD’s support for Bluetooth audio devices is rather weak. Some devices work via the virtual_oss port, but many don’t work. It also requires a lot of manual work.

Therefore I’ve been looking for an alternative, and I found this little USB Bluetooth dongle (the link points to Amazon Germany – if you use Amazon somewhere else, please use the search function to locate this or something similar; also see my reply #3 below for alternative products that work the same).

The special thing about this dongle is that it is not an actual USB Bluetooth adapter. Instead, the operating system recognizes it as a standard USB sound card. When you plug it in, FreeBSD automatically attaches it with the snd_uaudio(4) driver and creates a pcm(4) device that appears in /dev/sndstat. Bluetooth drivers are not required.

Here’s the dmesg output from the attachment:
Code:
ugen0.9: <vendor 0x0a12 B10> at usbus0
uaudio0 on uhub1
uaudio0: <vendor 0x0a12 B10, class 0/0, rev 2.00/25.19, addr 12> on usbus0
uaudio0: Play[0]: 48000 Hz, 2 ch, 16-bit S-LE PCM format, 2x8ms buffer.
uaudio0: Record[0]: 48000 Hz, 1 ch, 16-bit S-LE PCM format, 2x8ms buffer.
uaudio0: No MIDI sequencer.
pcm5: <USB audio> on uaudio0
uaudio0: HID volume keys found.
Output from cat /dev/sndstat
Code:
pcm0: <NVIDIA (0x0081) (HDMI/DP 8ch)> (play)
pcm1: <NVIDIA (0x0081) (HDMI/DP 8ch)> (play)
pcm2: <Realtek ALC1220 (Analog 5.1+HP/2.0)> (play/rec) default
pcm3: <Realtek ALC1220 (Rear Digital)> (play)
pcm4: <Realtek ALC1220 (Front Analog Mic)> (rec)
pcm5: <USB audio> (play/rec)
As you can see, the new device got the number 5 (“pcm5”), so the command sysctl hw.snd.default_unit=5 can be used to use it by default. Now the audio output from my favorite media player goes to my “Melomania 1” true-wireless headphones. Even the little volume buttons on the earbuds work – when I press them, the playback volume is adjusted, and I can see the value change in the output of the mixer(8) command.

According to the above output, the device also supports a “record” channel for the built-in microphone of head-sets and some speakers, so you should be able to use this for phone calls, video conferences and similar. I haven’t tried this, though, because I only need the playback function.
 
Last edited:
OP
olli@

olli@

Daemon
Developer

Reaction score: 1,230
Messages: 1,120

The following steps will set the default sound device automatically when you plug in the USB audio dongle.
You can make this work with other USB audio devices, just replace the vendor ID and product ID in the devd file below. To find out the IDs for your device, type usbconfig -d ugen0.5 dump_device_desc (you can see the USB device in dmesg; in this case it’s ugen0.5), and look for the lines labeled “idVendor” and “idProduct”.

Note: You have to perform the following steps with root privileges.

Step 1:
Create a file /usr/local/etc/devd/bluetooth-audio-b10.conf (or whatever name you see fit, but it must end with .conf):
Code:
#   When we attach the "B10" Bluetooth audio dongle,
#   automatically set it to be the default audio device.

attach 100 {
        match "vendor"  "0x0a12";
        match "product" "0x1004";
        action "sleep 1 ; /usr/local/sbin/update-snd-unit";
};
To make it work with other USB audio devices, replace the “vendor” and “product” codes as explained above.

Step 2:
Create a script /usr/local/sbin/update-snd-unit:
Code:
#!/bin/sh -
#   When a new pcm* device has been attached.
#   set it to be the default sound device.

set -Cefu
ME="${0##*/}"
FAC="daemon"    # Syslog facility, see syslog(3).
export PATH=/sbin:/bin:/usr/sbin:/usr/bin

PCM=$(
        dmesg \
        | awk '
                /^pcm[0-9]+: .* on / {
                        sub (/^pcm/, "")
                        sub (/:.*/, "")
                        pcm = $0
                }
                END {
                        print pcm
                }
        '
)

if [ -z "$PCM" ]; then
        logger -p $FAC.error -t "$ME" "Cannot determine new pcm device."
        exit 1
fi

sysctl hw.snd.default_unit=$PCM 2>&1 \
| logger -p $FAC.notice -t "$ME"
Step 3:
Make the script executable: chmod 755 /usr/local/sbin/update-snd-unit

Step 4:
Restart devd(8): service devd restart

Done!
 
OP
olli@

olli@

Daemon
Developer

Reaction score: 1,230
Messages: 1,120

PS: In case you have trouble finding that USB dongle mentioned above – Most USB Bluetooth dongles intended to be used for the Playstation (PS4, PS5) or for the Nintendo Switch work exactly the same, i.e. they are recognized as USB sound cards by the operating system, so no Bluetooth drivers are needed. For example, see this product on Amazon USA.

(Note that these are standard links to Amazon, not afiliate links. I do not get any revenues for them.)
 
Top