From b0ce3317313dea6ab011bc5d07a463283ee3640e Mon Sep 17 00:00:00 2001 From: Graham Jones Date: Thu, 11 Apr 2024 20:00:13 +0100 Subject: [PATCH] Added some try/catch blocks in SdDataSourceBLE2 to avoid crashes during shutdown. Re-introduced a data source re-start when we do a fault beep to try to rectify the problem --- .../openseizuredetector/MainActivity2.java | 7 +- .../openseizuredetector/SdDataSourceBLE2.java | 76 ++++++++++++------- .../uk/org/openseizuredetector/SdServer.java | 15 ++-- .../main/res/menu/main_activity_actions.xml | 3 +- 4 files changed, 64 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/uk/org/openseizuredetector/MainActivity2.java b/app/src/main/java/uk/org/openseizuredetector/MainActivity2.java index a38f523..bdc4576 100644 --- a/app/src/main/java/uk/org/openseizuredetector/MainActivity2.java +++ b/app/src/main/java/uk/org/openseizuredetector/MainActivity2.java @@ -205,14 +205,17 @@ public class MainActivity2 extends AppCompatActivity { finish(); return true; case R.id.action_start_stop: + // FIXME: We need to unbind the fragments from the service, or else unbindFromServer does not work! + // Disabled this menu option until I work out how to fix it! Log.i(TAG, "action_start_stop: restarting server"); + mUtil.unbindFromServer(this, mConnection ); mUtil.showToast("Stopping Background Service...."); mUtil.stopServer(); // Wait 1 second to give the server chance to shutdown, then re-start it mHandler.postDelayed(new Runnable() { public void run() { - mUtil.showToast("Re-Starting Background Service..."); - mUtil.startServer(); + mUtil.showToast("NOT Re-Starting Background Service..."); + //mUtil.startServer(); } }, 1000); return true; diff --git a/app/src/main/java/uk/org/openseizuredetector/SdDataSourceBLE2.java b/app/src/main/java/uk/org/openseizuredetector/SdDataSourceBLE2.java index a3ee497..519e8a9 100644 --- a/app/src/main/java/uk/org/openseizuredetector/SdDataSourceBLE2.java +++ b/app/src/main/java/uk/org/openseizuredetector/SdDataSourceBLE2.java @@ -124,7 +124,7 @@ public class SdDataSourceBLE2 extends SdDataSource { BluetoothGattCharacteristic mHrChar; BluetoothGattCharacteristic mBattChar; private BluetoothCentralManager mBluetoothCentralManager; - + private boolean mShutdown = false; public SdDataSourceBLE2(Context context, Handler handler, SdDataReceiver sdDataReceiver) { @@ -171,6 +171,7 @@ public class SdDataSourceBLE2 extends SdDataSource { ); // Look for the specified device Log.i(TAG,"bleConnect() - scanning for device: "+mBleDeviceAddr); + mShutdown = false; mBluetoothCentralManager.scanForPeripheralsWithAddresses(new String[]{mBleDeviceAddr}); } @@ -195,8 +196,15 @@ public class SdDataSourceBLE2 extends SdDataSource { } @Override public void onDisconnectedPeripheral(BluetoothPeripheral peripheral, HciStatus status) { - Log.i(TAG,"BluetoothCentralManagerCallback.onDisonnectedPeripheral - attempting to re-connect..."); - mBluetoothCentralManager.autoConnectPeripheral(peripheral, peripheralCallback); + if (mShutdown) { + Log.i(TAG,"BluetoothCentralManagerCallback.onDisonnectedPeripheral - mShutdown is set, so not reconnecting"); + } else { + Log.i(TAG,"BluetoothCentralManagerCallback.onDisonnectedPeripheral"); + //Log.i(TAG, "BluetoothCentralManagerCallback.onDisonnectedPeripheral - attempting to re-connect..."); + //bleDisconnect(); + //mShutdown=false; + //mBluetoothCentralManager.autoConnectPeripheral(peripheral, peripheralCallback); + } super.onDisconnectedPeripheral(peripheral, status); } @@ -488,32 +496,40 @@ public class SdDataSourceBLE2 extends SdDataSource { private void bleDisconnect() { - Log.i(TAG,"bleDisconnect() - Unregistering notifications"); - if (mBlePeripheral != null) { - if (mOsdChar != null) { - Log.i(TAG, "bleDisconnect() - unregistering mOsdChar"); - mBlePeripheral.setNotify(mOsdChar, false); + try { + Log.i(TAG, "bleDisconnect() - Unregistering notifications"); + if (mBlePeripheral != null) { + if (mOsdChar != null) { + Log.i(TAG, "bleDisconnect() - unregistering mOsdChar"); + mBlePeripheral.setNotify(mOsdChar, false); + } else { + Log.w(TAG, "bleDisconnect() - mOsdChar is null - not removing notification"); + } + if (mHrChar != null) { + Log.i(TAG, "bleDisconnect() - unregistering mHrChar"); + mBlePeripheral.setNotify(mHrChar, false); + } else { + Log.w(TAG, "bleDisconnect() - mHrChar is null - not removing notification"); + } + if (mBattChar != null) { + Log.i(TAG, "bleDisconnect() - unregistering mBattChar"); + mBlePeripheral.setNotify(mBattChar, false); + } else { + Log.w(TAG, "bleDisconnect() - mBattChar is null - not removing notification"); + } } else { - Log.w(TAG, "bleDisconnect() - mOsdChar is null - not removing notification"); + Log.w(TAG, "bleDisconnect() - mBlePeripheral is null - not removing notifications"); } - if (mHrChar != null) { - Log.i(TAG, "bleDisconnect() - unregistering mHrChar"); - mBlePeripheral.setNotify(mHrChar, false); - } else { - Log.w(TAG, "bleDisconnect() - mHrChar is null - not removing notification"); - } - if (mBattChar != null) { - Log.i(TAG, "bleDisconnect() - unregistering mBattChar"); - mBlePeripheral.setNotify(mBattChar, false); - } else { - Log.w(TAG, "bleDisconnect() - mBattChar is null - not removing notification"); - } - } else { - Log.w(TAG, "bleDisconnect() - mBlePeripheral is null - not removing notifications"); - } - Log.i(TAG,"bleDisconnect() - closing BluetoothCentralManager"); - mBluetoothCentralManager.close(); + mShutdown = true; + mBlePeripheral.cancelConnection(); + + Log.i(TAG, "bleDisconnect() - closing BluetoothCentralManager"); + mBluetoothCentralManager.close(); + } catch (Exception e) { + Log.e(TAG,"bleDisconnect() - Error: "+e.getMessage()); + mUtil.showToast("Error disconnecting from watch"); + } } /** @@ -524,8 +540,12 @@ public class SdDataSourceBLE2 extends SdDataSource { mUtil.writeToSysLogFile("SDDataSourceBLE.stop()"); super.stop(); - bleDisconnect(); - CurrentTimeService.stopServer(); + try { + bleDisconnect(); + CurrentTimeService.stopServer(); + } catch (Exception e) { + Log.e(TAG,"stop() - Error stopping data source: "+e.getMessage()); + } } private short[] parseDataToAccVals(byte[] rawDataBytes) { diff --git a/app/src/main/java/uk/org/openseizuredetector/SdServer.java b/app/src/main/java/uk/org/openseizuredetector/SdServer.java index 3d2f82f..57131d4 100644 --- a/app/src/main/java/uk/org/openseizuredetector/SdServer.java +++ b/app/src/main/java/uk/org/openseizuredetector/SdServer.java @@ -821,12 +821,15 @@ public class SdServer extends Service implements SdDataReceiver { // flag. if (mFaultTimerCompleted) { faultWarningBeep(); - //mSdDataSource.stop(); - //mHandler.postDelayed(new Runnable() { - // public void run() { - // mSdDataSource.start(); - // } - //}, 190); + // Re-start the data source to see if that fixes it + Log.w(TAG,"FAULT - stopping data source"); + mSdDataSource.stop(); + mHandler.postDelayed(new Runnable() { + public void run() { + Log.w(TAG,"FAULT - restarting data source"); + mSdDataSource.start(); + } + }, 2000); } else { startFaultTimer(); Log.v(TAG, "onSdDataFault() - starting Fault Timer"); diff --git a/app/src/main/res/menu/main_activity_actions.xml b/app/src/main/res/menu/main_activity_actions.xml index 3637529..8d443a7 100644 --- a/app/src/main/res/menu/main_activity_actions.xml +++ b/app/src/main/res/menu/main_activity_actions.xml @@ -12,7 +12,8 @@ android:id="@+id/action_start_stop" android:icon="@drawable/stop_server" app:showAsAction="never|withText" - android:title="@string/restart_server" /> + android:title="@string/restart_server" + android:enabled="false" />