Commit 615f1a03 authored by Kristin Muterspaw's avatar Kristin Muterspaw
Browse files

Sensor values are now updated on the screen even if streaming or sample button...

Sensor values are now updated on the screen even if streaming or sample button isn't pressed. The Bluetooth Sensors are updated every 1 second. Writing to the database with different logging intervals works again.
parent 9e30bc8d
......@@ -402,7 +402,9 @@ public class SensorSampleActivity extends Activity {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
site = parent.getItemAtPosition(position).toString();
sampleService.onSiteChanged(site);
if (sampleService != null) {
sampleService.onSiteChanged(site);
}
}
@Override
......@@ -418,7 +420,9 @@ public class SensorSampleActivity extends Activity {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
sector = parent.getItemAtPosition(position).toString();
sampleService.onSectorChanged(sector);
if (sampleService != null) {
sampleService.onSectorChanged(sector);
}
}
@Override
......@@ -439,7 +443,9 @@ public class SensorSampleActivity extends Activity {
@Override
public void afterTextChanged(Editable s) {
spot = s.toString();
sampleService.onSpotChanged(spot);
if (sampleService != null) {
sampleService.onSpotChanged(spot);
}
}
});
}
......
......@@ -33,10 +33,10 @@ public class SensorSampleService extends Service implements SensorEventListener
private BluetoothLeService btService;
private String sensorType, response;
private boolean sampleOnce, registered = false, writingToDB = false;
private boolean sampleOnce, registered = false, streaming = false;
private long loggingInterval;
private Handler dbHandler, btHandler;
private Runnable dbRunner, btRunNames, builtinListRunner;
private Runnable dbRunner, btRunNames, btRunValues;
private BluetoothSensor bluetoothSensor;
private ArrayList<aSensor> sensorList;
public ReadingsDatabase db;
......@@ -83,6 +83,9 @@ public class SensorSampleService extends Service implements SensorEventListener
public void onServiceDisconnected(ComponentName name) {
btService = null;
dbHandler.removeCallbacks(dbRunner);
if (sensorType.equals("bluetooth")){
btHandler.removeCallbacks(btRunValues);
}
}
};
......@@ -125,7 +128,6 @@ public class SensorSampleService extends Service implements SensorEventListener
@Override
public void onCreate() {
btHandler = new Handler();
btRunNames = new Runnable() {
@Override
......@@ -133,19 +135,16 @@ public class SensorSampleService extends Service implements SensorEventListener
getSensorNames();
}
};
builtinList = new ArrayList<Sensor>();
dbHandler = new Handler();
dbRunner = new Runnable() {
btRunValues = new Runnable() {
@Override
public void run() {
getSensorValues();
dbHandler.postDelayed(this, loggingInterval);
btHandler.postDelayed(this, 2000);
}
};
builtinListRunner = new Runnable() {
dbHandler = new Handler();
dbRunner = new Runnable() {
@Override
public void run() {
writeToDB();
......@@ -154,8 +153,10 @@ public class SensorSampleService extends Service implements SensorEventListener
};
response = "";
loggingInterval = 1000;
sensorList = new ArrayList<aSensor>();
builtinList = new ArrayList<Sensor>();
db = ReadingsDatabase.getInstance(this);
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
......@@ -183,7 +184,7 @@ public class SensorSampleService extends Service implements SensorEventListener
}
}
public void setLocation(Location location){
public void setLocation(Location location) {
if (location != null) {
lastLongitude = location.getLongitude();
lastLatitude = location.getLatitude();
......@@ -209,26 +210,15 @@ public class SensorSampleService extends Service implements SensorEventListener
spot = intent.getExtras().getString(SPOT);
dbHandler = new Handler();
writingToDB = true;
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Sampling wake lock");
mWakeLock.acquire();
switch (sensorType){
case "bluetooth":
if (sampleOnce){
getSensorValues();
} else {
dbHandler.postDelayed(dbRunner, loggingInterval);
}
break;
case "built-in":
if (sampleOnce){
writeToDB();
} else {
dbHandler.postDelayed(builtinListRunner, loggingInterval);
}
break;
if (sampleOnce){
writeToDB();
} else {
dbHandler.postDelayed(dbRunner, loggingInterval);
streaming = true;
}
// START_STICKY means that the service continues running until something outside tells it
......@@ -246,10 +236,10 @@ public class SensorSampleService extends Service implements SensorEventListener
}
@Override
public IBinder onBind(Intent intent){ return mBinder; }
public IBinder onBind(Intent intent) { return mBinder; }
@Override
public boolean onUnbind(Intent intent){
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
......@@ -259,6 +249,8 @@ public class SensorSampleService extends Service implements SensorEventListener
unregisterReceiver(mGattUpdateReceiver);
btService.disconnect();
btHandler.removeCallbacksAndMessages(btRunNames);
btHandler.removeMessages(0);
btHandler.removeCallbacksAndMessages(btRunValues);
btService.close();
unbindService(btServiceConnection);
}
......@@ -308,7 +300,7 @@ public class SensorSampleService extends Service implements SensorEventListener
}
}
btHandler.removeCallbacksAndMessages(btRunNames);
getSensorValues();
btHandler.postDelayed(btRunValues, 1000);
}
response = "";
}
......@@ -333,9 +325,6 @@ public class SensorSampleService extends Service implements SensorEventListener
}
}
if (registered) {
if (writingToDB) {
writeToDB();
}
broadcastUpdate(ACTION_UPDATED_LIST);
} else {
registered = true;
......@@ -397,10 +386,12 @@ public class SensorSampleService extends Service implements SensorEventListener
site = newSite;
}
public void onLoggingTimeChanged(long newLoggingInterval){
dbHandler.removeCallbacksAndMessages(dbRunner);
public void onLoggingTimeChanged(long newLoggingInterval) {
loggingInterval = newLoggingInterval;
dbHandler.postDelayed(dbRunner, loggingInterval);
if (streaming) {
dbHandler.removeCallbacksAndMessages(dbRunner);
dbHandler.postDelayed(dbRunner, loggingInterval);
}
}
public aSensor getSensor(String sensorID, Sensor sensor) {
......@@ -444,7 +435,6 @@ public class SensorSampleService extends Service implements SensorEventListener
message = "Could not write to the database. Try again.";
}
stopSelf();
dbHandler.removeCallbacksAndMessages(dbRunner);
}
}
if (success) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment