Skip to content

Instantly share code, notes, and snippets.

@peters
Last active April 24, 2024 14:52
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save peters/26315cd7a8a31e3d192ed05ef9a79ba7 to your computer and use it in GitHub Desktop.
Save peters/26315cd7a8a31e3d192ed05ef9a79ba7 to your computer and use it in GitHub Desktop.
Connecting Sony WH-1000XM4 to Ubuntu 22.04 (Jammy)

Connecting Sony WH-1000XM4 to Ubuntu 22.04 (Jammy)

This guide will help you connect your Sony WH-1000XM4 headset to Ubuntu 22.04 using Bluetooth. Once connected, you'll be able to listen to music and use the microphone on apps like Microsoft Teams.

1. Connect to Bluetooth Headset

Steps:

  1. Open a terminal and launch bluetoothctl:

    bluetoothctl
  2. Initialize the agent and set it as the default:

    agent on
    default-agent
    
  3. Scan for Bluetooth devices:

    scan on
    

    Look for the MAC address of your Sony WH-1000XM4 headset.

  4. Stop the scanning process:

    scan off
    
  5. Pair, trust, and connect to the headset using its MAC address:

    pair XX:XX:XX:XX:XX:XX
    trust XX:XX:XX:XX:XX:XX
    connect XX:XX:XX:XX:XX:XX
    

    (Replace XX:XX:XX:XX:XX:XX with the MAC address of your headset.)

  6. Exit bluetoothctl:

    exit
    

2. Enable PipeWire for Better Audio Handling

Ubuntu 22.04 partially uses PipeWire by default. To fully utilize it for audio and Bluetooth, follow these steps:

Installation:

  1. Install and start WirePlumber:
    sudo apt install pipewire-media-session- wireplumber
    systemctl --user --now enable wireplumber.service

Configuration:

  • For ALSA:

    sudo apt install pipewire-audio-client-libraries
    sudo cp /usr/share/doc/pipewire/examples/alsa.conf.d/99-pipewire-default.conf /etc/alsa/conf.d/
  • For Bluetooth:

    sudo apt install libldacbt-{abr,enc}2 libspa-0.2-bluetooth pulseaudio-module-bluetooth-

Validation:

After rebooting your system, you can check if PipeWire is active by running:

LANG=C pactl info | grep '^Server Name'

Switch Audio Profile using CLI

image

mkdir -p ~/.audio/bin && cd ~/.audio/bin
wget https://gist.githubusercontent.com/peters/26315cd7a8a31e3d192ed05ef9a79ba7/raw/9466f91da19b53d01cac862898cfdfcb18e3137e/wh1000xm4-audio-profile.sh
chmod +x wh1000xm4-audio-profile.sh
./wh1000xm4-audio-profile.sh

For microphone testing, click here to visit the microphone test site

#!/bin/bash
# Device alias to match against
DEVICE_ALIAS="WH-1000XM4"
# Fetch the card name for the device
CARD_NAME=$(pactl list cards | awk '/Name:/ {name=$2} /device.alias = "WH-1000XM4"/ {print name}')
# If the card name is empty, exit
if [ -z "$CARD_NAME" ]; then
echo "Unable to find the card for device $DEVICE_ALIAS"
exit 1
fi
# Retrieve all supported profiles for the card
SUPPORTED_PROFILES=$(pactl list cards | awk -v card="$CARD_NAME" '/Name: / {if ($2 == card) {f=1} else {f=0}} f && /Profiles:/, /Active Profile:/ {if (!/Profiles:/ && !/Active Profile:/) print $1}')
# Declare an associative array for profiles
declare -A PROFILES
PROFILES["a2dp-sink"]="High Quality Audio Only"
PROFILES["headset-head-unit-cvsd"]="Mono audio / mic"
# Create an array of profiles that we're interested in and are supported by the device
FILTERED_PROFILES=()
for profile in "${!PROFILES[@]}"; do
if echo "$SUPPORTED_PROFILES" | grep -q "^${profile}:"; then
FILTERED_PROFILES+=("$profile (${PROFILES[$profile]})")
fi
done
if [ ${#FILTERED_PROFILES[@]} -eq 0 ]; then
echo "None of the desired profiles are supported by device $DEVICE_ALIAS"
exit 1
fi
# Display the profiles and prompt the user to choose
echo "Available profiles for $DEVICE_ALIAS:"
select opt in "${FILTERED_PROFILES[@]}"; do
for profile in "${!PROFILES[@]}"; do
if [[ $opt == "$profile (${PROFILES[$profile]})" ]]; then
pactl set-card-profile "$CARD_NAME" "$profile"
echo "Switched to profile: $opt"
exit 0
fi
done
echo "Invalid option."
done
@sulalasu
Copy link

sulalasu commented Feb 7, 2024

just wanted to thank you. tried making it work since weeks and nothing would help.
Thanks a lot for this concise guide

@0michalsokolowski0
Copy link

0michalsokolowski0 commented Mar 12, 2024

It worked for me as well, thanks!

One note: for some reason, I had three devices paired with the same name, "LE_WH-1000XM4" but different IDs.
I had to remove all of them since I kept getting the error from the script "Unable to find the card for device"

@110CodingP
Copy link

It helped me too, thanks a lot!

@Swazib0y
Copy link

hi there, thanks for this - phew shouldn't be this hard!! Two questions:
What should the output be of "LANG=C pactl info | grep '^Server Name'" ? And secondly, I get a "Unable to find the card for device WH-1000XM4" when running the script - however it seems the headphones are working fine. Ubuntu 22.04.4 Tnx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment