Hardware Reference
In-Depth Information
else {
Log.d(TAG, "accessory open fail");
}
} // end void openAccessory...
private void setupAccessory() {
UsbManagerOBJ = UsbManager.getInstance(this);
Needed_Permission = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
registerReceiver(ADKReceiver, filter);
if (getLastNonConfigurationInstance() != null) {
ARDUINO_ADK = (UsbAccessory) getLastNonConfigurationInstance();
openAccessory(ARDUINO_ADK);
}
} // end private void setupAccessory()
private void closeAccessory() {
try {
if (ADKstreamObj != null) {
ADKstreamObj.close();
}
}// end try
catch (IOException e) {
Log.e(TAG, "IO Exception", e);
}
finally {
ADKstreamObj = null;
ARDUINO_ADK = null;
} // end of all try catch finally
} // end private void closeAccessory()
Part 6 contains a function that writes data to the output file stream that can be read by the Mega ADK board's
programming. For this example, only the output direction has been created. The write function accepts an array of bytes of
any size and will send all the bytes contained in the array; there is no need to define an amount—the whole array is sent.
Listing 4-6. CH4ExamplesActivity.java, Part 6 of 7
private void write(byte[] send){
if (SendtoADK != null) {
try {
SendtoADK.write(send);
}
catch (IOException e){
Log.e(TAG, "write failed", e);
}
}// end if (SendtoADK != null)
}// end private void write...
Part 7 is where the code performs functions based upon user interaction and later actions performed by the
Mega ADK. For the program to be able to interact with user interface objects (such as showing dynamic information
or reading object information), the objects need to be registered. The registerUIobjects function is responsible for
the registration; it finds the ID of the desired object and sets it to the variable created in the beginning of the class.
The variable is not defined in the function because other functions will need to use the objects.
Search WWH ::




Custom Search