Short question: Is it possible to scan for LE devices while periodically connecting to some and reading characteristics a) in general [does the protocol allow it] and b) with the BlueZ stack?
Long question: I'm writing a Python script to scan for LE devices advertising a certain UUID. If one is found, we should connect, read a specific characteristic value (call this ValueA
), and disconnect. The idea is to maintain a real-time list of what ValueA
's are present. I want to be able to spawn a thread that starts the LE scans and collects the advertising events into a thread-safe container. When a new device is discovered, a handler should be fired to read ValueA
.
My efforts using PyBluez and BluePy (to connect to peripherals) have failed. When I use terminal commands (sudo hcitool lescan --duplicates
and sudo gatttool -I
followed by the connect and read commands), these fail if I try to connect after I start scanning (scan aborts)
I am confused because I notice that if I am connected to a device via gatttool
prior to starting an LE scan, I can kick off the scan and continue to read ValueA
without impacting the scan, but not vice versa.
Any insights or good BlueZ programming documentation would be appreciated. Using BlueZ 4.1 on Ubuntu 14.10 Macbook Pro
I haven't checked recently, but I don't think PyBluez can do BLE.
Your issue with trying to connect while scanning is a known issue that happens with some hardware. It's an issue specific to certain hardware. Essentially you need to stop scanning, make your L2CAP/GATT connection, and then restart scanning... or buy a different dongle that doesn't have that issue.
All the work I've done with BLE in Python has been to mimic what hcitool
and gatttool
does in Python. It's unfortunate, but there's no library (that I know of) that does BLE yet. Here's a code snippet that may help you to get started: Finding Bluetooth low energy with python
Alternatively, you could interact with Bluez using the DBUS interface it provides. I've been told that you can't interact with attributes that aren't part of a Bluetooth profile, though. (So, you can interact with a BLE device that uses the heart-rate profile, but not read a value from an accelerometer)
EDIT: Seems there is a library for GATT now: https://pypi.python.org/pypi/gattlib
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With