IanHarvey / bluepy

Python interface to Bluetooth LE on Linux
Other
1.61k stars 490 forks source link

Connected device disconnected on scan #346

Open johnfwhitmore opened 5 years ago

johnfwhitmore commented 5 years ago

I'm connecting to a number of CC2650 SensorTag devices using a periodic scan for new devices. When I initiate a fresh scan for devices an already connected device shows up in the Scan's delegate handler. I'd have thought that since it's connected it would not be advertising and wouldn't show up in the scan.

That in itself is not a problem as I have a list of already connected MAC address so can ignore the callback for a currently connected device, but the device is being disconnected by the scan. I'll put together the minimum code required to reproduce this problem.

johnfwhitmore commented 5 years ago

I've created a python file to test this problem. I've only got a CC2650 LE Device so this could be a device/firmware issue rather then bluepy. If someone tested with another device it might clear that possibility.

$ sudo python3 scan.py

scan.py.zip

johnfwhitmore commented 5 years ago

After a forced break from this I'm back to it and still hitting issues, which may well be a problem with what I'm doing. I'm using CC2650 with the TI LED expansion board, which unfortunately seems to have been discontinued, along with the CC2650, and info seems to have been removed from the TI website as a result.

My current issue is still with the Scan for new devices. I'm currently flashing the LED on and off which means I'm sending BTLE commands to do that. If a write command happens during a periodic scan for devices, then the device you're trying to write to gets disconnected, (bad state).

I'm increasing the probability of that as I'm flashing the LEDs to conserve battery, instead of having them on all the time. However the LED write (on/off) is a reaction to asynchronous commands so at some point in time you're bound to get a write command at the same time as a scan.

So I guess that this is due to the fact that I have the scan in a different python thread. I don't think I can change that as the reaction is to an MQTT on_message callback and that's asynchronous.

Given all that I guess I have to consider complicating the bluepy to buffer commands if you're in the middle of a scan and once finished that scan, flush the buffer of commands, catching up with system status.

Would that be an acceptable change to the bluepy-helper?

sterwen commented 5 years ago

The bluepy-helper for scan is not the same process than the bluepy-helper for connecting the device. Avoid connecting devices while scanning