PulseAudio无法将音频设备配置为'a2dp_sink'。我该如何查看日志并找出问题所在?

我目前安装了Ubuntu 16.04,并且在刚安装时a2dp是可以正常工作的,但我希望将其作为最后的选择。当蓝牙设置中的配对被删除并重新配对时,有时它能够连接到a2dp配置文件,但如果耳机断开连接然后重新连接,a2dp配置文件就无法工作了(尽管显示为a2dp),只有在切换到HSP/HSF之后才能正常工作,而此时我无法将其设置为a2dp。
我已经尝试过:
 pacmd set-card-profile <index> a2dp_sink

但是出现了以下错误:
"Failed to set card profile to 'a2dp_sink'"

我在Linux方面经验不太丰富,所以不知道如何获取日志或者在日志中寻找什么内容,但是我从pacmd list-cards命令中得到了以下信息:
当它正常工作时:
name: <bluez_card.50_C9_71_97_A1_86>
driver: <module-bluez5-device.c>
owner module: 27
properties:
    device.description = "Jabra REVO a4.0.0"
    device.string = "50:C9:71:97:A1:86"
    device.api = "bluez"
    device.class = "sound"
    device.bus = "bluetooth"
    device.form_factor = "headset"
    bluez.path = "/org/bluez/hci0/dev_50_C9_71_97_A1_86"
    bluez.class = "0x240404"
    bluez.alias = "Jabra REVO a4.0.0"
    device.icon_name = "audio-headset-bluetooth"
    device.intended_roles = "phone"
profiles:
    headset_head_unit: Headset Head Unit (HSP/HFP) (priority 20, available: unknown)
    a2dp_sink: High Fidelity Playback (A2DP Sink) (priority 10, available: unknown)
    off: Off (priority 0, available: yes)
active profile: <a2dp_sink>
sinks:
    bluez_sink.50_C9_71_97_A1_86/#6: Jabra REVO a4.0.0
sources:
    bluez_sink.50_C9_71_97_A1_86.monitor/#10: Monitor of Jabra REVO a4.0.0
ports:
    headset-output: Headset (priority 0, latency offset 0 usec, available: unknown)
        properties:

    headset-input: Headset (priority 0, latency offset 0 usec, available: unknown)
        properties:

当它不这样做时:
name: <bluez_card.50_C9_71_97_A1_86>
driver: <module-bluez5-device.c>
owner module: 25
properties:
    device.description = "Jabra REVO a4.0.0"
    device.string = "50:C9:71:97:A1:86"
    device.api = "bluez"
    device.class = "sound"
    device.bus = "bluetooth"
    device.form_factor = "headset"
    bluez.path = "/org/bluez/hci0/dev_50_C9_71_97_A1_86"
    bluez.class = "0x240404"
    bluez.alias = "Jabra REVO a4.0.0"
    device.icon_name = "audio-headset-bluetooth"
    device.intended_roles = "phone"
profiles:
    headset_head_unit: Headset Head Unit (HSP/HFP) (priority 20, available: unknown)
    a2dp_sink: High Fidelity Playback (A2DP Sink) (priority 10, available: yes)
    off: Off (priority 0, available: yes)
active profile: <off>
ports:
    headset-output: Headset (priority 0, latency offset 0 usec, available: yes)
        properties:

    headset-input: Headset (priority 0, latency offset 0 usec, available: unknown)
        properties:

拥有的模块是:

name: <module-bluez5-device>
argument: <path=/org/bluez/hci0/dev_50_C9_71_97_A1_86>
used: 0
load once: no
properties:
    module.author = "João Paulo Rechi Vita"
    module.description = "BlueZ 5 Bluetooth audio sink and source"
    module.version = "8.0"

我已经尝试过修改/etc/bluetooth/audio.conf并安装blueman,但没有起作用。我还尝试重新安装bluez和pulseaudio,但仍然没有改变。
有没有办法可以知道出了什么问题并修复它?

请问你能解释一下你从哪里得到这个信息吗?还有指数是什么意思?谢谢! - Ezequiel Moreno
请点击这里查看:http://askubuntu.com/a/817926/33292 - pylover
1我在Debian Buster上使用我的传统Plantronics Backbeat Pro耳机和一个通用的蓝牙适配器时遇到了类似的问题。解决办法是使用原装的Plantronics BT600 USB蓝牙适配器,而不是通用的蓝牙适配器。希望这对某人有所帮助。 - Yapsr
15个回答

