From 7fb0d3f26f538d961e54467e1f8d01a76f710dc1 Mon Sep 17 00:00:00 2001 From: James Woglom Date: Wed, 3 May 2023 01:41:15 -0400 Subject: [PATCH] TandemBluetoothHandler: automatically unbond when unable to progress past initialPumpConnection This is an attempt to work around what is presumed to be an Android bug with bonded peripherals which results in the phone never receiving a reply after an initial connection is established and the first auth packet is sent to the pump. Manually unbonding the peripheral in Bluetooth settings seems to resolve this problem when it occurs. --- .../pump/bluetooth/TandemBluetoothHandler.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/androidLib/src/main/java/com/jwoglom/pumpx2/pump/bluetooth/TandemBluetoothHandler.java b/androidLib/src/main/java/com/jwoglom/pumpx2/pump/bluetooth/TandemBluetoothHandler.java index e903aa9..f50749c 100644 --- a/androidLib/src/main/java/com/jwoglom/pumpx2/pump/bluetooth/TandemBluetoothHandler.java +++ b/androidLib/src/main/java/com/jwoglom/pumpx2/pump/bluetooth/TandemBluetoothHandler.java @@ -228,6 +228,19 @@ private synchronized void checkIfInitialPumpConnectionEstablished(BluetoothPerip } tandemPump.onInitialPumpConnection(peripheral); + + handler.postDelayed(() -> { + if (remainingConnectionInitializationSteps.contains(ConnectionInitializationStep.ALREADY_INITIALIZED)) { + int requestsSent = Packetize.txId.get(); + int repliesReceived = PumpState.processedResponseMessages; + Timber.i("InitialPumpConnectionChecker: requestsSent=%d repliesReceived=%d", requestsSent, repliesReceived); + if (requestsSent > 0 && repliesReceived == 0) { + Timber.i("InitialPumpConnectionStuck: not getting pump replies. Disconnecting and unbonding: bondState=%s", peripheral.getBondState()); + peripheral.cancelConnection(); + central.removeBond(peripheral.getAddress()); + } + } + }, 5000); } else if (!remainingConnectionInitializationSteps.contains(ConnectionInitializationStep.ALREADY_INITIALIZED)) { Timber.i("TandemBluetoothHandler: initial pump connection is waiting for: %s", remainingConnectionInitializationSteps); }