我尝试在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 显示为“未找到设备”。
谁能帮我解决这个问题?
谢谢你。