【问题标题】:Qt How to properly connect to a phone programmatically (Bluetooth A2DP, AVRCP, HSP, HFP) in LinuxQt 如何在 Linux 中以编程方式正确连接手机(蓝牙 A2DP、AVRCP、HSP、HFP)
【发布时间】:2019-03-11 22:02:16
【问题描述】:

我正在尝试开发一个使用 bluez stack 以及 pulseaudio 和 ofono 的应用程序,以便连接到手机并实现媒体播放 (A2DP)、媒体控制 (AVRCP) 和免提电话 (HFP) 等任务)。当我通过bluetoothctl 连接到我的手机时,它会自动连接到所有可用的配置文件,因此可以通过我的程序使用所有配置文件 A2DP、AVRCP、HFP。如果我没有使用bluetoothctl 连接到我的手机,免提/HFP 调制解调器未启用/在ofono 中通电。

但是,当我在 Qt 中使用 QBluetoothSocket 并使用配置文件连接时,总是有一个配置文件未连接。例如,连接到免提配置文件,电话工作正常,但媒体控制不起作用。简而言之,我希望能够像bluetoothctl 那样连接到蓝牙。我在 Qt 中的内容如下(简而言之):

static const QList<QBluetoothUuid> audioUuids = QList<QBluetoothUuid>()
        << QBluetoothUuid::HeadsetAG
        << QBluetoothUuid::AV_RemoteControlTarget;
..
void BtConnection::setConnection(int index)
{
    if(m_bluetoothSocket == nullptr) {
        m_bluetoothSocket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
        qDebug() << "Created Bluetooth Socket";
    }
    if(m_bluetoothSocket != nullptr) {
        connect(m_bluetoothSocket, SIGNAL(connected()), this, SLOT(connected()));
        connect(m_bluetoothSocket, SIGNAL(disconnected()), this, SLOT(disconnected()));
        connect(m_bluetoothSocket, SIGNAL(error(QBluetoothSocket::SocketError)),
                this, SLOT(connectionError(QBluetoothSocket::SocketError)));
    }

    m_device = get(index);

    // Check if an element in m_device.serviceUuids() match with an element in audioUuids
    QList<QBluetoothUuid>::const_iterator uuid;
    for (uuid = audioUuids.begin(); uuid != audioUuids.end(); ++uuid) {
        if(m_device.serviceUuids().indexOf(*uuid) > 0) {
            // This device supports one of the uuids we have scanned for
            if(m_bluetoothSocket != nullptr) {
                qDebug() << "*****Connecting...   " << *uuid;
                m_bluetoothSocket->connectToService(m_device.address(), *uuid);
                return;
            }
        }
    }
    qDebug() << "*****Cannot connect to service...";
}

如果您不清楚,我愿意发布更多代码。非常感谢任何有关如何使用 Qt 连接到蓝牙的帮助,就像 bluetoothctl 所做的那样。

【问题讨论】:

  • 我也有同样的问题,你解决了吗?你是怎么做到的?谢谢

标签: qt bluetooth bluez pulseaudio a2dp


【解决方案1】:

不是直接的答案,但您可能想查看 KDE 的 KDEConnect 项目。它已经完成了您正在寻找的工作,并且可能是灵感的来源,或者您可以为项目做出贡献。

【讨论】:

  • KDEConnect是自切片面包以来最伟大的东西,但它并没有做到作者想要写的,即HFP。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-02
  • 1970-01-01
  • 2015-03-14
相关资源
最近更新 更多