[英]How to enumerate Bluetooth devices using Qt
我想搜索可用的蓝牙设备并列出它们,但我从文档中遵循的代码不起作用。
BluetoothDevices::BluetoothDevices(QObject *parent) : QObject(parent)
{
}
// search Bluetooth devices
void BluetoothDevices::startDeviceDiscovery()
{
qDebug() << "Bluetooth discovery started";
// Create a discovery agent and connect to its signals
//QBluetoothDeviceDiscoveryAgent *discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),
this, SLOT(deviceDiscovered(QBluetoothDeviceInfo)));
// Start a discovery
discoveryAgent->start();
}
void BluetoothDevices::deviceDiscovered(const QBluetoothDeviceInfo &device)
{
qDebug() << "Found new device:" << device.name() << '(' << device.address().toString() << ')';
}
在主窗口中,我开始搜索:
void MainWindow::on_pushButtonSearchBluetooth_clicked()
{
bluetoothDevices.startDeviceDiscovery();
}
我希望deviceDiscovered()
插槽在找到时使用设备名称被调用,但该插槽永远不会被调用。 在开始发现之后,我再也没有收到任何回复。 当我从控制面板手动搜索蓝牙设备时,它确实找到了几台笔记本电脑。 我还需要做什么才能通过我的应用找到它们?
我正在使用 Windows 7 和 Qt 5.5。
我也有同样的问题。 您可以连接信号connect(m_discoveryAgent, SIGNAL(error(QBluetoothServiceDiscoveryAgent::Error)), this, SLOT(error(QBluetoothServiceDiscoveryAgent::Error)));
您可以看到该插槽发出错误 InvalidBluetoothAdapterError。 我在 WIN7 和 WIN10 上尝试过,但没有成功。 在 Ubuntu 中,相同的代码可以正常工作。
windows平台似乎不支持蓝牙。
目前,该 API 支持以下平台:Android、iOS、Linux (BlueZ 4.x/5.x) 和 OS X。来自http://doc.qt.io/qt-5/qtbluetooth-index.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.