Issues with Qt Bluetooth Scanner (android)
-
Greetings,
I have defined Bluetooth Scanner in Class A (bluetooth_server) and I want to display the scanned available devices in Class B.
I have done it but it is working inefficiently, the problem is that scanned devices don't always show up in ListView, sometimes I need to close the app and make several try to display available devices.- Also, the scanner works fine if I turn off Bluetooth enter the app and then turn it on.
-
Greetings,
I have defined Bluetooth Scanner in Class A (bluetooth_server) and I want to display the scanned available devices in Class B.
I have done it but it is working inefficiently, the problem is that scanned devices don't always show up in ListView, sometimes I need to close the app and make several try to display available devices.- Also, the scanner works fine if I turn off Bluetooth enter the app and then turn it on.
-
@ahsan737
do you continuously scan, or only one scan ?There's no guarantee that all devices will be found on the first scan
@j-hilk only once using signal/slot of QBluetoothDeviceDiscoveryAgent.
here is the code.**Class A (bluetooth_server)** agent = new QBluetoothDeviceDiscoveryAgent; connect(agent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), this, SLOT(deviceDiscovered(QBluetoothDeviceInfo))); agent->start(); void bluetooth_server::deviceDiscovered(const QBluetoothDeviceInfo &device) { emit DevicesList(device.name(),device.address().toString()); } **Class B (UI)** server = new bluetooth_server(this); connect(server, &bluetooth_server::DevicesList, this, &bluetooth_settings::DevicesList) void bluetooth_settings::DevicesList(const QString &name, const QString &address) { ui->listWidget->addItem(name+"::"+address); }
-
@j-hilk only once using signal/slot of QBluetoothDeviceDiscoveryAgent.
here is the code.**Class A (bluetooth_server)** agent = new QBluetoothDeviceDiscoveryAgent; connect(agent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), this, SLOT(deviceDiscovered(QBluetoothDeviceInfo))); agent->start(); void bluetooth_server::deviceDiscovered(const QBluetoothDeviceInfo &device) { emit DevicesList(device.name(),device.address().toString()); } **Class B (UI)** server = new bluetooth_server(this); connect(server, &bluetooth_server::DevicesList, this, &bluetooth_settings::DevicesList) void bluetooth_settings::DevicesList(const QString &name, const QString &address) { ui->listWidget->addItem(name+"::"+address); }
@ahsan737
there you have it, you have to continue scanning after its finishedfor example:
connect(agent, &QBluetoothDeviceDiscoveryAgent::finished, agent, &QBluetoothDeviceDiscoveryAgent::start);
make sure you compare the uuids, so that you don't add already discovered devices
-
@ahsan737
there you have it, you have to continue scanning after its finishedfor example:
connect(agent, &QBluetoothDeviceDiscoveryAgent::finished, agent, &QBluetoothDeviceDiscoveryAgent::start);
make sure you compare the uuids, so that you don't add already discovered devices
-
@j-hilk oh thank you so much, I am very beginner so cannot even understand documentation sometimes. Can you please give a clue how to compare the UUIDs in order to avoid already discovered devices?
@ahsan737
QBluetoothDeviceInfo has an access function deviceUuid save them in a QList and before adding simply parse through the list to check if the uuid was already added -
@ahsan737
QBluetoothDeviceInfo has an access function deviceUuid save them in a QList and before adding simply parse through the list to check if the uuid was already added -
@j-hilk got it, thanks. I am trying it. I still cannot understand one fact that whenever I turn off Bluetooth, enter the app and then turn on Bluetooth (using localdevice.poweron ()) then it always scans immediately.