1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-20 23:41:45 +02:00

Continue scanning/connecting after user enables bluetooth

This commit is contained in:
Erik Johansson
2018-04-13 00:49:06 +02:00
parent ef11372ee0
commit ede4672f9e
3 changed files with 45 additions and 7 deletions

View File

@@ -18,6 +18,7 @@ package com.health.openscale.gui;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
@@ -80,6 +81,7 @@ public class MainActivity extends BaseAppCompatActivity
private static final int IMPORT_DATA_REQUEST = 100;
private static final int EXPORT_DATA_REQUEST = 101;
private static final int ENABLE_BLUETOOTH_REQUEST = 102;
private DrawerLayout drawerLayout;
private Toolbar toolbar;
@@ -461,6 +463,13 @@ public class MainActivity extends BaseAppCompatActivity
return;
}
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
if (!bluetoothManager.getAdapter().isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, ENABLE_BLUETOOTH_REQUEST);
return;
}
Toast.makeText(getApplicationContext(), getResources().getString(R.string.info_bluetooth_try_connection) + " " + deviceName, Toast.LENGTH_SHORT).show();
setBluetoothStatusIcon(R.drawable.ic_bluetooth_searching);
@@ -664,6 +673,16 @@ public class MainActivity extends BaseAppCompatActivity
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ENABLE_BLUETOOTH_REQUEST) {
if (resultCode == RESULT_OK) {
invokeConnectToBluetoothDevice();
}
else {
Toast.makeText(this, "Bluetooth " + getResources().getString(R.string.info_is_not_enable), Toast.LENGTH_SHORT).show();
}
return;
}
if (resultCode != RESULT_OK || data == null) {
return;
}

View File

@@ -15,6 +15,8 @@
*/
package com.health.openscale.gui.preferences;
import android.app.Activity;
import android.app.Fragment;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
@@ -207,10 +209,11 @@ public class BluetoothPreferences extends PreferenceFragment {
// Might have been started by another app
btAdapter.cancelDiscovery();
final Fragment fragment = this;
btScanner.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
if (PermissionHelper.requestBluetoothPermission(getActivity())) {
if (PermissionHelper.requestBluetoothPermission(getActivity(), fragment)) {
startBluetoothDiscovery();
}
return true;
@@ -244,6 +247,20 @@ public class BluetoothPreferences extends PreferenceFragment {
super.onDestroy();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PermissionHelper.ENABLE_BLUETOOTH_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
if (PermissionHelper.requestBluetoothPermission(getActivity(), this)) {
startBluetoothDiscovery();
}
}
else {
btScanner.getDialog().dismiss();
}
}
}
private class onClickListenerDeviceSelect implements Preference.OnPreferenceClickListener {
@Override
public boolean onPreferenceClick(final Preference preference) {
@@ -267,7 +284,7 @@ public class BluetoothPreferences extends PreferenceFragment {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startBluetoothDiscovery();
} else {
Toast.makeText(getActivity().getApplicationContext(), R.string.permission_not_granted, Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity(), R.string.permission_not_granted, Toast.LENGTH_SHORT).show();
}
break;
}

View File

@@ -18,6 +18,7 @@ package com.health.openscale.gui.utils;
import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
@@ -34,24 +35,25 @@ public class PermissionHelper {
public final static int PERMISSIONS_REQUEST_ACCESS_READ_STORAGE = 2;
public final static int PERMISSIONS_REQUEST_ACCESS_WRITE_STORAGE = 3;
public static boolean requestBluetoothPermission(final Activity activity) {
public final static int ENABLE_BLUETOOTH_REQUEST = 5;
public static boolean requestBluetoothPermission(final Activity activity, Fragment fragment) {
final BluetoothManager bluetoothManager = (BluetoothManager) activity.getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter btAdapter = bluetoothManager.getAdapter();
if (btAdapter == null || !btAdapter.isEnabled()) {
Toast.makeText(activity.getApplicationContext(), "Bluetooth " + activity.getResources().getString(R.string.info_is_not_enable), Toast.LENGTH_SHORT).show();
Toast.makeText(activity, "Bluetooth " + activity.getResources().getString(R.string.info_is_not_enable), Toast.LENGTH_SHORT).show();
if (btAdapter != null) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
activity.startActivity(enableBtIntent);
fragment.startActivityForResult(enableBtIntent, ENABLE_BLUETOOTH_REQUEST);
}
return false;
}
// Check if Bluetooth 4.x is available
if (!activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(activity.getApplicationContext(), "Bluetooth 4.x " + activity.getResources().getString(R.string.info_is_not_available), Toast.LENGTH_SHORT).show();
Toast.makeText(activity, "Bluetooth 4.x " + activity.getResources().getString(R.string.info_is_not_available), Toast.LENGTH_SHORT).show();
return false;
}