Solved Bluetooth Low Energy Python
-
Hi there,
I can only find C++ examples for Bluetooth Low Energy on Windows.
It is not easy to convert the C++ code into Python.
Are there any BLE examples for PyQt? ( scan, connect, notify, write....etc. ) Basic methods needed.Or at least... could somebody tell the pythonic version of the first sample - Scan Devices here:
https://doc.qt.io/qt-5/qtbluetooth-lowenergyscanner-example.html -
@sonus89 said in Bluetooth Low Energy Python:
QBluetoothDeviceDiscoveryAgent
Here is the documentation for that specific object
https://doc.qt.io/qt-5/qbluetoothdevicediscoveryagent.htmlfrom PyQt5.QtCore import QBluetoothDeviceDiscoveryAgent self.DiscvryAgent = QBluetoothDeviceDiscoveryAgent() self.DiscvryAgent.setLowEnergyDiscoveryTimeout(5000) self.DiscvryAgent.deviceDiscovered.connect(self.AddDevice) self.DiscvryAgent.start() @pyqtSlot(object) def AddDevice(self, BlueInfo): print('Got Bluetooth Information') Note: QBluetoothDeviceInfo is what BlueInfo would be -- which can also be found here >> https://doc.qt.io/qt-5/classes.html
-
Well first you would most likely need to find out if QBluetoothDeviceDiscoveryAgent class is available in PyQt5 and/or PySide2 however I would not hold my breath on that second one, but maybe it is. There are elements (if I understood their statements) of Qt5 that have not been ported to PyQt5 (which is the most complete of the 2 options) due to Qt not sharing those items openly but that could have been old news or new news or misunderstood/misquoted news -- not sure.
Basically if you can import QBluetoothDeviceDiscoveryAgent into PyQt5 I would say your golden -- you may have to pip install something first -- however you could just as easily perform all that stuff in straight python the only real thing IMO that PyQt5 can do (or should be used for) is rendering the GUI interface -- I do all the back end stuff in straight Python, C++, or C --- like talk to SQLite3, communicate with a Serial Port and the Serical Device attached to it and numerous other non-GUI related things -- and there are quite a few python libraries out there for various things
-
@Denni-0 said in Bluetooth Low Energy Python:
There are elements (if I understood their statements) of Qt5 that have not been ported to PyQt5 (which is the most complete of the 2 options) due to Qt not sharing those items openly but that could have been old news or new news or misunderstood/misquoted news -- not sure.
What statements are you talking about ?
The QtConnectivity module which provides Bluetooth support is available: https://code.qt.io/cgit/qt/qtconnectivity.git -
@Denni-0 Everything works fine in PyQt5. I could import the modules and I am half way done with converting C++ sample code into Python. However I am stuck at the "connect" method... I was curious if these codelines already exist somewhere in Python.
So the question is not "if it works in Python" the question was "how to convert the C++ example into Python".
-
C++
connect(fooWidget, &MyWidget::mySignal, barWidget, &HisWidget::mySlot);
Python;
fooWidget.mySignal.connect(barWidget.mySlot)
-
@SGaist Thanks!
This means that in C++:
discoveryAgent = new QBluetoothDeviceDiscoveryAgent();
discoveryAgent->setLowEnergyDiscoveryTimeout(5000);
connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered,
this, &Device::addDevice);
connect(discoveryAgent, QOverloadQBluetoothDeviceDiscoveryAgent::Error::of(&QBluetoothDeviceDiscoveryAgent::error),
this, &Device::deviceScanError);
connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &Device::deviceScanFinished);
discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);In Python:
discoveryAgent = QtBluetooth.QBluetoothDeviceDiscoveryAgent()
discoveryAgent.setLowEnergyDiscoveryTimeout(5000)
discoveryAgent.deviceDiscovered.connect(addDevice)
discoveryAgent.start()Can you confirm?
-
@sonus89
That's more or less right, I don't know how exact you want the answer to be. You're not passing the argument todiscoveryAgent.start()
, if that matters. You've omitted some of theconnect
s. Theconnect
you show would bediscoveryAgent.deviceDiscovered.connect(self.addDevice)
-
@sonus89 said in Bluetooth Low Energy Python:
QBluetoothDeviceDiscoveryAgent
Here is the documentation for that specific object
https://doc.qt.io/qt-5/qbluetoothdevicediscoveryagent.htmlfrom PyQt5.QtCore import QBluetoothDeviceDiscoveryAgent self.DiscvryAgent = QBluetoothDeviceDiscoveryAgent() self.DiscvryAgent.setLowEnergyDiscoveryTimeout(5000) self.DiscvryAgent.deviceDiscovered.connect(self.AddDevice) self.DiscvryAgent.start() @pyqtSlot(object) def AddDevice(self, BlueInfo): print('Got Bluetooth Information') Note: QBluetoothDeviceInfo is what BlueInfo would be -- which can also be found here >> https://doc.qt.io/qt-5/classes.html
-
@Denni-0 Thanks!
self.DiscvryAgent.start()
This needs an input parameter but I can not find it in python.
For example LowEnergyMethod