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

This commit is contained in:
Graham Jones
2024-04-11 20:00:13 +01:00
parent c23176ca8e
commit b0ce331731
4 changed files with 64 additions and 37 deletions

View File

@@ -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;

View File

@@ -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,7 +496,8 @@ public class SdDataSourceBLE2 extends SdDataSource {
private void bleDisconnect() {
Log.i(TAG,"bleDisconnect() - Unregistering notifications");
try {
Log.i(TAG, "bleDisconnect() - Unregistering notifications");
if (mBlePeripheral != null) {
if (mOsdChar != null) {
Log.i(TAG, "bleDisconnect() - unregistering mOsdChar");
@@ -512,8 +521,15 @@ public class SdDataSourceBLE2 extends SdDataSource {
Log.w(TAG, "bleDisconnect() - mBlePeripheral is null - not removing notifications");
}
Log.i(TAG,"bleDisconnect() - closing BluetoothCentralManager");
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();
try {
bleDisconnect();
CurrentTimeService.stopServer();
} catch (Exception e) {
Log.e(TAG,"stop() - Error stopping data source: "+e.getMessage());
}
}
private short[] parseDataToAccVals(byte[] rawDataBytes) {

View File

@@ -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");

View File

@@ -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" />
<item
android:id="@+id/action_exit"
android:icon="@drawable/stop_server"