0

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
}
ejams
  • 1
  • 1
  • "the number of points that are held in memory to protect from memory leaks" is an incorrect statement. You haven't run into OOM apparently. – Chisko Mar 10 '18 at 22:37
  • Where did you get the idea that you need to create a thread to get the data? Stop trusting everything you read out there – Chisko Mar 10 '18 at 22:40
  • Possible duplicate of [Android Bluetooth Example](https://stackoverflow.com/questions/12562875/android-bluetooth-example) – Chisko Mar 10 '18 at 22:44
  • Like I said, I'm new to Android and learning it as I go, so what would be more beneficial than simple criticism would be how to fix and do those things properly. – ejams Mar 10 '18 at 22:45
  • 1
    If pointing mistakes out is critizing for you, you're going to have a bad time in a job – Chisko Mar 10 '18 at 22:49
  • Criticism can be constructive as was the intention of asking this question. Also, it's not an incorrect statement as it comes straight from the documentation of GraphView: http://appsthatmatter.github.io/GraphView/javadoc/com/jjoe64/graphview/series/BaseSeries.html#appendData-E-boolean-int- – ejams Mar 10 '18 at 22:54

0 Answers0