0

I tried bluetooth connection test in the Ubuntu under Vmware environment. Ubuntu is 14.04 lts, Bluetooth is USB-dongle and BT stack is Bluez 5.35. I don't use pulseaudio.

I used bluetoothctl command to scan, pairing and connection. But the connection is fail. Below is the log of bluetoothctl

$ sudo bluetoothctl
[bluetooth]# power on
[bluetooth]# show
Controller 00:1A:7D:DA:xx:xx (public)
Name: ubuntu
Alias: ubuntu-0
Class: 0x00000000
Powered: yes
Discoverable: no
Pairable: yes
UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control        (0000110e-0000-1000-8000-00805f9b34fb)
UUID: PnP Information           (00001200-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
Modalias: usb:v1D6Bp0246d0532
Discovering: no
[bluetooth]# scan on         // Wait until found BT headset
[bluetooth]# scan off
[bluetooth]# devices
Device 11:11:22:xx:xx:xx Test
Device 0C:E0:E4:xx:xx:xx PLT_Legend

[bluetooth]# pair 0C:E0:E4:xx:xx:xx    // try pairing with 0C:E0:E4:xx:xx:xx
Attempting to pair with 0C:E0:E4:xx:xx:xx
[CHG] Device 0C:E0:E4:xx:xx:xx Connected: yes
[PLT_Legend]# interfaces_added
[CHG] Device 0C:E0:E4:xx:xx:xx Modalias: bluetooth:v0055p0113d0067
[CHG] Device 0C:E0:E4:xx:xx:xx UUIDs: 00001108-0000-1000-8000-00805f9b34fb
[CHG] Device 0C:E0:E4:xx:xx:xx UUIDs: 0000110b-0000-1000-8000-00805f9b34fb
[CHG] Device 0C:E0:E4:xx:xx:xx UUIDs: 0000110c-0000-1000-8000-00805f9b34fb
[CHG] Device 0C:E0:E4:xx:xx:xx UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device 0C:E0:E4:xx:xx:xx UUIDs: 0000111e-0000-1000-8000-00805f9b34fb
[CHG] Device 0C:E0:E4:xx:xx:xx UUIDs: 00001200-0000-1000-8000-00805f9b34fb
[CHG] Device 0C:E0:E4:xx:xx:xx UUIDs: 82972387-294e-4d62-97b5-2668aa35f618
[CHG] Device 0C:E0:E4:xx:xx:xx ServicesResolved: yes
[CHG] Device 0C:E0:E4:xx:xx:xx Paired: yes
Pairing successful
[CHG] Device 0C:E0:E4:xx:xx:xx ServicesResolved: no
[CHG] Device 0C:E0:E4:xx:xx:xx Connected: no
[PLT_Legend]# connect 0C:E0:E4:xx:xx:xx
Attempting to connect to 0C:E0:E4:xx:xx:xx
[CHG] Device 0C:E0:E4:xx:xx:xxConnected: yes
Failed to connect: org.bluez.Error.Failed

I found above bluez doesn't have a2dp profile. Actually, bluez5.x and pulseaudio is combined closely. If don't use pulseaudio, user has to add extra profile(a2dp, or hspag).

Below code is sample for add a2dpsink , a2dpsource and haspag.

#define A2DP_SOURCE_ENDPOINT "/MediaEndpoint/A2DPSource"
#define A2DP_SINK_ENDPOINT "/MediaEndpoint/A2DPSink"
#define HSP_AG_PROFILE "/Profile/HSPAGProfile"

#define PA_BLUETOOTH_UUID_HSP_AG      "00001112-0000-1000-8000-00805f9b34fb"
#define PA_BLUETOOTH_UUID_A2DP_SOURCE "0000110a-0000-1000-8000-00805f9b34fb"
#define PA_BLUETOOTH_UUID_A2DP_SINK   "0000110b-0000-1000-8000-00805f9b34fb"

static void register_profile_reply(DBusPendingCall *call, void *user_data)
{
    //struct bluetooth_profile *profile = user_data;
    DBusMessage *reply = dbus_pending_call_steal_reply(call);
    DBusError derr;

    dbus_error_init(&derr);
    if (!dbus_set_error_from_message(&derr, reply)) {
        printf("Profile %s registered", (char *)user_data);
        goto done;
    }

    //unregister_profile(profile);

    printf("bluetooth: RequestProfile error: %s, %s", derr.name,
                                derr.message);
    dbus_error_free(&derr);
done:
    dbus_message_unref(reply);
}
static DBusConnection *connection;
void btd_profile_add_hspag(const char *profile, const char *uuid) 
{
    DBusMessage *msg;
    DBusMessageIter iter, opt;
    DBusPendingCall *call;

    connection = btd_get_dbus_connection();
    msg = dbus_message_new_method_call("org.bluez", "/org/bluez",
                        "org.bluez.ProfileManager1",
                        "RegisterProfile");
    if( msg == NULL ) {
        printf("%s : msg is null \n", __func__);
    }

    dbus_message_iter_init_append(msg, &iter);
    dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH, &profile);
    dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &uuid);
    dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
                    DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
                    DBUS_TYPE_STRING_AS_STRING
                    DBUS_TYPE_VARIANT_AS_STRING
                    DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
                    &opt);
    dbus_message_iter_close_container(&iter, &opt);
    if (!g_dbus_send_message_with_reply(connection, msg, &call, -1)) {
        printf("%s:%s - fail g_dbus_send_message_with_reply() \n", __FILE__, __func__);
        //unregister_profile(profile);
        goto failed;
    }
    dbus_pending_call_set_notify(call, register_profile_reply, &profile,
                                    NULL);
    dbus_pending_call_unref(call);
failed:
    dbus_message_unref(msg);
    return;
}
void btd_profile_add(void)
{
    btd_profile_add_hspag(HSP_AG_PROFILE, PA_BLUETOOTH_UUID_HSP_AG);
    btd_profile_add_hspag(A2DP_SOURCE_ENDPOINT, PA_BLUETOOTH_UUID_A2DP_SOURCE);
    btd_profile_add_hspag(A2DP_SINK_ENDPOINT, PA_BLUETOOTH_UUID_A2DP_SINK);
}

After that, the bluez has a2dp, hspag profile without pulseaudio. I can check it with "show" command of the bluetoothctl.

    [bluetooth]# show
    Controller 00:1A:7D:xx:xx:xx
    Name: ubuntu
    Alias: ubuntu-0
    Class: 0x000000
    Powered: yes
    Discoverable: no
    Pairable: yes
    UUID: Headset AG                (00001112-0000-1000-8000-00805f9b34fb)
    UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
    UUID: A/V Remote Control        (0000110e-0000-1000-8000-00805f9b34fb)
    UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
    UUID: PnP Information           (00001200-0000-1000-8000-00805f9b34fb)
    UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
    UUID: Audio Source              (0000110a-0000-1000-8000-00805f9b34fb)
    UUID: Audio Sink                (0000110b-0000-1000-8000-00805f9b34fb)
    Modalias: usb:v1D6Bp0246d0523
    Discovering: no

The bluetooth connection still fail even though a2dp profile is added. Through btmon trace, the rfcomm connection is disconnected, and next, l2cap connection is also disconnected. I don't know what i'm missing. What do i need other to run bluez 5.35 without pulseaudio?

4

1 回答 1

0

日志显示您的“PLT_Legend”设备支持 A2DP SINK 角色,因此您的 Ubuntu 应充当 A2DP SOURCE 角色。要使其工作,您需要一个工作配置文件实现。

您可能不需要完整实现 A2DP 源配置文件来获取音频数据,但至少您需要注册基本功能才能使 bluetoothd 满意。

下图是直接从这里复制过来的。

在此处输入图像描述

在上面的序列中,您可以看到当连接了具有 A2DP 功能的新设备时,蓝牙会调用“ SelectConfiguration ”和“ SetConfiguration ”。

正如您所观察到的,连接状态最初移动到已连接,然后回退到“否”。这是因为,bluetoothd 在获得低级连接后会尝试与配置文件(SDP 服务发现协议)进行连接。一旦低级连接成功,设备被标记为已连接,SDP 继续进行。

在 SDP bluetoothd 尝试获取您的设备(在这种情况下为 ubuntu)的功能并使用“SelectConfiguration”与终端设备(PLT_Legend)协商编解码器支持并使用“SetConfiguration”选择一个。握手成功后,蓝牙标记 A2DP 配置文件已连接。

因此,对于您的配置文件的虚拟注册,由于它找不到方法和正确的编解码器返回,它标志着连接失败并终止 Ubuntu 和设备之间的连接。

如果您不想使用pulseaudio,可以尝试使用bluez-alsa https://github.com/Arkq/bluez-alsa并开始收听A2DP 配置文件。我认为这个包已经在 ubuntu repo 中可用,你可以试试

sudo apt-get install bluez-alsa

AFAIK,pulseaudio 没有与蓝牙紧密绑定,你仍然可以以蓝牙理解的方式实现你自己的 A2DP 实现。

注意,bluez-alsa 使用开源 fdk-aac 进行 AAC 编码和解码。

于 2018-06-22T08:45:03.153 回答