mirror of
https://github.com/mik3y/usb-serial-for-android.git
synced 2026-08-11 16:32:53 +00:00
API refactor, adding UsbSerialPort interface.
- UsbSerialDriver is now a discrete interface. - UsbSerialDriver provides getPorts() method, returning one or more usable UsbSerialPort. - Use of UsbDeviceConnection is deferred until open(), making it possible to probe for ports without permission from Android. (Thanks to Felix for inspiring some of these changes).
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
/* Copyright 2011 Google Inc.
|
||||
/* Copyright 2011-2013 Google Inc.
|
||||
* Copyright 2013 mike wakerly <opensource@hoho.com>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
@@ -15,7 +16,7 @@
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
||||
* USA.
|
||||
*
|
||||
* Project home page: http://code.google.com/p/usb-serial-for-android/
|
||||
* Project home page: https://github.com/mik3y/usb-serial-for-android
|
||||
*/
|
||||
|
||||
package com.hoho.android.usbserial.examples;
|
||||
@@ -41,6 +42,7 @@ import android.widget.TextView;
|
||||
import android.widget.TwoLineListItem;
|
||||
|
||||
import com.hoho.android.usbserial.driver.UsbSerialDriver;
|
||||
import com.hoho.android.usbserial.driver.UsbSerialPort;
|
||||
import com.hoho.android.usbserial.driver.UsbSerialProber;
|
||||
import com.hoho.android.usbserial.util.HexDump;
|
||||
|
||||
@@ -80,19 +82,8 @@ public class DeviceListActivity extends Activity {
|
||||
|
||||
};
|
||||
|
||||
/** Simple container for a UsbDevice and its driver. */
|
||||
private static class DeviceEntry {
|
||||
public UsbDevice device;
|
||||
public UsbSerialDriver driver;
|
||||
|
||||
DeviceEntry(UsbDevice device, UsbSerialDriver driver) {
|
||||
this.device = device;
|
||||
this.driver = driver;
|
||||
}
|
||||
}
|
||||
|
||||
private List<DeviceEntry> mEntries = new ArrayList<DeviceEntry>();
|
||||
private ArrayAdapter<DeviceEntry> mAdapter;
|
||||
private List<UsbSerialPort> mEntries = new ArrayList<UsbSerialPort>();
|
||||
private ArrayAdapter<UsbSerialPort> mAdapter;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
@@ -104,7 +95,8 @@ public class DeviceListActivity extends Activity {
|
||||
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
|
||||
mProgressBarTitle = (TextView) findViewById(R.id.progressBarTitle);
|
||||
|
||||
mAdapter = new ArrayAdapter<DeviceEntry>(this, android.R.layout.simple_expandable_list_item_2, mEntries) {
|
||||
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;
|
||||
@@ -116,14 +108,16 @@ public class DeviceListActivity extends Activity {
|
||||
row = (TwoLineListItem) convertView;
|
||||
}
|
||||
|
||||
final DeviceEntry entry = mEntries.get(position);
|
||||
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) entry.device.getVendorId()),
|
||||
HexDump.toHexString((short) entry.device.getProductId()));
|
||||
HexDump.toHexString((short) device.getVendorId()),
|
||||
HexDump.toHexString((short) device.getProductId()));
|
||||
row.getText1().setText(title);
|
||||
|
||||
final String subtitle = entry.driver != null ?
|
||||
entry.driver.getClass().getSimpleName() : "No Driver";
|
||||
final String subtitle = driver.getClass().getSimpleName();
|
||||
row.getText2().setText(subtitle);
|
||||
|
||||
return row;
|
||||
@@ -141,14 +135,8 @@ public class DeviceListActivity extends Activity {
|
||||
return;
|
||||
}
|
||||
|
||||
final DeviceEntry entry = mEntries.get(position);
|
||||
final UsbSerialDriver driver = entry.driver;
|
||||
if (driver == null) {
|
||||
Log.d(TAG, "No driver.");
|
||||
return;
|
||||
}
|
||||
|
||||
showConsoleActivity(driver);
|
||||
final UsbSerialPort port = mEntries.get(position);
|
||||
showConsoleActivity(port);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -168,31 +156,28 @@ public class DeviceListActivity extends Activity {
|
||||
private void refreshDeviceList() {
|
||||
showProgressBar();
|
||||
|
||||
new AsyncTask<Void, Void, List<DeviceEntry>>() {
|
||||
new AsyncTask<Void, Void, List<UsbSerialPort>>() {
|
||||
@Override
|
||||
protected List<DeviceEntry> doInBackground(Void... params) {
|
||||
protected List<UsbSerialPort> doInBackground(Void... params) {
|
||||
Log.d(TAG, "Refreshing device list ...");
|
||||
SystemClock.sleep(1000);
|
||||
final List<DeviceEntry> result = new ArrayList<DeviceEntry>();
|
||||
for (final UsbDevice device : mUsbManager.getDeviceList().values()) {
|
||||
final List<UsbSerialDriver> drivers =
|
||||
UsbSerialProber.probeSingleDevice(mUsbManager, device);
|
||||
Log.d(TAG, "Found usb device: " + device);
|
||||
if (drivers.isEmpty()) {
|
||||
Log.d(TAG, " - No UsbSerialDriver available.");
|
||||
result.add(new DeviceEntry(device, null));
|
||||
} else {
|
||||
for (UsbSerialDriver driver : drivers) {
|
||||
Log.d(TAG, " + " + driver);
|
||||
result.add(new DeviceEntry(device, driver));
|
||||
}
|
||||
}
|
||||
|
||||
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<DeviceEntry> result) {
|
||||
protected void onPostExecute(List<UsbSerialPort> result) {
|
||||
mEntries.clear();
|
||||
mEntries.addAll(result);
|
||||
mAdapter.notifyDataSetChanged();
|
||||
@@ -214,8 +199,8 @@ public class DeviceListActivity extends Activity {
|
||||
mProgressBar.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
|
||||
private void showConsoleActivity(UsbSerialDriver driver) {
|
||||
SerialConsoleActivity.show(this, driver);
|
||||
private void showConsoleActivity(UsbSerialPort port) {
|
||||
SerialConsoleActivity.show(this, port);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+31
-20
@@ -1,4 +1,5 @@
|
||||
/* Copyright 2011 Google Inc.
|
||||
/* Copyright 2011-2013 Google Inc.
|
||||
* Copyright 2013 mike wakerly <opensource@hoho.com>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
@@ -15,7 +16,7 @@
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
||||
* USA.
|
||||
*
|
||||
* Project home page: http://code.google.com/p/usb-serial-for-android/
|
||||
* Project home page: https://github.com/mik3y/usb-serial-for-android
|
||||
*/
|
||||
|
||||
package com.hoho.android.usbserial.examples;
|
||||
@@ -23,12 +24,14 @@ package com.hoho.android.usbserial.examples;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.hardware.usb.UsbDeviceConnection;
|
||||
import android.hardware.usb.UsbManager;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.widget.ScrollView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.hoho.android.usbserial.driver.UsbSerialDriver;
|
||||
import com.hoho.android.usbserial.driver.UsbSerialPort;
|
||||
import com.hoho.android.usbserial.util.HexDump;
|
||||
import com.hoho.android.usbserial.util.SerialInputOutputManager;
|
||||
|
||||
@@ -37,7 +40,7 @@ import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* Monitors a single {@link UsbSerialDriver} instance, showing all data
|
||||
* Monitors a single {@link UsbSerialPort} instance, showing all data
|
||||
* received.
|
||||
*
|
||||
* @author mike wakerly (opensource@hoho.com)
|
||||
@@ -48,7 +51,7 @@ public class SerialConsoleActivity extends Activity {
|
||||
|
||||
/**
|
||||
* Driver instance, passed in statically via
|
||||
* {@link #show(Context, UsbSerialDriver)}.
|
||||
* {@link #show(Context, UsbSerialPort)}.
|
||||
*
|
||||
* <p/>
|
||||
* This is a devious hack; it'd be cleaner to re-create the driver using
|
||||
@@ -56,7 +59,7 @@ public class SerialConsoleActivity extends Activity {
|
||||
* can get away with it because both activities will run in the same
|
||||
* process, and this is a simple demo.
|
||||
*/
|
||||
private static UsbSerialDriver sDriver = null;
|
||||
private static UsbSerialPort sPort = null;
|
||||
|
||||
private TextView mTitleTextView;
|
||||
private TextView mDumpTextView;
|
||||
@@ -98,13 +101,13 @@ public class SerialConsoleActivity extends Activity {
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
stopIoManager();
|
||||
if (sDriver != null) {
|
||||
if (sPort != null) {
|
||||
try {
|
||||
sDriver.close();
|
||||
sPort.close();
|
||||
} catch (IOException e) {
|
||||
// Ignore.
|
||||
}
|
||||
sDriver = null;
|
||||
sPort = null;
|
||||
}
|
||||
finish();
|
||||
}
|
||||
@@ -112,25 +115,33 @@ public class SerialConsoleActivity extends Activity {
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
Log.d(TAG, "Resumed, sDriver=" + sDriver);
|
||||
if (sDriver == null) {
|
||||
Log.d(TAG, "Resumed, port=" + sPort);
|
||||
if (sPort == null) {
|
||||
mTitleTextView.setText("No serial device.");
|
||||
} else {
|
||||
final UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
|
||||
|
||||
UsbDeviceConnection connection = usbManager.openDevice(sPort.getDriver().getDevice());
|
||||
if (connection == null) {
|
||||
mTitleTextView.setText("Opening device failed");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
sDriver.open();
|
||||
sDriver.setParameters(115200, 8, UsbSerialDriver.STOPBITS_1, UsbSerialDriver.PARITY_NONE);
|
||||
sPort.open(connection);
|
||||
sPort.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Error setting up device: " + e.getMessage(), e);
|
||||
mTitleTextView.setText("Error opening device: " + e.getMessage());
|
||||
try {
|
||||
sDriver.close();
|
||||
sPort.close();
|
||||
} catch (IOException e2) {
|
||||
// Ignore.
|
||||
}
|
||||
sDriver = null;
|
||||
sPort = null;
|
||||
return;
|
||||
}
|
||||
mTitleTextView.setText("Serial device: " + sDriver.getClass().getSimpleName());
|
||||
mTitleTextView.setText("Serial device: " + sPort.getClass().getSimpleName());
|
||||
}
|
||||
onDeviceStateChange();
|
||||
}
|
||||
@@ -144,9 +155,9 @@ public class SerialConsoleActivity extends Activity {
|
||||
}
|
||||
|
||||
private void startIoManager() {
|
||||
if (sDriver != null) {
|
||||
if (sPort != null) {
|
||||
Log.i(TAG, "Starting io manager ..");
|
||||
mSerialIoManager = new SerialInputOutputManager(sDriver, mListener);
|
||||
mSerialIoManager = new SerialInputOutputManager(sPort, mListener);
|
||||
mExecutor.submit(mSerialIoManager);
|
||||
}
|
||||
}
|
||||
@@ -169,8 +180,8 @@ public class SerialConsoleActivity extends Activity {
|
||||
* @param context
|
||||
* @param driver
|
||||
*/
|
||||
static void show(Context context, UsbSerialDriver driver) {
|
||||
sDriver = driver;
|
||||
static void show(Context context, UsbSerialPort port) {
|
||||
sPort = port;
|
||||
final Intent intent = new Intent(context, SerialConsoleActivity.class);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY);
|
||||
context.startActivity(intent);
|
||||
|
||||
Reference in New Issue
Block a user