-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gatt must be closed on disconnect. If not, connectGatt won't reconnect. #33
Conversation
Thanks @robgar2001 |
I like the idea to simplify the reconnection management, For example: from kivy.app import App
from kivy.clock import Clock
from kivy.logger import Logger
from kivy.uix.widget import Widget
from able import BluetoothDispatcher, GATT_SUCCESS
class BLESender(BluetoothDispatcher):
def __init__(self):
super().__init__()
self._connected = False
Clock.schedule_once(self.restart_scan_if_needed, 0)
def on_device(self, device, rssi, advertisement):
if device.getName() == "KivyBLETest":
self.device = device
self.stop_scan()
def on_scan_completed(self):
if self.device:
self.connect_gatt(self.device)
def on_connection_state_change(self, status, state):
Logger.info("Connection state changed <%d> <%d>", status, state)
if state and status == GATT_SUCCESS:
self._connected = True
else:
self._connected = False
Logging.info("Initiate reconnection")
self.gatt.connect()
# After 20 seconds check if reconnection was successful
Clock.schedule_once(self.restart_scan_if_needed, 20)
def restart_scan_if_needed(self, *args, **kwargs):
if not self._connected:
Logger.info("Start scan")
self.device = None
self.close_gatt() # Close the connection,
self.start_scan() # and start scan again
class ExampleApp(App):
def build(self):
BLESender()
return Widget()
if __name__ == '__main__':
ExampleApp().run() @robgar2001 what do you think, maybe this approach can be used in your application? |
…ot connect." This reverts commit b379f37.
HI @b3b Thanks for replying! Calling BluetoothGatt.connect() or closeGatt() in the disconnected state still feels a bit odd. Maybe a slight logging improvement to inform people would do wonders? |
@robgar2001 great, it can help users to debug if issue is happen. |
ABLE was not able to reconnect when walking out of range.
This was due to the connectGatt method. After walking out of range, the mBluetoothGatt object was not equal to null. Thereby blocking the ability to reconnect. When walking of out range, the onConnectionStateChanged method is called. In my case with state disconnected en status 8. To fix the connectGatt problem, it is sufficient to close the gatt connection when onConnectionStateChanged is called with state argument STATE_DISCONNECTED.