我尝试借助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视为“未找到设备”。
任何人都可以帮我解决这个问题吗?
谢谢。