Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class USBInfoAPI extends AppCompatActivity
- {
- TextView textInfo;
- TextView textInfoInterface;
- TextView textEndPoint;
- Spinner spInterface;
- ArrayList<String> listInterface;
- ArrayList<UsbInterface> listUsbInterface;
- ArrayAdapter<String> adapterInterface;
- Spinner spEndPoint;
- ArrayList<String> listEndPoint;
- ArrayList<UsbEndpoint> listUsbEndpoint;
- ArrayAdapter<String> adapterEndpoint;
- UsbDevice deviceFound = null;
- private static final String ACTION_USB_PERMISSION =
- "com.android.example.USB_PERMISSION";
- PendingIntent mPermissionIntent;
- UsbInterface usbInterface;
- UsbDeviceConnection usbDeviceConnection;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.usbinfoapi);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- getSupportActionBar().setTitle("usb service");
- if(getSupportActionBar() != null)
- {
- getSupportActionBar().setDisplayHomeAsUpEnabled(true);
- getSupportActionBar().setDisplayShowHomeEnabled(true);
- }
- spInterface = (Spinner)findViewById(R.id.spinnerinterface);
- spEndPoint = (Spinner)findViewById(R.id.spinnerendpoint);
- textInfo = (TextView) findViewById(R.id.info);
- textInfoInterface = (TextView)findViewById(R.id.infointerface);
- textEndPoint = (TextView)findViewById(R.id.infoendpoint);
- //register the broadcast receiver
- mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
- IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
- registerReceiver(mUsbReceiver, filter);
- registerReceiver(mUsbDeviceReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED));
- registerReceiver(mUsbDeviceReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED));
- connectUsb();
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem menuItem)
- {
- if(menuItem.getItemId() == android.R.id.home)
- finish();
- return super.onOptionsItemSelected(menuItem);
- }
- @Override
- protected void onDestroy() {
- releaseUsb();
- unregisterReceiver(mUsbReceiver);
- unregisterReceiver(mUsbDeviceReceiver);
- super.onDestroy();
- }
- private void connectUsb(){
- /*Toast.makeText(MainActivity.this,
- "connectUsb()",
- Toast.LENGTH_LONG).show();
- textStatus.setText("connectUsb()");*/
- checkDeviceInfo();
- if(deviceFound != null){
- doRawDescriptors();
- }
- }
- private void releaseUsb(){
- /*Toast.makeText(MainActivity.this,
- "releaseUsb()",
- Toast.LENGTH_LONG).show();
- textStatus.setText("releaseUsb()");*/
- if(usbDeviceConnection != null){
- if(usbInterface != null){
- usbDeviceConnection.releaseInterface(usbInterface);
- usbInterface = null;
- }
- usbDeviceConnection.close();
- usbDeviceConnection = null;
- }
- deviceFound = null;
- }
- private void doRawDescriptors(){
- UsbDevice deviceToRead = deviceFound;
- UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
- Boolean permitToRead = manager.hasPermission(deviceToRead);
- if(permitToRead){
- }else{
- manager.requestPermission(deviceToRead, mPermissionIntent);
- /*Toast.makeText(MainActivity.this,
- "Permission: " + permitToRead,
- Toast.LENGTH_LONG).show();
- textStatus.setText("Permission: " + permitToRead);*/
- }
- }
- private final BroadcastReceiver mUsbReceiver =
- new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (ACTION_USB_PERMISSION.equals(action)) {
- /*Toast.makeText(USBInfoAPI.this,
- "ACTION_USB_PERMISSION",
- Toast.LENGTH_LONG).show();
- textStatus.setText("ACTION_USB_PERMISSION");*/
- synchronized (this) {
- UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
- if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
- if(device != null){
- // doReadRawDescriptors(device);
- }
- }
- else {
- /*Toast.makeText(USBInfoAPI.this,
- "permission denied for device " + device,
- Toast.LENGTH_LONG).show();
- textStatus.setText("permission denied for device " + device);*/
- }
- }
- }
- }
- };
- private final BroadcastReceiver mUsbDeviceReceiver =
- new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
- deviceFound = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
- /*Toast.makeText(MainActivity.this,
- "ACTION_USB_DEVICE_ATTACHED: \n" +
- deviceFound.toString(),
- Toast.LENGTH_LONG).show();
- textStatus.setText("ACTION_USB_DEVICE_ATTACHED: \n" +
- deviceFound.toString());*/
- checkDeviceInfoSkipDeviceSearching();
- doRawDescriptors();
- }else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
- UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
- /*Toast.makeText(MainActivity.this,
- "ACTION_USB_DEVICE_DETACHED: \n" +
- device.toString(),
- Toast.LENGTH_LONG).show();
- textStatus.setText("ACTION_USB_DEVICE_DETACHED: \n" +
- device.toString());*/
- if(device!=null){
- if(device == deviceFound){
- releaseUsb();
- }
- }
- textInfo.setText("");
- textInfoInterface.setText("");
- textEndPoint.setText("");
- listInterface.clear();
- listUsbInterface.clear();
- adapterInterface.notifyDataSetChanged();
- listEndPoint.clear();
- listUsbEndpoint.clear();
- adapterEndpoint.notifyDataSetChanged();
- }
- }
- };
- private void checkDeviceInfo() {
- deviceFound = null;
- UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
- HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
- Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
- while (deviceIterator.hasNext()) {
- UsbDevice device = deviceIterator.next();
- //if(device.getVendorId()==targetVendorID){
- // if(device.getProductId()==targetProductID){
- deviceFound = device;
- // }
- // }
- }
- textInfo.setText("");
- textInfoInterface.setText("");
- textEndPoint.setText("");
- if(deviceFound==null){
- /*Toast.makeText(MainActivity.this,
- "device not found",
- Toast.LENGTH_LONG).show();
- textStatus.setText("device not found");*/
- }else{
- String i = deviceFound.toString() + "\n" +
- "DevicePath: " + deviceFound.getDeviceName() + "\n" +
- "DeviceClass: " + deviceFound.getDeviceClass() + " - "
- + translateDeviceClass(deviceFound.getDeviceClass()) + "\n" +
- "DeviceSubClass: " + deviceFound.getDeviceSubclass() + "\n" +
- "DeviceID: " + deviceFound.getDeviceId() + "\n" +
- "VendorID: " + deviceFound.getVendorId() + "\n" +
- "Vendor Name" + deviceFound.getDeviceName() + "\n" +
- "ProductID: " + deviceFound.getProductId() + "\n" +
- "Product Name" + deviceFound.getManufacturerName() + "\n" +
- "InterfaceCount: " + deviceFound.getInterfaceCount();
- textInfo.setText(i);
- checkUsbDevicve(deviceFound);
- }
- }
- private void checkDeviceInfoSkipDeviceSearching() {
- //called when ACTION_USB_DEVICE_ATTACHED,
- //device already found, skip device searching
- textInfo.setText("");
- textInfoInterface.setText("");
- textEndPoint.setText("");
- String i = deviceFound.toString() + "\n" +
- "DevicePath: " + deviceFound.getDeviceName() + "\n" +
- "DeviceClass: " + deviceFound.getDeviceClass() + " - "
- + translateDeviceClass(deviceFound.getDeviceClass()) + "\n" +
- "DeviceSubClass: " + deviceFound.getDeviceSubclass() + "\n" +
- "DeviceID: " + deviceFound.getDeviceId() + "\n" +
- "VendorID: " + deviceFound.getVendorId() + "\n" +
- "Vendor Name" + deviceFound.getDeviceName() + "\n" +
- "ProductID: " + deviceFound.getProductId() + "\n" +
- "Product Name" + deviceFound.getManufacturerName() + "\n" +
- "InterfaceCount: " + deviceFound.getInterfaceCount();
- textInfo.setText(i);
- checkUsbDevicve(deviceFound);
- }
- OnItemSelectedListener deviceOnItemSelectedListener =
- new OnItemSelectedListener(){
- @Override
- public void onItemSelected(AdapterView<?> parent,
- View view, int position, long id) {
- UsbDevice device = deviceFound;
- String i = device.toString() + "\n" +
- "DevicePath: " + deviceFound.getDeviceName() + "\n" +
- "DeviceClass: " + deviceFound.getDeviceClass() + " - "
- + translateDeviceClass(deviceFound.getDeviceClass()) + "\n" +
- "DeviceSubClass: " + deviceFound.getDeviceSubclass() + "\n" +
- "DeviceID: " + deviceFound.getDeviceId() + "\n" +
- "VendorID: " + deviceFound.getVendorId() + "\n" +
- "Vendor Name" + deviceFound.getDeviceName() + "\n" +
- "ProductID: " + deviceFound.getProductId() + "\n" +
- "Product Name" + deviceFound.getManufacturerName() + "\n" +
- "InterfaceCount: " + deviceFound.getInterfaceCount();
- textInfo.setText(i);
- checkUsbDevicve(device);
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {}
- };
- private void checkUsbDevicve(UsbDevice d) {
- listInterface = new ArrayList<String>();
- listUsbInterface = new ArrayList<UsbInterface>();
- for(int i=0; i<d.getInterfaceCount(); i++){
- UsbInterface usbif = d.getInterface(i);
- listInterface.add(usbif.toString());
- listUsbInterface.add(usbif);
- }
- adapterInterface = new ArrayAdapter<String>(this,
- android.R.layout.simple_spinner_item, listInterface);
- adapterInterface.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- spInterface.setAdapter(adapterInterface);
- spInterface.setOnItemSelectedListener(interfaceOnItemSelectedListener);
- }
- OnItemSelectedListener interfaceOnItemSelectedListener =
- new OnItemSelectedListener(){
- @Override
- public void onItemSelected(AdapterView<?> parent,
- View view, int position, long id) {
- UsbInterface selectedUsbIf = listUsbInterface.get(position);
- String sUsbIf = "\n" + selectedUsbIf.toString() + "\n"
- + "Id: " + selectedUsbIf.getId() + "\n"
- + "InterfaceClass: " + selectedUsbIf.getInterfaceClass() + "\n"
- + "InterfaceProtocol: " + selectedUsbIf.getInterfaceProtocol() + "\n"
- + "InterfaceSubclass: " + selectedUsbIf.getInterfaceSubclass() + "\n"
- + "EndpointCount: " + selectedUsbIf.getEndpointCount();
- textInfoInterface.setText(sUsbIf);
- checkUsbInterface(selectedUsbIf);
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {}
- };
- private void checkUsbInterface(UsbInterface uif) {
- listEndPoint = new ArrayList<String>();
- listUsbEndpoint = new ArrayList<UsbEndpoint>();
- for(int i=0; i<uif.getEndpointCount(); i++){
- UsbEndpoint usbEndpoint = uif.getEndpoint(i);
- listEndPoint.add(usbEndpoint.toString());
- listUsbEndpoint.add(usbEndpoint);
- }
- adapterEndpoint = new ArrayAdapter<String>(this,
- android.R.layout.simple_spinner_item, listEndPoint);
- adapterEndpoint.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- spEndPoint.setAdapter(adapterEndpoint);
- spEndPoint.setOnItemSelectedListener(endpointOnItemSelectedListener);
- }
- OnItemSelectedListener endpointOnItemSelectedListener =
- new OnItemSelectedListener(){
- @Override
- public void onItemSelected(AdapterView<?> parent,
- View view, int position, long id) {
- UsbEndpoint selectedEndpoint = listUsbEndpoint.get(position);
- String sEndpoint = "\n" + selectedEndpoint.toString() + "\n"
- + translateEndpointType(selectedEndpoint.getType());
- textEndPoint.setText(sEndpoint);
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {}
- };
- private String translateEndpointType(int type){
- switch(type){
- case UsbConstants.USB_ENDPOINT_XFER_CONTROL:
- return "USB_ENDPOINT_XFER_CONTROL (endpoint zero)";
- case UsbConstants.USB_ENDPOINT_XFER_ISOC:
- return "USB_ENDPOINT_XFER_ISOC (isochronous endpoint)";
- case UsbConstants.USB_ENDPOINT_XFER_BULK :
- return "USB_ENDPOINT_XFER_BULK (bulk endpoint)";
- case UsbConstants.USB_ENDPOINT_XFER_INT:
- return "USB_ENDPOINT_XFER_INT (interrupt endpoint)";
- default:
- return "unknown";
- }
- }
- private String translateDeviceClass(int deviceClass){
- switch(deviceClass){
- case UsbConstants.USB_CLASS_APP_SPEC:
- return "Application specific USB class";
- case UsbConstants.USB_CLASS_AUDIO:
- return "USB class for audio devices";
- case UsbConstants.USB_CLASS_CDC_DATA:
- return "USB class for CDC devices (communications device class)";
- case UsbConstants.USB_CLASS_COMM:
- return "USB class for communication devices";
- case UsbConstants.USB_CLASS_CONTENT_SEC:
- return "USB class for content security devices";
- case UsbConstants.USB_CLASS_CSCID:
- return "USB class for content smart card devices";
- case UsbConstants.USB_CLASS_HID:
- return "USB class for human interface devices (for example, mice and keyboards)";
- case UsbConstants.USB_CLASS_HUB:
- return "USB class for USB hubs";
- case UsbConstants.USB_CLASS_MASS_STORAGE:
- return "USB class for mass storage devices";
- case UsbConstants.USB_CLASS_MISC:
- return "USB class for wireless miscellaneous devices";
- case UsbConstants.USB_CLASS_PER_INTERFACE:
- return "USB class indicating that the class is determined on a per-interface basis";
- case UsbConstants.USB_CLASS_PHYSICA:
- return "USB class for physical devices";
- case UsbConstants.USB_CLASS_PRINTER:
- return "USB class for printers";
- case UsbConstants.USB_CLASS_STILL_IMAGE:
- return "USB class for still image devices (digital cameras)";
- case UsbConstants.USB_CLASS_VENDOR_SPEC:
- return "Vendor specific USB class";
- case UsbConstants.USB_CLASS_VIDEO:
- return "USB class for video devices";
- case UsbConstants.USB_CLASS_WIRELESS_CONTROLLER:
- return "USB class for wireless controller devices";
- default: return "Unknown USB class!";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment