1. Are you ready for the Galaxy S20? Here is everything we know so far!

Doubts with serial usb android

Discussion in 'Android Development' started by pilar, Oct 16, 2018.

  1. pilar

    pilar Lurker
    Thread Starter

    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:
    Code (Javascript):
    1. public View getView(int position, View convertView, ViewGroup parent)
    creating a list of available ports and waiting with the method
    Code (Javascript):
    1. 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
    Code (Javascript):
    1. 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

    Code (Javascript):
    1. 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?

    Code (Javascript):
    1. public class DeviceListActivity extends Activity {
    2.  
    3. private final String TAG = DeviceListActivity.class.getSimpleName();
    4.  
    5. private UsbManager mUsbManager;
    6. private ListView mListView;
    7.  
    8. private static final int MESSAGE_REFRESH = 101;
    9. private static final long REFRESH_TIMEOUT_MILLIS = 2500;
    10.  
    11. private final Handler mHandler = new Handler() {
    12.     @Override
    13.     public void handleMessage(Message msg) {
    14.         switch (msg.what) {
    15.             case MESSAGE_REFRESH:
    16.                 refreshDeviceList();
    17.                 mHandler.sendEmptyMessageDelayed(MESSAGE_REFRESH, REFRESH_TIMEOUT_MILLIS);
    18.                 break;
    19.             default:
    20.                 super.handleMessage(msg);
    21.                 break;
    22.         }
    23.     }
    24.  
    25. };
    26.  
    27. private List<UsbSerialPort> mEntries = new ArrayList<UsbSerialPort>();
    28. private ArrayAdapter<UsbSerialPort> mAdapter;
    29.  
    30. @Override
    31. public void onCreate(Bundle savedInstanceState) {
    32.     super.onCreate(savedInstanceState);
    33.     setContentView(R.layout.main);
    34.  
    35.     mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    36.     mListView = (ListView) findViewById(R.id.deviceList);
    37.  
    38.     mAdapter = new ArrayAdapter<UsbSerialPort>(this, android.R.layout.simple_expandable_list_item_2, mEntries) {
    39.         @Override
    40.         public View getView(int position, View convertView, ViewGroup parent) {
    41.             final TwoLineListItem row;
    42.             if (convertView == null){
    43.                 final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    44.                 row = (TwoLineListItem) inflater.inflate(android.R.layout.simple_list_item_2, null);
    45.             } else {
    46.                 row = (TwoLineListItem) convertView;
    47.             }
    48.  
    49.             final UsbSerialPort port = mEntries.get(position);
    50.             final UsbSerialDriver driver = port.getDriver();
    51.             final UsbDevice device = driver.getDevice();
    52.  
    53.             final String title = String.format("Vendor %s Product %s",HexDump.toHexString((short) device.getVendorId()),HexDump.toHexString((short) device.getProductId()));
    54.             row.getText1().setText(title);
    55.  
    56.             final String subtitle = driver.getClass().getSimpleName();
    57.             row.getText2().setText(subtitle);
    58.  
    59.             return row;
    60.         }
    61.  
    62.     };
    63.     mListView.setAdapter(mAdapter);
    64.  
    65.     mListView.setOnItemClickListener(new ListView.OnItemClickListener() {
    66.         @Override
    67.         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    68.             Log.d(TAG, "Pressed item " + position);
    69.             if (position >= mEntries.size()) {
    70.                 Log.w(TAG, "Illegal position.");
    71.                 return;
    72.             }
    73.             final UsbSerialPort port = mEntries.get(position);
    74.             showConsoleActivity(port);
    75.         }
    76.     });
    77. }
    78.  
    79. @Override
    80. protected void onResume() {
    81.     super.onResume();
    82.     mHandler.sendEmptyMessage(MESSAGE_REFRESH);
    83. }
    84.  
    85. @Override
    86. protected void onPause() {
    87.     super.onPause();
    88.     mHandler.removeMessages(MESSAGE_REFRESH);
    89. }
    90.  
    91. private void refreshDeviceList() {
    92.  
    93.     new AsyncTask<Void, Void, List<UsbSerialPort>>() {
    94.         @Override
    95.         protected List<UsbSerialPort> doInBackground(Void... params) {
    96.             Log.d(TAG, "Refreshing device list ...");
    97.             SystemClock.sleep(1000);
    98.  
    99.             final List<UsbSerialDriver> drivers = UsbSerialProber.getDefaultProber().findAllDrivers(mUsbManager);
    100.  
    101.             final List<UsbSerialPort> result = new ArrayList<UsbSerialPort>();
    102.             for (final UsbSerialDriver driver : drivers) {
    103.                 final List<UsbSerialPort> ports = driver.getPorts();
    104.                 Log.d(TAG, String.format("+ %s: %s port%s",driver, Integer.valueOf(ports.size()), ports.size() == 1 ? "" : "s"));
    105.                 result.addAll(ports);
    106.             }
    107.  
    108.             return result;
    109.         }
    110.  
    111.         @Override
    112.         protected void onPostExecute(List<UsbSerialPort> result) {
    113.             mEntries.clear();
    114.             mEntries.addAll(result);
    115.             mAdapter.notifyDataSetChanged();
    116.             int usb_ON = 1;
    117.                 Log.d(TAG, "Done refreshing, " + mEntries.size() + " entries found.");
    118.           }
    119.  
    120.     }.execute((Void) null);
    121.  
    122.     if (mEntries.size() > 0){
    123.         Toast.makeText( this, "Exito", Toast.LENGTH_SHORT ).show();
    124.         final UsbSerialPort port = mEntries.get(int position);
    125.         showConsoleActivity(port);
    126.  
    127.     }
    128. }
    129.  
    130. private void showConsoleActivity(UsbSerialPort port) {
    131.     SerialConsoleActivity.show(this, port);
    132. }
     


Loading...
Similar Threads - Doubts serial usb
  1. akmishra_99
    Replies:
    0
    Views:
    172
  2. andreaalvise
    Replies:
    17
    Views:
    578
  3. andreaalvise
    Replies:
    33
    Views:
    1,123
  4. kin4o
    Replies:
    3
    Views:
    846
  5. Sumit Mourya
    Replies:
    0
    Views:
    665
  6. ApolloDG
    Replies:
    1
    Views:
    303
  7. Mini500
    Replies:
    20
    Views:
    3,438
  8. Kishore1308
    Replies:
    0
    Views:
    332
  9. pilar
    Replies:
    3
    Views:
    549
  10. harishwaran27
    Replies:
    1
    Views:
    4,730

Share This Page

Loading...