Problema con API bluetooth
-
Buenas, he estado intentando utilizar la API de bluetooth que tiene implementado Qt con el fin de desarrollar una aplicación para la búsqueda de dispositivos cercanos.
Una vez conseguí que me devolviese los valores de los dispositivos cercanos, me percaté de que si alguno de ellos se apagaba me lo iba a seguir mostrando y si encendía o acercaba uno nuevo éste no sería detectado. Únicamente me va a mostrar los dispositivos que previamente han sido descubiertos por la propia herramienta de Ubuntu (estoy trabajando en Linux) para la búsqueda de dispositivos bluetooth. Aquí os dejo el código:Main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QBluetoothLocalDevice>
#include <QDebug>
#include <QBluetoothDeviceDiscoveryAgent>
#include <QtBluetooth/QBluetoothLocalDevice>
//! [include]
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtCore/QFile>
#include <QtCore/QObject>
#include <QtBluetooth/QBluetoothDeviceDiscoveryAgent>
#include <QtBluetooth/QBluetoothServiceDiscoveryAgent>
#include <QtBluetooth/QBluetoothTransferManager>
#include <QtBluetooth/QBluetoothTransferRequest>
#include <QtBluetooth/QBluetoothTransferReply>#include <QtBluetooth/QLowEnergyController>
#include <QtBluetooth/QLowEnergyService>
#include <QtBluetooth/QLowEnergyCharacteristic>int main(int argc, char *argv[])
{QBluetoothLocalDevice localDevice; QString localDeviceName; // Check if Bluetooth is available on this device if (localDevice.isValid()) { // Turn Bluetooth on localDevice.powerOn(); // Read local device name localDeviceName = localDevice.name(); // Make it visible to others localDevice.setHostMode(QBluetoothLocalDevice::HostDiscoverable); // Get connected devices QList<QBluetoothAddress> remotes; remotes = localDevice.connectedDevices(); qDebug() << remotes; } MyClass hola; hola.startDeviceDiscovery(); //while( 1);
}
void MyClass::startDeviceDiscovery()
{// Create a discovery agent and connect to its signals QBluetoothDeviceDiscoveryAgent *discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this); connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), this, SLOT(deviceDiscovered(QBluetoothDeviceInfo))); // Start a discovery discoveryAgent->start(); //...
}
// In your local slot, read information about the found devices
void MyClass::deviceDiscovered(const QBluetoothDeviceInfo &device)
{
qDebug() << "Found new device:" << device.name() << '(' << device.address().toString()<<' ' << device.minorDeviceClass()<< ')';
}Un saludo.