App for bluetooth scanning
-
Hello, I am developing an app that once connected to a device (call it box), it gets a list of all the objects (lamps, doors and other sensors) and send them commands like "switch off the light 1".
I want to know if from an external application is possible to get all the connected objects to the box or there are security issues.
The external app doesn't connect directly to the objects but must send the command to the box.
I was thinking to use signals.
Thank you for the help. -
Hi,
Are you reimplementing an equivalent to Apple's HomeKit ?
-
@SGaist said in App for bluetooth scanning:
Apple's HomeKit
I am currently working on similar project - controlling a radio transceiver.
I am basically replacing "wired control" with wireless - bluetooth.
The key is - the controlled device - "the box" - has to be enabled to allow such control. In my case the control is limited by design. I can "set the frequency", but I cannot "key the transmitter" .
You could call that "device security".
AT present the box is expecting plain serial communication commands , pretty much "modem style". These commands could be encoded to provide for additional bluetooth security .
Is that what you are referring to as "security"?'' It would probably be interesting to code , but given limited range of bluetooth and me controlling only single bluetooth device , I would consider that little of overkill .BUT - be aware that Qt / Linux implementation of bluetooth has a MAJOR issue - there is no option to scan / inquiry for physically present devices only.
There is a database of past devices and I have not found Qt/Linux way to delete non-existent devices from such database. .
I had to go back to HCI to clear this database. -
Thank you both for the replies.
@SGaist Yes I'm working on something similar.@AnneRanch as security I mean something like "you cannot see information of the devices or a devices list on an external app because Bluetooth doesn't allow thing like this"
I was thinking something like this:
- The app scan for devices to connect and I can choose the box (easy part, lot of examples in Qt);
- Once connected, the box send to the app a list of all the actually connected devices which will be displayed in the app;
- In the app I can select a device and a new window will appears with the buttons "on" and "off";
- Once I select one of the options, the app will send to the box a signal "device x, command y";
- The box reads the command and send it to the device that will perform the designated action
-
Thanks for the clear picture.
Can you explain what you were thinking to do with regards to the use of signals ?
It seems that you have a pretty well defined API so I think you'll have a controller that will be responsible for the communication with the "hub" device. Is it so ?
-
@AndreaFurlani said in App for bluetooth scanning:
The app scan for devices to connect and I can choose the box (easy part, lot of examples in Qt);
I am not sure what you call "the box" .
I do not want to be pedantic - but IMHO "home brew " terminology can became unnecessary issue in the long run.
During my , still ongoing , development I found that it is helpful to call "local (bluetooth) device " the one connected to your controller and "remote (bluetooth) device ".
I have found that "client /server" is little misleading when "two way communication" is involved , so does "home ".Again - not that important when it is all "under one roof " .
I am not sure that monitoring the bluetooth "raw data" is best way to limit the function of the "remote device". But I came from "telecommunication background " and I always view the communication payload as "transparent ". In my case - I cannot "key-up" the remote transmitter device so I won't even attempt to send anything close to such command.
-
Thanks for the clear picture.
Can you explain what you were thinking to do with regards to the use of signals ?
It seems that you have a pretty well defined API so I think you'll have a controller that will be responsible for the communication with the "hub" device. Is it so ?
@SGaist Since I'm quite new in Qt and specially in bluetooth, I have no idea on how my app can ask to the box for the list of devices, so my idea was like:
1- the app send a request signal to the box
2- the box answer back by sending a list containing all the devices connected
3- once the user has selected what to do with a device, the app send back a variable containing the device and the action to perform -
@SGaist Since I'm quite new in Qt and specially in bluetooth, I have no idea on how my app can ask to the box for the list of devices, so my idea was like:
1- the app send a request signal to the box
2- the box answer back by sending a list containing all the devices connected
3- once the user has selected what to do with a device, the app send back a variable containing the device and the action to perform@AndreaFurlani said in App for bluetooth scanning:
@SGaist Since I'm quite new in Qt and specially in bluetooth, I have no idea on how my app can ask to the box for the list of devices, so my idea was like:
1- the app send a request signal to the box
2- the box answer back by sending a list containing all the devices connected
3- once the user has selected what to do with a device, the app send back a variable containing the device and the action to performOK, I am still missing something here .
Again , from my background :
in general - "communication" has three basic parts and for simplicity it is defined as communication in one direction only.sender -> communication media ->receiver
or
source communication media sink / loadWhere is your "box" in this scheme ? Does look like this ?
sender communication media (bluetooth) "receiver BOX sender " communication media ( actual media ?) receiver
where "BOX sender" is actually multiple senders ?
-
Your box needs to have a protocol established.
See the exchanging data between devices part of the Bluetooth chapter in Qt's documentation.
You'll then have to establish a clear protocol for your "service".
-
Your box needs to have a protocol established.
See the exchanging data between devices part of the Bluetooth chapter in Qt's documentation.
You'll then have to establish a clear protocol for your "service".
@SGaist said in App for bluetooth scanning:
Your box needs to have a protocol established.
See the exchanging data between devices part of the Bluetooth chapter in Qt's documentation.
You'll then have to establish a clear protocol for your "service".
Very helpful post .
Since I started by using btscanner example I was wondering why my "remote bluetooth device " is "identified as "SPP" . Now I have an answer.
After I get this "inquiry" mess cleaned - up my next step is indeed using "chat "... -
Your box needs to have a protocol established.
See the exchanging data between devices part of the Bluetooth chapter in Qt's documentation.
You'll then have to establish a clear protocol for your "service".
@SGaist Thank you very much. I'll try to read it and make some tries.
-
Hello again, I returned Here since I'm still stuck. In this moment I'm trying to create a new bluetooth service (in the box code), where I'll ask for creating an retrieve a list of all connected devices (and I still have not idea on how to do it, I saw this in the documentation, maybe could be useful).
So in this way I can create another service (on the app code) where once retrieved the list will associate to a device a command (and again I still have not idea on how to do it) and send it back to the box to execute it.
Any idea on how I can do it?
Thank you again for all the help. -
Something is not clear, what will run on that box ?
-
Hello again, I returned Here since I'm still stuck. In this moment I'm trying to create a new bluetooth service (in the box code), where I'll ask for creating an retrieve a list of all connected devices (and I still have not idea on how to do it, I saw this in the documentation, maybe could be useful).
So in this way I can create another service (on the app code) where once retrieved the list will associate to a device a command (and again I still have not idea on how to do it) and send it back to the box to execute it.
Any idea on how I can do it?
Thank you again for all the help.@AndreaFurlani I would suggest to take a look at examples. Start with "qtconnectivity" - btscanner.
It does the basics - scans for nearby bluetooth devices etc.
However it is scarcely documented - in code - and assumes some knowledge about bluetooth.
From my experience - do not assume that your "local bluetooth device " is enabled to be used to scan for "remote bluetooth devices".
I have two with different dongles with different "class of service" and the scan results are NOT the same.Same for "remote bluetooth device" - my powered "remote device " with serial USB to bluetooth adapter" can be "found" , my powered remote "boom box" has to be activated to be found.
Beware of running active operating system "bluetooth manager" software - it actually over-rides , takes precedence, of SOME of your code. -
@SGaist The box is connected to some lights and sensors via zigbee or bluetooth. I need a list of all the connected devices to the box
-
@AndreaFurlani I would suggest to take a look at examples. Start with "qtconnectivity" - btscanner.
It does the basics - scans for nearby bluetooth devices etc.
However it is scarcely documented - in code - and assumes some knowledge about bluetooth.
From my experience - do not assume that your "local bluetooth device " is enabled to be used to scan for "remote bluetooth devices".
I have two with different dongles with different "class of service" and the scan results are NOT the same.Same for "remote bluetooth device" - my powered "remote device " with serial USB to bluetooth adapter" can be "found" , my powered remote "boom box" has to be activated to be found.
Beware of running active operating system "bluetooth manager" software - it actually over-rides , takes precedence, of SOME of your code.@AnneRanch thank you for the answer. I already watched all the bluetooth examples in Qt creator but no one of them is useful to understand how to create a new service and in particular how to do the things I need.
-
@AnneRanch thank you for the answer. I already watched all the bluetooth examples in Qt creator but no one of them is useful to understand how to create a new service and in particular how to do the things I need.
@AndreaFurlani said in App for bluetooth scanning:
how to create a new service
Since bluetooth devices have "class of service" be careful using the term.
I am still unclear about your overall task . I think I asked for simple hardware picture before .
Let me make analogy with my project
local bt device (PC) -> bluetooth media communication path -> remote bt device (radio )one on my "services" would be - set the radio frequency
so "my box - radio " would turn on correct frequency and display it on build-in LCD
Are you attempting to do similar tasks via your box ?
-
@SGaist The box is connected to some lights and sensors via zigbee or bluetooth. I need a list of all the connected devices to the box
@AndreaFurlani said in App for bluetooth scanning:
@SGaist The box is connected to some lights and sensors via zigbee or bluetooth. I need a list of all the connected devices to the box
Hence my question what software runs on that box ?
-
Hello I'm back again. I made some progress in writing my code. Now the box starts the advertising without problem.
Here's the code:void bluetoothDevice::start(bool debugMode, bool protocolDebug) { //Personalized Service for connected bluetooth devices list static const QLatin1String serviceUuid("11223344-5566-7788-99aa-bbccddeeff00"); //static const QLatin1String writeListUuid("11223344-5566-7788-99aa-bbccddeeff11"); static const QLatin1String readListUuid("11223344-5566-7788-99aa-bbccddeeff11"); // [Advertising Data] QLowEnergyAdvertisingData advertisingData; advertisingData.setDiscoverability(QLowEnergyAdvertisingData::DiscoverabilityGeneral); advertisingData.setIncludePowerLevel(true); advertisingData.setLocalName("BOX"); advertisingData.setServices(QList<QBluetoothUuid>() << QBluetoothUuid(serviceUuid)); // [Service Data] QLowEnergyCharacteristicData readList; readList.setUuid(QBluetoothUuid(readListUuid)); readList.setProperties(QLowEnergyCharacteristic::Read); const QLowEnergyDescriptorData clientConfig(QBluetoothUuid::CharacteristicUserDescription,QByteArray(2, 0)); readList.addDescriptor(clientConfig); //writeList.setUuid(QBluetoothUuid(writeListUuid)); //writeList.setProperties(QLowEnergyCharacteristic::Write); QLowEnergyServiceData serviceData; serviceData.setType(QLowEnergyServiceData::ServiceTypePrimary); serviceData.setUuid(QBluetoothUuid(serviceUuid)); serviceData.addCharacteristic(readList); //serviceData.addCharacteristic(writeList); // [Start Advertising] leController.reset(QLowEnergyController::createPeripheral()); service_list.reset(leController->addService(serviceData)); leController->startAdvertising(QLowEnergyAdvertisingParameters(), advertisingData,advertisingData); qCDebug(BLELog) << " Start BLE backend [...] "; ConnectionHandler connectionHandler; DeviceHandler m_deviceHandler; // TODO! Restore debug options. GenericDeviceIf::start(debugMode, protocolDebug); // Initialize reachable state to unreachable. DataModel::getInstance()->setDeviceCategoryReachable(_category, false); _endpoints = DataModel::getInstance() ->getEndpointsListByCategory(dataBaseClasses::ENDPOINT_CATEGORY_BLE); m_deviceHandler.setDevice(nullptr); qDeleteAll(devices); devices.clear(); emit devicesChanged(); qCDebug(BLELog) << "Scanning for devices..."; discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod); emit scanningChanged(); // [Retrieving the list of devices] characteristic = service_list->characteristic(QBluetoothUuid(readListUuid)); for (const auto &e: _endpoints){ L.append(QString(e->getMapping()).toUtf8()); } if (L.empty()) qCDebug(BLELog) << "No endpoints loaded in the list"; for (const auto &list: L) { service_list -> writeCharacteristic(characteristic, list) ; } }
In this way I am able to connect to the BOX using an external BLE scanner app on my phone and once connected I would like to see the content of "list".
With this code the problem is that I can see only the last MAC address in the list, how can I see the complete content?