简体   繁体   English

Qt/C++ 连接经典蓝牙设备

[英]Qt/C++ Connect to Classic Bluetooth device

I am trying to pair and connect a bluetooth speaker to my device in Qt.我正在尝试在 Qt 中将蓝牙扬声器配对并连接到我的设备。 Qt offers with QBluetoothLocalDevice the functionality I need for pairing, which works fine for my purpose. Qt 为QBluetoothLocalDevice提供了我需要的配对功能,这对我来说很好用。 But I can not find a method to connect to the speaker after it has been paired.但是我找不到配对后连接到扬声器的方法。

I need to connect to it so when calling pactl list sinks I see the sink I need for playback.我需要连接到它,因此在调用pactl list sinks我会看到播放所需的pactl list sinks The class QBluetoothLocalDevice offers signals for deviceConnected and disconnected but it does not have a connection method.QBluetoothLocalDevice为 deviceConnected 和 disconnected 提供信号,但它没有连接方法。 I would be optimal to have the connection be still alive after the destruction of the pointer to QBluetoothLocalDevice.在销毁指向 QBluetoothLocalDevice 的指针后,我最好让连接仍然存在。

Anybody know to solve this?有人知道解决这个问题吗?

It looks like there is no way to programmatically do that in QT at the moment.目前似乎无法在 QT 中以编程方式执行此操作。

You are going to have to use DBus API to send a connection request to Bluez.您将不得不使用DBus API向 Bluez 发送连接请求。

As a simple workaround, you can run bluetoothctl if it is installed in your system:作为一个简单的解决方法,如果您的系统中安装了 bluetoothctl,您可以运行它:

QProcess process;
process.setProcessChannelMode(QProcess::SeparateChannels);
process.start("bluetoothctl");
process.waitForStarted();
process.write(QString("connect %1\n").arg(address).toUtf8());
process.closeWriteChannel();
process.waitForFinished();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM