mardi 3 mars 2015

Saving sensor data from Android device and sending all data to server

I'm having a bit of trouble as I am somewhat new to Android development. I'm attempting to read in sensor data, temporarily save that data to a database, and do a bulk insert to a server I created so that I can visualize the data online.


I have my activity that is a SensorEventListener, and I have each event's values being written out to TextViews. I'm reading event data just fine. I assumed that sending data to a server too frequently could be a problem, so I decided to have the saving of data to the internal db for my Android app be initiated via the click of a menu button and the act of taking that data from the internal db and sending it to my server initiated with a different menu button.


My onSensorChanged(SensorEvent event) method looks like:



@Override
public void onSensorChanged(SensorEvent event) {
// Accuracy is handled in the onAccuracy Changed ( - 1 )
for (int i = 0; i < mTextViews.length - 1; i++) {
mTextViews[i].setText("Value " + i + ": " + event.values[i]);
}

if (mSensorEvents == null) mSensorEvents = new ArrayList<>();
if (mSensorEvents.size() < VALS_MAX) {
mSensorEvents.add(event);
} else {
mSensorManager.unregisterListener(this);
new SensorEventLoggerTask().execute(mSensorEvents);
}
}


As I said earlier, the saving and sending actions are initiated via menu buttons, and here is where I handle that:



@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_save_data) {
mSensorManager.unregisterListener(this);
new SensorEventLoggerTask().execute(mSensorEvents);
} else if (id == R.id.action_send_data) {
new SendSensorDataTask().execute();
}

return super.onOptionsItemSelected(item);
}


Those tasks look like this:



private class SensorEventLoggerTask extends AsyncTask<List<SensorEvent>, Void, Void> {

@Override
protected Void doInBackground(List<SensorEvent>... events) {
for (int i = 0; i < events.length; i++) {
SensorEvent event = events[0].get(i);
String value = Arrays.toString(event.values);
long timestamp = (new Date()).getTime() + (event.timestamp - System.nanoTime()) / 100000L;
ContentValues values = new ContentValues();
values.put(SensorDataHelper.COL_ACCURACY, event.accuracy);
values.put(SensorDataHelper.COL_NAME, mSensorName);
// TODO: get the email from the user
values.put(SensorDataHelper.COL_EMAIL, "jakehockey10@gmail.com");
values.put(SensorDataHelper.COL_TIMESTAMP, timestamp);
values.put(SensorDataHelper.COL_VALUE, value);
getActivity().getContentResolver().insert(SensorDataProvider.CONTENT_URI, values);
}
return null;
}
}


and...



private class SendSensorDataTask extends AsyncTask<String, String, String> {
private List<HashMap<String, String>> mData = null; // post data

/**
* constructor
*/
public SendSensorDataTask(){}

/**
* background
*/
@Override
protected String doInBackground(String... params) {

Cursor query = getActivity().getContentResolver().query(SensorDataProvider.CONTENT_URI, null, null, null, null);

return null;
}
}


The query method in my SensorDataProvider class just does this:



@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
Cursor cursor = getDatabase(false).rawQuery("select * from table", null);
return cursor;
}


As you can see, I don't have the post request happening yet, but that's because I'm not quite sure how to do it, although I've found some examples online. But I can't get the records I've added to my internal db out and if I can figure that out, my next step would be to create JSON objects out of those records and make a POST request.


From what you see, is there some advice you have for me? Am I approaching this the right way? Where am I screwing up? Please let me know if you need me to post any more snippets. I would really appreciate some guidance...


Aucun commentaire:

Enregistrer un commentaire