代码之家  ›  专栏  ›  技术社区  ›  mozcelikors

Qt如何在Linux中以编程方式正确连接手机(蓝牙A2DP、AVRCP、HSP、HFP)

  •  1
  • mozcelikors  · 技术社区  · 6 年前

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

    然而,当我在Qt中使用QBluetoothSocket并使用配置文件连接时,总会有一个配置文件未连接。例如,连接到免提模式时,电话功能正常,但媒体控制不起作用。简而言之,我希望能够连接到蓝牙作为 蓝牙CTL 做我对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 as连接蓝牙的任何帮助 蓝牙CTL

    1 回复  |  直到 6 年前
        1
  •  2
  •   SGaist    6 年前

    不是直接的答案,但你可能想查看KDE的 KDEConnect 项目它已经完成了你想要的,可能是灵感的来源,也可能是你对项目的贡献。

    推荐文章