我正在使用此示例https://github.com/mik3y/usb-serial-for-android。为了实现与Android的外部设备的串行通信,该示例可行,Android在检测到外部设备已连接至其USB端口时执行以下操作:
public View getView(int position, View convertView, ViewGroup parent)
创建可用端口列表并等待方法
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
通过按列出的端口等待选择它以执行方法
private void showConsoleActivity(UsbSerialPort port)
我需要更改此顺序,以便Android一旦检测到有可用的USB端口,就会自动运行该方法
private void showConsoleActivity(UsbSerialPort port)
我的问题是如何获取参数“端口”,以便执行此方法/ 有人可以帮助我吗?
public class DeviceListActivity extends Activity {
private final String TAG = DeviceListActivity.class.getSimpleName();
private UsbManager mUsbManager;
private ListView mListView;
private static final int MESSAGE_REFRESH = 101;
private static final long REFRESH_TIMEOUT_MILLIS = 2500;
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_REFRESH:
refreshDeviceList();
mHandler.sendEmptyMessageDelayed(MESSAGE_REFRESH, REFRESH_TIMEOUT_MILLIS);
break;
default:
super.handleMessage(msg);
break;
}
}
};
private List<UsbSerialPort> mEntries = new ArrayList<UsbSerialPort>();
private ArrayAdapter<UsbSerialPort> mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
mListView = (ListView) findViewById(R.id.deviceList);
mAdapter = new ArrayAdapter<UsbSerialPort>(this, android.R.layout.simple_expandable_list_item_2, mEntries) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final TwoLineListItem row;
if (convertView == null){
final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = (TwoLineListItem) inflater.inflate(android.R.layout.simple_list_item_2, null);
} else {
row = (TwoLineListItem) convertView;
}
final UsbSerialPort port = mEntries.get(position);
final UsbSerialDriver driver = port.getDriver();
final UsbDevice device = driver.getDevice();
final String title = String.format("Vendor %s Product %s",HexDump.toHexString((short) device.getVendorId()),HexDump.toHexString((short) device.getProductId()));
row.getText1().setText(title);
final String subtitle = driver.getClass().getSimpleName();
row.getText2().setText(subtitle);
return row;
}
};
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "Pressed item " + position);
if (position >= mEntries.size()) {
Log.w(TAG, "Illegal position.");
return;
}
final UsbSerialPort port = mEntries.get(position);
showConsoleActivity(port);
}
});
}
@Override
protected void onResume() {
super.onResume();
mHandler.sendEmptyMessage(MESSAGE_REFRESH);
}
@Override
protected void onPause() {
super.onPause();
mHandler.removeMessages(MESSAGE_REFRESH);
}
private void refreshDeviceList() {
new AsyncTask<Void, Void, List<UsbSerialPort>>() {
@Override
protected List<UsbSerialPort> doInBackground(Void... params) {
Log.d(TAG, "Refreshing device list ...");
SystemClock.sleep(1000);
final List<UsbSerialDriver> drivers = UsbSerialProber.getDefaultProber().findAllDrivers(mUsbManager);
final List<UsbSerialPort> result = new ArrayList<UsbSerialPort>();
for (final UsbSerialDriver driver : drivers) {
final List<UsbSerialPort> ports = driver.getPorts();
Log.d(TAG, String.format("+ %s: %s port%s",driver, Integer.valueOf(ports.size()), ports.size() == 1 ? "" : "s"));
result.addAll(ports);
}
return result;
}
@Override
protected void onPostExecute(List<UsbSerialPort> result) {
mEntries.clear();
mEntries.addAll(result);
mAdapter.notifyDataSetChanged();
int usb_ON = 1;
Log.d(TAG, "Done refreshing, " + mEntries.size() + " entries found.");
}
}.execute((Void) null);
if (mEntries.size() > 0){
Toast.makeText( this, "Exito", Toast.LENGTH_SHORT ).show();
final UsbSerialPort port = mEntries.get(int position);
showConsoleActivity(port);
}
}
private void showConsoleActivity(UsbSerialPort port) {
SerialConsoleActivity.show(this, port);
}
}