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") @SuppressWarnings("unused")
private final String TAG = "HTSActivity"; private final String TAG = "HTSActivity";
private static final String VALUE = "value";
private TextView mTempValue; private TextView mTempValue;
private TextView mUnit; private TextView mUnit;
private TextView mBatteryLevelView; private TextView mBatteryLevelView;
private Float mValueC;
@Override @Override
protected void onCreateView(final Bundle savedInstanceState) { protected void onCreateView(final Bundle savedInstanceState) {
@@ -63,22 +61,9 @@ public class HTSActivity extends BleProfileServiceReadyActivity<HTSService.HTSBi
setGUI(); setGUI();
} }
@Override
protected void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);
if (mValueC != null)
outState.putDouble(VALUE, mValueC);
}
@Override @Override
protected void onInitialize(final Bundle savedInstanceState) { protected void onInitialize(final Bundle savedInstanceState) {
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, makeIntentFilter()); LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, makeIntentFilter());
if (savedInstanceState != null) {
if (savedInstanceState.containsKey(VALUE))
mValueC = savedInstanceState.getFloat(VALUE);
}
} }
@Override @Override
@@ -91,9 +76,6 @@ public class HTSActivity extends BleProfileServiceReadyActivity<HTSService.HTSBi
mTempValue = findViewById(R.id.text_hts_value); mTempValue = findViewById(R.id.text_hts_value);
mUnit = findViewById(R.id.text_hts_unit); mUnit = findViewById(R.id.text_hts_unit);
mBatteryLevelView = findViewById(R.id.battery); mBatteryLevelView = findViewById(R.id.battery);
if (mValueC != null)
mTempValue.setText(String.valueOf(mValueC));
} }
@Override @Override
@@ -104,7 +86,6 @@ public class HTSActivity extends BleProfileServiceReadyActivity<HTSService.HTSBi
@Override @Override
protected void setDefaultUI() { protected void setDefaultUI() {
mValueC = null;
mTempValue.setText(R.string.not_available_value); mTempValue.setText(R.string.not_available_value);
mBatteryLevelView.setText(R.string.not_available); mBatteryLevelView.setText(R.string.not_available);
@@ -126,13 +107,11 @@ public class HTSActivity extends BleProfileServiceReadyActivity<HTSService.HTSBi
mUnit.setText(R.string.hts_unit_kelvin); mUnit.setText(R.string.hts_unit_kelvin);
break; break;
} }
if (mValueC != null)
onTemperatureMeasurementReceived(mValueC);
} }
@Override @Override
protected void onServiceBound(final HTSService.HTSBinder binder) { protected void onServiceBound(final HTSService.HTSBinder binder) {
// not used onTemperatureMeasurementReceived(binder.getTemperature());
} }
@Override @Override
@@ -193,22 +172,26 @@ public class HTSActivity extends BleProfileServiceReadyActivity<HTSService.HTSBi
mBatteryLevelView.setText(R.string.not_available); mBatteryLevelView.setText(R.string.not_available);
} }
private void onTemperatureMeasurementReceived(float value) { private void onTemperatureMeasurementReceived(Float value) {
mValueC = value; if (value != null) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 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) { switch (unit) {
case SettingsFragment.SETTINGS_UNIT_F: case SettingsFragment.SETTINGS_UNIT_F:
value = value * 1.8f + 32f; value = value * 1.8f + 32f;
break; break;
case SettingsFragment.SETTINGS_UNIT_K: case SettingsFragment.SETTINGS_UNIT_K:
value += 273.15; value += 273.15f;
break; break;
case SettingsFragment.SETTINGS_UNIT_C: case SettingsFragment.SETTINGS_UNIT_C:
break; break;
}
mTempValue.setText(getString(R.string.hts_value, value));
} else {
mTempValue.setText(R.string.not_available_value);
} }
mTempValue.setText(getString(R.string.hts_value, value));
} }
public void onBatteryLevelChanged(final int 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 NOTIFICATION_ID = 267;
private final static int OPEN_ACTIVITY_REQ = 0; private final static int OPEN_ACTIVITY_REQ = 0;
private final static int DISCONNECT_REQ = 1; private final static int DISCONNECT_REQ = 1;
/** The last received temperature value in Celsius degrees. */
private Float mTemp;
@SuppressWarnings("unused") @SuppressWarnings("unused")
private HTSManager mManager; 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 * This local binder is an interface for the bonded activity to operate with the HTS sensor
*/ */
class HTSBinder extends LocalBinder { class HTSBinder extends LocalBinder {
// empty /**
* Returns the last received temperature value.
*
* @return Temperature value in Celsius.
*/
Float getTemperature() {
return mTemp;
}
} }
@Override @Override
@@ -111,14 +120,22 @@ public class HTSService extends BleProfileService implements HTSManagerCallbacks
createNotification(R.string.hts_notification_connected_message, 0); createNotification(R.string.hts_notification_connected_message, 0);
} }
@Override
public void onDeviceDisconnected(final BluetoothDevice device) {
super.onDeviceDisconnected(device);
mTemp = null;
}
@Override @Override
public void onTemperatureMeasurementReceived(@NonNull final BluetoothDevice device, public void onTemperatureMeasurementReceived(@NonNull final BluetoothDevice device,
final float temperature, final int unit, final float temperature, final int unit,
@Nullable final Calendar calendar, @Nullable final Calendar calendar,
@Nullable final Integer type) { @Nullable final Integer type) {
mTemp = TemperatureMeasurementCallback.toCelsius(temperature, unit);
final Intent broadcast = new Intent(BROADCAST_HTS_MEASUREMENT); final Intent broadcast = new Intent(BROADCAST_HTS_MEASUREMENT);
broadcast.putExtra(EXTRA_DEVICE, getBluetoothDevice()); broadcast.putExtra(EXTRA_DEVICE, getBluetoothDevice());
broadcast.putExtra(EXTRA_TEMPERATURE, TemperatureMeasurementCallback.toCelsius(temperature, unit)); broadcast.putExtra(EXTRA_TEMPERATURE, mTemp);
// ignore the rest // ignore the rest
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast); LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);