I'm writing an app that fetches data from a separate device, plots it on a GraphView graph, and I want to write that data to a file to keep a record of it for later viewing.
The problem is that GraphView limits the size of the number of points that are held in memory to protect from memory leaks, so I can't simply wait until the trial is complete to write to the file.
I'm learning Android as I go, and from what I understand, I create a thread to constantly fetch new data from the device, but from that point I'm not sure how to proceed. I thought I figured I could both plot the data, then write the data to a file in that same thread but I'm sure that'll back up the data fetching process and cause issues so can I split that into multiple threads (and if so, how would I structure it?)
Here's what I have right now:
Runnable mUpdater = new Runnable() {
@Override
public void run() {
if (isBtConnected) {
// Check if still in focus
if (!mRunning) return;
// Receive data and update screen/file
try {
receiveData(btSocket);
} catch (IOException e) {
Thread t = Thread.currentThread();
t.getUncaughtExceptionHandler().uncaughtException(t, e);
}
}
// Schedule next run
mHandler.postDelayed(this, 250); // <<-- SET TIME TO REFRESH HERE
}
};
public void receiveData(BluetoothSocket socket) throws IOException {
InputStream socketInputStream = socket.getInputStream();
byte[] buffer = new byte[256];
int bytes;
bytes = socketInputStream.read(buffer);
// Convert bytes to double
Double value = ByteBuffer.wrap(buffer, 0, bytes).getDouble();
// Get current time and append to graph as (time, data)
// Write the (time, data) point to a file
}