Skip to content

Commit efce26d

Browse files
Disconnect and exit app on Back Pressed
1 parent f2f4cda commit efce26d

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

app/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ android {
77
minSdkVersion 14
88
targetSdkVersion 25
99
versionCode 1
10-
versionName "0.5b_20180325"
10+
versionName "1.0.0"
1111
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1212
}
1313
buildTypes {

app/src/main/java/eu/basicairdata/airdatabridge/AirDataBridgeActivity.java

+9
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ public void onPageSelected(int position) {
9696
tabLayout.setTabMode(TabLayout.MODE_FIXED);
9797
tabLayout.setupWithViewPager(mViewPager);
9898

99+
EventBus.getDefault().post(EventBusMSG.START_APP);
100+
99101
/*
100102
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
101103
fab.setOnClickListener(new View.OnClickListener() {
@@ -139,6 +141,13 @@ public void onPause() {
139141
}
140142

141143

144+
@Override
145+
public void onBackPressed() {
146+
//moveTaskToBack(true);
147+
EventBus.getDefault().post(EventBusMSG.EXIT_APP);
148+
finish();
149+
}
150+
142151

143152
@Override
144153
protected void onDestroy(){

app/src/main/java/eu/basicairdata/airdatabridge/AirDataBridgeApplication.java

+29-3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public class AirDataBridgeApplication extends Application {
7171

7272
boolean StoragePermissionChecked = false;
7373
boolean StoragePermissionGranted = false;
74+
boolean BluetoothAutoReconnect = true; // Auto reconnect if disconnected
7475

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

124+
public boolean isBluetoothAutoReconnect() {
125+
return BluetoothAutoReconnect;
126+
}
127+
128+
public void setBluetoothAutoReconnect(boolean bluetoothAutoReconnect) {
129+
BluetoothAutoReconnect = bluetoothAutoReconnect;
130+
}
131+
123132
public boolean isStatusViewEnabled() {
124133
return StatusViewEnabled;
125134
}
@@ -601,8 +610,8 @@ public void onBluetoothHelperConnectionStateChanged(BluetoothHelper bluetoothhel
601610
if (isConnected) {
602611
BluetoothConnectionStatus = EventBusMSG.BLUETOOTH_CONNECTED;
603612
EventBus.getDefault().post(EventBusMSG.BLUETOOTH_CONNECTED);
604-
mBluetooth.SendMessage("$HBQ,AirDataBridge,0.4");
605-
mBluetooth.SendMessage("$HBQ,AirDataBridge,0.4");
613+
mBluetooth.SendMessage("$HBQ,AirDataBridge," + BuildConfig.VERSION_NAME);
614+
mBluetooth.SendMessage("$HBQ,AirDataBridge," + BuildConfig.VERSION_NAME);
606615
startCommTimeout();
607616
Log.w("myApp", "[#] AirDataBridgeApplication.java - BLUETOOTH_CONNECTED");
608617
// Do something
@@ -613,7 +622,7 @@ public void onBluetoothHelperConnectionStateChanged(BluetoothHelper bluetoothhel
613622
//}
614623
//Log.w("myApp", "[#] AirDataBridgeApplication.java - EventBusMSG.REMOTE_UPDATE_LOGLIST");
615624
// Auto reconnect
616-
if (BluetoothConnectionStatus != EventBusMSG.BLUETOOTH_OFF) {
625+
if ((BluetoothConnectionStatus != EventBusMSG.BLUETOOTH_OFF) && (BluetoothAutoReconnect)) {
617626
BluetoothConnectionStatus = EventBusMSG.BLUETOOTH_CONNECTING;
618627
EventBus.getDefault().post(EventBusMSG.BLUETOOTH_CONNECTING);
619628
mBluetooth.Connect(BluetoothDeviceName);
@@ -655,6 +664,23 @@ public void onTerminate() {
655664
@Subscribe
656665
public void onEvent(Short msg) {
657666
switch (msg) {
667+
case EventBusMSG.START_APP:
668+
if (!BluetoothAutoReconnect) {
669+
BluetoothAutoReconnect = true;
670+
if (mBluetoothAdapter.isEnabled()) {
671+
BluetoothConnectionStatus = EventBusMSG.BLUETOOTH_CONNECTING;
672+
EventBus.getDefault().post(EventBusMSG.BLUETOOTH_CONNECTING);
673+
mBluetooth.Connect(BluetoothDeviceName);
674+
} else {
675+
BluetoothConnectionStatus = EventBusMSG.BLUETOOTH_OFF;
676+
EventBus.getDefault().post(EventBusMSG.BLUETOOTH_OFF);
677+
}
678+
}
679+
break;
680+
case EventBusMSG.EXIT_APP:
681+
BluetoothAutoReconnect = false;
682+
mBluetooth.Disconnect();
683+
break;
658684
case EventBusMSG.STORAGE_PERMISSION_GRANTED:
659685
StoragePermissionGranted = true;
660686
// Create folder if not exists

app/src/main/java/eu/basicairdata/airdatabridge/EventBusMSG.java

+3
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,7 @@ public class EventBusMSG {
6666
static final short UPDATE_SETTINGS = 201; // Signal to update (read) preferences
6767

6868
static final short ERROR_FILE_ALREADY_EXISTS = 210; // You are trying to create a file that already exists
69+
70+
static final short START_APP = 254; // Start the app
71+
static final short EXIT_APP = 255; // Close the app
6972
}

0 commit comments

Comments
 (0)