2

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

4

2 Answers 2

1

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

0
0

Android Docs:

getDeviceList()

Returns a HashMap containing all USB devices currently attached. USB device name is the key for the returned HashMap. The result will be empty if no devices are attached, or if USB host mode is inactive or unsupported.

Try adding this in your manifest file.

<uses-feature
    android:name="android.hardware.usb.host"
    android:required="true"/>

https://stackoverflow.com/a/26728839/3225001 provides details on how to check the usbhost capabilities of the device.

4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.