mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-21 07:51:46 +02:00
Connect directly to the bluetooth device found in scan
Instead of always performing a scan before connecting, use the address stored in the scan to connect directly to the scale. This also means that the location is permission is only needed during the scan and can be revoked afterwards.
This commit is contained in:
@@ -78,8 +78,7 @@ public class OpenScale {
|
|||||||
private ScaleUser selectedScaleUser;
|
private ScaleUser selectedScaleUser;
|
||||||
private List<ScaleMeasurement> scaleMeasurementList;
|
private List<ScaleMeasurement> scaleMeasurementList;
|
||||||
|
|
||||||
private BluetoothCommunication btCom;
|
private BluetoothCommunication btDeviceDriver;
|
||||||
private String btDeviceName;
|
|
||||||
private AlarmHandler alarmHandler;
|
private AlarmHandler alarmHandler;
|
||||||
|
|
||||||
private Context context;
|
private Context context;
|
||||||
@@ -89,7 +88,7 @@ public class OpenScale {
|
|||||||
private OpenScale(Context context) {
|
private OpenScale(Context context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
alarmHandler = new AlarmHandler();
|
alarmHandler = new AlarmHandler();
|
||||||
btCom = null;
|
btDeviceDriver = null;
|
||||||
fragmentList = new ArrayList<>();
|
fragmentList = new ArrayList<>();
|
||||||
|
|
||||||
reopenDatabase();
|
reopenDatabase();
|
||||||
@@ -472,32 +471,35 @@ public class OpenScale {
|
|||||||
return measurementDAO.getAllInRange(startCalender.getTime(), endCalender.getTime(), selectedUserId);
|
return measurementDAO.getAllInRange(startCalender.getTime(), endCalender.getTime(), selectedUserId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean startSearchingForBluetooth(String deviceName, Handler callbackBtHandler) {
|
public boolean connectToBluetoothDevice(String deviceName, String hwAddress, Handler callbackBtHandler) {
|
||||||
Log.d("OpenScale", "Bluetooth Server started! I am searching for device ...");
|
Log.d("OpenScale", "Trying to connect to bluetooth device " + hwAddress
|
||||||
|
+ " (" + deviceName + ")");
|
||||||
|
|
||||||
btCom = BluetoothFactory.createDeviceDriver(context, deviceName);
|
disconnectFromBluetoothDevice();
|
||||||
if (btCom == null) {
|
|
||||||
|
btDeviceDriver = BluetoothFactory.createDeviceDriver(context, deviceName);
|
||||||
|
if (btDeviceDriver == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
btCom.registerCallbackHandler(callbackBtHandler);
|
btDeviceDriver.registerCallbackHandler(callbackBtHandler);
|
||||||
btDeviceName = deviceName;
|
btDeviceDriver.connect(hwAddress);
|
||||||
|
|
||||||
btCom.startSearching(btDeviceName);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean stopSearchingForBluetooth() {
|
public boolean disconnectFromBluetoothDevice() {
|
||||||
if (btCom != null) {
|
if (btDeviceDriver == null) {
|
||||||
btCom.stopSearching();
|
|
||||||
btCom = null;
|
|
||||||
Log.d("OpenScale", "Bluetooth Server explicit stopped!");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Log.d("OpenScale", "Disconnecting from bluetooth device");
|
||||||
|
btDeviceDriver.disconnect(true);
|
||||||
|
btDeviceDriver = null;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public void registerFragment(FragmentUpdateListener fragment) {
|
public void registerFragment(FragmentUpdateListener fragment) {
|
||||||
fragmentList.add(fragment);
|
fragmentList.add(fragment);
|
||||||
|
|
||||||
|
@@ -23,10 +23,7 @@ import android.bluetooth.BluetoothGattCallback;
|
|||||||
import android.bluetooth.BluetoothGattCharacteristic;
|
import android.bluetooth.BluetoothGattCharacteristic;
|
||||||
import android.bluetooth.BluetoothGattDescriptor;
|
import android.bluetooth.BluetoothGattDescriptor;
|
||||||
import android.bluetooth.BluetoothProfile;
|
import android.bluetooth.BluetoothProfile;
|
||||||
import android.content.BroadcastReceiver;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
|
||||||
import android.content.IntentFilter;
|
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@@ -46,12 +43,10 @@ public abstract class BluetoothCommunication {
|
|||||||
protected Context context;
|
protected Context context;
|
||||||
|
|
||||||
private Handler callbackBtHandler;
|
private Handler callbackBtHandler;
|
||||||
private static BluetoothGatt bluetoothGatt;
|
private BluetoothGatt bluetoothGatt;
|
||||||
|
private boolean connectionEstablished;
|
||||||
protected BluetoothGattCallback gattCallback;
|
protected BluetoothGattCallback gattCallback;
|
||||||
protected BluetoothAdapter btAdapter;
|
protected BluetoothAdapter btAdapter;
|
||||||
private Handler searchHandler;
|
|
||||||
private String btDeviceName;
|
|
||||||
public boolean isReceiverRegistered;
|
|
||||||
|
|
||||||
private int cmdStepNr;
|
private int cmdStepNr;
|
||||||
private int initStepNr;
|
private int initStepNr;
|
||||||
@@ -66,10 +61,9 @@ public abstract class BluetoothCommunication {
|
|||||||
{
|
{
|
||||||
this.context = context;
|
this.context = context;
|
||||||
btAdapter = BluetoothAdapter.getDefaultAdapter();
|
btAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
searchHandler = new Handler();
|
|
||||||
gattCallback = new GattCallback();
|
gattCallback = new GattCallback();
|
||||||
isReceiverRegistered = false;
|
|
||||||
bluetoothGatt = null;
|
bluetoothGatt = null;
|
||||||
|
connectionEstablished = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -328,88 +322,38 @@ public abstract class BluetoothCommunication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start searching for a Bluetooth device.
|
* Connect to a Bluetooth device.
|
||||||
*
|
|
||||||
* @note the hardware address is checked. Bluetooth device address has to be start with one of hwAddresses().
|
|
||||||
*
|
*
|
||||||
* On successfully connection Bluetooth machine state is automatically triggered.
|
* On successfully connection Bluetooth machine state is automatically triggered.
|
||||||
* If no device was found the search process is automatically stopped.
|
* If the device is not found the process is automatically stopped.
|
||||||
*
|
*
|
||||||
* @param deviceName the Bluetooth device name that is compared to the found devices.
|
* @param hwAddress the Bluetooth address to connect to
|
||||||
*/
|
*/
|
||||||
public void startSearching(String deviceName) {
|
public void connect(String hwAddress) {
|
||||||
btDeviceName = deviceName;
|
btAdapter.cancelDiscovery();
|
||||||
|
|
||||||
IntentFilter filter = new IntentFilter();
|
BluetoothDevice device = btAdapter.getRemoteDevice(hwAddress);
|
||||||
|
bluetoothGatt = device.connectGatt(context, false, gattCallback);
|
||||||
filter.addAction(BluetoothDevice.ACTION_FOUND);
|
|
||||||
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
|
|
||||||
|
|
||||||
if (isReceiverRegistered == false) {
|
|
||||||
context.registerReceiver(mReceiver, filter);
|
|
||||||
isReceiverRegistered = true;
|
|
||||||
btAdapter.startDiscovery();
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
context.unregisterReceiver(mReceiver);
|
|
||||||
isReceiverRegistered = false;
|
|
||||||
} catch (Exception e) {
|
|
||||||
isReceiverRegistered = false;
|
|
||||||
}
|
|
||||||
startSearching(deviceName);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
/**
|
||||||
public void onReceive(Context context, Intent intent) {
|
* Disconnect from a Bluetooth device
|
||||||
String action = intent.getAction();
|
*/
|
||||||
|
public void disconnect(boolean doCleanup) {
|
||||||
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
|
|
||||||
//discovery finishes, dismis progress dialog
|
|
||||||
if (bluetoothGatt == null) {
|
if (bluetoothGatt == null) {
|
||||||
setBtStatus(BT_STATUS_CODE.BT_NO_DEVICE_FOUND);
|
|
||||||
}
|
|
||||||
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
|
|
||||||
//bluetooth device found
|
|
||||||
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
|
||||||
|
|
||||||
if (device.getName() == null) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (device.getName().toLowerCase().equals(btDeviceName.toLowerCase())) {
|
if (btMachineState != BT_MACHINE_STATE.BT_CLEANUP_STATE && doCleanup) {
|
||||||
Log.d("BluetoothCommunication", btDeviceName + " found trying to connect...");
|
setBtMachineState(BT_MACHINE_STATE.BT_CLEANUP_STATE);
|
||||||
|
nextMachineStateStep();
|
||||||
|
}
|
||||||
|
|
||||||
bluetoothGatt = device.connectGatt(context, true, gattCallback);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stop searching for a Bluetooth device
|
|
||||||
*/
|
|
||||||
public void stopSearching() {
|
|
||||||
if (bluetoothGatt != null)
|
|
||||||
{
|
|
||||||
bluetoothGatt.disconnect();
|
bluetoothGatt.disconnect();
|
||||||
bluetoothGatt.close();
|
bluetoothGatt.close();
|
||||||
bluetoothGatt = null;
|
bluetoothGatt = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isReceiverRegistered == true) {
|
|
||||||
try {
|
|
||||||
context.unregisterReceiver(mReceiver);
|
|
||||||
isReceiverRegistered = false;
|
|
||||||
} catch (Exception e) {
|
|
||||||
isReceiverRegistered = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
searchHandler.removeCallbacksAndMessages(null);
|
|
||||||
btAdapter.cancelDiscovery();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invoke next step for internal Bluetooth state machine.
|
* Invoke next step for internal Bluetooth state machine.
|
||||||
*/
|
*/
|
||||||
@@ -439,14 +383,16 @@ public abstract class BluetoothCommunication {
|
|||||||
private void handleRequests() {
|
private void handleRequests() {
|
||||||
synchronized (openRequest) {
|
synchronized (openRequest) {
|
||||||
// check for pending request
|
// check for pending request
|
||||||
if (openRequest)
|
if (openRequest) {
|
||||||
return; // yes, do nothing
|
return; // yes, do nothing
|
||||||
|
}
|
||||||
|
|
||||||
// handle descriptor requests first
|
// handle descriptor requests first
|
||||||
BluetoothGattDescriptor descriptorRequest = descriptorRequestQueue.poll();
|
BluetoothGattDescriptor descriptorRequest = descriptorRequestQueue.poll();
|
||||||
if (descriptorRequest != null) {
|
if (descriptorRequest != null) {
|
||||||
if (!bluetoothGatt.writeDescriptor(descriptorRequest))
|
if (!bluetoothGatt.writeDescriptor(descriptorRequest)) {
|
||||||
Log.d("BTC", "Descriptor Write failed(" + byteInHex(descriptorRequest.getValue()) + ")");
|
Log.d("BTC", "Descriptor Write failed(" + byteInHex(descriptorRequest.getValue()) + ")");
|
||||||
|
}
|
||||||
openRequest = true;
|
openRequest = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -454,8 +400,9 @@ public abstract class BluetoothCommunication {
|
|||||||
// handle characteristics requests second
|
// handle characteristics requests second
|
||||||
BluetoothGattCharacteristic characteristicRequest = characteristicRequestQueue.poll();
|
BluetoothGattCharacteristic characteristicRequest = characteristicRequestQueue.poll();
|
||||||
if (characteristicRequest != null) {
|
if (characteristicRequest != null) {
|
||||||
if (!bluetoothGatt.writeCharacteristic(characteristicRequest))
|
if (!bluetoothGatt.writeCharacteristic(characteristicRequest)) {
|
||||||
Log.d("BTC", "Characteristic Write failed(" + byteInHex(characteristicRequest.getValue()) + ")");
|
Log.d("BTC", "Characteristic Write failed(" + byteInHex(characteristicRequest.getValue()) + ")");
|
||||||
|
}
|
||||||
openRequest = true;
|
openRequest = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -472,11 +419,15 @@ public abstract class BluetoothCommunication {
|
|||||||
@Override
|
@Override
|
||||||
public void onConnectionStateChange(final BluetoothGatt gatt, int status, int newState) {
|
public void onConnectionStateChange(final BluetoothGatt gatt, int status, int newState) {
|
||||||
if (newState == BluetoothProfile.STATE_CONNECTED) {
|
if (newState == BluetoothProfile.STATE_CONNECTED) {
|
||||||
|
connectionEstablished = true;
|
||||||
setBtStatus(BT_STATUS_CODE.BT_CONNECTION_ESTABLISHED);
|
setBtStatus(BT_STATUS_CODE.BT_CONNECTION_ESTABLISHED);
|
||||||
gatt.discoverServices();
|
gatt.discoverServices();
|
||||||
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
|
}
|
||||||
setBtStatus(BT_STATUS_CODE.BT_CONNECTION_LOST);
|
else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
|
||||||
stopSearching();
|
setBtStatus(connectionEstablished
|
||||||
|
? BT_STATUS_CODE.BT_CONNECTION_LOST
|
||||||
|
: BT_STATUS_CODE.BT_NO_DEVICE_FOUND);
|
||||||
|
disconnect(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -501,8 +452,8 @@ public abstract class BluetoothCommunication {
|
|||||||
// Empty
|
// Empty
|
||||||
}
|
}
|
||||||
|
|
||||||
btMachineState = BT_MACHINE_STATE.BT_INIT_STATE;
|
// Start the state machine
|
||||||
nextMachineStateStep();
|
setBtMachineState(BT_MACHINE_STATE.BT_INIT_STATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -516,7 +467,7 @@ public abstract class BluetoothCommunication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCharacteristicWrite (BluetoothGatt gatt,
|
public void onCharacteristicWrite(BluetoothGatt gatt,
|
||||||
BluetoothGattCharacteristic characteristic,
|
BluetoothGattCharacteristic characteristic,
|
||||||
int status) {
|
int status) {
|
||||||
synchronized (openRequest) {
|
synchronized (openRequest) {
|
||||||
@@ -526,7 +477,7 @@ public abstract class BluetoothCommunication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCharacteristicRead (BluetoothGatt gatt,
|
public void onCharacteristicRead(BluetoothGatt gatt,
|
||||||
BluetoothGattCharacteristic characteristic,
|
BluetoothGattCharacteristic characteristic,
|
||||||
int status) {
|
int status) {
|
||||||
onBluetoothDataRead(gatt, characteristic, status);
|
onBluetoothDataRead(gatt, characteristic, status);
|
||||||
@@ -543,4 +494,3 @@ public abstract class BluetoothCommunication {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -27,7 +27,6 @@ import java.io.InputStream;
|
|||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public class BluetoothCustomOpenScale extends BluetoothCommunication {
|
public class BluetoothCustomOpenScale extends BluetoothCommunication {
|
||||||
@@ -63,18 +62,15 @@ public class BluetoothCustomOpenScale extends BluetoothCommunication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void startSearching(String deviceName) {
|
public void connect(String hwAddress) {
|
||||||
|
|
||||||
if (btAdapter == null) {
|
if (btAdapter == null) {
|
||||||
setBtStatus(BT_STATUS_CODE.BT_NO_DEVICE_FOUND);
|
setBtStatus(BT_STATUS_CODE.BT_NO_DEVICE_FOUND);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
|
for (BluetoothDevice device : btAdapter.getBondedDevices()) {
|
||||||
|
if (device != null && device.getAddress().equals(hwAddress)) {
|
||||||
for (BluetoothDevice device : pairedDevices) {
|
|
||||||
// check if we can found bluetooth device name in the pairing list
|
|
||||||
if (device != null && device.getName().equals(deviceName)) {
|
|
||||||
btDevice = device;
|
btDevice = device;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -101,7 +97,7 @@ public class BluetoothCustomOpenScale extends BluetoothCommunication {
|
|||||||
}
|
}
|
||||||
} catch (IOException connectException) {
|
} catch (IOException connectException) {
|
||||||
// Unable to connect; close the socket and get out
|
// Unable to connect; close the socket and get out
|
||||||
stopSearching();
|
disconnect(false);
|
||||||
setBtStatus(BT_STATUS_CODE.BT_NO_DEVICE_FOUND);
|
setBtStatus(BT_STATUS_CODE.BT_NO_DEVICE_FOUND);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -116,7 +112,7 @@ public class BluetoothCustomOpenScale extends BluetoothCommunication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stopSearching() {
|
public void disconnect(boolean doCleanup) {
|
||||||
if (btSocket != null) {
|
if (btSocket != null) {
|
||||||
if (btSocket.isConnected()) {
|
if (btSocket.isConnected()) {
|
||||||
try {
|
try {
|
||||||
|
@@ -27,7 +27,6 @@ import com.health.openscale.core.datatypes.ScaleMeasurement;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@@ -74,24 +73,22 @@ public class BluetoothIhealthHS3 extends BluetoothCommunication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void startSearching(String deviceName) {
|
public void connect(String hwAddress) {
|
||||||
|
|
||||||
// Log.w("openscale","iHealth HS3 - startSearching "+deviceName);
|
// Log.w("openscale","iHealth HS3 - connect "+hwAddress);
|
||||||
|
|
||||||
if (btAdapter == null) {
|
if (btAdapter == null) {
|
||||||
setBtStatus(BT_STATUS_CODE.BT_NO_DEVICE_FOUND);
|
setBtStatus(BT_STATUS_CODE.BT_NO_DEVICE_FOUND);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
|
|
||||||
|
|
||||||
// Log.w("openscale","about to start searching paired devices");
|
// Log.w("openscale","about to start searching paired devices");
|
||||||
|
|
||||||
for (BluetoothDevice device : pairedDevices) {
|
for (BluetoothDevice device : btAdapter.getBondedDevices()) {
|
||||||
// check if we can found bluetooth device name in the pairing list
|
// check if we can found bluetooth device name in the pairing list
|
||||||
// if (device != null ) { Log.w("openscale","Looking at device "+device.getName()); } ;
|
// if (device != null ) { Log.w("openscale","Looking at device "+device.getName()); } ;
|
||||||
|
|
||||||
if (device != null && device.getName().equals(deviceName)) {
|
if (device != null && device.getAddress().equals(hwAddress)) {
|
||||||
btDevice = device;
|
btDevice = device;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -118,7 +115,7 @@ public class BluetoothIhealthHS3 extends BluetoothCommunication {
|
|||||||
}
|
}
|
||||||
} catch (IOException connectException) {
|
} catch (IOException connectException) {
|
||||||
// Unable to connect; close the socket and get out
|
// Unable to connect; close the socket and get out
|
||||||
stopSearching();
|
disconnect(false);
|
||||||
setBtStatus(BT_STATUS_CODE.BT_NO_DEVICE_FOUND);
|
setBtStatus(BT_STATUS_CODE.BT_NO_DEVICE_FOUND);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -133,9 +130,9 @@ public class BluetoothIhealthHS3 extends BluetoothCommunication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stopSearching() {
|
public void disconnect(boolean doCleanup) {
|
||||||
|
|
||||||
Log.w("openscale","HS3 - stopSearching");
|
Log.w("openscale","HS3 - disconnect");
|
||||||
if (btSocket != null) {
|
if (btSocket != null) {
|
||||||
if (btSocket.isConnected()) {
|
if (btSocket.isConnected()) {
|
||||||
try {
|
try {
|
||||||
|
@@ -18,11 +18,11 @@ package com.health.openscale.gui;
|
|||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
|
import android.bluetooth.BluetoothManager;
|
||||||
import android.content.ActivityNotFoundException;
|
import android.content.ActivityNotFoundException;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.pm.PackageManager;
|
|
||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
@@ -62,7 +62,7 @@ import com.health.openscale.gui.fragments.GraphFragment;
|
|||||||
import com.health.openscale.gui.fragments.OverviewFragment;
|
import com.health.openscale.gui.fragments.OverviewFragment;
|
||||||
import com.health.openscale.gui.fragments.StatisticsFragment;
|
import com.health.openscale.gui.fragments.StatisticsFragment;
|
||||||
import com.health.openscale.gui.fragments.TableFragment;
|
import com.health.openscale.gui.fragments.TableFragment;
|
||||||
import com.health.openscale.gui.utils.PermissionHelper;
|
import com.health.openscale.gui.preferences.BluetoothPreferences;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
@@ -196,6 +196,7 @@ public class MainActivity extends BaseAppCompatActivity
|
|||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
prefs.unregisterOnSharedPreferenceChangeListener(this);
|
prefs.unregisterOnSharedPreferenceChangeListener(this);
|
||||||
|
OpenScale.getInstance(getApplicationContext()).disconnectFromBluetoothDevice();
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -382,7 +383,12 @@ public class MainActivity extends BaseAppCompatActivity
|
|||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
return true;
|
return true;
|
||||||
case R.id.action_bluetooth_status:
|
case R.id.action_bluetooth_status:
|
||||||
invokeSearchBluetoothDevice();
|
if (OpenScale.getInstance(getApplicationContext()).disconnectFromBluetoothDevice()) {
|
||||||
|
setBluetoothStatusIcon(R.drawable.ic_bluetooth_disabled);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
invokeConnectToBluetoothDevice();
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
case R.id.importData:
|
case R.id.importData:
|
||||||
importCsvFile();
|
importCsvFile();
|
||||||
@@ -405,11 +411,19 @@ public class MainActivity extends BaseAppCompatActivity
|
|||||||
|
|
||||||
bluetoothStatus = menu.findItem(R.id.action_bluetooth_status);
|
bluetoothStatus = menu.findItem(R.id.action_bluetooth_status);
|
||||||
|
|
||||||
|
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
|
||||||
|
boolean hasBluetooth = bluetoothManager.getAdapter() != null;
|
||||||
|
|
||||||
|
if (!hasBluetooth) {
|
||||||
|
bluetoothStatus.setEnabled(false);
|
||||||
|
setBluetoothStatusIcon(R.drawable.ic_bluetooth_disabled);
|
||||||
|
}
|
||||||
// Just search for a bluetooth device just once at the start of the app and if start preference enabled
|
// Just search for a bluetooth device just once at the start of the app and if start preference enabled
|
||||||
if (firstAppStart && prefs.getBoolean("btEnable", false)) {
|
else if (firstAppStart && prefs.getBoolean("btEnable", false)) {
|
||||||
invokeSearchBluetoothDevice();
|
invokeConnectToBluetoothDevice();
|
||||||
firstAppStart = false;
|
firstAppStart = false;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
// Set current bluetooth status icon while e.g. orientation changes
|
// Set current bluetooth status icon while e.g. orientation changes
|
||||||
setBluetoothStatusIcon(bluetoothStatusIcon);
|
setBluetoothStatusIcon(bluetoothStatusIcon);
|
||||||
}
|
}
|
||||||
@@ -429,7 +443,7 @@ public class MainActivity extends BaseAppCompatActivity
|
|||||||
drawerToggle.onConfigurationChanged(newConfig);
|
drawerToggle.onConfigurationChanged(newConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void invokeSearchBluetoothDevice() {
|
private void invokeConnectToBluetoothDevice() {
|
||||||
final OpenScale openScale = OpenScale.getInstance(getApplicationContext());
|
final OpenScale openScale = OpenScale.getInstance(getApplicationContext());
|
||||||
|
|
||||||
if (openScale.getSelectedScaleUserId() == -1) {
|
if (openScale.getSelectedScaleUserId() == -1) {
|
||||||
@@ -437,36 +451,23 @@ public class MainActivity extends BaseAppCompatActivity
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (openScale.stopSearchingForBluetooth()) {
|
String deviceName = prefs.getString(
|
||||||
setBluetoothStatusIcon(R.drawable.ic_bluetooth_disabled);
|
BluetoothPreferences.PREFERENCE_KEY_BLUETOOTH_DEVICE_NAME, "-");
|
||||||
return;
|
String hwAddress = prefs.getString(
|
||||||
}
|
BluetoothPreferences.PREFERENCE_KEY_BLUETOOTH_HW_ADDRESS, "");
|
||||||
|
|
||||||
String deviceName = prefs.getString("btDeviceName", "-");
|
|
||||||
|
|
||||||
boolean permGrantedCoarseLocation = false;
|
|
||||||
|
|
||||||
// Check if Bluetooth 4.x is available
|
|
||||||
if (!deviceName.equals("openScale_MCU")) {
|
|
||||||
permGrantedCoarseLocation = PermissionHelper.requestBluetoothPermission(this, false);
|
|
||||||
} else {
|
|
||||||
permGrantedCoarseLocation = PermissionHelper.requestBluetoothPermission(this, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (permGrantedCoarseLocation) {
|
|
||||||
if (deviceName.equals("-")) {
|
if (deviceName.equals("-")) {
|
||||||
Toast.makeText(getApplicationContext(), getResources().getString(R.string.info_bluetooth_no_device_set), Toast.LENGTH_SHORT).show();
|
Toast.makeText(getApplicationContext(), R.string.info_bluetooth_no_device_set, Toast.LENGTH_SHORT).show();
|
||||||
return;
|
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);
|
||||||
|
|
||||||
if (!openScale.startSearchingForBluetooth(deviceName, callbackBtHandler)) {
|
if (!openScale.connectToBluetoothDevice(deviceName, hwAddress, 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private final Handler callbackBtHandler = new Handler() {
|
private final Handler callbackBtHandler = new Handler() {
|
||||||
@Override
|
@Override
|
||||||
@@ -706,28 +707,6 @@ public class MainActivity extends BaseAppCompatActivity
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
|
|
||||||
boolean permissionGranted = true;
|
|
||||||
switch (requestCode) {
|
|
||||||
case PermissionHelper.PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION:
|
|
||||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
||||||
invokeSearchBluetoothDevice();
|
|
||||||
} else {
|
|
||||||
setBluetoothStatusIcon(R.drawable.ic_bluetooth_disabled);
|
|
||||||
permissionGranted = false;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!permissionGranted) {
|
|
||||||
Toast.makeText(getApplicationContext(), getResources().getString(
|
|
||||||
R.string.permission_not_granted), Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
|
|
||||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressLint("RestrictedApi")
|
@SuppressLint("RestrictedApi")
|
||||||
public static void disableShiftMode(BottomNavigationView view) {
|
public static void disableShiftMode(BottomNavigationView view) {
|
||||||
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
|
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
|
||||||
|
@@ -83,7 +83,7 @@ public class BluetoothPreferences extends PreferenceFragment {
|
|||||||
}
|
}
|
||||||
}, progressUpdatePeriod);
|
}, progressUpdatePeriod);
|
||||||
|
|
||||||
OpenScale.getInstance(getActivity()).stopSearchingForBluetooth();
|
OpenScale.getInstance(getActivity()).disconnectFromBluetoothDevice();
|
||||||
|
|
||||||
btScanner.getDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
|
btScanner.getDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
|
||||||
@Override
|
@Override
|
||||||
@@ -210,7 +210,7 @@ public class BluetoothPreferences extends PreferenceFragment {
|
|||||||
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(), true)) {
|
if (PermissionHelper.requestBluetoothPermission(getActivity())) {
|
||||||
startBluetoothDiscovery();
|
startBluetoothDiscovery();
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@@ -34,7 +34,7 @@ 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, boolean BLE) {
|
public static boolean requestBluetoothPermission(final Activity activity) {
|
||||||
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();
|
||||||
|
|
||||||
@@ -42,18 +42,18 @@ public class PermissionHelper {
|
|||||||
|
|
||||||
Toast.makeText(activity.getApplicationContext(), "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) {
|
||||||
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
||||||
activity.startActivityForResult(enableBtIntent, 1);
|
activity.startActivity(enableBtIntent);
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if Bluetooth 4.x is available
|
// Check if Bluetooth 4.x is available
|
||||||
if (BLE) {
|
|
||||||
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.getApplicationContext(), "Bluetooth 4.x " + activity.getResources().getString(R.string.info_is_not_available), Toast.LENGTH_SHORT).show();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
|
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
|
||||||
@@ -68,13 +68,11 @@ public class PermissionHelper {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
AlertDialog dialog = builder.create();
|
builder.show();
|
||||||
dialog.show();
|
return false;
|
||||||
} else {
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean requestReadPermission(final Activity activity) {
|
public static boolean requestReadPermission(final Activity activity) {
|
||||||
|
@@ -189,7 +189,7 @@
|
|||||||
<string name="label_weeks_view">Week view</string>
|
<string name="label_weeks_view">Week view</string>
|
||||||
|
|
||||||
<string name="permission_not_granted">Permission not granted</string>
|
<string name="permission_not_granted">Permission not granted</string>
|
||||||
<string name="permission_bluetooth_info">openScale requires permission to access the coarse location to search for Bluetooth devices</string>
|
<string name="permission_bluetooth_info">openScale requires permission to access the coarse location to search for Bluetooth devices. The permission can be revoked after the device is found.</string>
|
||||||
<string name="permission_bluetooth_info_title">Information</string>
|
<string name="permission_bluetooth_info_title">Information</string>
|
||||||
<string name="label_next">Next</string>
|
<string name="label_next">Next</string>
|
||||||
<string name="label_press_hold_reorder">Press and hold to reorder</string>
|
<string name="label_press_hold_reorder">Press and hold to reorder</string>
|
||||||
|
Reference in New Issue
Block a user