Guest User

Untitled

a guest
Oct 21st, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. manager = (UsbManager) getSystemService(Context.USB_SERVICE);
  2. // ------------------------------------------------------------------
  3. mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(
  4. ACTION_USB_PERMISSION), 0);
  5. IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
  6. registerReceiver(mUsbReceiver, filter);
  7. // -------------------------------------------------------------------
  8. HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
  9. Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
  10. String i = "";
  11. while (deviceIterator.hasNext()) {
  12. device = deviceIterator.next();
  13. manager.requestPermission(device, mPermissionIntent);
  14. i += "n" + "DeviceID: " + device.getDeviceId() + "n"
  15. + "DeviceName: " + device.getDeviceName() + "n"
  16. + "DeviceClass: " + device.getDeviceClass() + " - "
  17. + "DeviceSubClass: " + device.getDeviceSubclass() + "n"
  18. + "VendorID: " + device.getVendorId() + "n"
  19. + "ProductID: " + device.getProductId() + "n"
  20. + "DeviceProtocol: " + device.getDeviceProtocol() + "n"
  21. + "Interface: " + device.getInterface(0) + "n";
  22.  
  23. textInfo.setText(i);
  24.  
  25. UsbInterface usbInterface = device.getInterface(0);
  26. UsbEndpoint usbEndpoint = usbInterface.getEndpoint(0);
  27.  
  28. UsbDeviceConnection connection = manager.openDevice(device);
  29. connection.claimInterface(usbInterface, true);
  30.  
  31. String setData = "AT";
  32. byte[] bytesVar = setData.getBytes();
  33.  
  34. connection.bulkTransfer(endpoint, bytesVar, bytesVar.length, TIMEOUT);
  35.  
  36. private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
  37.  
  38. public void onReceive(Context context, Intent intent) {
  39. String action = intent.getAction();
  40. if (ACTION_USB_PERMISSION.equals(action)) {
  41. synchronized (this) {
  42. UsbDevice device = (UsbDevice) intent
  43. .getParcelableExtra(UsbManager.EXTRA_DEVICE);
  44. if (intent.getBooleanExtra(
  45. UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
  46. if (device != null) {
  47. // call method to set up device communication
  48. }
  49. } else {
  50. Log.d("ERROR", "permission denied for device " + device);
  51. }
  52. }
  53. }
  54. }
  55. };
Add Comment
Please, Sign In to add comment