为了让您的系统被识别为A2DP接收器(例如,通过计算机扬声器播放手机音乐),请在/etc/bluetooth/audio.conf中的[General]下添加Enable=Source,Sink,Media,Socket
完成后,请重新启动蓝牙守护进程。
systemctl restart bluetooth

连接您的耳机设备
bluetoothctl
connect <MAC>

检查您的卡片并设置个人资料
pactl list cards short
pacmd set-card-profile <index> <profile>

3在使用Ubuntu 16.04时,/etc/bluetooth/audio.conf文件不存在,那么我应该在哪里写入这行代码呢? - thebeancounter
这个方法在Debian Sid上解决了我的问题,谢谢! - Xerz
@thebeancounter 如果文件不存在,请添加文件。 - Jimubao
偶尔如果您与A2DP音频接收器失去连接,请尝试在bluetoothctl中执行"power off",然后执行"systemctl restart bluetooth",之后在bluetoothctl中执行"power on"并重新连接您的设备。 - Jimubao
11在 ARch / Manjaro 上工作过!但是最近版本的文件现在位于/etc/bluetooth/main.conf - MCMZL
我翻遍了很多帖子、博客文章、维基页面、问题等,时断时续地找不到解决方法,但这个方法对我起了作用。我使用的是 Arch 系统,bluez 版本为 5.54,使用的是 "CB3 Hush" 无线耳机(因为我在一些找到的帖子中发现有人声称这是特定耳机的问题)。正如 MCMZL 所说,我将此添加到 /etc/bluetooth/main.conf 中。 - Dylan
谢谢。对我的Linux Mint 20.1和Ugreen USB蓝牙适配器起作用了! - Trung Ta
当我的耳机音质突然变差时,只需执行systemctl restart bluetooth命令就解决了问题。 - Fabian Röling
1如果这个答案对你不起作用,请尝试最高票的此问题的答案:基本上,将Disable=Headset添加到文件/etc/bluetooth/main.conf中,而不是上面回答中所描述的方法。 - HongboZhu
谢谢!在Ubuntu 20.04中可以工作了,尽管文件现在是/etc/bluetooth/main.conf。 - happyskeptic
我只是添加了Enable=Sink,因为我永远不希望它变成其他任何东西。如果这样做会导致问题,我会回报的,但目前看起来似乎可以正常工作。 - Fabian Röling
起初在我的pacmd卡列表中找不到a2dp配置文件,但在/etc/bluetooth/main.conf中添加Enable解决了问题。谢谢! - undefined

根据“active profile: <off>”这一行,声音配置文件没有激活。

一个易于使用的更改配置文件的程序是pavucontrol。您可以使用sudo apt install pavucontrol -y进行安装。然后从命令行或Alt+F2(如果您在Kubuntu上)运行pavucontrol。

选择配置选项卡,然后尝试更改耳机的配置文件,看看是否更新。Linux中似乎仍存在蓝牙a2dp的错误,并且影响了我的两个16.04安装。如果它已经说A2DP,请将其更改为OFF,在蓝牙小程序中断开设备,然后重新连接它。最后,继续将配置文件更改回A2DP,它应该可以正常工作。

您还可以尝试在命令行上执行以下操作,它应该会自动完成大部分内容。将下面的pacmd命令中的数字2替换为当前索引号。

pacmd set-card-profile 2 off
bluetoothctl
disconnect 50:C9:71:97:A1:86
connect 50:C9:71:97:A1:86
pacmd set-card-profile 2 a2dp_sink

为了更复杂但自动化地完成相同的操作,将配置文件循环切换到关闭状态,然后再切换回A2DP,但不进行蓝牙设备的断开和连接。
index=`pacmd list-cards | grep bluez_card -B1 | grep index | awk '{print $2}'`
pacmd set-card-profile $index off
pacmd set-card-profile $index a2dp_sink 

