如何获取连接到安卓设备的 USB 配件列表? [英] How to get list of USB accessories connected to android device?

查看:44
本文介绍了如何获取连接到安卓设备的 USB 配件列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于棒棒糖的安卓盒子.盒子有一个 USB 端口,类似于 Android 手机中的微型 USB 端口.我想检查是否有任何主机连接到 USB 端口与设备模式下的 android 盒,类似于手机连接到 PC 时.至少,我只需要检查是否有什么东西连接到这个 USB 端口,最好是获取连接主机的一些信息(制造商、型号、串行).我不需要任何数据通信.

I have a Lollipop based android box. The box has a USB port similarly to micro USB port in Android phones. I want to check if any host is connected to the USB port with the android box in device mode similarly to when a phone is connected to a PC. At the minimum, I just need to check if something is connected to this USB port and at best get some info(manufacturer, model, serial) of the host connected. I don't need any data communication.

我试过了

UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
manager.getAccessoryList();

但即使我已将笔记本电脑连接到它并且 adb 和文件传输工作正常,它仍然是空的.

but it turns up empty even though I have connected my laptop to it and adb and file transfers are working.

尽管在进一步阅读中,android USB 配件 API 似乎仅限于特殊的 android 配件.

Although on further reading it seems android USB accessories API is limited to special android accessories only.

推荐答案

UsbManager m = (UsbManager)getApplicationContext().getSystemService(USB_SERVICE);
HashMap<String, UsbDevice> usbDevices = m.getDeviceList();
Collection<UsbDevice> ite = usbDevices.values();
UsbDevice[] usbs = ite.toArray(new UsbDevice[]{});
for (UsbDevice usb : usbs) {    
    Log.d("Connected usb devices","Connected usb devices are "+ usb.getDeviceName());
}

这篇关于如何获取连接到安卓设备的 USB 配件列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