Hardware Reference
In-Depth Information
Listing 4-6. CH4ExamplesActivity.java, Part 4 of 7
private BroadcastReceiver ADKReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action) == true) {
synchronized (this) {
UsbAccessory accessory = UsbManager.getAccessory(intent);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
openAccessory(accessory);
}
else {
Log.d(TAG, "permission denied for accessory "+ accessory);
}
IsPermissionNeeded = true;
} // end synchronized (this)
} // end if (ACTION_USB_PERMISSION.equals...
else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
UsbAccessory accessory = UsbManager.getAccessory(intent);
if (accessory != null && accessory.equals(ARDUINO_ADK)) {
closeAccessory();
}
} // end else if (UsbManager...
} // end void onReceive(Context contex ...
}; // end private BroadcastReceiver..
@Override
public Object onRetainNonConfigurationInstance() {
if (ARDUINO_ADK != null) {
return ARDUINO_ADK;
}
else {
return super.onRetainNonConfigurationInstance();
}
} // end public Object onRetainNon*...
Part 5 sets up the accessory to be used and handled by the program with the registration of the accessory object
and the I/O streams. This section defines what needs to be done when opening and closing the accessory.
Listing 4-6. CH4ExamplesActivity.java, Part 5 of 7
private void openAccessory(UsbAccessory accessory) {
ADKstreamObj = UsbManagerOBJ.openAccessory(accessory);
if (ADKstreamObj != null) {
ARDUINO_ADK = accessory;
FileDescriptor fd = ADKstreamObj.getFileDescriptor();
ReciveFromADK = new FileInputStream(fd);
SendtoADK = new FileOutputStream(fd);
Log.d(TAG, "accessory opened");
} // end if (ADKstreamObj
Search WWH ::




Custom Search