getsenic / gatt-python

Bluetooth GATT SDK for Python
MIT License
318 stars 86 forks source link

Cannot connect to peripherals #25

Open Snevzor opened 6 years ago

Snevzor commented 6 years ago

Using the connect.py example. With BlueZ 5.44 on a raspberry pi with Python 3.4.2 and a venv I get: "Connection failed: Device does not exist, check adapter name and MAC address"

With BlueZ 5.48 on my local PC with Python 3.5.2 but without a venv I get: "Connection failed: No such property 'ServicesResolved'"

On both systems I can execute the discovery.py example successfully and I can connect to the peripheral with gatttool.

Snevzor commented 6 years ago

On my local PC a reboot fixed it somehow. I did install a newer BlueZ version without rebooting. Still struggling with the venv though.

Snevzor commented 6 years ago

Discovery with gatt-python is mandatory to be able to connect to peripherals (as clearly mentioned in the documentation). Previously I scanned with "hcitool lescan --passive" but that resulted always in "Connection failed: Device does not exist, check adapter name and MAC address" with gatt-python. After performing discovery.py example I can connect. Tested on both local PC and a raspberry pi.

The strange thing is that I'm using the discovery.py and connect.py examples separately. So it appears that the discovered devices are stored somewhere on the system? After rebooting both devices it was still possible to connect to the peripheral so it also appears that the discovered data is persistent after reboot. Would someone be able to give me some insight on this? Especially why it does not work when using another scanner/discoverer.

Best regards

larsblumberg commented 6 years ago

Dear @Snevzor, a Bluetooth discovery of the device is necessary before you connect to the device. This is due to the fact that BlueZ (running as a service) stores discovered devices "in your system".

BlueZ's D-BUS API provides methods to add devices without previous discovery but this library doesn't implement such API endpoints yet. Hence you need to have your machine discover your Bluetooth peripheral first.

Snevzor commented 6 years ago

Dear @larsblumberg, thanks for confirming!

After installing bluez-tools I am able to view that internal list with 'bt-device -l'.

Maybe by explaining what I actually need someone can give me some pointers on which route to take :) We have some BLE peripherals with useful information in the advertisement data. To save battery life we don't want to use active scanning, but passive scanning. It also has to be fast (like 'hcitool lescan') because we are interested in the RSSI values for some basic location determination. I presume the discovery function of this library does active scanning only, so I stumbled upon https://github.com/frawau/aioblescan which can run safely in parallel. We then need gatt-python to do all the other work. But when scanning with aioblescan or hcitool BlueZ does not store the device and as a result gatt-python cannot connect.