我还制作了一个工作但粗糙的脚本,可以做同样的事情,还可以断开和重新连接蓝牙耳机。它获取配置文件索引,因为每次设备断开和重新连接时索引都会改变。这是使用你上面列出的耳机设备ID进行硬编码的。
pacmd set-card-profile `pacmd list-cards | grep bluez_card -B1 | grep index | awk '{print $2}'` off
sleep 2
echo -e "disconnect 50:C9:71:97:A1:86\n quit"|bluetoothctl
sleep 5
echo -e "connect 50:C9:71:97:A1:86\n quit"|bluetoothctl
sleep 5
pacmd set-card-profile `pacmd list-cards | grep bluez_card -B1 | grep index | awk '{print $2}'` a2dp_sink

在测试过程中,我发现睡眠时间需要设定为5秒而不是3秒,因为设备尚未准备好更改音频配置文件。你的结果可能会有几秒钟的偏差,正负5秒。关于一个潜在的陷阱的编辑。我发现有时需要运行较大的脚本两次才能使其正常工作。我不确定增加睡眠时间是否会有帮助,因为我还没有测试过。但是我已经在两台使用相同品牌蓝牙耳机的机器上使用它,并且它正在按照预期自动化处理过程以使耳机的声音正常工作。
最终,这个问题将在蓝牙软件包中修复,但似乎在Ubuntu的新版本以及其他Linux版本中仍然存在。但无论如何,希望对你有所帮助。

怎样才能查看日志并找出问题所在?如果您能回答这个问题就太好了 :) - kitingChris
在pavucontrol图形界面中将声音设定为蓝牙耳机对我来说很有效。比Gnome声音工具好用得多。 - QkiZ
1两年后,我需要这个答案与我的Sony WH-H900N耳机相关。 - Joakim Wimmerstedt
(一年后)使用XFCE和blueman-manager,我经常需要在右键单击设备菜单之前执行bluetoothctl disconnect aa:bb:cc:dd:ee:ff && bluetoothctl connect aa:bb:cc:dd:ee:ff,才能切换音频配置文件为A2DP。有时候当设备菜单加载时间过长时,我还需要终止一个子进程的blueman-manager。 - ericP
感谢提供这些命令。这是唯一在我的Ubuntu 18.04和Sony WH-1000xm2上有效的解决方法。我已经将其脚本化并上传至此gist链接:https://gist.github.com/egelev/2e6b57d5a8ba62cf6df6fff2878c3fd4 - egelev

我在Ubuntu 16.04.1 LTS上遇到了这个问题,并且应用了在https://wiki.debian.org/BluetoothUser/a2dp中找到的GDM解决方法来解决它。
具体来说,创建/编辑/var/lib/gdm3/.config/pulse/client.conf文件,内容如下:
autospawn = no
daemon-binary = /bin/true

然后
sudo chown gdm:gdm /var/lib/gdm3/.config/pulse/client.conf

我还添加了/etc/pulse/default.pa
load-module module-switch-on-connect

虽然这可能并不是严格必要的。
重新启动后,我通过蓝牙管理器连接成功,并能够成功切换音频配置为 a2dp_sink

在链接的第一步中,我遇到了bluez-firmware不可用的问题,然后用户gdm找不到。此外,gdm3文件夹也不存在,所以我不得不创建整个路径。 - wordsforthewise
我按照Debian链接中的第二个解决方法来做了一下,效果棒极了! - Amfasis

在Xubuntu 16.04.2上使用blueman-manager:
  1. 连接 -> 关闭配置文件
  2. 重新连接 -> 设置配置文件为a2dp
  3. 现在你的耳机已经正常工作了!

我的JBL bash脚本

#!/bin/bash
#headset mac
mac="00:1D:DF:67:0A:29"
profile="a2dp"
# Special Bluetooth controller, default is empty
btMac=""
#connect|disconnect wait time
waitTime=5

macId="${mac//:/_}"
deviceId="bluez_card.$macId"

declare -A profiles
profiles['a2dp']='a2dp_sink'
profiles['hsp']='headset_head_unit'
profiles['off']='off'

