Тема: Qt Bluetooth Low Energy
Нарешті знову взявся за це. І на самому ж початку спотикнувся.
Може я упускаю щось очевидне?
Чому оцей код із прикладу працює:
▼Прихований текст
Device::Device():
connected(false), controller(0), m_deviceScanState(false), randomAddress(false)
{
//! [les-devicediscovery-1]
discoveryAgent = new QBluetoothDeviceDiscoveryAgent();
connect(discoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)),
this, SLOT(addDevice(const QBluetoothDeviceInfo&)));
connect(discoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)),
this, SLOT(deviceScanError(QBluetoothDeviceDiscoveryAgent::Error)));
connect(discoveryAgent, SIGNAL(finished()), this, SLOT(deviceScanFinished()));
//! [les-devicediscovery-1]
setUpdate("Search");
}
void Device::startDeviceDiscovery()
{
qDeleteAll(devices);
devices.clear();
emit devicesUpdated();
setUpdate("Scanning for devices ...");
//! [les-devicediscovery-2]
discoveryAgent->start();
//! [les-devicediscovery-2]
if (discoveryAgent->isActive()) {
m_deviceScanState = true;
Q_EMIT stateChanged();
}
}
Де Device це:
class Device: public QObject
{
Q_OBJECT
Q_PROPERTY(QVariant devicesList READ getDevices NOTIFY devicesUpdated)
Q_PROPERTY(QVariant servicesList READ getServices NOTIFY servicesUpdated)
Q_PROPERTY(QVariant characteristicList READ getCharacteristics NOTIFY characteristicsUpdated)
Q_PROPERTY(QString update READ getUpdate WRITE setUpdate NOTIFY updateChanged)
Q_PROPERTY(bool useRandomAddress READ isRandomAddress WRITE setRandomAddress NOTIFY randomAddressChanged)
Q_PROPERTY(bool state READ state NOTIFY stateChanged)
Q_PROPERTY(bool controllerError READ hasControllerError)
public:
Device();
~Device();
QVariant getDevices();
QVariant getServices();
QVariant getCharacteristics();
QString getUpdate();
bool state();
bool hasControllerError() const;
bool isRandomAddress() const;
void setRandomAddress(bool newValue);
public slots:
void startDeviceDiscovery();
void scanServices(const QString &address);
void connectToService(const QString &uuid);
void disconnectFromDevice();
private slots:
// QBluetoothDeviceDiscoveryAgent related
void addDevice(const QBluetoothDeviceInfo&);
void deviceScanFinished();
void deviceScanError(QBluetoothDeviceDiscoveryAgent::Error);
// QLowEnergyController realted
void addLowEnergyService(const QBluetoothUuid &uuid);
void deviceConnected();
void errorReceived(QLowEnergyController::Error);
void serviceScanDone();
void deviceDisconnected();
// QLowEnergyService related
void serviceDetailsDiscovered(QLowEnergyService::ServiceState newState);
Q_SIGNALS:
void devicesUpdated();
void servicesUpdated();
void characteristicsUpdated();
void updateChanged();
void stateChanged();
void disconnected();
void randomAddressChanged();
private:
void setUpdate(QString message);
QBluetoothDeviceDiscoveryAgent *discoveryAgent;
DeviceInfo currentDevice;
QList<QObject*> devices;
QList<QObject*> m_services;
QList<QObject*> m_characteristics;
QString m_previousAddress;
QString m_message;
bool connected;
QLowEnergyController *controller;
bool m_deviceScanState;
bool randomAddress;
};
А у цьому discoveryAgent->isActive() завжди True і список пристроїв завжди пустий:
▼Прихований текст
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QBluetoothDeviceDiscoveryAgent *discoveryAgent = new QBluetoothDeviceDiscoveryAgent();
discoveryAgent->start();
QThread::sleep(10);
while (discoveryAgent->isActive())
{
QThread::sleep(1);
qDebug("wait...");
// qDebug("Count: %d", discoveryAgent->discoveredDevices().count());
}
QList<QBluetoothDeviceInfo> deviceList = discoveryAgent->discoveredDevices();
qDebug("Count: %d", deviceList.count());
}