From 9cfd8c8da3b354358a019094d995a71d4e3a3422 Mon Sep 17 00:00:00 2001 From: Martin Nowack Date: Fri, 4 Aug 2017 09:47:21 +0200 Subject: [PATCH] Add support to send arbitrary messages to openScale user. This can be used to send any string with an argument which will be string.format() replaced. --- .../core/bluetooth/BluetoothCommunication.java | 14 +++++++++++++- .../com/health/openscale/gui/MainActivity.java | 4 ++++ 2 files changed, 17 insertions(+), 1 deletion(-) 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 dfa8176f..92193bd3 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 @@ -35,7 +35,9 @@ import java.util.Queue; import java.util.UUID; public abstract class BluetoothCommunication { - 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 enum BT_STATUS_CODE {BT_RETRIEVE_SCALE_DATA, BT_INIT_PROCESS, BT_CONNECTION_ESTABLISHED, + BT_CONNECTION_LOST, BT_NO_DEVICE_FOUND, BT_UNEXPECTED_ERROR, BT_SCALE_MESSAGE + }; public enum BT_MACHINE_STATE {BT_INIT_STATE, BT_CMD_STATE, BT_CLEANUP_STATE} protected Context context; @@ -125,6 +127,16 @@ public abstract class BluetoothCommunication { callbackBtHandler.obtainMessage(BT_STATUS_CODE.BT_RETRIEVE_SCALE_DATA.ordinal(), scaleData).sendToTarget(); } + /** + * Send message to openScale user + * + * @param msg the string id to be send + * @param value the value to be used + */ + protected void sendMessage(int msg, Object value) { + callbackBtHandler.obtainMessage(BT_STATUS_CODE.BT_SCALE_MESSAGE.ordinal(), msg, 0, value).sendToTarget(); + } + /** * Is the Bluetooth initialized process supported. * 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 37c0f0a0..de79f1cd 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 @@ -244,6 +244,10 @@ public class MainActivity extends ActionBarActivity implements 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); break; + case BT_SCALE_MESSAGE: + String toastMessage = String.format(getResources().getString(msg.arg1), msg.obj); + Toast.makeText(getApplicationContext(), toastMessage, Toast.LENGTH_LONG).show(); + break; } } };