function btCmd() {
    cmd="$1\nquit"
    [ ! -z "$btMac" ] && cmd="select $btMac\n$cmd"
    echo -e "$cmd" | bluetoothctl
}
function setProfile() {
    cmd="pactl set-card-profile $deviceId ${profiles[$1]}"
    echo $cmd
    $cmd
}
function btWaitConnect() {
    conState=$1
    for ((i=1;i<=$waitTime;++i)); do
        tmp="`btCmd "info $mac"|grep 'Connected: '`"
        [ ! -z "`echo "$tmp"|grep $conState`" ] && echo "$tmp" && return 0
        sleep 1s
    done
    echo "$tmp"
    return 1
}
function btConnect() {
    tmp="`btCmd "trust $mac\nconnect $mac" | grep -v 'NEW\|DEL\| quit'`"
    echo "$tmp"

    tmp="`btWaitConnect yes`"
    echo $tmp
    [ -z "`echo "$tmp" | grep yes`" ] && echo -e "Device $mac:\n\tConnected: fail." && return 1
    sleep 2s
    return 0
}
function btDisConnect() {
    tmp="`btCmd "disconnect $mac" | grep -v 'NEW\|DEL\| quit'`"
    echo "$tmp"

    tmp="`btWaitConnect no`"
    echo $tmp
    [ -z "`echo "$tmp" | grep no`" ] && echo -e "Device $mac:\n\tdisconnected: fail." && return 1
    sleep 1s
    return 0    
}


# controller
echo ""
btCtls="`btCmd list | grep '^Controller' | grep " $btMac"`"
echo -e "controller:\n$btCtls"
[ -z "$btCtls" -o ! -z "`echo "$btCtls" | grep "not available"`" ] && exit 1


# connect -> set off
echo ""
tmp="`btCmd paired-devices | grep '^Device' | grep " $mac"`"
echo -e "paired-devices:\n$tmp"
[ `echo "$tmp" | wc -l` != 1 ] && echo "Please pair the Bluetooth headset first: $mac" && exit 1
btConnect || exit $?
setProfile off

# reconnect -> set profile
btDisConnect || exit $?
btConnect || exit $?
setProfile $profile

这个回答和@freespace的回答对我在i3设置方面非常有帮助。 - Luis Lobo Borobia
最新的更新包含设置默认音频输出设备,但我不确定是否必要。 - jarno

lagerismilaunchpad上发布了一个基于Jamie S.的答案的脚本。

  • 在脚本内部更改设备的MAC地址!(可以使用bluetoothctl找到-->设备)

  • 将脚本设置为可执行文件chmod a-x bluetooth_headphone_reactivation.sh

要实现开机自动执行(仅在耳机已经打开时有效):

  • 将脚本添加到KDE控制面板中的启动/关闭脚本中

要进行快速手动执行:

  • 在应用程序启动器中添加一个条目(您还可以设置一个键盘快捷键,如“CTRL+ALT+H”,并/或将此启动器固定到窗口栏上)

脚本内容:

#!/bin/sh  
device_mac='<your-devices-mac-address-here>' # From bluetoothctl info
pacmd set-card-profile "$(pacmd list-cards | grep bluez_card -B1 | grep index | awk '{print $2}')" off
sleep 2
echo "disconnect $device_mac\n quit" | bluetoothctl
sleep 5
echo "connect $device_mac\n quit" | bluetoothctl
sleep 5
pacmd set-card-profile "$(pacmd list-cards | grep bluez_card -B1 | grep index | awk '{print $2}')" a2dp_sink

你应该在 echo 命令中使用 -e 参数。 - jarno

所以,如果你正在使用Debian,你可以按照以下步骤操作:拒绝切换到a2dp_sink:未连接 问题:
蓝牙耳机已连接,但ALSA/PulseAudio无法识别已连接的设备,或者没有可供选择的设备。这是因为GDM在会话开始时捕获了A2DP音频输出,因为GDM需要在gdm会话中使用pulseaudio来实现辅助功能,例如屏幕阅读器需要它。
解决方案:
为了防止GDM在会话开始时捕获A2DP音频输出,请编辑/var/lib/gdm3/.config/pulse/client.conf文件(如果不存在,则创建该文件)。
autospawn = no
daemon-binary = /bin/true

之后,您需要将此文件的访问权限授予Debian-gdm用户:
chown Debian-gdm:Debian-gdm /var/lib/gdm3/.config/pulse/client.conf

你还需要禁用pulseaudio的启动。
rm /var/lib/gdm3/.config/systemd/user/sockets.target.wants/pulseaudio.socket

为了让某些设备自动连接A2DP,请将以下内容添加到`/etc/pulse/default.pa`文件中:
load-module module-switch-on-connect

重启。

