1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-21 16:02:04 +02:00

Make it possible to disconnect bluetooth

by clicking on the bluetooth button when bluetooth is connected. As
requested in #235.
This commit is contained in:
Erik Johansson
2018-04-08 00:09:55 +02:00
parent d95976c7fe
commit c5431dfa2c
2 changed files with 15 additions and 6 deletions

View File

@@ -488,11 +488,14 @@ public class OpenScale {
return true; return true;
} }
public void stopSearchingForBluetooth() { public boolean stopSearchingForBluetooth() {
if (btCom != null) { if (btCom != null) {
btCom.stopSearching(); btCom.stopSearching();
btCom = null;
Log.d("OpenScale", "Bluetooth Server explicit stopped!"); Log.d("OpenScale", "Bluetooth Server explicit stopped!");
return true;
} }
return false;
} }
public void registerFragment(FragmentUpdateListener fragment) { public void registerFragment(FragmentUpdateListener fragment) {

View File

@@ -430,24 +430,31 @@ public class MainActivity extends BaseAppCompatActivity
} }
private void invokeSearchBluetoothDevice() { private void invokeSearchBluetoothDevice() {
if (OpenScale.getInstance(getApplicationContext()).getSelectedScaleUserId() == -1) { final OpenScale openScale = OpenScale.getInstance(getApplicationContext());
if (openScale.getSelectedScaleUserId() == -1) {
showNoSelectedUserDialog(); showNoSelectedUserDialog();
return; return;
} }
if (openScale.stopSearchingForBluetooth()) {
setBluetoothStatusIcon(R.drawable.ic_bluetooth_disabled);
return;
}
String deviceName = prefs.getString("btDeviceName", "-"); String deviceName = prefs.getString("btDeviceName", "-");
boolean permGrantedCoarseLocation = false; boolean permGrantedCoarseLocation = false;
// Check if Bluetooth 4.x is available // Check if Bluetooth 4.x is available
if (deviceName != "openScale_MCU") { if (!deviceName.equals("openScale_MCU")) {
permGrantedCoarseLocation = PermissionHelper.requestBluetoothPermission(this, false); permGrantedCoarseLocation = PermissionHelper.requestBluetoothPermission(this, false);
} else { } else {
permGrantedCoarseLocation = PermissionHelper.requestBluetoothPermission(this, true); permGrantedCoarseLocation = PermissionHelper.requestBluetoothPermission(this, true);
} }
if (permGrantedCoarseLocation) { if (permGrantedCoarseLocation) {
if (deviceName == "-") { if (deviceName.equals("-")) {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.info_bluetooth_no_device_set), Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), getResources().getString(R.string.info_bluetooth_no_device_set), Toast.LENGTH_SHORT).show();
return; return;
} }
@@ -455,8 +462,7 @@ public class MainActivity extends BaseAppCompatActivity
Toast.makeText(getApplicationContext(), getResources().getString(R.string.info_bluetooth_try_connection) + " " + deviceName, Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), getResources().getString(R.string.info_bluetooth_try_connection) + " " + deviceName, Toast.LENGTH_SHORT).show();
setBluetoothStatusIcon(R.drawable.ic_bluetooth_searching); setBluetoothStatusIcon(R.drawable.ic_bluetooth_searching);
OpenScale.getInstance(getApplicationContext()).stopSearchingForBluetooth(); if (!openScale.startSearchingForBluetooth(deviceName, callbackBtHandler)) {
if (!OpenScale.getInstance(getApplicationContext()).startSearchingForBluetooth(deviceName, callbackBtHandler)) {
Toast.makeText(getApplicationContext(), deviceName + " " + getResources().getString(R.string.label_bt_device_no_support), Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), deviceName + " " + getResources().getString(R.string.label_bt_device_no_support), Toast.LENGTH_SHORT).show();
} }
} }