Scanning BLE beacons
-
I start a scan
BT_DEVICE::BT_DEVICE() { discoveryAgent = new QBluetoothDeviceDiscoveryAgent(); discoveryAgent->setLowEnergyDiscoveryTimeout(5000); connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &BT_DEVICE::AddDevice); connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &BT_DEVICE::ScanFinished); } void BT_DEVICE::BLE_Scan() { qDeleteAll(devices); devices.clear(); discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod); if (discoveryAgent->isActive()) { m_deviceScanState = true; } }
Then on deviceDiscovered event I add my remote device to a list
void BT_DEVICE::AddDevice(const QBluetoothDeviceInfo &info) { if (info.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) { auto d = new DeviceInfo(info); if (d->getName() == "MyDeviceName") devices.append(d); } }
Finally on finished event
void BT_DEVICE::ScanFinished() { QVariant dev_list = QVariant::fromValue(devices); }
But what actually is finished ? Remote devices sends beacons constantly, so as for me it never finished. I should scan it constantly.
-
I start a scan
BT_DEVICE::BT_DEVICE() { discoveryAgent = new QBluetoothDeviceDiscoveryAgent(); discoveryAgent->setLowEnergyDiscoveryTimeout(5000); connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &BT_DEVICE::AddDevice); connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &BT_DEVICE::ScanFinished); } void BT_DEVICE::BLE_Scan() { qDeleteAll(devices); devices.clear(); discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod); if (discoveryAgent->isActive()) { m_deviceScanState = true; } }
Then on deviceDiscovered event I add my remote device to a list
void BT_DEVICE::AddDevice(const QBluetoothDeviceInfo &info) { if (info.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) { auto d = new DeviceInfo(info); if (d->getName() == "MyDeviceName") devices.append(d); } }
Finally on finished event
void BT_DEVICE::ScanFinished() { QVariant dev_list = QVariant::fromValue(devices); }
But what actually is finished ? Remote devices sends beacons constantly, so as for me it never finished. I should scan it constantly.
@jenya7 said in Scanning BLE beacons:
But what actually is finished ? Remote devices sends beacons constantly, so as for me it never finished. I should scan it constantly.
The scan is done. If you want to scan constantly, run the scan again, with a timer.
-
@jenya7 said in Scanning BLE beacons:
But what actually is finished ? Remote devices sends beacons constantly, so as for me it never finished. I should scan it constantly.
The scan is done. If you want to scan constantly, run the scan again, with a timer.
@sierdzio said in Scanning BLE beacons:
@jenya7 said in Scanning BLE beacons:
But what actually is finished ? Remote devices sends beacons constantly, so as for me it never finished. I should scan it constantly.
The scan is done. If you want to scan constantly, run the scan again, with a timer.
Say I have 10 beacons. I trigger the scan and it gets 5 beacons. Then I trigger again - how can I be sure it scans another 5 and not the same fives?
-
@sierdzio said in Scanning BLE beacons:
@jenya7 said in Scanning BLE beacons:
But what actually is finished ? Remote devices sends beacons constantly, so as for me it never finished. I should scan it constantly.
The scan is done. If you want to scan constantly, run the scan again, with a timer.
Say I have 10 beacons. I trigger the scan and it gets 5 beacons. Then I trigger again - how can I be sure it scans another 5 and not the same fives?
@jenya7 said in Scanning BLE beacons:
Say I have 10 beacons. I trigger the scan and it gets 5 beacons. Then I trigger again - how can I be sure it scans another 5 and not the same fives?
no guarantee, the beacons (and other le devices) have to advertise during your scan phase.
The trick is, storing a list of found devices and checking if a new one was added or not
-
@jenya7 said in Scanning BLE beacons:
Say I have 10 beacons. I trigger the scan and it gets 5 beacons. Then I trigger again - how can I be sure it scans another 5 and not the same fives?
no guarantee, the beacons (and other le devices) have to advertise during your scan phase.
The trick is, storing a list of found devices and checking if a new one was added or not
@J-Hilk said in Scanning BLE beacons:
@jenya7 said in Scanning BLE beacons:
Say I have 10 beacons. I trigger the scan and it gets 5 beacons. Then I trigger again - how can I be sure it scans another 5 and not the same fives?
no guarantee, the beacons (and other le devices) have to advertise during your scan phase.
The trick is, storing a list of found devices and checking if a new one was added or not
I see. So I have some pain to get all data from all devices. Have to figure it out how to do it.
Another question - how can I iterate all devicesfor (int i = 0; i < devices.length(); i++) { devices[i]. //??? it has no name and data field }
-
I don't know what your variable
devices
is, so I can't say.But you can use discoveredDevices() which returns a QList, so:
for (const auto &device : discoveryAgent->discoveredDevices()) { // do something with `device` }
-
@J-Hilk said in Scanning BLE beacons:
@jenya7 said in Scanning BLE beacons:
Say I have 10 beacons. I trigger the scan and it gets 5 beacons. Then I trigger again - how can I be sure it scans another 5 and not the same fives?
no guarantee, the beacons (and other le devices) have to advertise during your scan phase.
The trick is, storing a list of found devices and checking if a new one was added or not
I see. So I have some pain to get all data from all devices. Have to figure it out how to do it.
Another question - how can I iterate all devicesfor (int i = 0; i < devices.length(); i++) { devices[i]. //??? it has no name and data field }
@jenya7 https://doc.qt.io/qt-5/qbluetoothdeviceinfo.html has this.
What is this Device class? Its constructor takes a QBluetoothDeviceInfo instance, so it should have the information... -
@J-Hilk said in Scanning BLE beacons:
@jenya7 said in Scanning BLE beacons:
Say I have 10 beacons. I trigger the scan and it gets 5 beacons. Then I trigger again - how can I be sure it scans another 5 and not the same fives?
no guarantee, the beacons (and other le devices) have to advertise during your scan phase.
The trick is, storing a list of found devices and checking if a new one was added or not
I see. So I have some pain to get all data from all devices. Have to figure it out how to do it.
Another question - how can I iterate all devicesfor (int i = 0; i < devices.length(); i++) { devices[i]. //??? it has no name and data field }
@jenya7
usually, you react to the deviceDiscovered signal.
https://doc.qt.io/qt-5/qbluetoothdevicediscoveryagent.html#deviceDiscoveredthat has a QBluetoothDeviceInfo argument, make a slot that accepts that as argument.
https://doc.qt.io/qt-5/qbluetoothdeviceinfo.htmlI personally have a class an interface class that takes a QBluetoothDeviceInfo as constructor argument, and I store that in a list to iterate over.
-
@jenya7 https://doc.qt.io/qt-5/qbluetoothdeviceinfo.html has this.
What is this Device class? Its constructor takes a QBluetoothDeviceInfo instance, so it should have the information...@jsulm said in Scanning BLE beacons:
@jenya7 https://doc.qt.io/qt-5/qbluetoothdeviceinfo.html has this.
What is this Device class? Its constructor takes a QBluetoothDeviceInfo instance, so it should have the information...I added it to a list
if (d->getName() == "MyDeviceName") devices.append(d);
How do I retrieve the device data?
No fields like
devices[i].Name
devices[i].Data -
It's your custom class of some sort! We can't know how to use a class we don't know :-)
-
@jsulm said in Scanning BLE beacons:
@jenya7 https://doc.qt.io/qt-5/qbluetoothdeviceinfo.html has this.
What is this Device class? Its constructor takes a QBluetoothDeviceInfo instance, so it should have the information...I added it to a list
if (d->getName() == "MyDeviceName") devices.append(d);
How do I retrieve the device data?
No fields like
devices[i].Name
devices[i].Data@jenya7 said in Scanning BLE beacons:
How do I retrieve the device data?
Why do you ask us?
Device is your class, you did not even show its code - how should we know?! -
@jsulm said in Scanning BLE beacons:
@jenya7 https://doc.qt.io/qt-5/qbluetoothdeviceinfo.html has this.
What is this Device class? Its constructor takes a QBluetoothDeviceInfo instance, so it should have the information...I added it to a list
if (d->getName() == "MyDeviceName") devices.append(d);
How do I retrieve the device data?
No fields like
devices[i].Name
devices[i].Data@jenya7 said in Scanning BLE beacons:
if (d->getName() == "MyDeviceName")
devices.append(d);According to this code you do it this way:
devices[i]->getName();
-
@jenya7 said in Scanning BLE beacons:
How do I retrieve the device data?
Why do you ask us?
Device is your class, you did not even show its code - how should we know?!@jsulm said in Scanning BLE beacons:
@jenya7 said in Scanning BLE beacons:
How do I retrieve the device data?
Why do you ask us?
Device is your class, you did not even show its code - how should we know?!Why? It's Qt class
QBluetoothDeviceInfo &info
Here
void BT_DEVICE::AddDevice(const QBluetoothDeviceInfo &info) { if (info.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) { auto d = new DeviceInfo(info); if (d->getName() == "MyDeviceName") devices.append(d); } }
I see my device name (d->getName() ) so it probably should hold the beacon raw data (like d->data())
-
@jenya7 said in Scanning BLE beacons:
auto d = new DeviceInfo(info);
DeviceInfo
is NOTQBluetoothDeviceInfo
. It's some custom class you created. We don't know how to access it.If you are asking how to get data from
info
(which is an instance ofQBluetoothDeviceInfo
), then just read the docs we have already linked: https://doc.qt.io/qt-5/qbluetoothdeviceinfo.html -
@jenya7 said in Scanning BLE beacons:
auto d = new DeviceInfo(info);
DeviceInfo
is NOTQBluetoothDeviceInfo
. It's some custom class you created. We don't know how to access it.If you are asking how to get data from
info
(which is an instance ofQBluetoothDeviceInfo
), then just read the docs we have already linked: https://doc.qt.io/qt-5/qbluetoothdeviceinfo.html@sierdzio said in Scanning BLE beacons:
@jenya7 said in Scanning BLE beacons:
auto d = new DeviceInfo(info);
DeviceInfo
is NOTQBluetoothDeviceInfo
. It's some custom class you created. We don't know how to access it.If you are asking how to get data from
info
(which is an instance ofQBluetoothDeviceInfo
), then just read the docs we have already linked: https://doc.qt.io/qt-5/qbluetoothdeviceinfo.htmlI see. Yes I use this class (from an example - C:\Qt\Examples\Qt-5.12.0\bluetooth\lowenergyscanner)
class DeviceInfo: public QObject { Q_OBJECT Q_PROPERTY(QString deviceName READ getName NOTIFY deviceChanged) Q_PROPERTY(QString deviceAddress READ getAddress NOTIFY deviceChanged) public: DeviceInfo() = default; DeviceInfo(const QBluetoothDeviceInfo &d); QString getAddress() const; QString getName() const; QBluetoothDeviceInfo getDevice(); void setDevice(const QBluetoothDeviceInfo &dev); Q_SIGNALS: void deviceChanged(); private: QBluetoothDeviceInfo device; };
So it has
QString DeviceInfo::getName() const { return device.name(); }
What if I want to add
DeviceInfo::getData() const { return device. //???? - no data field }
-
But what data do you want to read? Manufacturer data? Device UUID? Address?
Or you want to read the data that the device is sending? To do that, you need to connect to it first. https://doc.qt.io/qt-5/qtbluetooth-lowenergyscanner-example.html#connecting-to-services
-
But what data do you want to read? Manufacturer data? Device UUID? Address?
Or you want to read the data that the device is sending? To do that, you need to connect to it first. https://doc.qt.io/qt-5/qtbluetooth-lowenergyscanner-example.html#connecting-to-services
@sierdzio said in Scanning BLE beacons:
But what data do you want to read? Manufacturer data? Device UUID? Address?
Or you want to read the data that the device is sending? To do that, you need to connect to it first. https://doc.qt.io/qt-5/qtbluetooth-lowenergyscanner-example.html#connecting-to-services
OK. I understand. I can use info directly like info.name, but how do I get data (no info.data).
Yes I want to se all beacon raw data - payload. I need no services. It's a remote sensor sending me temperature, humidity, pressure, etc. -
But what data do you want to read? Manufacturer data? Device UUID? Address?
Or you want to read the data that the device is sending? To do that, you need to connect to it first. https://doc.qt.io/qt-5/qtbluetooth-lowenergyscanner-example.html#connecting-to-services
@sierdzio said in Scanning BLE beacons:
Or you want to read the data that the device is sending? To do that, you need to connect to it first. https://doc.qt.io/qt-5/qtbluetooth-lowenergyscanner-example.html#connecting-to-services
not with beacon, beacons are not connectable. And the Qt Api gives no way to identify that.
what the op is looking for should be in
https://doc.qt.io/qt-5/qbluetoothdeviceinfo.html#manufacturerData-1 -
It's been a long time since I've done it, sorry I don't remember the details. Check the example docs above, it explains how to connect to various services and characteristics, then you can start receiving the data.