非常感谢您,先生,不随波逐流,而是真正解释了问题!我可以确认,在Debian stretch上,这确实有所帮助。 - Marcin Owsiany

这对我的固定卡片配置问题有所帮助:https://bugs.launchpad.net/ubuntu/+source/pulseaudio/+bug/1181106/comments/15

引用:

1) Modify the /etc/bluetooth/audio.conf
================================

[General]
Enable = Source,Sink,Headset,Gateway,Control,Media
Disable = Socket

HFP=false

[A2DP]
SBCSources=1
MPEG12Sources=0

2) Check for pulseaudio module :
==========================

$ pactl list | grep -i module-bluetooth

If you dont have :
     module-bluetooth-policy
     module-bluetooth-discover
     module-bluetooth-device

Just do :
$ sudo apt-get install pulseaudio-module-bluetooth
$ pulseaudio -k
$ pactl load-module module-bluetooth-device
$ pactl load-module module-switch-on-connect

3) Connect the bluetooth headset
===========================

Use blueman to pair, and connect audio sink
Use pavucontrol to change for A2DP (even if blueman said "audio profile off")

Please confirm that works for you too.

1这是唯一一个在Ubuntu 20.04上适用于Sennheiser HD 350BT的解决方案。 - Eugene Gr. Philippov
试过了,但是module-bluetooth-device并没有列出来,而且安装pulseaudio-module-bluetooth也没有安装缺失的模块。 - ThEBiShOp

我的问题通过这个得到了解决。
mv ~/.config/pulse/ ~/.config/pulse.old
systemctl --user restart pulseaudio
systemctl restart bluetooth

1虽然这可能解决问题,但它并没有回答所提出的问题。他们问如何查看日志。 - David

我确信之前的回答实际上试图解决问题的根本原因。然而,我无法让它们中的任何一个起作用。我遇到了与楼主完全相同的问题,但是在Linux Mint 17/18上。我写了this script来可靠地连接蓝牙并使用a2dp。
为了保存记录,我将其复制/粘贴下来,但问题的关键是它完全复制了楼主(和我)在用户界面中所要做的事情。
Dominik的脚本对我来说不可靠,因为它只会尝试一次设置所有内容。我必须多次运行它才能使其正常工作。以下是我最终得到的每次都有效的解决方法。
#!/bin/bash

MAC=$1
CARD=$(echo "${MAC}" | sed -e 's/:/_/g')
CARD="bluez_card.${CARD}"

$(pactl list | grep -qi 'Active Profile: a2dp_sink')
a2dpUsed=$?

# this loops until a2dp is _actually used_ on the given MAC.
while [ ${a2dpUsed} -ne 0 ];
do
    #
    echo "Restarting bluetooth."
    rfkill unblock bluetooth
    sudo service bluetooth restart
    sudo hciconfig hci0 up

    # reconnect
    echo -e "power on\nconnect ${MAC}" | bluetoothctl

    #
    echo "Waiting for headset to be connected..."
    btConnected=1

    while [ ${btConnected} -gt 0 ];
    do

        sleep .1
        $(bluetoothctl <<< "info ${MAC}" | grep -qi "Connected: yes")
        btConnected=$?
    done

    #
    echo "Bluetooth connected, waiting for profiles to register"

    cardFound=1
    while [ ${cardFound} -ne 0 ];
    do
        $(pactl list | grep -qi "${CARD}")
        cardFound=$?
    done

    #
    echo "Setting bluetooth a2dp profile"
    pactl set-card-profile ${CARD} a2dp_sink
    $(pactl list | grep -qi 'Active Profile: a2dp_sink')
    a2dpUsed=$?
done

echo "a2dp is working."

正如这里所记录的那样,这是因为GDM在会话启动时捕获了A2DP音频输出,因为GDM需要在gdm会话中使用pulseaudio以实现辅助功能。 尝试将以下内容添加到"/var/lib/gdm3/.config/pulse/default.pa"文件中,如果该文件不存在则创建它,然后重新启动系统:

#!/usr/bin/pulseaudio -nF
#

# load system wide configuration
.include /etc/pulse/default.pa

### unload driver modules for Bluetooth hardware
.ifexists module-bluetooth-policy.so
  unload-module module-bluetooth-policy
.endif

.ifexists module-bluetooth-discover.so
  unload-module module-bluetooth-discover
.endif