Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : how to detect already connected usb device?

Tags:

android

usb

I'm trying to detect usb devices which are already connected to android. I understand there are actions to detect when USB is either attached or detached. But I don't really get how to check devices after connecting usb device to android. Also, I've found that each USB device has it's device class code, but how do I figure out what kind of device is connected? For instance, I need to detect both usb mouse and keyboard; how do I differentiate them?

like image 354
Junsfavorite Avatar asked May 12 '15 05:05

Junsfavorite


People also ask

How can I see all the devices connected to my USB?

In Device Manager, select your computer so that it's highlighted. Select Action, and then select Scan for hardware changes. Select View, and then select Hidden Devices to display additional devices (for example, those that are not currently active).

Where is the USB notification on Android?

Depending on your Android version, you must do either of the following: Drag down the status bar, tap Connected as <connection type> under Notifications, and select Camera (PTP). Go to Settings > Storage > Menu > USB computer connection, and select Camera (PTP).

How do I check USB connection history?

If you don't know the name of your computer, go to Settings > System > About. Your computer name is shown at the top. Click the Start button to see the USB history. You can then expand the results to see details such as the time and date it was last used.


1 Answers

Try this:

  1. First register Broadcast for USB connection. manifest permission:

:

<intent-filter> <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> </intent-filter>
  1. Get the List of USB Device with details by using this

    public void getDetail() {
    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();
    
        manager.requestPermission(device, mPermissionIntent);
        String Model = device.getDeviceName();
    
        int DeviceID = device.getDeviceId();
        int Vendor = device.getVendorId();
        int Product = device.getProductId();
        int Class = device.getDeviceClass();
        int Subclass = device.getDeviceSubclass();
    
    }}
    
like image 96
Anshuman Avatar answered Oct 06 '22 00:10

Anshuman