Hardware Reference
In-Depth Information
The toggle button is no longer needed for this example, and the variable declaration can be replaced by the
following four lines of code in the variable section inside the class:
private Button buttonSend;
private Button ScreenClear;
private EditText DataFromArduino;
private EditText outgoingData ;
Listing 4-14 describes the last three functions needed to complete the application. The data-handling
function was described in Listing 4-11 and should already be located below the registerUIobjects function. The
IncomingDataHandler is already to go and includes the code to print the data to the EditText box. The EditText box,
along with the three other user interface objects, needs to be linked to the program by type casting the return value of
the findViewById method; the type cast follows this format:
(<object type>) findViewById(R.id.<object ID>);
The clearScreen and SendData functions are called when the user instates the event that is associated with the
button in main.xml . When the clearScreen function is called, it sets the EditText box identified as incomingData
back to the original sate by setting the text to null . The last function, SendData , grabs the text in outgoingData as a
string and then converts that data to a byte array before calling the write function.
Listing 4-14. New and Edited Functions for CH4ExamplesActivity.java
private void registerUIobjects(){
buttonSend = (Button) findViewById(R.id.sendbutton); // called in for other use, not
// implemented
ScreenClear = (Button) findViewById(R.id.clear); // in this program
DataFromArduino = (EditText)findViewById(R.id.incomingData);
outgoingData = (EditText)findViewById(R.id.outgoingData);
} // end registerUIobjects
//////////////////////////////////////////////
// Listing 4-11 code is inserted inplace of this block
//////////////////////////////////////////////
public void clearScreen (View v) {
DataFromArduino.setText(null);
} // end clearScreen (View v)
public void SendData(View v) {
String convert = outgoingData.getText().toString();
byte[] BytestoSend = convert .getBytes();
write(BytestoSend); // sends buffer to the ADK
outgoingData.setText(null);
} // end void SendData(View v)
} // end class CH4ExamplesActivity
The Android application is ready to be installed after any possible errors are fixed. The Eclipse IDE throws two
warnings for this code because the buttons are declared and initialized but not referenced elsewhere in the code. It is
good practice to declare the buttons even if the attributes or functions of the object are not going to be used; having
the objects all set up and ready helps keep track of the objects available. This program will respond to any accessory
that has the proper identifiable information, and will take the incoming bytes and print the value as related to
 
Search WWH ::




Custom Search