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.

Bleak can't connect on ubuntu

See original GitHub issue
  • bleak version: 0.4.3
  • Python version: 3.7.3 & 3.6.8
  • Operating System: Xubuntu 18.04

  • bleak version: 0.4.3
  • Python version: 3.6.8
  • Operating System: Lubuntu 19.04

Description

Bleak randomly throw an exception during any connexion attempt on linux (almost every time):

me@my-computer~$ python3 test.py

Traceback (most recent call last):
  File "/home/me/.local/lib/python3.7/site-packages/bleak/backends/bluezdbus/client.py", line 98, in connect
    ).asFuture(self.loop)
txdbus.error.RemoteError: org.bluez.Error.Failed: Software caused connection abort

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 25, in <module>
    loop.run_until_complete(run(address, loop, True))
  File "/usr/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
    return future.result()
  File "test.py", line 18, in run
    async with BleakClient(address, loop=loop) as client:
  File "/home/me/.local/lib/python3.7/site-packages/bleak/backends/client.py", line 41, in __aenter__
    await self.connect()
  File "/home/me/.local/lib/python3.7/site-packages/bleak/backends/bluezdbus/client.py", line 100, in connect
    raise BleakError(str(e))
bleak.exc.BleakError: org.bluez.Error.Failed: Software caused connection abort

What I Did

Pasted this program in a file named test.py and then launched it.

import asyncio
from bleak import BleakClient


async def run(address, loop, debug=False):
    async with BleakClient(address, loop=loop) as client:
        x = await client.is_connected()
        print("Connected: {0}".format(x))

if __name__ == "__main__":
    address = "F8:F0:05:F4:C5:B4"
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run(address, loop))

But discover() works well though.

What I’m doing

I created a small benchmark, who launch X times this program and display success/total attempts :

import asyncio, bleak


async def run(address, loop, debug=False):
	try:
		async with bleak.BleakClient(address, loop=loop) as client:
			x = await client.is_connected(timeout=5)
			print(" V", end="", flush=True)
			return True
	except Exception as e:
		print(" X", end="", flush=True)
		return False

if __name__ == "__main__":
	success = 0
	address = "MAC ADDR"

	for i in range(0, 10000):
		loop = asyncio.get_event_loop()
		if loop.run_until_complete(run(address, loop)):
			success += 1
		print("\033[K\r" + str(success) + "/" + str(i + 1), sep=" ", end="", flush=True)
		# \033[K erase the line & \r return to the first char of the line 

Current status: image

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
corentinbettiolcommented, Jul 26, 2019

I’m on my personal computer right now (xubuntu 18.04 too), I have BlueZ 5.48 and here’s my adapter:

me@home$ lspci -knn | grep Net -A2; lsusb
02:00.0 Network controller [0280]: Intel Corporation Dual Band Wireless-AC 3168NGW [Stone Peak] [8086:24fb] (rev 10)
	Subsystem: Intel Corporation Device [8086:2110]
	Kernel driver in use: iwlwifi
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 1770:ff00  
Bus 001 Device 002: ID 1532:0037 Razer USA, Ltd 
Bus 001 Device 005: ID 0bda:0129 Realtek Semiconductor Corp. RTS5129 Card Reader Controller
Bus 001 Device 004: ID 8087:0aa7 Intel Corp. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

I’m able to discover some bluetooth devices, as well as using bleak: image
(using scan on from bluetoothctl)

image (using bleak on an app)

Starting search… Found 3 devices.


I updated BlueZ to 5.50, and it’s working fine too (using bluetoothctl and Bleak):

me@home$ bluetoothctl -v
bluetoothctl: 5.50

me@home$ bluetoothctl
Agent registered
[bluetooth]# scan on
Discovery started
[CHG] Controller F8:94:C2:6B:1C:6D Discovering: yes
[CHG] Device 1B:6F:4A:48:94:2D RSSI: -58
[CHG] Device 68:A3:78:2C:B8:68 RSSI: -78
[CHG] Device 90:DD:5D:9C:EA:C9 RSSI: -58
[NEW] Device 51:42:EE:FF:C6:75 51-42-EE-FF-C6-75
[bluetooth]# scan off
Discovery stopped
[CHG] Controller F8:94:C2:6B:1C:6D Discovering: no
[CHG] Device 51:42:EE:FF:C6:75 RSSI is nil
[CHG] Device 90:DD:5D:9C:EA:C9 RSSI is nil
[CHG] Device 68:A3:78:2C:B8:68 RSSI is nil
[CHG] Device 1B:6F:4A:48:94:2D RSSI is nil
[bluetooth]# exit
0reactions
hbldhcommented, Nov 2, 2019

I will close this issue and hope that the proposed solution above works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting — bleak 0.20.0a1 documentation
If you're having difficulty connecting to multiple devices, try to do a scan first and pass the returned BLEDevice objects to BleakClient calls....
Read more >
Bleak device cant be found on connect but on scan
Hello i need a little help, im running a raspberry 4 (Ubuntu) with a Logilink BT Stick. BlueZ is Version: 5.56-0ubuntu4.2.
Read more >
bleak 0.10.0 - PyPI
NET BleakClient connection. BleakClient in .NET backend will reuse previous device information when reconnecting so that it doesn't have to scan/discover again.
Read more >
Python Tutorial 1: Connect BLE : Open GoPro - GitHub Pages
The Bleak BLE controller does not currently support autonomous pairing for the BlueZ backend. So if you are using BlueZ (i.e. Ubuntu, ...
Read more >
How to Send Data between PC and Arduino using Bluetooth ...
A how-to guide on connecting your PC to an Arduino using Bluetooth LE and Python. To make it easier, we will use bleak...
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