Search code examples
androidusbusb-hostcontroller

getDeviceList() always gives empty in Android


Working in Samsung S4 device, but not working in MotoG, Samsung A5(2016)

Below Code always return zero, I tried with giving permission in manifest file but still it returns null.. Can anyone give me any suggestion

In the Activity code:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        UsbManager usbManager = (UsbManager) getSystemService(USB_SERVICE);
        HashMap<String, UsbDevice> devicelist = usbManager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = devicelist.values().iterator();
        Log.i("List", "size =" + devicelist.size());
        Log.i("List", "List" + devicelist);

        while(deviceIterator.hasNext()) {
            UsbDevice usbDevice = deviceIterator.next();
            Log.i("List", "Model     : " +usbDevice.getDeviceName());
            Log.i("List", "Id        : " +usbDevice.getDeviceId());

        }
    }

}

Manifest Code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pathtech.usbapp" >

    <permission android:name="android.hardware.usb.host"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
            </intent-filter>
        </activity>
        <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
            android:resource="@xml/device_filter" />
    </application>
</manifest>

manager.getDeviceList() always return zero Can anyone give me any suggestion? Log information gives like below:

09-12 23:26:28.692    5053-5053/com.example.usbapp I/List﹕ size =0
09-12 23:26:28.692    5053-5053/com.example.usbapp I/List﹕ List{ }

Please give me suggestions.. Thanks in advance


Solution

  • It works fine for me(for Samsung devices) thanku to @Gurupad Mamadapur,

    To enable USB host API support you should add a file(xml file) and that containing the following lines:

    <permissions>
     <feature name="android.hardware.usb.host"/>
    </permissions>
    

    into folder(Location of the file)

    /system/etc/permissions
    

    in that folder find file named handheld_core_hardware.xml or tablet_core_hardware.xml

    and add

    <feature name="android.hardware.usb.host" />
    

    and Reboot your device. Usb host api should work.

    Source: Android USB host and hidden devices

    But for Motog device still not detecting