我正在研究Qt C ++应用程序,以将蓝牙Walketalkie(带有“一键通”按钮)连接到基于嵌入式linux(Cortex-A9核心)的集成设备。我的设备具有一个集成的蓝牙适配器450-0159,该适配器带有Cypress / Broadcom CYW4343W / bcm4343W BT / WLAN芯片。我正在使用bluez-5.43和bluez-alsa连接蓝牙音频服务(A2DP和HFP)。在跨蓝牙库的应用程序中,我可以正确管理蓝牙适配器,开机,关机,配对,但无法连接蓝牙音频配置文件。因此,我实现了以下方法来连接/断开蓝牙对讲机:
void BluetoothMgmt::connectDevice(QString address) {
QString command = "echo -e \"connect " + address + "\nquit\" | bluetoothctl";
QString output = execSystemCommand(command);
qDebug() << "OUTPUT EXEC SYSTEM COMMAND 1:" << output;
}
void BluetoothMgmt::disconnectDevice(QString address) {
QString command = "echo -e \"disconnect " + address + "\nquit\" | bluetoothctl";
QString output = execSystemCommand(command);
qDebug() << "OUTPUT EXEC SYSTEM COMMAND 1:" << output;
}
bool BluetoothMgmt::isDeviceConnected(QString address) {
bool connected = false;
QString command = "echo -e \"info " + address + "\nquit\" | bluetoothctl | grep Connected";
QString output = execSystemCommand(command);
qDebug() << "command output:" << output;
QStringList outputParts = output.split(WHITESPACE_CHAR);
QString status = outputParts.at(1);
if(status.contains("yes",Qt::CaseInsensitive))
connected = true;
return connected;
}
调用“ connectDevice”功能后,我的Walketalkie已正确连接到A2DP和HFP配置文件,并且我正在使用“ isDeviceConnected”功能来了解蓝牙设备的状态(无论是否已连接)。现在,我应该获取有关RFCOMM套接字的信息,以了解即按即讲按钮的状态(如果按下或不按下),因此我已实现以下代码来连接RFCOMM套接字:
QBluetoothSocket *socket;
socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
connect(socket,SIGNAL(connected()),this,SLOT(socketConnected()));
connect(socket,SIGNAL(disconnected()),SLOT(socketDisconnect()));
connect(socket,SIGNAL(readyRead()),SLOT(readSocket()));
connect(socket,SIGNAL(error(QBluetoothSocket::SocketError)),SLOT(socketError(QBluetoothSocket::SocketError)));
void BluetoothMgmt::socketConnected() {
qDebug() << "Socket connected:" << socket->peerName();
}
void BluetoothMgmt::socketDisconnect() {
qDebug() << "Socket disconnected";
}
void BluetoothMgmt::readSocket() {
QString message;
message = QString::fromLatin1(socket->readAll().data());
/* Code to parse Push-To-Talk button status */
}
void BluetoothMgmt::socketError(QBluetoothSocket::SocketError error) {
qDebug() << "Socket Error:" << socket->errorString();
}
/* To connect QBluetoothSocket */
int channel = 11
socket->connectToService(bluetooth_device_address,channel);
要知道套接字连接的通道,我已经通过命令行启动了“ sdptool记录bluetooth_device_address”。我的设备的输出如下:
root@localhost:/dev# sdptool records 00:1F:82:3A:6E:5F
Service RecHandle: 0x10002
Service Class ID List:
"Audio Sink" (0x110b)
Protocol Descriptor List:
"L2CAP" (0x0100)
PSM: 25
"AVDTP" (0x0019)
uint16: 0x0103
Profile Descriptor List:
"Advanced Audio" (0x110d)
Version: 0x0103
Service RecHandle: 0x10003
Service Class ID List:
"PnP Information" (0x1200)
Service RecHandle: 0x10004
Service Class ID List:
"AV Remote" (0x110e)
"AV Remote Controller" (0x110f)
Protocol Descriptor List:
"L2CAP" (0x0100)
PSM: 23
"AVCTP" (0x0017)
uint16: 0x0104
Profile Descriptor List:
"AV Remote" (0x110e)
Version: 0x0106
Service RecHandle: 0x10005
Service Class ID List:
"AV Remote Target" (0x110c)
Protocol Descriptor List:
"L2CAP" (0x0100)
PSM: 23
"AVCTP" (0x0017)
uint16: 0x0104
Profile Descriptor List:
"AV Remote" (0x110e)
Version: 0x0106
Service Name: Headset
Service RecHandle: 0x1000a
Service Class ID List:
"Headset" (0x1108)
"" (0x1131)
"Generic Audio" (0x1203)
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 13
Language Base Attr List:
code_ISO639: 0x656e
encoding: 0x6a
base_offset: 0x100
Profile Descriptor List:
"Headset" (0x1108)
Version: 0x0102
Service Name: Hands-Free unit
Service RecHandle: 0x1000d
Service Class ID List:
"Handsfree" (0x111e)
"Generic Audio" (0x1203)
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 12
Language Base Attr List:
code_ISO639: 0x656e
encoding: 0x6a
base_offset: 0x100
Profile Descriptor List:
"Handsfree" (0x111e)
Version: 0x0105
我正在使用13号通道进行套接字(RfcommProtocol)连接。
RFCOMM连接工作正常,并且我可以读取一键通按钮状态。 问题是我能够连接音频服务(A2DP和HFP)或RFCOMM连接,但是我应该同时连接所有配置文件。使用“ bluetoothctl”工具连接设备后,如何阅读RFCOMM消息? 显示带有“一键通”消息的蓝牙数据包的唯一方法是从命令行启动“ btmon”(蓝牙监视器),但是我相信这不是正确的方法,而且我也不知道如何将其集成到我的c ++中应用。 不幸的是,无法使用Qt库连接蓝牙音频配置文件,因此,我正在使用“ bluetoothctl”工具。 “ Bluetoothctl”可能发送允许“ bluez-library”连接A2DP和SCO配置文件并使用alsa驱动程序创建虚拟PCM声卡的dbus消息。 有什么建议么?感谢您的建议。