如何检测USB设备在Android平板电脑充当USB主机? [英] How to detect USB device in Android tablet which acts as USB Host?
问题描述
我试图发展与从的 Developers.android.com 。
我的code看起来像这样
My code looks like this
public class MainActivity extends Activity {
UsbManager manager;
HashMap<String, UsbDevice> deviceList;
Button scanButton;
UsbDevice device;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scanButton = (Button)this.findViewById(R.id.button1);
scanButton.setOnClickListener(new OnClickListener ()
{
public void onClick(View v)
{
checkForDevices ();
}
});
}
@Override
public void onResume ()
{
super.onResume();
checkForDevices ();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
protected void checkForDevices ()
{
manager = (UsbManager) getSystemService(Context.USB_SERVICE);
deviceList = manager.getDeviceList();
device = deviceList.get("deviceName");
//Collection<UsbDevice> devices = deviceList.values();
if (device != null)
Toast.makeText(this, "Device Found", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "Device NOT Found", Toast.LENGTH_LONG).show();
}
}
当我运行这个code连接了USB设备时,我总是得到面包为设备未找到。
When I run this code with a USB device connected, I always get the Toast as "Device NOT Found".
我想我的应用程序检测到USB设备,读取输入和USB主机模式写输出。
I want my app to detect the USB device and Read input and Write Output in USB Host Mode.
有什么方法来检测USB设备在我们的应用程序?
Is there any way to detect an USB device in our App?
推荐答案
我有同样的问题,我怀疑是主机功能可能会在平板电脑上被禁用,因此我建议你检查。
I had the same problem and I suspect that the host feature may be disabled on your tablet so I suggest that you check.
以下职位可能是看,因为它简洁的最佳参考
Android的USB主机和隐藏的设备
The following post is probably the best reference to look at as it is concise Android USB host and hidden devices
您应该能够检查文件的 android.hardware.usb.host.xml 与亚行外壳存在
You should be able to check if the file android.hardware.usb.host.xml exist with the adb shell
这篇关于如何检测USB设备在Android平板电脑充当USB主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!