I am currently using the GraphView from the developer jjoe64 on GitGub and I was wondering how I would retrieve the double I created in my BT connected thread class to the GraphView class. This is the original function to call random data, but I want the serial data from my BlueTooth class
The current function in this realtime graph is:
private double getRandom() {
double high = 3;
double low = 0.5;
return Math.random() * (high - low) + low;
}
In my Bluetooth class, I have the command ConnectedThread.read(), but It’s not really working. Here it is:
public static double read() {
try {
byte[] buffer = new byte[1024];
double bytes = mmInStream.read(buffer);
return bytes;
} catch(IOException e) {
return 5;
}
}
I am not sure if I it’s just my phone that’s too slow, it’s running Android2.3 (DesireHD), but my professor at my school said it should work fine if I just call ConnectedThread.read() and have it equal a double. Any advice?
You haven’t provided enough information for a out-of-the box solution, but I’ll give it a shot anyway.
First of all, I presume that
mmInStreamis anInputStreamor its subclass. Look at the API ofint InputStream.read(byte[] b):This means that what you’re returning from your
read()method is just the number of bytes that have been written to thebufferfrommmInStream. That is probably not what you want to do. What you probably want to do is read just the value from this stream. To do that you should:wrap your
mmInStreamin aDataInputStreamjust after themmInStreamis created:read the
doublevalue from thedataInStream. But as in all computer systems you must be aware of the exact format that your input value comes in. You must refer to the specification of the device you’re using to fetch the input data.Now the
dataInStreamcomes in handy because it abstracts the necessary low-level IO operations and lets you focus on the data. It will automatically translate your queries for the data to the IO operations. For example:If your data is in
doubleformat (and I believe that is the case according to the words of your professor), yourread()method is as simple as:And in case the data is coming in the
floatformat:But again, be sure to consult the specification of the device you’re using for the exact format. Some devices may pass you data in exotic formats like for example: “first 2 bytes are the integer part of the resulting value, second 2 bytes are the fractional part”. It is up to you as a consumer of the data to follow its format.