Skip to content

Commit

Permalink
Disconnect and exit app on Back Pressed
Browse files Browse the repository at this point in the history
  • Loading branch information
GrazianoCapelli committed Mar 31, 2018
1 parent f2f4cda commit efce26d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "0.5b_20180325"
versionName "1.0.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public void onPageSelected(int position) {
tabLayout.setTabMode(TabLayout.MODE_FIXED);
tabLayout.setupWithViewPager(mViewPager);

EventBus.getDefault().post(EventBusMSG.START_APP);

/*
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
Expand Down Expand Up @@ -139,6 +141,13 @@ public void onPause() {
}


@Override
public void onBackPressed() {
//moveTaskToBack(true);
EventBus.getDefault().post(EventBusMSG.EXIT_APP);
finish();
}


@Override
protected void onDestroy(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class AirDataBridgeApplication extends Application {

boolean StoragePermissionChecked = false;
boolean StoragePermissionGranted = false;
boolean BluetoothAutoReconnect = true; // Auto reconnect if disconnected

boolean StatusViewEnabled = false; // If true, the status updates are enabled
boolean ForceRemoteLST = true; // If true, ask $FMQ,LST also if recording
Expand Down Expand Up @@ -120,6 +121,14 @@ public void setStoragePermissionGranted(boolean storagePermissionGranted) {
StoragePermissionGranted = storagePermissionGranted;
}

public boolean isBluetoothAutoReconnect() {
return BluetoothAutoReconnect;
}

public void setBluetoothAutoReconnect(boolean bluetoothAutoReconnect) {
BluetoothAutoReconnect = bluetoothAutoReconnect;
}

public boolean isStatusViewEnabled() {
return StatusViewEnabled;
}
Expand Down Expand Up @@ -601,8 +610,8 @@ public void onBluetoothHelperConnectionStateChanged(BluetoothHelper bluetoothhel
if (isConnected) {
BluetoothConnectionStatus = EventBusMSG.BLUETOOTH_CONNECTED;
EventBus.getDefault().post(EventBusMSG.BLUETOOTH_CONNECTED);
mBluetooth.SendMessage("$HBQ,AirDataBridge,0.4");
mBluetooth.SendMessage("$HBQ,AirDataBridge,0.4");
mBluetooth.SendMessage("$HBQ,AirDataBridge," + BuildConfig.VERSION_NAME);
mBluetooth.SendMessage("$HBQ,AirDataBridge," + BuildConfig.VERSION_NAME);
startCommTimeout();
Log.w("myApp", "[#] AirDataBridgeApplication.java - BLUETOOTH_CONNECTED");
// Do something
Expand All @@ -613,7 +622,7 @@ public void onBluetoothHelperConnectionStateChanged(BluetoothHelper bluetoothhel
//}
//Log.w("myApp", "[#] AirDataBridgeApplication.java - EventBusMSG.REMOTE_UPDATE_LOGLIST");
// Auto reconnect
if (BluetoothConnectionStatus != EventBusMSG.BLUETOOTH_OFF) {
if ((BluetoothConnectionStatus != EventBusMSG.BLUETOOTH_OFF) && (BluetoothAutoReconnect)) {
BluetoothConnectionStatus = EventBusMSG.BLUETOOTH_CONNECTING;
EventBus.getDefault().post(EventBusMSG.BLUETOOTH_CONNECTING);
mBluetooth.Connect(BluetoothDeviceName);
Expand Down Expand Up @@ -655,6 +664,23 @@ public void onTerminate() {
@Subscribe
public void onEvent(Short msg) {
switch (msg) {
case EventBusMSG.START_APP:
if (!BluetoothAutoReconnect) {
BluetoothAutoReconnect = true;
if (mBluetoothAdapter.isEnabled()) {
BluetoothConnectionStatus = EventBusMSG.BLUETOOTH_CONNECTING;
EventBus.getDefault().post(EventBusMSG.BLUETOOTH_CONNECTING);
mBluetooth.Connect(BluetoothDeviceName);
} else {
BluetoothConnectionStatus = EventBusMSG.BLUETOOTH_OFF;
EventBus.getDefault().post(EventBusMSG.BLUETOOTH_OFF);
}
}
break;
case EventBusMSG.EXIT_APP:
BluetoothAutoReconnect = false;
mBluetooth.Disconnect();
break;
case EventBusMSG.STORAGE_PERMISSION_GRANTED:
StoragePermissionGranted = true;
// Create folder if not exists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,7 @@ public class EventBusMSG {
static final short UPDATE_SETTINGS = 201; // Signal to update (read) preferences

static final short ERROR_FILE_ALREADY_EXISTS = 210; // You are trying to create a file that already exists

static final short START_APP = 254; // Start the app
static final short EXIT_APP = 255; // Close the app
}

0 comments on commit efce26d

Please sign in to comment.