3

I'm using Galaxy SII (Android version 4.1.2), and I'm trying to build an app that can tell me if I connect an USB device.

I also have an OTG adapter (that works great..) When I connect a keyboard/mice, my Android recognizes them and I can use them smoothly.

In my code, I try to iterate over the connected devices, but it's like nothing is connected. This is my code:

UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
// Get the list of attached devices
HashMap<String, UsbDevice> devices = manager.getDeviceList();
    
Toast.makeText(this, "Number of devices: " + devices.size(), Toast.LENGTH_LONG).show();     

// Iterate over all devices
Iterator<String> it = devices.keySet().iterator();
while (it.hasNext())
{
  String deviceName = it.next();
  UsbDevice device = devices.get(deviceName);
  
  String VID = Integer.toHexString(device.getVendorId()).toUpperCase();
  String PID = Integer.toHexString(device.getProductId()).toUpperCase();
  Toast.makeText(this, deviceName + " " +  VID + ":" + PID + " " + manager.hasPermission(device), Toast.LENGTH_LONG).show();            
}

This is the Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.ipad"
  android:versionCode="1"
  android:versionName="1.0" >

  <uses-feature android:name="android.hardware.usb.host"/>
  <uses-sdk android:minSdkVersion="12" />

  <application
  android:allowBackup="true"
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name"
  android:theme="@style/AppTheme">         
  <activity
      android:name="com.example.ipad.MainActivity"
      android:label="@string/app_name" >
      <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />               
      </intent-filter>            
    </activity>   
  </application>
</manifest>

Thanks for your help :)

Unheilig
  • 16,196
  • 193
  • 68
  • 98
Rubi
  • 41
  • 3

1 Answers1

1

apparently Galaxy S2 doesn't support Usb 1.x connections.

Found here: http://www.xda-developers.com/android/samsung-galaxy-s-i9000-gains-usb-host-functionality/

Rubi
  • 41
  • 3