3

I have found the correct Bluetooth address of the device I want to connect to. When I run the code below, it prints "Connecting to device..." but then hangs and never prints "Connected" or finishes running. No errors are thrown.

import asyncio
from bleak import BleakClient

address = "24:71:89:cc:09:05" # Replaced with actual bluetooth address

async def main(address):
    print("Connecting to device...")
    async with BleakClient(address) as client:
        print("Connected")

asyncio.run(main(address))

Is this issue related to my PC software/drivers? I am on a Lenovo laptop running Windows 11.

Or, do some devices not respond to simple "connection requests"? The device I am connecting to is a Tesla Model 3, and I know there is a particular set of data I should be sending to authenticate. If this is the case, how do I send data without connecting with a BleakClient in this way?

Update: I should mention that scanning / discovering devices works just fine via Bleak. And, I tried connecting to other devices via Bleak and the same issue occurred. Bluetooth through the Windows Settings app works fine though.

kbrin-1372
  • 135
  • 1
  • 8
  • 2
    You haven't said if you have already done "Bluetooth Pairing" of the PC and the car. I suspect that is needed before connection is allowed. You might also want to use a generic Bluetooth Low Energy scanning and exploration tool like [nRF Connect](https://www.nordicsemi.com/Products/Development-tools/nrf-connect-for-mobile) for testing. If you have the Chrome browser on your PC you can do testing with the URL `chrome://bluetooth-internals/#devices` – ukBaz Dec 01 '21 at 07:22
  • @ukBaz This was the issue! I paired using Microsoft's UWP BLE Sample, and then Bleak was able to connect. Do you know if there is a way to do the pairing directly in Bleak? – kbrin-1372 Dec 02 '21 at 14:48
  • Bleak doesn't appear to have a pairing agent in the released library. It looks like they might have the start of something in the `develop` branch. However, given that the pairing is a one-off setup activity, is it so important? Now you have the pairing between the two machines you should be good to connect with Bleak from now on. – ukBaz Dec 02 '21 at 15:29
  • this error is still unsolved, in the project they claim: still waiting for someone to capture bluetooth packets. – Falco Alexander Jan 29 '23 at 10:30

3 Answers3

1

I was also having issues using any bleak commands beyond discover(). I downgraded from 0.14.3 to 0.14.0 and that resolved my problem.

Key_coder
  • 534
  • 3
  • 27
Xyphota
  • 19
  • 2
0

When I looked up "python tesla bluetooth api" I found my way to this site, which documents the API which seems great for what you are doing. That lets you do lots of things like unlocking, opening trunk, etc.

It sounds like the main problem is that you can't connect at all in order to do stuff. The way that site is written glosses over that part, just saying "send it over".

I would ensure that you have the right address, you should use this scanning code from the Bleak doc. Also, make sure that you can use bluetooth normally, like by connecting to a wireless speaker.

Luke B
  • 2,075
  • 2
  • 18
  • 26
  • Thanks for the response! I am actually working on improving and contributing to that site at the moment :) Anyways, scanning does work just fine and I can see the vehicle when I use the scanning code you linked. Interestingly, any device hangs when I try to connect via bleak, not just the car. Bluetooth devices work fine when I connect to them through Windows Settings though. – kbrin-1372 Dec 01 '21 at 04:33
0

Sadly, this error is still unsolved in Bleak, in the project they claim: still waiting for someone to capture bluetooth packets.

Trying to connect, you get either an endless hang with no response or if the device is paired upfront: Connection error: [WinError -2147483629] The object has been closed

It seems the Library isn't very mature for windows: https://github.com/hbldh/bleak/issues?q=label%3A%22Backend%3A+WinRT%22+

Falco Alexander
  • 3,092
  • 2
  • 20
  • 39