Keeping Temp value in the service

This commit is contained in:
Aleksander Nowakowski
2018-06-25 11:53:55 +02:00
parent fc3207ee09
commit aad8536f0f
2 changed files with 38 additions and 38 deletions

View File

@@ -51,11 +51,9 @@ public class HTSActivity extends BleProfileServiceReadyActivity<HTSService.HTSBi
@SuppressWarnings("unused")
private final String TAG = "HTSActivity";
private static final String VALUE = "value";
private TextView mTempValue;
private TextView mUnit;
private TextView mBatteryLevelView;
private Float mValueC;
@Override
protected void onCreateView(final Bundle savedInstanceState) {
@@ -63,22 +61,9 @@ public class HTSActivity extends BleProfileServiceReadyActivity<HTSService.HTSBi
setGUI();
}
@Override
protected void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);
if (mValueC != null)
outState.putDouble(VALUE, mValueC);
}
@Override
protected void onInitialize(final Bundle savedInstanceState) {
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, makeIntentFilter());
if (savedInstanceState != null) {
if (savedInstanceState.containsKey(VALUE))
mValueC = savedInstanceState.getFloat(VALUE);
}
}
@Override
@@ -91,9 +76,6 @@ public class HTSActivity extends BleProfileServiceReadyActivity<HTSService.HTSBi
mTempValue = findViewById(R.id.text_hts_value);
mUnit = findViewById(R.id.text_hts_unit);
mBatteryLevelView = findViewById(R.id.battery);
if (mValueC != null)
mTempValue.setText(String.valueOf(mValueC));
}
@Override
@@ -104,7 +86,6 @@ public class HTSActivity extends BleProfileServiceReadyActivity<HTSService.HTSBi
@Override
protected void setDefaultUI() {
mValueC = null;
mTempValue.setText(R.string.not_available_value);
mBatteryLevelView.setText(R.string.not_available);
@@ -126,13 +107,11 @@ public class HTSActivity extends BleProfileServiceReadyActivity<HTSService.HTSBi
mUnit.setText(R.string.hts_unit_kelvin);
break;
}
if (mValueC != null)
onTemperatureMeasurementReceived(mValueC);
}
@Override
protected void onServiceBound(final HTSService.HTSBinder binder) {
// not used
onTemperatureMeasurementReceived(binder.getTemperature());
}
@Override
@@ -193,22 +172,26 @@ public class HTSActivity extends BleProfileServiceReadyActivity<HTSService.HTSBi
mBatteryLevelView.setText(R.string.not_available);
}
private void onTemperatureMeasurementReceived(float value) {
mValueC = value;
private void onTemperatureMeasurementReceived(Float value) {
if (value != null) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final int unit = Integer.parseInt(preferences.getString(SettingsFragment.SETTINGS_UNIT, String.valueOf(SettingsFragment.SETTINGS_UNIT_DEFAULT)));
final int unit = Integer.parseInt(preferences.getString(SettingsFragment.SETTINGS_UNIT,
String.valueOf(SettingsFragment.SETTINGS_UNIT_DEFAULT)));
switch (unit) {
case SettingsFragment.SETTINGS_UNIT_F:
value = value * 1.8f + 32f;
break;
case SettingsFragment.SETTINGS_UNIT_K:
value += 273.15;
value += 273.15f;
break;
case SettingsFragment.SETTINGS_UNIT_C:
break;
}
mTempValue.setText(getString(R.string.hts_value, value));
} else {
mTempValue.setText(R.string.not_available_value);
}
}
public void onBatteryLevelChanged(final int value) {

View File

@@ -58,6 +58,8 @@ public class HTSService extends BleProfileService implements HTSManagerCallbacks
private final static int NOTIFICATION_ID = 267;
private final static int OPEN_ACTIVITY_REQ = 0;
private final static int DISCONNECT_REQ = 1;
/** The last received temperature value in Celsius degrees. */
private Float mTemp;
@SuppressWarnings("unused")
private HTSManager mManager;
@@ -68,7 +70,14 @@ public class HTSService extends BleProfileService implements HTSManagerCallbacks
* This local binder is an interface for the bonded activity to operate with the HTS sensor
*/
class HTSBinder extends LocalBinder {
// empty
/**
* Returns the last received temperature value.
*
* @return Temperature value in Celsius.
*/
Float getTemperature() {
return mTemp;
}
}
@Override
@@ -111,14 +120,22 @@ public class HTSService extends BleProfileService implements HTSManagerCallbacks
createNotification(R.string.hts_notification_connected_message, 0);
}
@Override
public void onDeviceDisconnected(final BluetoothDevice device) {
super.onDeviceDisconnected(device);
mTemp = null;
}
@Override
public void onTemperatureMeasurementReceived(@NonNull final BluetoothDevice device,
final float temperature, final int unit,
@Nullable final Calendar calendar,
@Nullable final Integer type) {
mTemp = TemperatureMeasurementCallback.toCelsius(temperature, unit);
final Intent broadcast = new Intent(BROADCAST_HTS_MEASUREMENT);
broadcast.putExtra(EXTRA_DEVICE, getBluetoothDevice());
broadcast.putExtra(EXTRA_TEMPERATURE, TemperatureMeasurementCallback.toCelsius(temperature, unit));
broadcast.putExtra(EXTRA_TEMPERATURE, mTemp);
// ignore the rest
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);