question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

error when I combine bleak with gui

See original GitHub issue
  • bleak version: 0.7.1
  • Python version: 3.7.7
  • Operating System: MacOS Catalina 10.15.4

Description

Hi, I have a problem. I saw another issue about tkkinter. I’m trying to connect the bleak library with the graphics library. On windows everything works great, unfortunately again I have a problem with macOS. If it’s not the fault of the Bleak, I’m sorry.

What I Did

I send a code that scans and then connects and displays the notified data.

Very often ( 75% ) times when I connect I get a error:

File "main.py", line 251, in <module>
    loop.run_until_complete(run(device, loop))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/asyncqt/__init__.py", line 309, in run_until_complete
    return future.result()
  File "main.py", line 237, in run
    async with BleakClient(address, loop=loop) as client:
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/bleak/backends/client.py", line 49, in __aenter__
    await self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/bleak/backends/corebluetooth/client.py", line 83, in connect
    manager = self._device_info.manager().delegate()
AttributeError: 'NoneType' object has no attribute 'delegate'

import sys
import asyncio
import os

from PyQt5.QtWidgets import QApplication, QProgressBar, QFileDialog
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget, QPushButton, QListWidget, QListWidgetItem
from PyQt5.QtCore import QSize 
from asyncqt import QEventLoop, QThreadExecutor
from PyQt5.QtWidgets import QDialog

import bleak
from bleak import discover, BleakClient

class MainWindow(QDialog):
    def __init__(self):
        super(MainWindow, self).__init__(None)

        self.setMinimumSize(QSize(500, 500))    
        self.setWindowTitle("Bleak GUI")         
        
        self.lDeviceCapt = QLabel("Device:", self)
        self.lDeviceCapt.resize(50, 32)
        self.lDeviceCapt.move(50, 50)
        
        self.lDeviceInfo = QLabel("[xxx]", self)
        self.lDeviceInfo.resize(200, 32)
        self.lDeviceInfo.move(200, 50)
        
        self.lCurrMessage = QLabel("", self)
        self.lCurrMessage.resize(200, 32)
        self.lCurrMessage.move(200, 100)
        
        self.lMessages = QListWidget(self)
        self.lMessages.resize(200, 300)
        self.lMessages.move(200,150)
        self.started = False
        
    def notify(self, msg):
        self.lMessages.addItem(str(msg))

    def did_connect(self, address):
        self.lDeviceInfo.setText(str(address))
    
app = QApplication(sys.argv)
loop = QEventLoop(app)
asyncio.set_event_loop(loop)

app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()

devices_list = []

async def scan():
    devices = await discover()
    for d in devices:
        print(d)
        devices_list.append(d)

loop = asyncio.get_event_loop()
loop.run_until_complete(scan())
	
device = devices_list[0].address

def callback(sender, data):
    print(f"{sender}: {data}")
    mainWin.notify(data)

async def run(address, loop):

     async with BleakClient(address, loop=loop) as client:
        
        mainWin.did_connect(address)

        await client.start_notify(YOUR_UUID, callback)

        while True:
            await asyncio.sleep(5.0)
            if not await client.is_connected():
                print("Disconnect")
                break

with loop:
    loop.run_until_complete(run(device, run))

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dlechcommented, Aug 11, 2020

QBluetooth is basically the same as Bleak - it provides a common abstraction for cross-platform backends. You do not have to create your own backend. I’ve used it successfully with PyQt before. I didn’t think it was too difficult to translate the C++ examples to Python. Using QBluetooth seemed easier than trying to integrate asyncio into the Qt run loop.

0reactions
ivanjimenezcommented, Jul 7, 2022

QBluetooth is basically the same as Bleak - it provides a common abstraction for cross-platform backends. You do not have to create your own backend. I’ve used it successfully with PyQt before. I didn’t think it was too difficult to translate the C++ examples to Python. Using QBluetooth seemed easier than trying to integrate asyncio into the Qt run loop.

I’m trying to integrate bleak, pyqt5, asyncio, qasync. But I don’t think that qasync will be maintened in the future with support for Pyqt6 or above. PyQTBlueTooth is poor documented. I have scanned and connected a IMU sensor but I don’t know if you could publish some code using QtBluetooth for Python with examples with write_gatt_char, read_gatt_char, etc.

I apreciate your help. Thanks!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting — bleak 0.20.0a1 documentation
ImportError: cannot import name 'BleakClient' from partially initialized module 'bleak ' (most likely due to a circular import) (bleak.py)`. To fix the error ......
Read more >
Real time plotting of BLE data using Bleak and PyQtGraph ...
I am trying to plot sensor data in real time using an ESP32-based sensor and BLE. I've attempted to use Bleak and combine...
Read more >
2022.11: A heck of a release!
This works when editing an automation in the UI, and even when reloading ... Fix esphome bleak client seeing disconnects too late (@bdraco ......
Read more >
Nordic Q&A
Hi I am using SDK 12.3.0 with softdevice s130 v2. When I try to combine ble_app_hrs_peripheral code with app_uart.c, uart module gives error...
Read more >
Python program for word guessing game
user will win the game if failure is 0. # and 'You Win' will be given as output. print ( "You Win" )....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found