@Jusit suggested I use a global object to pass data from a background thread to an activity and it worked. But now I am worried about deadlock and if there is a good way to avoid/prevent it. Also I am not sure my choice of String as a global object is such a good one. Here's an explanation what I have currently done.
Actually the global object is just a public static variable created in Blueterm.java as shown below:
public class BlueTerm extends Activity {
// a global object I guess
public static String strData = "";
// a couple of booleans to control reads/writes
public static boolean strWrite = false;
public static boolean strRead = false;
...
}
So strData I suppose is my global object created and written to by a background thread as follows:
while (BlueSentry.strRead == true);
BlueSentry.strWrite = true;
BlueSentry.strData = BlueSentry.strData + Character.toString(printableB);
BlueSentry.strWrite = false;
My intention is for strRead to prevent strData from being over written while my plotting activity is reading it. strWrite prevents my plotting activity from reading strData while it is being written as follows:
while (BlueSentry.strWrite == true);
BlueSentry.strRead = true;
String strData = BlueSentry.strData;
BlueSentry.strData = "";
BlueSentry.strRead = false
You can see that strData gets reset to an empty string after the plotting activity is through reading it.
I have experienced a force close while my phone was not connected to debugging with the USB, so I am not sure what caused the force close. And it has not happened since.
Any suggestions for achieving concurrency here without deadlock? I have read about mutual-exclusion (semaphores, locks, mutex), but I am not sure which if any I should use. Also maybe my choice of String as a global object is a bad one, since I want real-time data transfer. That is I don't want the thread blocked waiting for the activity finish reading. And I don't want the activity blocked waiting for the background thread to finish writing. May I should get rid of String and implement/use a FIFO buffer instead, no?
Use a Handler (see https://developer.android.com/reference/android/os/Handler.html) as the communication technique if you are going from a background thread to an Activity. See https://web.archive.org/web/20200810154212/http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html for a quick tutorial, but in short:
Character.toString(printableB)
in a Message.No globally shared variables, no potential read/write errors, everyone goes home happy.