mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-21 16:02:04 +02:00
Continue scanning/connecting after user enables bluetooth
This commit is contained in:
@@ -18,6 +18,7 @@ package com.health.openscale.gui;
|
|||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothManager;
|
import android.bluetooth.BluetoothManager;
|
||||||
import android.content.ActivityNotFoundException;
|
import android.content.ActivityNotFoundException;
|
||||||
import android.content.DialogInterface;
|
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 IMPORT_DATA_REQUEST = 100;
|
||||||
private static final int EXPORT_DATA_REQUEST = 101;
|
private static final int EXPORT_DATA_REQUEST = 101;
|
||||||
|
private static final int ENABLE_BLUETOOTH_REQUEST = 102;
|
||||||
|
|
||||||
private DrawerLayout drawerLayout;
|
private DrawerLayout drawerLayout;
|
||||||
private Toolbar toolbar;
|
private Toolbar toolbar;
|
||||||
@@ -461,6 +463,13 @@ public class MainActivity extends BaseAppCompatActivity
|
|||||||
return;
|
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();
|
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);
|
||||||
|
|
||||||
@@ -664,6 +673,16 @@ public class MainActivity extends BaseAppCompatActivity
|
|||||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||||
super.onActivityResult(requestCode, resultCode, 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) {
|
if (resultCode != RESULT_OK || data == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@@ -15,6 +15,8 @@
|
|||||||
*/
|
*/
|
||||||
package com.health.openscale.gui.preferences;
|
package com.health.openscale.gui.preferences;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.app.Fragment;
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
@@ -207,10 +209,11 @@ public class BluetoothPreferences extends PreferenceFragment {
|
|||||||
// Might have been started by another app
|
// Might have been started by another app
|
||||||
btAdapter.cancelDiscovery();
|
btAdapter.cancelDiscovery();
|
||||||
|
|
||||||
|
final Fragment fragment = this;
|
||||||
btScanner.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
btScanner.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public boolean onPreferenceClick(Preference preference) {
|
public boolean onPreferenceClick(Preference preference) {
|
||||||
if (PermissionHelper.requestBluetoothPermission(getActivity())) {
|
if (PermissionHelper.requestBluetoothPermission(getActivity(), fragment)) {
|
||||||
startBluetoothDiscovery();
|
startBluetoothDiscovery();
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -244,6 +247,20 @@ public class BluetoothPreferences extends PreferenceFragment {
|
|||||||
super.onDestroy();
|
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 {
|
private class onClickListenerDeviceSelect implements Preference.OnPreferenceClickListener {
|
||||||
@Override
|
@Override
|
||||||
public boolean onPreferenceClick(final Preference preference) {
|
public boolean onPreferenceClick(final Preference preference) {
|
||||||
@@ -267,7 +284,7 @@ public class BluetoothPreferences extends PreferenceFragment {
|
|||||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
startBluetoothDiscovery();
|
startBluetoothDiscovery();
|
||||||
} else {
|
} 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;
|
break;
|
||||||
}
|
}
|
||||||
|
@@ -18,6 +18,7 @@ package com.health.openscale.gui.utils;
|
|||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
|
import android.app.Fragment;
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothManager;
|
import android.bluetooth.BluetoothManager;
|
||||||
import android.content.Context;
|
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_READ_STORAGE = 2;
|
||||||
public final static int PERMISSIONS_REQUEST_ACCESS_WRITE_STORAGE = 3;
|
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);
|
final BluetoothManager bluetoothManager = (BluetoothManager) activity.getSystemService(Context.BLUETOOTH_SERVICE);
|
||||||
BluetoothAdapter btAdapter = bluetoothManager.getAdapter();
|
BluetoothAdapter btAdapter = bluetoothManager.getAdapter();
|
||||||
|
|
||||||
if (btAdapter == null || !btAdapter.isEnabled()) {
|
if (btAdapter == null || !btAdapter.isEnabled()) {
|
||||||
|
Toast.makeText(activity, "Bluetooth " + activity.getResources().getString(R.string.info_is_not_enable), Toast.LENGTH_SHORT).show();
|
||||||
Toast.makeText(activity.getApplicationContext(), "Bluetooth " + activity.getResources().getString(R.string.info_is_not_enable), Toast.LENGTH_SHORT).show();
|
|
||||||
|
|
||||||
if (btAdapter != null) {
|
if (btAdapter != null) {
|
||||||
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
||||||
activity.startActivity(enableBtIntent);
|
fragment.startActivityForResult(enableBtIntent, ENABLE_BLUETOOTH_REQUEST);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if Bluetooth 4.x is available
|
// Check if Bluetooth 4.x is available
|
||||||
if (!activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user