如何在Android平板电脑中检测USB作为USB主机?

时间:2012-08-20 06:22:19

标签: android

我尝试借助USB Host的代码开发一个示例应用。

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

当我连接USB设备运行此代码时,我将Toast视为“未找到设备”。

任何人都可以帮我解决这个问题吗?

谢谢。

1 个答案:

答案 0 :(得分:2)

检查您的设备是否支持主机模式。 尝试这可能这可能会有所帮助:Android USB host and hidden devices