Advertisement
sgccarey

HelloADK Java Class (Android App)

Oct 27th, 2011
1,736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. package com.saltapps.helloADK;
  2.  
  3. import java.io.FileDescriptor;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7.  
  8. import com.android.future.usb.UsbAccessory;
  9. import com.android.future.usb.UsbManager;
  10.  
  11. import android.app.Activity;
  12. import android.app.PendingIntent;
  13. import android.content.BroadcastReceiver;
  14. import android.content.Context;
  15. import android.content.Intent;
  16. import android.content.IntentFilter;
  17. import android.os.Bundle;
  18. import android.os.ParcelFileDescriptor;
  19. import android.util.Log;
  20. import android.widget.SeekBar;
  21. import android.widget.SeekBar.OnSeekBarChangeListener;
  22.  
  23. public class Main extends Activity implements Runnable, OnSeekBarChangeListener {
  24.  
  25. private static final String TAG = "DemoKit";
  26.  
  27. private static final String ACTION_USB_PERMISSION = "com.saltapps.helloADK.action.USB_PERMISSION";
  28.  
  29. private UsbManager mUsbManager; // This class allows you to access the state of USB and communicate with USB devices.
  30. private PendingIntent mPermissionIntent; // By giving a PendingIntent to another application, you are granting it the right to perform
  31. // the operation you have specified as if the other application was yourself
  32. private boolean mPermissionRequestPending;
  33. private SeekBar ledSeekBar;
  34.  
  35. UsbAccessory mAccessory;
  36. ParcelFileDescriptor mFileDescriptor;
  37. FileInputStream mInputStream;
  38. FileOutputStream mOutputStream;
  39.  
  40. public static final byte LED_SERVO_COMMAND = 2;
  41.  
  42. private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
  43. @Override
  44. public void onReceive(Context context, Intent intent) {
  45. String action = intent.getAction();
  46. if (ACTION_USB_PERMISSION.equals(action)) {
  47. synchronized (this) {
  48. UsbAccessory accessory = UsbManager.getAccessory(intent);
  49. if (intent.getBooleanExtra(
  50. UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
  51. openAccessory(accessory);
  52. } else {
  53. Log.d(TAG, "permission denied for accessory "
  54. + accessory);
  55. }
  56. mPermissionRequestPending = false;
  57. }
  58. } else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
  59. UsbAccessory accessory = UsbManager.getAccessory(intent);
  60. if (accessory != null && accessory.equals(mAccessory)) {
  61. closeAccessory();
  62. }
  63. }
  64. }
  65. };
  66.  
  67.  
  68. @Override
  69. public void onCreate(Bundle savedInstanceState) {
  70. super.onCreate(savedInstanceState);
  71.  
  72. mUsbManager = UsbManager.getInstance(this);
  73. mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(
  74. ACTION_USB_PERMISSION), 0);
  75. IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
  76. filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
  77. registerReceiver(mUsbReceiver, filter);
  78.  
  79. if (getLastNonConfigurationInstance() != null) {
  80. mAccessory = (UsbAccessory) getLastNonConfigurationInstance();
  81. openAccessory(mAccessory);
  82. }
  83.  
  84. setContentView(R.layout.main);
  85. ledSeekBar = (SeekBar) findViewById(R.id.led_seek_bar);
  86. ledSeekBar.setOnSeekBarChangeListener(this);
  87. ledSeekBar.setMax(255);
  88.  
  89. }
  90.  
  91. @Override
  92. public Object onRetainNonConfigurationInstance() {
  93. if (mAccessory != null) {
  94. return mAccessory;
  95. } else {
  96. return super.onRetainNonConfigurationInstance();
  97. }
  98. }
  99.  
  100. @Override
  101. public void onResume() {
  102. super.onResume();
  103.  
  104. Intent intent = getIntent();
  105. if (mInputStream != null && mOutputStream != null) {
  106. return;
  107. }
  108.  
  109. UsbAccessory[] accessories = mUsbManager.getAccessoryList();
  110. UsbAccessory accessory = (accessories == null ? null : accessories[0]);
  111. if (accessory != null) {
  112. if (mUsbManager.hasPermission(accessory)) {
  113. openAccessory(accessory);
  114. } else {
  115. synchronized (mUsbReceiver) {
  116. if (!mPermissionRequestPending) {
  117. mUsbManager.requestPermission(accessory,
  118. mPermissionIntent);
  119. mPermissionRequestPending = true;
  120. }
  121. }
  122. } // if/else 2
  123. } else {
  124. Log.d(TAG, "mAccessory is null");
  125. } // if/else 1
  126. } // onResume
  127.  
  128. @Override
  129. public void onPause() {
  130. super.onPause();
  131. closeAccessory();
  132. } // onPause
  133.  
  134. @Override
  135. public void onDestroy() {
  136. unregisterReceiver(mUsbReceiver);
  137. super.onDestroy();
  138. } // onDestroy
  139.  
  140. private void openAccessory(UsbAccessory accessory) {
  141. mFileDescriptor = mUsbManager.openAccessory(accessory);
  142. if (mFileDescriptor != null) {
  143. mAccessory = accessory;
  144. FileDescriptor fd = mFileDescriptor.getFileDescriptor();
  145. mInputStream = new FileInputStream(fd);
  146. mOutputStream = new FileOutputStream(fd);
  147. Thread thread = new Thread(null, this, "DemoKit");
  148. thread.start();
  149. Log.d(TAG, "accessory opened");
  150. enableControls(true);
  151. } else {
  152. Log.d(TAG, "accessory open fail");
  153. } // if/else
  154. } // openAccessory
  155.  
  156. private void enableControls(boolean b) {
  157.  
  158. } // enableControls
  159.  
  160. private void closeAccessory() {
  161. enableControls(false);
  162.  
  163. try {
  164. if (mFileDescriptor != null) {
  165. mFileDescriptor.close();
  166. }
  167. } catch (IOException e) {
  168. } finally {
  169. mFileDescriptor = null;
  170. mAccessory = null;
  171. } // try/catch
  172. } // closeAccessory
  173.  
  174. public void sendCommand(byte command, byte target, int value) { // sends message to accessory
  175. byte[] buffer = new byte[3];
  176. if (value > 255)
  177. value = 255;
  178.  
  179. buffer[0] = command;
  180. buffer[1] = target;
  181. buffer[2] = (byte) value;
  182. if (mOutputStream != null && buffer[1] != -1) {
  183. try {
  184. mOutputStream.write(buffer);
  185. } catch (IOException e) {
  186. Log.e(TAG, "write failed", e);
  187. }
  188. }
  189. }
  190.  
  191. public void run() {
  192. int ret = 0;
  193. byte[] buffer = new byte[16384];
  194. int i;
  195.  
  196. while (ret >= 0) {
  197. try {
  198. ret = mInputStream.read(buffer);
  199. } catch (IOException e) {
  200. break;
  201. } // try/catch
  202.  
  203. i = 0;
  204. while (i < ret) {
  205. int len = ret - i;
  206.  
  207. switch (buffer[i]) {
  208. default:
  209. Log.d(TAG, "unknown msg: " + buffer[i]);
  210. i = len;
  211. break;
  212. } // switch
  213. } // while 2
  214.  
  215. } // while 1
  216. } // run()
  217.  
  218. @Override
  219. public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
  220. sendCommand(LED_SERVO_COMMAND,(byte)0,(byte)progress);
  221. } // onProgressChanged
  222.  
  223. @Override
  224. public void onStartTrackingTouch(SeekBar arg0) {
  225. } // onStartTrackingTouch
  226.  
  227. @Override
  228. public void onStopTrackingTouch(SeekBar arg0) {
  229. } // onStopTrackingTouch
  230.  
  231. } // Class
  232.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement