Back to project page Robook.
The source code is released under:
MIT License
If you think the Android project Robook listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package unipg.dmi.robook; /*from ww w. j av a 2 s . c o m*/ import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import android.app.Activity; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.hardware.usb.UsbAccessory; import android.hardware.usb.UsbManager; import android.os.Bundle; import android.os.ParcelFileDescriptor; import android.util.Log; import android.widget.Toast; public abstract class AbstractAdkActivity extends Activity { private static int RQS_USB_PERMISSION = 0; private static final String ACTION_USB_PERMISSION = "arduino-er.usb_permission"; private PendingIntent PendingIntent_UsbPermission; private UsbManager myUsbManager; private UsbAccessory myUsbAccessory; private ParcelFileDescriptor myAdkParcelFileDescriptor; private FileInputStream myAdkInputStream; private static FileOutputStream myAdkOutputStream; boolean firstRqsPermission; protected abstract void doOnCreate(Bundle savedInstanceState); protected abstract void doAdkRead(String stringIn); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); myUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED); registerReceiver(myUsbReceiver, intentFilter); // Chiede Permessi USB dal utente Intent intent_UsbPermission = new Intent(ACTION_USB_PERMISSION); PendingIntent_UsbPermission = PendingIntent.getBroadcast(this, RQS_USB_PERMISSION, intent_UsbPermission, 0); IntentFilter intentFilter_UsbPermission = new IntentFilter( ACTION_USB_PERMISSION); registerReceiver(myUsbPermissionReceiver, intentFilter_UsbPermission); firstRqsPermission = true; doOnCreate(savedInstanceState); } @Override protected void onResume() { super.onResume(); if (myAdkInputStream == null || myAdkOutputStream == null) { UsbAccessory[] usbAccessoryList = myUsbManager.getAccessoryList(); UsbAccessory usbAccessory = null; if (usbAccessoryList != null) { usbAccessory = usbAccessoryList[0]; if (usbAccessory != null) { if (myUsbManager.hasPermission(usbAccessory)) { OpenUsbAccessory(usbAccessory); } else { if (firstRqsPermission) { firstRqsPermission = false; synchronized (myUsbReceiver) { myUsbManager.requestPermission(usbAccessory, PendingIntent_UsbPermission); } } } } } } } public static void WriteAdk(String text) { byte[] buffer = text.getBytes(); if (myAdkOutputStream != null) { try { myAdkOutputStream.write(buffer); Log.e("WriteAdk ", " was successfully called!"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { /* * Toast.makeText(this, "Failed ", * android.widget.Toast.LENGTH_SHORT).show(); */ Log.e("WriteAdk ", " failed!"); Log.e("Status of outputStreams is ", Boolean.toString(myAdkOutputStream == null)); } } @Override protected void onPause() { super.onPause(); closeUsbAccessory(); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(myUsbReceiver); unregisterReceiver(myUsbPermissionReceiver); } Runnable runnableReadAdk = new Runnable() { @Override public void run() { int numberOfByteRead = 0; byte[] buffer = new byte[255]; while (numberOfByteRead >= 0) { try { numberOfByteRead = myAdkInputStream.read(buffer, 0, buffer.length); final StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < numberOfByteRead; i++) { stringBuilder.append((char) buffer[i]); } runOnUiThread(new Runnable() { @Override public void run() { doAdkRead(stringBuilder.toString()); } }); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); break; } } } }; private BroadcastReceiver myUsbReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(UsbManager.ACTION_USB_ACCESSORY_DETACHED)) { UsbAccessory usbAccessory = (UsbAccessory) intent .getParcelableExtra(UsbManager.EXTRA_ACCESSORY); if (usbAccessory != null && usbAccessory.equals(myUsbAccessory)) { closeUsbAccessory(); } } } }; private BroadcastReceiver myUsbPermissionReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(ACTION_USB_PERMISSION)) { synchronized (this) { UsbAccessory usbAccessory = (UsbAccessory) intent .getParcelableExtra(UsbManager.EXTRA_ACCESSORY); if (intent.getBooleanExtra( UsbManager.EXTRA_PERMISSION_GRANTED, false)) { OpenUsbAccessory(usbAccessory); } else { finish(); } } } } }; private void OpenUsbAccessory(UsbAccessory acc) { myAdkParcelFileDescriptor = myUsbManager.openAccessory(acc); if (myAdkParcelFileDescriptor != null) { myUsbAccessory = acc; FileDescriptor fileDescriptor = myAdkParcelFileDescriptor .getFileDescriptor(); myAdkInputStream = new FileInputStream(fileDescriptor); myAdkOutputStream = new FileOutputStream(fileDescriptor); Thread thread = new Thread(runnableReadAdk); thread.start(); } } private void closeUsbAccessory() { if (myAdkParcelFileDescriptor != null) { try { myAdkParcelFileDescriptor.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } myAdkParcelFileDescriptor = null; myUsbAccessory = null; } }