crownstone / crownstone-lib-python-ble

bluetooth module of the python library
Apache License 2.0
0 stars 2 forks source link

Failed connect() does not throw an error. #5

Open vliedel opened 3 years ago

vliedel commented 3 years ago

When calling connect(), it sometimes fails like:

Connecting...
Connected

Often followed by some other error:

Connecting...
Disconnect error
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/crownstone_ble-1.0.0-py3.7.egg/crownstone_ble/core/ble_modules/BleHandler.py", line 172, in getCharacteristics
  File "/usr/local/lib/python3.7/dist-packages/bluepy-1.3.0-py3.7.egg/bluepy/btle.py", line 507, in getServiceByUUID
    self._writeCmd("svcs %s\n" % uuid)
  File "/usr/local/lib/python3.7/dist-packages/bluepy-1.3.0-py3.7.egg/bluepy/btle.py", line 315, in _writeCmd
    raise BTLEInternalError("Helper not started (did you call connect()?)")
bluepy.btle.BTLEInternalError: Helper not started (did you call connect()?)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "example_setup_and_reset.py", line 54, in <module>
    ibeaconMinor=456
  File "/usr/local/lib/python3.7/dist-packages/crownstone_ble-1.0.0-py3.7.egg/crownstone_ble/core/CrownstoneBle.py", line 77, in setupCrownstone
  File "/usr/local/lib/python3.7/dist-packages/crownstone_ble-1.0.0-py3.7.egg/crownstone_ble/core/ble_modules/SetupHandler.py", line 20, in setup
  File "/usr/local/lib/python3.7/dist-packages/crownstone_ble-1.0.0-py3.7.egg/crownstone_ble/core/ble_modules/BleHandler.py", line 174, in getCharacteristics
crownstone_core.Exceptions.CrownstoneBleException: (<BleError.CAN_NOT_FIND_SERVICE: 'CAN_NOT_FIND_SERVICE'>, 'Can not find service: 24f10000-7d10-4805-bfc1-7663a01c3bff')

Here is a hackish way to see if the connect was successful: https://github.com/Kvanr23/CrownstoneTestSuite/blob/7ef388ecd7ab6ce0d58f13a6a4e4ae49e87f3677/Switching.py#L184

AlexDM0 commented 3 years ago

Has it really failed to connect in the first example or did it immediately lose connection afterwards.

I think many of these issues relate to just crappy bluetooth hardware. We know that this is highly dependent on the usage of supported BLE hardware, like an external BLE dongle. Most laptops or raspi native bluetooth chips are just unreliable to use together with bluepy.

Additionally, there might be a version mismatch with the version of bluez on de host and the one that's bundled with bluepy.

Before handling the issues in code, maybe first check that to see if the problem is at a lower level.

mrquincle commented 3 years ago

I would like to remark that most of our current hardware is newer than the software that comes with bluepy. If you look at https://github.com/IanHarvey/bluepy you see that there is a version of bluez-5.47 which is 3 years old.

Hence, regarding the possible source of bugs it is in this order:

I've seen bug reports where bluepy-helper became a zombie, however, I'm assuming here a simple bug. Just run with strace and you'll probably find a mismatch in reporting connection state or something like that.