Pavle_nis

Android USB Devices List

Jan 30th, 2017
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.99 KB | None | 0 0
  1. public class USBInfoAPI extends AppCompatActivity
  2. {
  3.     TextView textInfo;
  4.     TextView textInfoInterface;
  5.     TextView textEndPoint;
  6.  
  7.     Spinner spInterface;
  8.     ArrayList<String> listInterface;
  9.     ArrayList<UsbInterface> listUsbInterface;
  10.     ArrayAdapter<String> adapterInterface;
  11.  
  12.     Spinner spEndPoint;
  13.     ArrayList<String> listEndPoint;
  14.     ArrayList<UsbEndpoint> listUsbEndpoint;
  15.     ArrayAdapter<String> adapterEndpoint;
  16.  
  17.     UsbDevice deviceFound = null;
  18.  
  19.     private static final String ACTION_USB_PERMISSION =
  20.             "com.android.example.USB_PERMISSION";
  21.     PendingIntent mPermissionIntent;
  22.  
  23.     UsbInterface usbInterface;
  24.     UsbDeviceConnection usbDeviceConnection;
  25.  
  26.  
  27.     @Override
  28.     protected void onCreate(Bundle savedInstanceState)
  29.     {
  30.         super.onCreate(savedInstanceState);
  31.         setContentView(R.layout.usbinfoapi);
  32.  
  33.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  34.         setSupportActionBar(toolbar);
  35.         getSupportActionBar().setTitle("usb service");
  36.         if(getSupportActionBar() != null)
  37.         {
  38.             getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  39.             getSupportActionBar().setDisplayShowHomeEnabled(true);
  40.         }
  41.  
  42.         spInterface = (Spinner)findViewById(R.id.spinnerinterface);
  43.         spEndPoint = (Spinner)findViewById(R.id.spinnerendpoint);
  44.         textInfo = (TextView) findViewById(R.id.info);
  45.         textInfoInterface = (TextView)findViewById(R.id.infointerface);
  46.         textEndPoint = (TextView)findViewById(R.id.infoendpoint);
  47.  
  48.         //register the broadcast receiver
  49.         mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
  50.         IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
  51.         registerReceiver(mUsbReceiver, filter);
  52.  
  53.         registerReceiver(mUsbDeviceReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED));
  54.         registerReceiver(mUsbDeviceReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED));
  55.  
  56.         connectUsb();
  57.     }
  58.  
  59.     @Override
  60.     public boolean onOptionsItemSelected(MenuItem menuItem)
  61.     {
  62.         if(menuItem.getItemId() == android.R.id.home)
  63.             finish();
  64.         return super.onOptionsItemSelected(menuItem);
  65.     }
  66.  
  67.     @Override
  68.     protected void onDestroy() {
  69.         releaseUsb();
  70.         unregisterReceiver(mUsbReceiver);
  71.         unregisterReceiver(mUsbDeviceReceiver);
  72.         super.onDestroy();
  73.     }
  74.  
  75.     private void connectUsb(){
  76.  
  77.         /*Toast.makeText(MainActivity.this,
  78.                 "connectUsb()",
  79.                 Toast.LENGTH_LONG).show();
  80.         textStatus.setText("connectUsb()");*/
  81.  
  82.         checkDeviceInfo();
  83.         if(deviceFound != null){
  84.             doRawDescriptors();
  85.         }
  86.     }
  87.  
  88.     private void releaseUsb(){
  89.  
  90.         /*Toast.makeText(MainActivity.this,
  91.                 "releaseUsb()",
  92.                 Toast.LENGTH_LONG).show();
  93.         textStatus.setText("releaseUsb()");*/
  94.  
  95.         if(usbDeviceConnection != null){
  96.             if(usbInterface != null){
  97.                 usbDeviceConnection.releaseInterface(usbInterface);
  98.                 usbInterface = null;
  99.             }
  100.             usbDeviceConnection.close();
  101.             usbDeviceConnection = null;
  102.         }
  103.         deviceFound = null;
  104.     }
  105.  
  106.     private void doRawDescriptors(){
  107.         UsbDevice deviceToRead = deviceFound;
  108.         UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
  109.  
  110.         Boolean permitToRead = manager.hasPermission(deviceToRead);
  111.  
  112.         if(permitToRead){
  113.            
  114.         }else{
  115.             manager.requestPermission(deviceToRead, mPermissionIntent);
  116.             /*Toast.makeText(MainActivity.this,
  117.                     "Permission: " + permitToRead,
  118.                     Toast.LENGTH_LONG).show();
  119.             textStatus.setText("Permission: " + permitToRead);*/
  120.         }
  121.     }
  122.  
  123.     private final BroadcastReceiver mUsbReceiver =
  124.             new BroadcastReceiver() {
  125.  
  126.                 @Override
  127.                 public void onReceive(Context context, Intent intent) {
  128.                     String action = intent.getAction();
  129.                     if (ACTION_USB_PERMISSION.equals(action)) {
  130.  
  131.                         /*Toast.makeText(USBInfoAPI.this,
  132.                                 "ACTION_USB_PERMISSION",
  133.                                 Toast.LENGTH_LONG).show();
  134.                         textStatus.setText("ACTION_USB_PERMISSION");*/
  135.  
  136.                         synchronized (this) {
  137.                             UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
  138.  
  139.                             if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
  140.                                 if(device != null){
  141.                                     // doReadRawDescriptors(device);
  142.                                 }
  143.                             }
  144.                             else {
  145.                                 /*Toast.makeText(USBInfoAPI.this,
  146.                                         "permission denied for device " + device,
  147.                                         Toast.LENGTH_LONG).show();
  148.                                 textStatus.setText("permission denied for device " + device);*/
  149.                             }
  150.                         }
  151.                     }
  152.                 }
  153.             };
  154.  
  155.     private final BroadcastReceiver mUsbDeviceReceiver =
  156.             new BroadcastReceiver() {
  157.  
  158.                 @Override
  159.                 public void onReceive(Context context, Intent intent) {
  160.                     String action = intent.getAction();
  161.                     if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
  162.  
  163.                         deviceFound = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
  164.                         /*Toast.makeText(MainActivity.this,
  165.                                 "ACTION_USB_DEVICE_ATTACHED: \n" +
  166.                                         deviceFound.toString(),
  167.                                 Toast.LENGTH_LONG).show();
  168.                         textStatus.setText("ACTION_USB_DEVICE_ATTACHED: \n" +
  169.                                 deviceFound.toString());*/
  170.  
  171.                         checkDeviceInfoSkipDeviceSearching();
  172.                         doRawDescriptors();
  173.                     }else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
  174.  
  175.                         UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
  176.  
  177.                         /*Toast.makeText(MainActivity.this,
  178.                                 "ACTION_USB_DEVICE_DETACHED: \n" +
  179.                                         device.toString(),
  180.                                 Toast.LENGTH_LONG).show();
  181.                         textStatus.setText("ACTION_USB_DEVICE_DETACHED: \n" +
  182.                                 device.toString());*/
  183.  
  184.                         if(device!=null){
  185.                             if(device == deviceFound){
  186.                                 releaseUsb();
  187.                             }
  188.                         }
  189.  
  190.                         textInfo.setText("");
  191.                         textInfoInterface.setText("");
  192.                         textEndPoint.setText("");
  193.  
  194.                         listInterface.clear();
  195.                         listUsbInterface.clear();
  196.                         adapterInterface.notifyDataSetChanged();
  197.  
  198.                         listEndPoint.clear();
  199.                         listUsbEndpoint.clear();
  200.                         adapterEndpoint.notifyDataSetChanged();
  201.                     }
  202.                 }
  203.  
  204.             };
  205.  
  206.     private void checkDeviceInfo() {
  207.  
  208.         deviceFound = null;
  209.  
  210.         UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
  211.         HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
  212.         Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
  213.  
  214.         while (deviceIterator.hasNext()) {
  215.             UsbDevice device = deviceIterator.next();
  216.  
  217.             //if(device.getVendorId()==targetVendorID){
  218.             //  if(device.getProductId()==targetProductID){
  219.             deviceFound = device;
  220.             // }
  221.             // }
  222.         }
  223.  
  224.         textInfo.setText("");
  225.         textInfoInterface.setText("");
  226.         textEndPoint.setText("");
  227.  
  228.         if(deviceFound==null){
  229.             /*Toast.makeText(MainActivity.this,
  230.                     "device not found",
  231.                     Toast.LENGTH_LONG).show();
  232.             textStatus.setText("device not found");*/
  233.         }else{
  234.             String i = deviceFound.toString() + "\n" +
  235.                     "DevicePath: " + deviceFound.getDeviceName() + "\n" +
  236.                     "DeviceClass: " + deviceFound.getDeviceClass() + " - "
  237.                     + translateDeviceClass(deviceFound.getDeviceClass()) + "\n" +
  238.                     "DeviceSubClass: " + deviceFound.getDeviceSubclass() + "\n" +
  239.                     "DeviceID: " + deviceFound.getDeviceId() + "\n" +
  240.                     "VendorID: " + deviceFound.getVendorId() + "\n" +
  241.                     "Vendor Name" + deviceFound.getDeviceName() + "\n" +
  242.                     "ProductID: " + deviceFound.getProductId() + "\n" +
  243.                     "Product Name" + deviceFound.getManufacturerName() + "\n" +
  244.                     "InterfaceCount: " + deviceFound.getInterfaceCount();
  245.             textInfo.setText(i);
  246.  
  247.             checkUsbDevicve(deviceFound);
  248.         }
  249.  
  250.     }
  251.  
  252.     private void checkDeviceInfoSkipDeviceSearching() {
  253.         //called when ACTION_USB_DEVICE_ATTACHED,
  254.         //device already found, skip device searching
  255.  
  256.         textInfo.setText("");
  257.         textInfoInterface.setText("");
  258.         textEndPoint.setText("");
  259.  
  260.         String i = deviceFound.toString() + "\n" +
  261.                 "DevicePath: " + deviceFound.getDeviceName() + "\n" +
  262.                 "DeviceClass: " + deviceFound.getDeviceClass() + " - "
  263.                 + translateDeviceClass(deviceFound.getDeviceClass()) + "\n" +
  264.                 "DeviceSubClass: " + deviceFound.getDeviceSubclass() + "\n" +
  265.                 "DeviceID: " + deviceFound.getDeviceId() + "\n" +
  266.                 "VendorID: " + deviceFound.getVendorId() + "\n" +
  267.                 "Vendor Name" + deviceFound.getDeviceName() + "\n" +
  268.                 "ProductID: " + deviceFound.getProductId() + "\n" +
  269.                 "Product Name" + deviceFound.getManufacturerName() + "\n" +
  270.                 "InterfaceCount: " + deviceFound.getInterfaceCount();
  271.         textInfo.setText(i);
  272.  
  273.         checkUsbDevicve(deviceFound);
  274.  
  275.     }
  276.  
  277.     OnItemSelectedListener deviceOnItemSelectedListener =
  278.             new OnItemSelectedListener(){
  279.  
  280.                 @Override
  281.                 public void onItemSelected(AdapterView<?> parent,
  282.                                            View view, int position, long id) {
  283.                     UsbDevice device = deviceFound;
  284.  
  285.                     String i = device.toString() + "\n" +
  286.                             "DevicePath: " + deviceFound.getDeviceName() + "\n" +
  287.                             "DeviceClass: " + deviceFound.getDeviceClass() + " - "
  288.                             + translateDeviceClass(deviceFound.getDeviceClass()) + "\n" +
  289.                             "DeviceSubClass: " + deviceFound.getDeviceSubclass() + "\n" +
  290.                             "DeviceID: " + deviceFound.getDeviceId() + "\n" +
  291.                             "VendorID: " + deviceFound.getVendorId() + "\n" +
  292.                             "Vendor Name" + deviceFound.getDeviceName() + "\n" +
  293.                             "ProductID: " + deviceFound.getProductId() + "\n" +
  294.                             "Product Name" + deviceFound.getManufacturerName() + "\n" +
  295.                             "InterfaceCount: " + deviceFound.getInterfaceCount();
  296.                     textInfo.setText(i);
  297.  
  298.                     checkUsbDevicve(device);
  299.                 }
  300.  
  301.                 @Override
  302.                 public void onNothingSelected(AdapterView<?> parent) {}
  303.  
  304.             };
  305.  
  306.     private void checkUsbDevicve(UsbDevice d) {
  307.         listInterface = new ArrayList<String>();
  308.         listUsbInterface = new ArrayList<UsbInterface>();
  309.  
  310.         for(int i=0; i<d.getInterfaceCount(); i++){
  311.             UsbInterface usbif = d.getInterface(i);
  312.             listInterface.add(usbif.toString());
  313.             listUsbInterface.add(usbif);
  314.         }
  315.  
  316.         adapterInterface = new ArrayAdapter<String>(this,
  317.                 android.R.layout.simple_spinner_item, listInterface);
  318.         adapterInterface.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  319.         spInterface.setAdapter(adapterInterface);
  320.         spInterface.setOnItemSelectedListener(interfaceOnItemSelectedListener);
  321.     }
  322.  
  323.     OnItemSelectedListener interfaceOnItemSelectedListener =
  324.             new OnItemSelectedListener(){
  325.  
  326.                 @Override
  327.                 public void onItemSelected(AdapterView<?> parent,
  328.                                            View view, int position, long id) {
  329.  
  330.                     UsbInterface selectedUsbIf = listUsbInterface.get(position);
  331.  
  332.                     String sUsbIf = "\n" + selectedUsbIf.toString() + "\n"
  333.                             + "Id: " + selectedUsbIf.getId() + "\n"
  334.                             + "InterfaceClass: " + selectedUsbIf.getInterfaceClass() + "\n"
  335.                             + "InterfaceProtocol: " + selectedUsbIf.getInterfaceProtocol() + "\n"
  336.                             + "InterfaceSubclass: " + selectedUsbIf.getInterfaceSubclass() + "\n"
  337.                             + "EndpointCount: " + selectedUsbIf.getEndpointCount();
  338.  
  339.                     textInfoInterface.setText(sUsbIf);
  340.                     checkUsbInterface(selectedUsbIf);
  341.                 }
  342.  
  343.                 @Override
  344.                 public void onNothingSelected(AdapterView<?> parent) {}
  345.  
  346.             };
  347.  
  348.     private void checkUsbInterface(UsbInterface uif) {
  349.         listEndPoint = new ArrayList<String>();
  350.         listUsbEndpoint = new ArrayList<UsbEndpoint>();
  351.  
  352.         for(int i=0; i<uif.getEndpointCount(); i++){
  353.             UsbEndpoint usbEndpoint = uif.getEndpoint(i);
  354.             listEndPoint.add(usbEndpoint.toString());
  355.             listUsbEndpoint.add(usbEndpoint);
  356.         }
  357.  
  358.         adapterEndpoint = new ArrayAdapter<String>(this,
  359.                 android.R.layout.simple_spinner_item, listEndPoint);
  360.         adapterEndpoint.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  361.         spEndPoint.setAdapter(adapterEndpoint);
  362.         spEndPoint.setOnItemSelectedListener(endpointOnItemSelectedListener);
  363.     }
  364.  
  365.     OnItemSelectedListener endpointOnItemSelectedListener =
  366.             new OnItemSelectedListener(){
  367.  
  368.                 @Override
  369.                 public void onItemSelected(AdapterView<?> parent,
  370.                                            View view, int position, long id) {
  371.  
  372.                     UsbEndpoint selectedEndpoint = listUsbEndpoint.get(position);
  373.  
  374.                     String sEndpoint = "\n" + selectedEndpoint.toString() + "\n"
  375.                             + translateEndpointType(selectedEndpoint.getType());
  376.  
  377.                     textEndPoint.setText(sEndpoint);
  378.                 }
  379.  
  380.                 @Override
  381.                 public void onNothingSelected(AdapterView<?> parent) {}
  382.  
  383.             };
  384.  
  385.     private String translateEndpointType(int type){
  386.         switch(type){
  387.             case UsbConstants.USB_ENDPOINT_XFER_CONTROL:
  388.                 return "USB_ENDPOINT_XFER_CONTROL (endpoint zero)";
  389.             case UsbConstants.USB_ENDPOINT_XFER_ISOC:
  390.                 return "USB_ENDPOINT_XFER_ISOC (isochronous endpoint)";
  391.             case UsbConstants.USB_ENDPOINT_XFER_BULK :
  392.                 return "USB_ENDPOINT_XFER_BULK (bulk endpoint)";
  393.             case UsbConstants.USB_ENDPOINT_XFER_INT:
  394.                 return "USB_ENDPOINT_XFER_INT (interrupt endpoint)";
  395.             default:
  396.                 return "unknown";
  397.         }
  398.     }
  399.  
  400.     private String translateDeviceClass(int deviceClass){
  401.         switch(deviceClass){
  402.             case UsbConstants.USB_CLASS_APP_SPEC:
  403.                 return "Application specific USB class";
  404.             case UsbConstants.USB_CLASS_AUDIO:
  405.                 return "USB class for audio devices";
  406.             case UsbConstants.USB_CLASS_CDC_DATA:
  407.                 return "USB class for CDC devices (communications device class)";
  408.             case UsbConstants.USB_CLASS_COMM:
  409.                 return "USB class for communication devices";
  410.             case UsbConstants.USB_CLASS_CONTENT_SEC:
  411.                 return "USB class for content security devices";
  412.             case UsbConstants.USB_CLASS_CSCID:
  413.                 return "USB class for content smart card devices";
  414.             case UsbConstants.USB_CLASS_HID:
  415.                 return "USB class for human interface devices (for example, mice and keyboards)";
  416.             case UsbConstants.USB_CLASS_HUB:
  417.                 return "USB class for USB hubs";
  418.             case UsbConstants.USB_CLASS_MASS_STORAGE:
  419.                 return "USB class for mass storage devices";
  420.             case UsbConstants.USB_CLASS_MISC:
  421.                 return "USB class for wireless miscellaneous devices";
  422.             case UsbConstants.USB_CLASS_PER_INTERFACE:
  423.                 return "USB class indicating that the class is determined on a per-interface basis";
  424.             case UsbConstants.USB_CLASS_PHYSICA:
  425.                 return "USB class for physical devices";
  426.             case UsbConstants.USB_CLASS_PRINTER:
  427.                 return "USB class for printers";
  428.             case UsbConstants.USB_CLASS_STILL_IMAGE:
  429.                 return "USB class for still image devices (digital cameras)";
  430.             case UsbConstants.USB_CLASS_VENDOR_SPEC:
  431.                 return "Vendor specific USB class";
  432.             case UsbConstants.USB_CLASS_VIDEO:
  433.                 return "USB class for video devices";
  434.             case UsbConstants.USB_CLASS_WIRELESS_CONTROLLER:
  435.                 return "USB class for wireless controller devices";
  436.             default: return "Unknown USB class!";
  437.  
  438.         }
  439.     }
  440. }
Advertisement
Add Comment
Please, Sign In to add comment