android - 在 Android 中查找 USB 设备

标签 android keyboard usb galaxy

我正在使用 Galaxy SII(Android 版本 4.1.2),并且我正在尝试构建一个应用程序,它可以在我连接 USB 设备时告诉我。

我还有一个 OTG 适配器(非常好..)当我连接键盘/鼠标时,我的 Android 可以识别它们并且我可以顺利使用它们。

在我的代码中,我尝试遍历已连接的设备,但好像没有任何连接。这是我的代码:

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();            
}

这是 list :

<?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>

感谢您的帮助:)

最佳答案

显然 Galaxy S2 不支持 USB 1.x 连接。

在这里找到:http://www.xda-developers.com/android/samsung-galaxy-s-i9000-gains-usb-host-functionality/

关于android - 在 Android 中查找 USB 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19181009/

相关文章:

android - 具有单独行布局的 ListView 无法正确生成 View

ios - 键盘始终显示在 ios 中

Android USB Host模式服务——基于USB_DEVICE_ATTACHED启动

USB闪存驱动器上的Ubuntu

android - 在 Android 9 上绕过 android usb 主机权限确认对话框

android - 更改操作栏选项卡高度

android - 如果一次被多次调用,如何避免多次显示对话框?

android - 如果像 flipkart 价格范围一样在 RecyclerView 中检查过,如何将复选框设置为 true

iphone - 当我在 UITextField 外部单击到 UITableView 上时,如何退出FirstResponder

ios - 默认 iPad 字母数字键盘进入数字模式