我正在尝试开发一个使用 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
。