title: Bleak integration in PyQT5
body: <ul>
  <li>bleak version: 0.14.2</li>
  <li>Python version: 3.8.1rc1</li>
  <li>Operating System: Windows 10 Enterprise LTBS</li>
  <li>BlueZ version (<code>bluetoothctl -v</code>) in case of Linux: None</li>
  </ul>
  <h3>Description</h3>
  <p>I'm coding a desktop app for Windows to manage intertial sensors. The app is been developed in PyQT5 building a GUI for connect to sensor by bleak and sending/receiving by uart. So, I want to integrate in a GUI in Pyqt the bleak library that uses asyncio.</p>
  <p>I can scan devices without problem. But when I want to connect to sensor and run infinite loop reading and writing bytes like <a href="https://github.com/hbldh/bleak/blob/develop/examples/uart_service.py" rel="nofollow noreferrer">https://github.com/hbldh/bleak/blob/develop/examples/uart_service.py</a></p>
  <p>the GUI is frozen and not responding. I have used asyncio routines in differente ways in order to run the process in backgroud with no success.</p>
  <p>Anybody has experience integrating bleak in Pyqt5/Pyside2.</p>
  <h3>What I Did</h3>
  <p>I have a main.py file with the UI and two simple modules for scan and connecting. I can connect it and receive data but after seconds GUI does not respond. I have seen some libraries like qtrio for this purposes but I want to know if Pyqt and bleak has a good integration.</p>
  <p>Thanks for your support.</p>
  <p>Here my starter code in the UI</p>
  <pre><code>
  import asyncio
  import qasync
  from PyQt5 import QtWidgets, uic
  from PyQt5.QtWidgets import QTextEdit, QPushButton, QListWidgetItem, QListWidget, QApplication
  from PyQt5 import QtBluetooth, QtCore
  from PyQt5.QtCore import QThread
  from PyQt5.QtBluetooth import QBluetoothAddress
  import sys
  from bleak import discover
  from ble import discover
  from ble import connect
  from qasync import QEventLoop
  
  #main.py
  class MainWindow(QtWidgets.QMainWindow):
      def __init__(self):
          super(MainWindow, self).__init__()
          uic.loadUi('mainwindow.ui', self)
          self.streamtext = self.findChild(QTextEdit, &quot;streamtext&quot;)
          self.listDevicesText = self.findChild(QListWidget, &quot;addressList&quot;)
          self.scanButton = self.findChild(QPushButton, &quot;scanButton&quot;)
          self.connectButton = self.findChild(QPushButton,&quot;connectButton&quot;)
          self.exitButton = self.findChild(QPushButton,&quot;exitButton&quot;)
          self.exitButton.clicked.connect(self.quit)
          self.scanButton.clicked.connect(self.discover_devices)
          self.connectButton.clicked.connect(self.connect_devices)
          self.show()
  
      def discover_devices(self):
          list = {}
          list = asyncio.run(discover.main(),debug=True)
          self.listDevicesText.addItem(list.get(&quot;Sensor1&quot;))
  
      def connect_devices(self):
          # Get the first line is macaddress
          asyncio.run(connect.run(self.listDevicesText.item(0).text()))
  
  
      def quit(self):
          sys.exit(0)
  
  if __name__ == &quot;__main__&quot;:
  
      try:
          app = QtWidgets.QApplication(sys.argv)
          window = MainWindow()
          app.exec()
      except Exception as e:
          print(e)
  
  #ble/connect.py
  async def run(macaddress):
     
      async with BleakClient(macaddress, disconnected_callback=handle_disconnect) as client:
          
          while True:
            # here is reading/writing gatt char process
  
  #ble/discover.py
  import asyncio
  from bleak import BleakScanner
  
  async def main():
      devicelist = {}
      devices = await BleakScanner.discover()
      for d in devices:
          devicelist.update({d.name:d.address})
      return devicelist
  
  
  </code></pre>