简体   繁体   中英

How to enumerate Bluetooth devices using Qt

I want to search bluetooth devices available and list them but the code I am following from documenation doesn't work.

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() << ')';
}

In main window I start the search:

void MainWindow::on_pushButtonSearchBluetooth_clicked()
{
    bluetoothDevices.startDeviceDiscovery();
}

I expect the deviceDiscovered() slot to get called with the device names as it gets found but this slot never gets called. I never hear anything back after initiating discovery. When I do search bluetooth devices manually from control panel, it does find couple of laptops. What else do I need to do to find them through my app?

I am using windows 7 and Qt 5.5.

I have the same problem. You can connect signal connect(m_discoveryAgent, SIGNAL(error(QBluetoothServiceDiscoveryAgent::Error)), this, SLOT(error(QBluetoothServiceDiscoveryAgent::Error))); and you can see that slot emitted with error InvalidBluetoothAdapterError. I try it on WIN7 and WIN10 and it not worked. In Ubuntu the same code work properly.

It seems that bluetooth not supported in windows platforms.

Currently, the API is supported on the following platforms: Android, iOS, Linux (BlueZ 4.x/5.x) and OS X. from http://doc.qt.io/qt-5/qtbluetooth-index.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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