I am using this example https://github.com/mik3y/usb-serial-for-android. to achieve a serial communication of an external device with the Android, the example works, the Android when detecting that an external device is connected to its USB port executes the:
creating a list of available ports and waiting with the method
waiting for it to be selected by pressing the port listed to execute the method
I need to change this sequence, so that as soon as the Android detects that there is a usb port available automatically run the method
my problem is how do I get the parameter "port" so that this method is executed.
Someone can help me?
JavaScript:
public View getView(int position, View convertView, ViewGroup parent)
creating a list of available ports and waiting with the method
JavaScript:
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
waiting for it to be selected by pressing the port listed to execute the method
JavaScript:
private void showConsoleActivity(UsbSerialPort port)
I need to change this sequence, so that as soon as the Android detects that there is a usb port available automatically run the method
JavaScript:
private void showConsoleActivity(UsbSerialPort port)
my problem is how do I get the parameter "port" so that this method is executed.
Someone can help me?
JavaScript:
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);
}