Hardware Reference
In-Depth Information
A new data class has to be created to efficiently pass information from the thread to a receiving function.
The class is created outside of the current Java file but still within the same package. Right-click the package name
ch4.example.proArduino and select a new file to bring up a wizard, and enter the name BufferData.java for the
newly created file. This new file will contain the BufferData class called by run and used for a data handler. The class
declares two variables and has three functions; the variables are for the buffer and the amount of revived bytes. The
first function takes both values in at once and stores them in the appropriate variable. The next two functions will
return one of the two variables depending on the functions called. The class is outlined in Listing 4-9 because this file
is part of the same package—the class does not need to be imported.
Listing 4-9. Buffer Data Container Class
package ch4.example.proArduino;
public class BufferData {
private byte[] Buffer;
private int length;
public BufferData ( byte[] Buffer , int length) {
this.Buffer = Buffer; // set data to variables
this.length = length;
}
public byte[] getBuffer() {
return Buffer; // set data out
}
public int getLength(){
return length; // set data out
}
}// end BufferData
The last thing needed to complete the framework for use in this application or other applications is a handler. This
mechanism allows the rest of the program to work with the incoming data. The handler is included via an Android class
and needs to be imported along with a message class so that run can notify the main program of changes. The Listing 4-10
shows the two import lines that need to be added to the import section at the beginning of the file.
Listing 4-10. Two New Lines for the Import Section
import android.os.Handler;
import android.os.Message;
For convenience, the function to be created in Listing 4-11 that uses the two new classes will be placed toward
the end of the main file, or what was part 7 in Listing 4-6, just after the registerUIobjects function. The placement
is important because the handler function is heavily modified between different projects. This function is
wrapped inside of a class of type Handler and overrides the original class function of handleMessage . The original
functionality of handleMessage is not needed and not included with a call to super.<functions name> . The function
handleMessage links the data sent from run to a new BufferData object. At this point, the framework is complete and
ready for the development of the rest of the program. To prep the data for the user, the code converts the BufferData
to a string and appends the string to an editText widget for display.
Listing 4-11. The Last Part of the Framework
Handler IncomingDataHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
BufferData IncomingBuffer = (BufferData) msg.obj;
// after this point the data is available for manipulation
String str = new String(IncomingBuffer.getBuffer());
 
Search WWH ::




Custom Search