mirror of
https://github.com/mik3y/usb-serial-for-android.git
synced 2026-08-19 04:12:55 +00:00
compile+target sdk 28, gradle 4.6
This commit is contained in:
+206
@@ -0,0 +1,206 @@
|
||||
/* 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
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
||||
* USA.
|
||||
*
|
||||
* Project home page: https://github.com/mik3y/usb-serial-for-android
|
||||
*/
|
||||
|
||||
package com.hoho.android.usbserial.examples;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.hardware.usb.UsbDevice;
|
||||
import android.hardware.usb.UsbManager;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.os.SystemClock;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.ListView;
|
||||
import android.widget.ProgressBar;
|
||||
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;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Shows a {@link ListView} of available USB devices.
|
||||
*
|
||||
* @author mike wakerly (opensource@hoho.com)
|
||||
*/
|
||||
public class DeviceListActivity extends Activity {
|
||||
|
||||
private final String TAG = DeviceListActivity.class.getSimpleName();
|
||||
|
||||
private UsbManager mUsbManager;
|
||||
private ListView mListView;
|
||||
private TextView mProgressBarTitle;
|
||||
private ProgressBar mProgressBar;
|
||||
|
||||
private static final int MESSAGE_REFRESH = 101;
|
||||
private static final long REFRESH_TIMEOUT_MILLIS = 5000;
|
||||
|
||||
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);
|
||||
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
|
||||
mProgressBarTitle = (TextView) findViewById(R.id.progressBarTitle);
|
||||
|
||||
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() {
|
||||
showProgressBar();
|
||||
|
||||
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();
|
||||
mProgressBarTitle.setText(
|
||||
String.format("%s device(s) found",Integer.valueOf(mEntries.size())));
|
||||
hideProgressBar();
|
||||
Log.d(TAG, "Done refreshing, " + mEntries.size() + " entries found.");
|
||||
}
|
||||
|
||||
}.execute((Void) null);
|
||||
}
|
||||
|
||||
private void showProgressBar() {
|
||||
mProgressBar.setVisibility(View.VISIBLE);
|
||||
mProgressBarTitle.setText(R.string.refreshing);
|
||||
}
|
||||
|
||||
private void hideProgressBar() {
|
||||
mProgressBar.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
|
||||
private void showConsoleActivity(UsbSerialPort port) {
|
||||
SerialConsoleActivity.show(this, port);
|
||||
}
|
||||
|
||||
}
|
||||
+230
@@ -0,0 +1,230 @@
|
||||
/* 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
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
||||
* USA.
|
||||
*
|
||||
* Project home page: https://github.com/mik3y/usb-serial-for-android
|
||||
*/
|
||||
|
||||
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.CheckBox;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.ScrollView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.hoho.android.usbserial.driver.UsbSerialPort;
|
||||
import com.hoho.android.usbserial.util.HexDump;
|
||||
import com.hoho.android.usbserial.util.SerialInputOutputManager;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* Monitors a single {@link UsbSerialPort} instance, showing all data
|
||||
* received.
|
||||
*
|
||||
* @author mike wakerly (opensource@hoho.com)
|
||||
*/
|
||||
public class SerialConsoleActivity extends Activity {
|
||||
|
||||
private final String TAG = SerialConsoleActivity.class.getSimpleName();
|
||||
|
||||
/**
|
||||
* Driver instance, passed in statically via
|
||||
* {@link #show(Context, UsbSerialPort)}.
|
||||
*
|
||||
* <p/>
|
||||
* This is a devious hack; it'd be cleaner to re-create the driver using
|
||||
* arguments passed in with the {@link #startActivity(Intent)} intent. We
|
||||
* can get away with it because both activities will run in the same
|
||||
* process, and this is a simple demo.
|
||||
*/
|
||||
private static UsbSerialPort sPort = null;
|
||||
|
||||
private TextView mTitleTextView;
|
||||
private TextView mDumpTextView;
|
||||
private ScrollView mScrollView;
|
||||
private CheckBox chkDTR;
|
||||
private CheckBox chkRTS;
|
||||
|
||||
private final ExecutorService mExecutor = Executors.newSingleThreadExecutor();
|
||||
|
||||
private SerialInputOutputManager mSerialIoManager;
|
||||
|
||||
private final SerialInputOutputManager.Listener mListener =
|
||||
new SerialInputOutputManager.Listener() {
|
||||
|
||||
@Override
|
||||
public void onRunError(Exception e) {
|
||||
Log.d(TAG, "Runner stopped.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNewData(final byte[] data) {
|
||||
SerialConsoleActivity.this.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
SerialConsoleActivity.this.updateReceivedData(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.serial_console);
|
||||
mTitleTextView = (TextView) findViewById(R.id.demoTitle);
|
||||
mDumpTextView = (TextView) findViewById(R.id.consoleText);
|
||||
mScrollView = (ScrollView) findViewById(R.id.demoScroller);
|
||||
chkDTR = (CheckBox) findViewById(R.id.checkBoxDTR);
|
||||
chkRTS = (CheckBox) findViewById(R.id.checkBoxRTS);
|
||||
|
||||
chkDTR.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
try {
|
||||
sPort.setDTR(isChecked);
|
||||
}catch (IOException x){}
|
||||
}
|
||||
});
|
||||
|
||||
chkRTS.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
try {
|
||||
sPort.setRTS(isChecked);
|
||||
}catch (IOException x){}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
stopIoManager();
|
||||
if (sPort != null) {
|
||||
try {
|
||||
sPort.close();
|
||||
} catch (IOException e) {
|
||||
// Ignore.
|
||||
}
|
||||
sPort = null;
|
||||
}
|
||||
finish();
|
||||
}
|
||||
|
||||
void showStatus(TextView theTextView, String theLabel, boolean theValue){
|
||||
String msg = theLabel + ": " + (theValue ? "enabled" : "disabled") + "\n";
|
||||
theTextView.append(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
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 {
|
||||
sPort.open(connection);
|
||||
sPort.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
|
||||
|
||||
showStatus(mDumpTextView, "CD - Carrier Detect", sPort.getCD());
|
||||
showStatus(mDumpTextView, "CTS - Clear To Send", sPort.getCTS());
|
||||
showStatus(mDumpTextView, "DSR - Data Set Ready", sPort.getDSR());
|
||||
showStatus(mDumpTextView, "DTR - Data Terminal Ready", sPort.getDTR());
|
||||
showStatus(mDumpTextView, "DSR - Data Set Ready", sPort.getDSR());
|
||||
showStatus(mDumpTextView, "RI - Ring Indicator", sPort.getRI());
|
||||
showStatus(mDumpTextView, "RTS - Request To Send", sPort.getRTS());
|
||||
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Error setting up device: " + e.getMessage(), e);
|
||||
mTitleTextView.setText("Error opening device: " + e.getMessage());
|
||||
try {
|
||||
sPort.close();
|
||||
} catch (IOException e2) {
|
||||
// Ignore.
|
||||
}
|
||||
sPort = null;
|
||||
return;
|
||||
}
|
||||
mTitleTextView.setText("Serial device: " + sPort.getClass().getSimpleName());
|
||||
}
|
||||
onDeviceStateChange();
|
||||
}
|
||||
|
||||
private void stopIoManager() {
|
||||
if (mSerialIoManager != null) {
|
||||
Log.i(TAG, "Stopping io manager ..");
|
||||
mSerialIoManager.stop();
|
||||
mSerialIoManager = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void startIoManager() {
|
||||
if (sPort != null) {
|
||||
Log.i(TAG, "Starting io manager ..");
|
||||
mSerialIoManager = new SerialInputOutputManager(sPort, mListener);
|
||||
mExecutor.submit(mSerialIoManager);
|
||||
}
|
||||
}
|
||||
|
||||
private void onDeviceStateChange() {
|
||||
stopIoManager();
|
||||
startIoManager();
|
||||
}
|
||||
|
||||
private void updateReceivedData(byte[] data) {
|
||||
final String message = "Read " + data.length + " bytes: \n"
|
||||
+ HexDump.dumpHexString(data) + "\n\n";
|
||||
mDumpTextView.append(message);
|
||||
mScrollView.smoothScrollTo(0, mDumpTextView.getBottom());
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the activity, using the supplied driver instance.
|
||||
*
|
||||
* @param context
|
||||
* @param 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