使用android USB API 无法获取USB设备的解决方法

  1. UsbManager manager = (UsbManager) m_context.getSystemService(Context.USB_SERVICE);  
  2. HashMap<String, UsbDevice> deviceList = manager.getDeviceList();  
  3. Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();  
  4.   
  5. while (deviceIterator.hasNext())  
  6. {  
  7.     UsbDevice device = deviceIterator.next();  
  8.     m_devList.add(device.getDeviceName());  

上面代码getDeviceList返回列表为空,即使用UsbManager.getDeviceList获取不到USB设备信息!
需要进行如下处理才能获取到USB设备信息:
在目录(/system/etc/permissions)下,添加一个名为(android.hardware.usb.host.xml)的文件;
其内容包含如下的信息:
<permissions>
 <feature name="android.hardware.usb.host" />
</permissions>

这该目录中,找到文件handheld_core_hardware.xml)或(tablet_core_hardware.xml);
在它的(<permissions>)段,添加一句(<featurename="android.hardware.usb.host" />);
然后,重启设备,应该就可以了!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
获取连接到 Android 设备USB 设备,您可以使用 AndroidUSB 主机 API。以下是获取连接设备的步骤: 1. 获取 USB 设备管理器 ``` UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE); ``` 2. 获取已连接的 USB 设备列表 ``` HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList(); ``` 3. 获取特定的 USB 设备 ``` UsbDevice device = deviceList.get(deviceName); ``` 其中,deviceName 是您想要获取USB 设备的名称。 4. 检查 USB 设备的权限 在 Android 中,USB 设备需要获得权限才能被访问。您可以使用 UsbManager 的 hasPermission() 方法来检查 USB 设备是否已被授予权限: ``` if (!usbManager.hasPermission(device)) { // 请求权限 PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); usbManager.requestPermission(device, pi); return; } ``` 在这里,ACTION_USB_PERMISSION 是自定义的广播,用于接收 USB 设备许可的结果。 5. 获取 USB 设备的接口 ``` UsbInterface usbInterface = device.getInterface(interfaceIndex); ``` 其中,interfaceIndex 表示 USB 设备的接口索引。 6. 获取 USB 设备的端点 ``` UsbEndpoint endpoint = usbInterface.getEndpoint(endpointIndex); ``` 其中,endpointIndex 表示 USB 设备的端点索引。 现在,您就可以使用 endpoint 对 USB 设备进行读写操作了。 注意:使用 USB 主机 API 需要在 AndroidManifest.xml 文件中添加以下权限: ``` <uses-feature android:name="android.hardware.usb.host" /> <uses-permission android:name="android.permission.USB_PERMISSION" /> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值