IanHarvey / bluepy

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

Peripheral._connect() TypeError issue. #433

Closed LucDachary closed 3 years ago

LucDachary commented 3 years ago

Good morning,

I'm using the library to establish connections to peripherals. Trying to connect to random devices (as this point in my development any target does the job) I get the following error:

2021-01-07 21:40:41,803 [WARNI] Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/luc/custom_file.py", line 159, in ble_get_battery_level
    peripheral = self.ble_peripheral
  File "/home/luc/custom_file.py", line 377, in ble_peripheral
    self._ble_peripheral = bluepy.btle.Peripheral(
  File "/home/luc/.venv/lib/python3.8/site-packages/bluepy/btle.py", line 411, in __init__
    self._connect(deviceAddr, addrType, iface, timeout)
  File "/home/luc/.venv/lib/python3.8/site-packages/bluepy/btle.py", line 464, in _connect
    while rsp['state'][0] == 'tryconn':
TypeError: 'NoneType' object is not subscriptable

I have opened the source code and it seems that rsp becomes None on this line, on the first iteration.

while rsp['state'][0] == 'tryconn':
    logging.warning("Looping %r %r %r", rsp, rsp["state"], rsp["state"][0])
    rsp = self._getResp('stat', timeout)  # <-- this line
    logging.warning("After loop %r", rsp)

Here is the logged trace:

2021-01-07 21:54:15,439 [WARNI] Looping {'rsp': ['stat'], 'state': ['tryconn'], 'dst': ['7d:b9:db:64:5a:60'], 'mtu': [0], 'sec': ['low']} ['tryconn'] 'tryconn'
2021-01-07 21:54:18,439 [WARNI] After loop None

None being one of _getResp() method's return values I guess _connect() should expect it and step out of the loop gracefully.