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(); finish();
return true; return true;
case R.id.action_start_stop: 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"); Log.i(TAG, "action_start_stop: restarting server");
mUtil.unbindFromServer(this, mConnection );
mUtil.showToast("Stopping Background Service...."); mUtil.showToast("Stopping Background Service....");
mUtil.stopServer(); mUtil.stopServer();
// Wait 1 second to give the server chance to shutdown, then re-start it // Wait 1 second to give the server chance to shutdown, then re-start it
mHandler.postDelayed(new Runnable() { mHandler.postDelayed(new Runnable() {
public void run() { public void run() {
mUtil.showToast("Re-Starting Background Service..."); mUtil.showToast("NOT Re-Starting Background Service...");
mUtil.startServer(); //mUtil.startServer();
} }
}, 1000); }, 1000);
return true; return true;

View File

@@ -124,7 +124,7 @@ public class SdDataSourceBLE2 extends SdDataSource {
BluetoothGattCharacteristic mHrChar; BluetoothGattCharacteristic mHrChar;
BluetoothGattCharacteristic mBattChar; BluetoothGattCharacteristic mBattChar;
private BluetoothCentralManager mBluetoothCentralManager; private BluetoothCentralManager mBluetoothCentralManager;
private boolean mShutdown = false;
public SdDataSourceBLE2(Context context, Handler handler, public SdDataSourceBLE2(Context context, Handler handler,
SdDataReceiver sdDataReceiver) { SdDataReceiver sdDataReceiver) {
@@ -171,6 +171,7 @@ public class SdDataSourceBLE2 extends SdDataSource {
); );
// Look for the specified device // Look for the specified device
Log.i(TAG,"bleConnect() - scanning for device: "+mBleDeviceAddr); Log.i(TAG,"bleConnect() - scanning for device: "+mBleDeviceAddr);
mShutdown = false;
mBluetoothCentralManager.scanForPeripheralsWithAddresses(new String[]{mBleDeviceAddr}); mBluetoothCentralManager.scanForPeripheralsWithAddresses(new String[]{mBleDeviceAddr});
} }
@@ -195,8 +196,15 @@ public class SdDataSourceBLE2 extends SdDataSource {
} }
@Override @Override
public void onDisconnectedPeripheral(BluetoothPeripheral peripheral, HciStatus status) { public void onDisconnectedPeripheral(BluetoothPeripheral peripheral, HciStatus status) {
Log.i(TAG,"BluetoothCentralManagerCallback.onDisonnectedPeripheral - attempting to re-connect..."); if (mShutdown) {
mBluetoothCentralManager.autoConnectPeripheral(peripheral, peripheralCallback); 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); super.onDisconnectedPeripheral(peripheral, status);
} }
@@ -488,32 +496,40 @@ public class SdDataSourceBLE2 extends SdDataSource {
private void bleDisconnect() { private void bleDisconnect() {
Log.i(TAG,"bleDisconnect() - Unregistering notifications"); try {
if (mBlePeripheral != null) { Log.i(TAG, "bleDisconnect() - Unregistering notifications");
if (mOsdChar != null) { if (mBlePeripheral != null) {
Log.i(TAG, "bleDisconnect() - unregistering mOsdChar"); if (mOsdChar != null) {
mBlePeripheral.setNotify(mOsdChar, false); 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 { } 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"); mShutdown = true;
mBluetoothCentralManager.close(); 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()"); mUtil.writeToSysLogFile("SDDataSourceBLE.stop()");
super.stop(); super.stop();
bleDisconnect(); try {
CurrentTimeService.stopServer(); bleDisconnect();
CurrentTimeService.stopServer();
} catch (Exception e) {
Log.e(TAG,"stop() - Error stopping data source: "+e.getMessage());
}
} }
private short[] parseDataToAccVals(byte[] rawDataBytes) { private short[] parseDataToAccVals(byte[] rawDataBytes) {

View File

@@ -821,12 +821,15 @@ public class SdServer extends Service implements SdDataReceiver {
// flag. // flag.
if (mFaultTimerCompleted) { if (mFaultTimerCompleted) {
faultWarningBeep(); faultWarningBeep();
//mSdDataSource.stop(); // Re-start the data source to see if that fixes it
//mHandler.postDelayed(new Runnable() { Log.w(TAG,"FAULT - stopping data source");
// public void run() { mSdDataSource.stop();
// mSdDataSource.start(); mHandler.postDelayed(new Runnable() {
// } public void run() {
//}, 190); Log.w(TAG,"FAULT - restarting data source");
mSdDataSource.start();
}
}, 2000);
} else { } else {
startFaultTimer(); startFaultTimer();
Log.v(TAG, "onSdDataFault() - starting Fault Timer"); Log.v(TAG, "onSdDataFault() - starting Fault Timer");

View File

@@ -12,7 +12,8 @@
android:id="@+id/action_start_stop" android:id="@+id/action_start_stop"
android:icon="@drawable/stop_server" android:icon="@drawable/stop_server"
app:showAsAction="never|withText" app:showAsAction="never|withText"
android:title="@string/restart_server" /> android:title="@string/restart_server"
android:enabled="false" />
<item <item
android:id="@+id/action_exit" android:id="@+id/action_exit"
android:icon="@drawable/stop_server" android:icon="@drawable/stop_server"