Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BlueZ/Pybluez - Bluetooth LE scan while connected to peripheral

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

like image 308
thegeebe Avatar asked Apr 03 '15 18:04

thegeebe


1 Answers

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

like image 82
Tim Tisdall Avatar answered Sep 24 '22 10:09

Tim Tisdall