diff --git a/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java b/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java index 9c9603cf..dcf5a5cc 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java +++ b/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java @@ -25,10 +25,6 @@ import android.util.Log; import com.health.openscale.core.alarm.AlarmHandler; import com.health.openscale.core.bluetooth.BluetoothCommunication; -import com.health.openscale.core.bluetooth.BluetoothCustomOpenScale; -import com.health.openscale.core.bluetooth.BluetoothMedisanaBS444; -import com.health.openscale.core.bluetooth.BluetoothMiScale; -import com.health.openscale.core.bluetooth.BluetoothSanitasSbf70; import com.health.openscale.core.database.ScaleDatabase; import com.health.openscale.core.database.ScaleUserDatabase; import com.health.openscale.core.datatypes.ScaleData; @@ -48,11 +44,6 @@ import java.util.ArrayList; import java.util.Map; import java.util.TreeMap; -import static com.health.openscale.core.bluetooth.BluetoothCommunication.BT_MEDISANA_BS444; -import static com.health.openscale.core.bluetooth.BluetoothCommunication.BT_MI_SCALE; -import static com.health.openscale.core.bluetooth.BluetoothCommunication.BT_OPEN_SCALE; -import static com.health.openscale.core.bluetooth.BluetoothCommunication.BT_SANITAS_SBF70; - public class OpenScale { private static OpenScale instance; @@ -354,24 +345,7 @@ public class OpenScale { public void startSearchingForBluetooth(int btScales, String deviceName, Handler callbackBtHandler) { Log.d("OpenScale", "Bluetooth Server started! I am searching for device ..."); - switch (btScales) { - case BT_MI_SCALE: - btCom = new BluetoothMiScale(context); - break; - case BT_OPEN_SCALE: - btCom = new BluetoothCustomOpenScale(); - break; - case BT_SANITAS_SBF70: - btCom = new BluetoothSanitasSbf70(context); - break; - case BT_MEDISANA_BS444: - btCom = new BluetoothMedisanaBS444(context); - break; - default: - Log.e("OpenScale", "No valid scale type selected"); - return; - } - + btCom = BluetoothCommunication.getBtDevice(context, btScales); btCom.registerCallbackHandler(callbackBtHandler); btDeviceName = deviceName; diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java index 3dc0dc58..dd0ad7d7 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java @@ -17,34 +17,79 @@ package com.health.openscale.core.bluetooth; import android.bluetooth.BluetoothAdapter; +import android.content.Context; import android.os.Handler; +import com.health.openscale.core.datatypes.ScaleData; + +import static com.health.openscale.core.bluetooth.BluetoothCommunication.BT_STATUS_CODE.BT_RETRIEVE_SCALE_DATA; + public abstract class BluetoothCommunication { - public static final int BT_MI_SCALE = 0; - public static final int BT_OPEN_SCALE = 1; - public static final int BT_SANITAS_SBF70 = 2; - public static final int BT_MEDISANA_BS444 = 3; + public enum BT_STATUS_CODE {BT_RETRIEVE_SCALE_DATA, BT_INIT_PROCESS, BT_CONNECTION_ESTABLISHED, BT_CONNECTION_LOST, BT_NO_DEVICE_FOUND, BT_UNEXPECTED_ERROR }; - public static final int BT_RETRIEVE_SCALE_DATA = 0; - public static final int BT_INIT_PROCESS = 1; - public static final int BT_CONNECTION_ESTABLISHED = 2; - public static final int BT_CONNECTION_LOST = 3; - public static final int BT_NO_DEVICE_FOUND = 4; - public static final int BT_UNEXPECTED_ERROR = 5; - - protected Handler callbackBtHandler; + private Handler callbackBtHandler; protected BluetoothAdapter btAdapter; - public BluetoothCommunication() + protected Context context; + + public BluetoothCommunication(Context context) { + this.context = context; btAdapter = BluetoothAdapter.getDefaultAdapter(); } + public static BluetoothCommunication getBtDevice(Context context, int i) { + switch (i) { + case 0: + return new BluetoothCustomOpenScale(context); + case 1: + return new BluetoothMiScale(context); + case 2: + return new BluetoothSanitasSbf70(context); + case 3: + return new BluetoothMedisanaBS444(context); + } + + return null; + } + public void registerCallbackHandler(Handler cbBtHandler) { callbackBtHandler = cbBtHandler; } + + protected void setBtStatus(BT_STATUS_CODE statusCode) { + setBtStatus(statusCode, ""); + } + + protected void setBtStatus(BT_STATUS_CODE statusCode, String infoText) { + callbackBtHandler.obtainMessage(statusCode.ordinal(), infoText).sendToTarget(); + } + + protected void addScaleData(ScaleData scaleData) { + callbackBtHandler.obtainMessage(BT_RETRIEVE_SCALE_DATA.ordinal(), scaleData).sendToTarget(); + } + + abstract public String deviceName(); + abstract public String defaultDeviceName(); + abstract public void startSearching(String deviceName); abstract public void stopSearching(); + + public boolean initSupported() { + return true; + } + + public boolean transferSupported() { + return true; + } + + public boolean historySupported() { + return true; + } + + public boolean isBLE() { + return true; + } } diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCustomOpenScale.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCustomOpenScale.java index 2a13ca05..4081a730 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCustomOpenScale.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCustomOpenScale.java @@ -17,6 +17,7 @@ package com.health.openscale.core.bluetooth; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; +import android.content.Context; import android.util.Log; import com.health.openscale.core.datatypes.ScaleData; @@ -29,6 +30,11 @@ import java.text.SimpleDateFormat; import java.util.Set; import java.util.UUID; +import static com.health.openscale.core.bluetooth.BluetoothCommunication.BT_STATUS_CODE.BT_CONNECTION_ESTABLISHED; +import static com.health.openscale.core.bluetooth.BluetoothCommunication.BT_STATUS_CODE.BT_CONNECTION_LOST; +import static com.health.openscale.core.bluetooth.BluetoothCommunication.BT_STATUS_CODE.BT_NO_DEVICE_FOUND; +import static com.health.openscale.core.bluetooth.BluetoothCommunication.BT_STATUS_CODE.BT_UNEXPECTED_ERROR; + public class BluetoothCustomOpenScale extends BluetoothCommunication { private final UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); // Standard SerialPortService ID @@ -37,11 +43,29 @@ public class BluetoothCustomOpenScale extends BluetoothCommunication { private BluetoothConnectedThread btConnectThread = null; + public BluetoothCustomOpenScale(Context context) { + super(context); + } + @Override + public String deviceName() { + return "Custom Open Scale"; + } + + @Override + public String defaultDeviceName() { + return "openScale_MCU"; + } + + public boolean isBLE() { + return false; + } + + @Override public void startSearching(String deviceName) { if (btAdapter == null) { - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_NO_DEVICE_FOUND).sendToTarget(); + setBtStatus(BT_NO_DEVICE_FOUND); return; } @@ -56,7 +80,7 @@ public class BluetoothCustomOpenScale extends BluetoothCommunication { // Get a BluetoothSocket to connect with the given BluetoothDevice btSocket = btDevice.createRfcommSocketToServiceRecord(uuid); } catch (IOException e) { - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_UNEXPECTED_ERROR, "Can't get a bluetooth socket").sendToTarget(); + setBtStatus(BT_UNEXPECTED_ERROR, "Can't get a bluetooth socket"); } Thread socketThread = new Thread() { @@ -69,7 +93,7 @@ public class BluetoothCustomOpenScale extends BluetoothCommunication { btSocket.connect(); // Bluetooth connection was successful - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_CONNECTION_ESTABLISHED).sendToTarget(); + setBtStatus(BT_CONNECTION_ESTABLISHED); btConnectThread = new BluetoothConnectedThread(); btConnectThread.start(); @@ -77,7 +101,7 @@ public class BluetoothCustomOpenScale extends BluetoothCommunication { } catch (IOException connectException) { // Unable to connect; close the socket and get out stopSearching(); - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_NO_DEVICE_FOUND).sendToTarget(); + setBtStatus(BT_NO_DEVICE_FOUND); } } }; @@ -87,7 +111,7 @@ public class BluetoothCustomOpenScale extends BluetoothCommunication { } } - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_NO_DEVICE_FOUND).sendToTarget(); + setBtStatus(BT_NO_DEVICE_FOUND); } @Override @@ -98,7 +122,7 @@ public class BluetoothCustomOpenScale extends BluetoothCommunication { btSocket.close(); btSocket = null; } catch (IOException closeException) { - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_UNEXPECTED_ERROR, "Can't close bluetooth socket").sendToTarget(); + setBtStatus(BT_UNEXPECTED_ERROR, "Can't close bluetooth socket"); } } } @@ -140,7 +164,7 @@ public class BluetoothCustomOpenScale extends BluetoothCommunication { btInStream = btSocket.getInputStream(); btOutStream = btSocket.getOutputStream(); } catch (IOException e) { - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_UNEXPECTED_ERROR, "Can't get bluetooth input or output stream " + e.getMessage()).sendToTarget(); + setBtStatus(BT_UNEXPECTED_ERROR, "Can't get bluetooth input or output stream " + e.getMessage()); } } @@ -159,7 +183,7 @@ public class BluetoothCustomOpenScale extends BluetoothCommunication { ScaleData scaleData = parseBtString(btLine.toString()); if (scaleData != null) { - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_RETRIEVE_SCALE_DATA, scaleData).sendToTarget(); + addScaleData(scaleData); } btLine.setLength(0); @@ -167,7 +191,7 @@ public class BluetoothCustomOpenScale extends BluetoothCommunication { } catch (IOException e) { cancel(); - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_CONNECTION_LOST).sendToTarget(); + setBtStatus(BT_CONNECTION_LOST); } } } @@ -177,7 +201,7 @@ public class BluetoothCustomOpenScale extends BluetoothCommunication { btString = btString.substring(0, btString.length() - 1); // delete newline '\n' of the string if (btString.charAt(0) != '$' && btString.charAt(2) != '$') { - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_UNEXPECTED_ERROR, "Parse error of bluetooth string. String has not a valid format").sendToTarget(); + setBtStatus(BT_UNEXPECTED_ERROR, "Parse error of bluetooth string. String has not a valid format"); } String btMsg = btString.substring(3, btString.length()); // message string @@ -224,16 +248,16 @@ public class BluetoothCustomOpenScale extends BluetoothCommunication { return scaleBtData; } else { - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_UNEXPECTED_ERROR, "Error calculated checksum (" + checksum + ") and received checksum (" + btChecksum + ") is different").sendToTarget(); + setBtStatus(BT_UNEXPECTED_ERROR, "Error calculated checksum (" + checksum + ") and received checksum (" + btChecksum + ") is different"); } } catch (ParseException e) { - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_UNEXPECTED_ERROR, "Error while decoding bluetooth date string (" + e.getMessage() + ")").sendToTarget(); + setBtStatus(BT_UNEXPECTED_ERROR, "Error while decoding bluetooth date string (" + e.getMessage() + ")"); } catch (NumberFormatException e) { - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_UNEXPECTED_ERROR, "Error while decoding a number of bluetooth string (" + e.getMessage() + ")").sendToTarget(); + setBtStatus(BT_UNEXPECTED_ERROR, "Error while decoding a number of bluetooth string (" + e.getMessage() + ")"); } break; default: - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_UNEXPECTED_ERROR, "Error unknown MCU command").sendToTarget(); + setBtStatus(BT_UNEXPECTED_ERROR, "Error unknown MCU command"); } return null; @@ -243,7 +267,7 @@ public class BluetoothCustomOpenScale extends BluetoothCommunication { try { btOutStream.write(bytes); } catch (IOException e) { - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_UNEXPECTED_ERROR, "Error while writing to bluetooth socket " + e.getMessage()).sendToTarget(); + setBtStatus(BT_UNEXPECTED_ERROR, "Error while writing to bluetooth socket " + e.getMessage()); } } diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMedisanaBS444.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMedisanaBS444.java index d065a242..077a8df1 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMedisanaBS444.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMedisanaBS444.java @@ -31,6 +31,10 @@ import com.health.openscale.core.datatypes.ScaleData; import java.util.Date; import java.util.UUID; +import static com.health.openscale.core.bluetooth.BluetoothCommunication.BT_STATUS_CODE.BT_CONNECTION_ESTABLISHED; +import static com.health.openscale.core.bluetooth.BluetoothCommunication.BT_STATUS_CODE.BT_CONNECTION_LOST; +import static com.health.openscale.core.bluetooth.BluetoothCommunication.BT_STATUS_CODE.BT_NO_DEVICE_FOUND; + public class BluetoothMedisanaBS444 extends BluetoothCommunication { private BluetoothGatt bluetoothGatt; private BluetoothAdapter.LeScanCallback scanCallback; @@ -45,17 +49,30 @@ public class BluetoothMedisanaBS444 extends BluetoothCommunication { private String btDeviceName; private Handler searchHandler; - private Context context; private ScaleData btScaleData; private int nextCmdState; public BluetoothMedisanaBS444(Context context) { - this.context = context; + super(context); searchHandler = new Handler(); btScaleData = new ScaleData(); scanCallback = null; } + @Override + public String deviceName() { + return "Medisana BS444"; + } + + @Override + public String defaultDeviceName() { + return "Medisana BS444"; + } + + public boolean initSupported() { + return false; + } + @Override public void startSearching(String deviceName) { btDeviceName = deviceName; @@ -82,7 +99,7 @@ public class BluetoothMedisanaBS444 extends BluetoothCommunication { public void run() { btAdapter.stopLeScan(scanCallback); - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_NO_DEVICE_FOUND).sendToTarget(); + setBtStatus(BT_NO_DEVICE_FOUND); } }, 10000); @@ -105,10 +122,10 @@ public class BluetoothMedisanaBS444 extends BluetoothCommunication { @Override public void onConnectionStateChange(final BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_CONNECTION_ESTABLISHED).sendToTarget(); + setBtStatus(BT_CONNECTION_ESTABLISHED); gatt.discoverServices(); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_CONNECTION_LOST).sendToTarget(); + setBtStatus(BT_CONNECTION_LOST); stopSearching(); } } @@ -204,7 +221,7 @@ public class BluetoothMedisanaBS444 extends BluetoothCommunication { if (characteristic.getUuid().equals(FEATURE_MEASUREMENT_CHARACTERISTIC)) { parseFeatureData(data); - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_RETRIEVE_SCALE_DATA, btScaleData).sendToTarget(); + addScaleData(btScaleData); } } }; diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMiScale.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMiScale.java index 81f1347c..f00a7fe1 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMiScale.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMiScale.java @@ -39,6 +39,12 @@ import java.util.Date; import java.util.Random; import java.util.UUID; +import static com.health.openscale.core.bluetooth.BluetoothCommunication.BT_STATUS_CODE.BT_CONNECTION_ESTABLISHED; +import static com.health.openscale.core.bluetooth.BluetoothCommunication.BT_STATUS_CODE.BT_CONNECTION_LOST; +import static com.health.openscale.core.bluetooth.BluetoothCommunication.BT_STATUS_CODE.BT_INIT_PROCESS; +import static com.health.openscale.core.bluetooth.BluetoothCommunication.BT_STATUS_CODE.BT_NO_DEVICE_FOUND; +import static com.health.openscale.core.bluetooth.BluetoothCommunication.BT_STATUS_CODE.BT_UNEXPECTED_ERROR; + public class BluetoothMiScale extends BluetoothCommunication { private BluetoothGatt bluetoothGatt; @@ -51,7 +57,6 @@ public class BluetoothMiScale extends BluetoothCommunication { private final UUID WEIGHT_MEASUREMENT_CONFIG = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"); private Handler searchHandler; - private Context context; private String btDeviceName; private int nextCmdState; private int nextInitState; @@ -59,12 +64,23 @@ public class BluetoothMiScale extends BluetoothCommunication { private boolean initProcessOn; private boolean clearUpProcessOn; - public BluetoothMiScale(Context con) { + public BluetoothMiScale(Context context) { + super(context); + searchHandler = new Handler(); - context = con; scanCallback = null; } + @Override + public String deviceName() { + return "Xiaomi Mi Scale"; + } + + @Override + public String defaultDeviceName() { + return "MI_SCALE"; + } + @Override public void startSearching(String deviceName) { btDeviceName = deviceName; @@ -98,7 +114,7 @@ public class BluetoothMiScale extends BluetoothCommunication { public void run() { btAdapter.stopLeScan(scanCallback); - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_NO_DEVICE_FOUND).sendToTarget(); + setBtStatus(BT_NO_DEVICE_FOUND); } }, 10000); @@ -164,13 +180,13 @@ public class BluetoothMiScale extends BluetoothCommunication { scaleBtData.setWeight(weight); scaleBtData.setDateTime(date_time); - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_RETRIEVE_SCALE_DATA, scaleBtData).sendToTarget(); + addScaleData(scaleBtData); } else { Log.e("BluetoothMiScale", "Invalid Mi scale weight year " + year); } } } catch (ParseException e) { - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_UNEXPECTED_ERROR, "Error while decoding bluetooth date string (" + e.getMessage() + ")").sendToTarget(); + setBtStatus(BT_UNEXPECTED_ERROR, "Error while decoding bluetooth date string (" + e.getMessage() + ")"); } } @@ -231,11 +247,11 @@ public class BluetoothMiScale extends BluetoothCommunication { public void onConnectionStateChange(final BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { Log.d("BluetoothMiScale", "Connection established"); - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_CONNECTION_ESTABLISHED).sendToTarget(); + setBtStatus(BT_CONNECTION_ESTABLISHED); gatt.discoverServices(); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { Log.d("BluetoothMiScale", "Connection lost"); - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_CONNECTION_LOST).sendToTarget(); + setBtStatus(BT_CONNECTION_LOST); stopSearching(); } } @@ -376,7 +392,7 @@ public class BluetoothMiScale extends BluetoothCommunication { switch (nextInitState) { case 0: - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_INIT_PROCESS).sendToTarget(); + setBtStatus(BT_INIT_PROCESS); // set current time characteristic = gatt.getService(WEIGHT_MEASUREMENT_SERVICE) diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothSanitasSbf70.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothSanitasSbf70.java index 8cc98200..b44d87c8 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothSanitasSbf70.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothSanitasSbf70.java @@ -33,6 +33,11 @@ import java.util.LinkedList; import java.util.Queue; import java.util.UUID; +import static com.health.openscale.core.bluetooth.BluetoothCommunication.BT_STATUS_CODE.BT_CONNECTION_ESTABLISHED; +import static com.health.openscale.core.bluetooth.BluetoothCommunication.BT_STATUS_CODE.BT_CONNECTION_LOST; +import static com.health.openscale.core.bluetooth.BluetoothCommunication.BT_STATUS_CODE.BT_NO_DEVICE_FOUND; +import static com.health.openscale.core.bluetooth.BluetoothCommunication.BT_STATUS_CODE.BT_UNEXPECTED_ERROR; + public class BluetoothSanitasSbf70 extends BluetoothCommunication { public final static String TAG = "BluetoothSanitasSbf70"; @@ -94,7 +99,6 @@ public class BluetoothSanitasSbf70 extends BluetoothCommunication { private static final UUID CUSTOM_CHARACTERISTIC_IMG_BLOCK = // write-only, notify UUID.fromString("F000FFC2-0451-4000-8000-000000000000"); - private Context context; private BluetoothAdapter.LeScanCallback scanCallback = null; // default name is usually "SANITAS SBF70" private String btDeviceName = null; @@ -104,11 +108,24 @@ public class BluetoothSanitasSbf70 extends BluetoothCommunication { private BluetoothGatt bluetoothGatt; public BluetoothSanitasSbf70(Context context) { - super(); - this.context = context; + super(context); searchHandler = new Handler(); } + @Override + public String deviceName() { + return "Sanitas SBF70"; + } + + @Override + public String defaultDeviceName() { + return "SANITAS SBF70"; + } + + public boolean initSupported() { + return false; + } + private static final String HEX_DIGITS = "0123456789ABCDEF"; /** @@ -164,11 +181,11 @@ public class BluetoothSanitasSbf70 extends BluetoothCommunication { Log.d(TAG, "onConnectionStatechange(" + status + ", " + newState + ")"); if (newState == BluetoothProfile.STATE_CONNECTED) { Log.d(TAG, "Connection established"); - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_CONNECTION_ESTABLISHED).sendToTarget(); + setBtStatus(BT_CONNECTION_ESTABLISHED); gatt.discoverServices(); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { Log.d(TAG, "Connection lost"); - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_CONNECTION_LOST).sendToTarget(); + setBtStatus(BT_CONNECTION_LOST); stopSearching(); } } @@ -329,9 +346,7 @@ public class BluetoothSanitasSbf70 extends BluetoothCommunication { Log.i(TAG, "Got muscle: " + muscle + "%"); scaleBtData.setMuscle(muscle); - callbackBtHandler.obtainMessage( - BluetoothCommunication.BT_RETRIEVE_SCALE_DATA, scaleBtData - ).sendToTarget(); + addScaleData(scaleBtData); Log.d(TAG, "ACK Extra data (end)"); msgQueue.add(new byte[] { @@ -359,8 +374,7 @@ public class BluetoothSanitasSbf70 extends BluetoothCommunication { } else { Log.w(TAG, "Unidentified notification !"); - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_UNEXPECTED_ERROR, - "Error while decoding bluetooth value").sendToTarget(); + setBtStatus(BT_UNEXPECTED_ERROR, "Error while decoding bluetooth value"); return; } } @@ -407,7 +421,7 @@ public class BluetoothSanitasSbf70 extends BluetoothCommunication { public void run() { btAdapter.stopLeScan(scanCallback); - callbackBtHandler.obtainMessage(BluetoothCommunication.BT_NO_DEVICE_FOUND).sendToTarget(); + setBtStatus(BT_NO_DEVICE_FOUND); } }, 10000); diff --git a/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java b/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java index a14e5e8b..37c0f0a0 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java @@ -39,8 +39,8 @@ import android.view.MenuItem; import android.widget.Toast; import com.health.openscale.R; -import com.health.openscale.core.bluetooth.BluetoothCommunication; import com.health.openscale.core.OpenScale; +import com.health.openscale.core.bluetooth.BluetoothCommunication; import com.health.openscale.core.datatypes.ScaleData; import com.health.openscale.gui.activities.SettingsActivity; import com.health.openscale.gui.activities.UserSettingsActivity; @@ -184,10 +184,10 @@ public class MainActivity extends ActionBarActivity implements SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); String deviceName = prefs.getString("btDeviceName", "MI_SCALE"); - String deviceType = prefs.getString("btDeviceTypes", "0"); + int deviceType = Integer.parseInt(prefs.getString("btDeviceTypes", "0")); // Check if Bluetooth 4.x is available - if (Integer.parseInt(deviceType) != BluetoothCommunication.BT_OPEN_SCALE) { + if (BluetoothCommunication.getBtDevice(getApplicationContext(), deviceType).isBLE()) { if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { setBluetoothStatusIcon(R.drawable.bluetooth_disabled); Toast.makeText(getApplicationContext(), "Bluetooth 4.x " + getResources().getString(R.string.info_is_not_available), Toast.LENGTH_SHORT).show(); @@ -199,15 +199,17 @@ public class MainActivity extends ActionBarActivity implements setBluetoothStatusIcon(R.drawable.bluetooth_searching); OpenScale.getInstance(getApplicationContext()).stopSearchingForBluetooth(); - OpenScale.getInstance(getApplicationContext()).startSearchingForBluetooth(Integer.parseInt(deviceType), deviceName, callbackBtHandler); + OpenScale.getInstance(getApplicationContext()).startSearchingForBluetooth(deviceType, deviceName, callbackBtHandler); } private final Handler callbackBtHandler = new Handler() { @Override public void handleMessage(Message msg) { - switch (msg.what) { - case BluetoothCommunication.BT_RETRIEVE_SCALE_DATA: + BluetoothCommunication.BT_STATUS_CODE btStatusCode = BluetoothCommunication.BT_STATUS_CODE.values()[msg.what]; + + switch (btStatusCode) { + case BT_RETRIEVE_SCALE_DATA: setBluetoothStatusIcon(R.drawable.bluetooth_connection_success); ScaleData scaleBtData = (ScaleData) msg.obj; @@ -217,27 +219,27 @@ public class MainActivity extends ActionBarActivity implements Toast.makeText(getApplicationContext(), getResources().getString(R.string.info_no_selected_user), Toast.LENGTH_SHORT).show(); } break; - case BluetoothCommunication.BT_INIT_PROCESS: + case BT_INIT_PROCESS: setBluetoothStatusIcon(R.drawable.bluetooth_connection_success); Toast.makeText(getApplicationContext(), getResources().getString(R.string.info_bluetooth_init), Toast.LENGTH_SHORT).show(); Log.d("OpenScale", "Bluetooth initializing"); break; - case BluetoothCommunication.BT_CONNECTION_LOST: + case BT_CONNECTION_LOST: setBluetoothStatusIcon(R.drawable.bluetooth_connection_lost); Toast.makeText(getApplicationContext(), getResources().getString(R.string.info_bluetooth_connection_lost), Toast.LENGTH_SHORT).show(); Log.d("OpenScale", "Bluetooth connection lost"); break; - case BluetoothCommunication.BT_NO_DEVICE_FOUND: + case BT_NO_DEVICE_FOUND: setBluetoothStatusIcon(R.drawable.bluetooth_connection_lost); Toast.makeText(getApplicationContext(), getResources().getString(R.string.info_bluetooth_no_device), Toast.LENGTH_SHORT).show(); Log.d("OpenScale", "No Bluetooth device found"); break; - case BluetoothCommunication.BT_CONNECTION_ESTABLISHED: + case BT_CONNECTION_ESTABLISHED: setBluetoothStatusIcon(R.drawable.bluetooth_connection_success); Toast.makeText(getApplicationContext(), getResources().getString(R.string.info_bluetooth_connection_successful), Toast.LENGTH_SHORT).show(); Log.d("OpenScale", "Bluetooth connection successful established"); break; - case BluetoothCommunication.BT_UNEXPECTED_ERROR: + case BT_UNEXPECTED_ERROR: setBluetoothStatusIcon(R.drawable.bluetooth_connection_lost); Toast.makeText(getApplicationContext(), getResources().getString(R.string.info_bluetooth_connection_error) + ": " + msg.obj, Toast.LENGTH_SHORT).show(); Log.e("OpenScale", "Bluetooth unexpected error: " + msg.obj); diff --git a/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothPreferences.java b/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothPreferences.java index 11103e6a..f883a14d 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothPreferences.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothPreferences.java @@ -27,38 +27,37 @@ import android.preference.PreferenceGroup; import android.text.Html; import com.health.openscale.R; +import com.health.openscale.core.bluetooth.BluetoothCommunication; import java.util.ArrayList; import java.util.List; import java.util.Set; -public class BluetoothPreferences extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener { +import static com.health.openscale.core.bluetooth.BluetoothCommunication.getBtDevice; - public static final String PREFERENCE_KEY_BLUETOOTH_SMARTUSERASSIGN = "smartUserAssign"; +public class BluetoothPreferences extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener { + private static final String PREFERENCE_KEY_BLUETOOTH_DEVICE_TYPE = "btDeviceTypes"; + private static final String PREFERENCE_KEY_BLUETOOTH_DEVICE_NAME = "btDeviceName"; + private static final String PREFERENCE_KEY_BLUETOOTH_SMARTUSERASSIGN = "smartUserAssign"; private static final String PREFERENCE_KEY_BLUETOOTH_IGNOREOUTOFRANGE = "ignoreOutOfRange"; + private ListPreference deviceTypes; + private EditTextPreference deviceName; private CheckBoxPreference smartAssignEnable; private CheckBoxPreference ignoreOutOfRangeEnable; - private String[] btDeviceSupportInit; - private String[] btDeviceSupportDataTransfer; - private String[] btDeviceSupportDataHistory; - private String[] btDeviceDefaultName; - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.bluetooth_preferences); + deviceTypes = (ListPreference)findPreference(PREFERENCE_KEY_BLUETOOTH_DEVICE_TYPE); + deviceName = (EditTextPreference)findPreference(PREFERENCE_KEY_BLUETOOTH_DEVICE_NAME); smartAssignEnable = (CheckBoxPreference) findPreference(PREFERENCE_KEY_BLUETOOTH_SMARTUSERASSIGN); ignoreOutOfRangeEnable = (CheckBoxPreference) findPreference(PREFERENCE_KEY_BLUETOOTH_IGNOREOUTOFRANGE); - btDeviceSupportInit = getResources().getStringArray(R.array.bt_device_support_initializing); - btDeviceSupportDataTransfer = getResources().getStringArray(R.array.bt_device_support_data_transfer); - btDeviceSupportDataHistory = getResources().getStringArray(R.array.bt_device_support_data_history); - btDeviceDefaultName = getResources().getStringArray(R.array.bt_device_default_name); - + updateBluetoothPreferences(); initSummary(getPreferenceScreen()); } @@ -73,6 +72,29 @@ public class BluetoothPreferences extends PreferenceFragment implements SharedPr } } + public void updateBluetoothPreferences() { + int i = 0; + + ArrayList btEntries = new ArrayList(); + ArrayList btEntryValues = new ArrayList(); + + while (true) { + BluetoothCommunication btCom = getBtDevice(getActivity().getApplicationContext(), i); + + if (btCom == null) { + break; + } + + btEntries.add(btCom.deviceName()); + btEntryValues.add(String.valueOf(i)); + + i++; + } + + deviceTypes.setEntries(btEntries.toArray(new CharSequence[btEntries.size()])); + deviceTypes.setEntryValues(btEntryValues.toArray(new CharSequence[btEntryValues.size()])); + } + @Override public void onResume() { super.onResume(); @@ -103,17 +125,35 @@ public class BluetoothPreferences extends PreferenceFragment implements SharedPr int i = Integer.parseInt(listPref.getValue()); - p.setSummary(Html.fromHtml(listPref.getEntry() + "
" + - getResources().getString(R.string.label_bt_device_support) + ":" + "
" + - getResources().getString(R.string.label_bt_device_initialization) + ": " + btDeviceSupportInit[i] + "
" + - getResources().getString(R.string.label_bt_device_data_transfer) + ": " + btDeviceSupportDataTransfer[i] + "
" + - getResources().getString(R.string.label_bt_device_data_history) + ": " + btDeviceSupportDataHistory[i] - )); + BluetoothCommunication btCom = BluetoothCommunication.getBtDevice(getActivity().getApplicationContext(), i); - getPreferenceManager().getDefaultSharedPreferences(getActivity().getApplicationContext()).edit().putString("btDeviceName", btDeviceDefaultName[i]).commit(); - EditTextPreference prefDeviceName = (EditTextPreference)findPreference("btDeviceName"); - prefDeviceName.setSummary(btDeviceDefaultName[i]); - prefDeviceName.setText(btDeviceDefaultName[i]); + String summary = new String(); + + summary += listPref.getEntry() + "
" + + getResources().getString(R.string.label_bt_device_support) + ":" + "
"; + + if (btCom.initSupported()) { + summary += getResources().getString(R.string.label_bt_device_initialization) + ": " + getResources().getString(R.string.label_yes)+ "
"; + } else { + summary += getResources().getString(R.string.label_bt_device_initialization) + ": " + getResources().getString(R.string.label_no)+ "
"; + } + + if (btCom.transferSupported()) { + summary += getResources().getString(R.string.label_bt_device_data_transfer) + ": " + getResources().getString(R.string.label_yes)+ "
"; + } else { + summary += getResources().getString(R.string.label_bt_device_data_transfer) + ": " + getResources().getString(R.string.label_no)+ "
"; + } + + if (btCom.historySupported()) { + summary += getResources().getString(R.string.label_bt_device_data_history) + ": " + getResources().getString(R.string.label_yes); + } else { + summary += getResources().getString(R.string.label_bt_device_data_history) + ": " + getResources().getString(R.string.label_no); + } + + p.setSummary(Html.fromHtml(summary)); + + deviceName.setSummary(btCom.defaultDeviceName()); + deviceName.setText(btCom.defaultDeviceName()); } if (p instanceof EditTextPreference) { diff --git a/android_app/app/src/main/res/values/type_btdevices.xml b/android_app/app/src/main/res/values/type_btdevices.xml deleted file mode 100644 index f2530968..00000000 --- a/android_app/app/src/main/res/values/type_btdevices.xml +++ /dev/null @@ -1,45 +0,0 @@ - - - - Xiaomi Mi Scale v1 - openScale Custom Scale - Sanitas SBF-70# - Medisana BS444 Connect - - - - 0 - 1 - 2 - 3 - - - - MI_SCALE - openScale_MCU - SANITAS SBF70 - Medisana BS444 - - - - @string/label_yes - @string/label_yes - @string/label_no - @string/label_no - - - - @string/label_yes - @string/label_yes - @string/label_yes - @string/label_yes - - - - @string/label_yes - @string/label_yes - @string/label_no - @string/label_yes - - - \ No newline at end of file diff --git a/android_app/app/src/main/res/xml/bluetooth_preferences.xml b/android_app/app/src/main/res/xml/bluetooth_preferences.xml index f6281199..3878cbd6 100644 --- a/android_app/app/src/main/res/xml/bluetooth_preferences.xml +++ b/android_app/app/src/main/res/xml/bluetooth_preferences.xml @@ -1,7 +1,7 @@ - - + +