Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Bluetooth Low Energy Python

    Qt for Python
    4
    9
    1566
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      sonus89 last edited by sonus89

      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

      1 Reply Last reply Reply Quote 0
      • Denni 0
        Denni 0 Banned last edited by Denni 0

        @sonus89 said in Bluetooth Low Energy Python:

        QBluetoothDeviceDiscoveryAgent

        Here is the documentation for that specific object
        https://doc.qt.io/qt-5/qbluetoothdevicediscoveryagent.html

        from 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
        
        S 1 Reply Last reply Reply Quote 0
        • Denni 0
          Denni 0 Banned last edited by

          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

          S 1 Reply Last reply Reply Quote 1
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            @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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 1
            • S
              sonus89 @Denni 0 last edited by

              @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".

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                C++

                connect(fooWidget, &MyWidget::mySignal, barWidget, &HisWidget::mySlot);
                

                Python;

                fooWidget.mySignal.connect(barWidget.mySlot)
                

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                S 1 Reply Last reply Reply Quote 1
                • S
                  sonus89 @SGaist last edited by

                  @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?

                  JonB 1 Reply Last reply Reply Quote 0
                  • JonB
                    JonB @sonus89 last edited by JonB

                    @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 to discoveryAgent.start(), if that matters. You've omitted some of the connects. The connect you show would be

                    discoveryAgent.deviceDiscovered.connect(self.addDevice)
                    
                    1 Reply Last reply Reply Quote 1
                    • Denni 0
                      Denni 0 Banned last edited by Denni 0

                      @sonus89 said in Bluetooth Low Energy Python:

                      QBluetoothDeviceDiscoveryAgent

                      Here is the documentation for that specific object
                      https://doc.qt.io/qt-5/qbluetoothdevicediscoveryagent.html

                      from 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
                      
                      S 1 Reply Last reply Reply Quote 0
                      • S
                        sonus89 @Denni 0 last edited by

                        @Denni-0 Thanks!

                        self.DiscvryAgent.start()

                        This needs an input parameter but I can not find it in python.
                        For example LowEnergyMethod

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post