mirror of
https://github.com/mik3y/usb-serial-for-android
synced 2025-06-09 17:06:21 +00:00
Update demo activity.
Demo now consists of two activities: - DeviceListActivity shows all usb devices (including unsupported ones) in a ListView. - SerialConsoleActivity dumps the data stream for a device selected in DeviceListActivity.
This commit is contained in:
parent
b07bbcf292
commit
b709823906
@ -5,28 +5,35 @@
|
|||||||
android:versionName="1.0" >
|
android:versionName="1.0" >
|
||||||
|
|
||||||
<uses-sdk android:minSdkVersion="12" />
|
<uses-sdk android:minSdkVersion="12" />
|
||||||
|
|
||||||
<uses-feature android:name="android.hardware.usb.host" />
|
<uses-feature android:name="android.hardware.usb.host" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:icon="@drawable/ic_launcher"
|
android:icon="@drawable/ic_launcher"
|
||||||
android:label="@string/app_name" >
|
android:label="@string/app_name" >
|
||||||
<activity
|
<activity
|
||||||
|
android:name="com.hoho.android.usbserial.examples.DeviceListActivity"
|
||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:name="com.hoho.android.usbserial.examples.DemoActivity" >
|
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
|
||||||
<intent-filter >
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
|
||||||
<category android:name="android.intent.category.DEFAULT" />
|
<category android:name="android.intent.category.DEFAULT" />
|
||||||
<category android:name="android.intent.category.LAUNCHER" />
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action
|
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
|
||||||
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
|
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
|
|
||||||
<meta-data
|
<meta-data
|
||||||
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
|
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
|
||||||
android:resource="@xml/device_filter" />
|
android:resource="@xml/device_filter" />
|
||||||
</activity>
|
</activity>
|
||||||
|
<activity
|
||||||
|
android:name="com.hoho.android.usbserial.examples.SerialConsoleActivity"
|
||||||
|
android:label="@string/app_name"
|
||||||
|
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
|
||||||
|
</activity>
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
@ -1,26 +1,50 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:layout_width="fill_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="fill_parent"
|
android:layout_height="match_parent" >
|
||||||
android:orientation="vertical" >
|
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/demoTitle"
|
android:id="@+id/demoTitle"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_alignParentTop="true"
|
||||||
|
android:layout_centerHorizontal="true"
|
||||||
|
android:text="@string/app_title"
|
||||||
|
android:textSize="24sp"
|
||||||
|
android:textStyle="bold" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/progressBarTitle"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_below="@+id/demoTitle"
|
||||||
|
android:layout_centerHorizontal="true"
|
||||||
|
android:text="@string/refreshing"
|
||||||
|
android:padding="8dp"
|
||||||
|
android:textSize="18sp" />
|
||||||
|
|
||||||
|
<ProgressBar
|
||||||
|
android:id="@+id/progressBar"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="@string/hello" />
|
android:layout_below="@+id/progressBarTitle"
|
||||||
|
android:layout_centerHorizontal="true"
|
||||||
<ScrollView
|
android:padding="8dp"
|
||||||
android:id="@+id/demoScroller"
|
style="@android:style/Widget.Holo.ProgressBar.Horizontal"
|
||||||
|
android:indeterminate="true" />
|
||||||
|
|
||||||
|
<View
|
||||||
|
android:id="@+id/separator"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content" >
|
android:layout_height="1dip"
|
||||||
|
android:layout_below="@+id/progressBar"
|
||||||
|
android:layout_centerHorizontal="true"
|
||||||
|
android:background="#eeeeee" />
|
||||||
|
|
||||||
<TextView
|
<ListView
|
||||||
android:id="@+id/demoText"
|
android:id="@+id/deviceList"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="wrap_content"
|
||||||
android:typeface="monospace" />
|
android:layout_below="@+id/separator" />
|
||||||
|
|
||||||
</ScrollView>
|
|
||||||
|
|
||||||
</LinearLayout>
|
</RelativeLayout>
|
36
UsbSerialExamples/res/layout/serial_console.xml
Normal file
36
UsbSerialExamples/res/layout/serial_console.xml
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent" >
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/demoTitle"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_alignParentTop="true"
|
||||||
|
android:text="@string/app_title"
|
||||||
|
android:textSize="24sp"
|
||||||
|
android:textStyle="bold" />
|
||||||
|
|
||||||
|
<View
|
||||||
|
android:id="@+id/separator"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_below="@+id/demoTitle"
|
||||||
|
android:layout_height="1dip"
|
||||||
|
android:background="#eeeeee" />
|
||||||
|
|
||||||
|
<ScrollView
|
||||||
|
android:id="@+id/demoScroller"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_below="@+id/separator" >
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/consoleText"
|
||||||
|
android:textIsSelectable="true"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:typeface="monospace" />
|
||||||
|
</ScrollView>
|
||||||
|
|
||||||
|
</RelativeLayout>
|
@ -1,7 +1,8 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources>
|
<resources>
|
||||||
|
|
||||||
<string name="hello">Hello World, DemoActivity!</string>
|
<string name="app_title">USB Serial Example</string>
|
||||||
<string name="app_name">UsbSerialExamples</string>
|
<string name="app_name">Serial Example</string>
|
||||||
|
<string name="refreshing">Refreshing...</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -0,0 +1,221 @@
|
|||||||
|
/* Copyright 2011 Google Inc.
|
||||||
|
*
|
||||||
|
* 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: http://code.google.com/p/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.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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
/** 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;
|
||||||
|
|
||||||
|
@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<DeviceEntry>(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 DeviceEntry entry = mEntries.get(position);
|
||||||
|
final String title = String.format("Vendor %s Product %s",
|
||||||
|
HexDump.toHexString((short) entry.device.getVendorId()),
|
||||||
|
HexDump.toHexString((short) entry.device.getProductId()));
|
||||||
|
row.getText1().setText(title);
|
||||||
|
|
||||||
|
final String subtitle = entry.driver != null ?
|
||||||
|
entry.driver.getClass().getSimpleName() : "No Driver";
|
||||||
|
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 DeviceEntry entry = mEntries.get(position);
|
||||||
|
final UsbSerialDriver driver = entry.driver;
|
||||||
|
if (driver == null) {
|
||||||
|
Log.d(TAG, "No driver.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
showConsoleActivity(driver);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@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<DeviceEntry>>() {
|
||||||
|
@Override
|
||||||
|
protected List<DeviceEntry> 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(List<DeviceEntry> 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(UsbSerialDriver driver) {
|
||||||
|
SerialConsoleActivity.show(this, driver);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -22,14 +22,13 @@ package com.hoho.android.usbserial.examples;
|
|||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.hardware.usb.UsbManager;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.ScrollView;
|
import android.widget.ScrollView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.hoho.android.usbserial.driver.UsbSerialDriver;
|
import com.hoho.android.usbserial.driver.UsbSerialDriver;
|
||||||
import com.hoho.android.usbserial.driver.UsbSerialProber;
|
|
||||||
import com.hoho.android.usbserial.util.HexDump;
|
import com.hoho.android.usbserial.util.HexDump;
|
||||||
import com.hoho.android.usbserial.util.SerialInputOutputManager;
|
import com.hoho.android.usbserial.util.SerialInputOutputManager;
|
||||||
|
|
||||||
@ -38,23 +37,26 @@ import java.util.concurrent.ExecutorService;
|
|||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A sample Activity demonstrating USB-Serial support.
|
* Monitors a single {@link UsbSerialDriver} instance, showing all data
|
||||||
|
* received.
|
||||||
*
|
*
|
||||||
* @author mike wakerly (opensource@hoho.com)
|
* @author mike wakerly (opensource@hoho.com)
|
||||||
*/
|
*/
|
||||||
public class DemoActivity extends Activity {
|
public class SerialConsoleActivity extends Activity {
|
||||||
|
|
||||||
private final String TAG = DemoActivity.class.getSimpleName();
|
private final String TAG = SerialConsoleActivity.class.getSimpleName();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The device currently in use, or {@code null}.
|
* Driver instance, passed in statically via
|
||||||
|
* {@link #show(Context, UsbSerialDriver)}.
|
||||||
|
*
|
||||||
|
* <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 UsbSerialDriver mSerialDevice;
|
private static UsbSerialDriver sDriver = null;
|
||||||
|
|
||||||
/**
|
|
||||||
* The system's USB service.
|
|
||||||
*/
|
|
||||||
private UsbManager mUsbManager;
|
|
||||||
|
|
||||||
private TextView mTitleTextView;
|
private TextView mTitleTextView;
|
||||||
private TextView mDumpTextView;
|
private TextView mDumpTextView;
|
||||||
@ -74,10 +76,10 @@ public class DemoActivity extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNewData(final byte[] data) {
|
public void onNewData(final byte[] data) {
|
||||||
DemoActivity.this.runOnUiThread(new Runnable() {
|
SerialConsoleActivity.this.runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
DemoActivity.this.updateReceivedData(data);
|
SerialConsoleActivity.this.updateReceivedData(data);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -86,10 +88,9 @@ public class DemoActivity extends Activity {
|
|||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.main);
|
setContentView(R.layout.serial_console);
|
||||||
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
|
|
||||||
mTitleTextView = (TextView) findViewById(R.id.demoTitle);
|
mTitleTextView = (TextView) findViewById(R.id.demoTitle);
|
||||||
mDumpTextView = (TextView) findViewById(R.id.demoText);
|
mDumpTextView = (TextView) findViewById(R.id.consoleText);
|
||||||
mScrollView = (ScrollView) findViewById(R.id.demoScroller);
|
mScrollView = (ScrollView) findViewById(R.id.demoScroller);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,38 +98,39 @@ public class DemoActivity extends Activity {
|
|||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
stopIoManager();
|
stopIoManager();
|
||||||
if (mSerialDevice != null) {
|
if (sDriver != null) {
|
||||||
try {
|
try {
|
||||||
mSerialDevice.close();
|
sDriver.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// Ignore.
|
// Ignore.
|
||||||
}
|
}
|
||||||
mSerialDevice = null;
|
sDriver = null;
|
||||||
}
|
}
|
||||||
|
finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
mSerialDevice = UsbSerialProber.findFirstDevice(mUsbManager);
|
Log.d(TAG, "Resumed, sDriver=" + sDriver);
|
||||||
Log.d(TAG, "Resumed, mSerialDevice=" + mSerialDevice);
|
if (sDriver == null) {
|
||||||
if (mSerialDevice == null) {
|
|
||||||
mTitleTextView.setText("No serial device.");
|
mTitleTextView.setText("No serial device.");
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
mSerialDevice.open();
|
sDriver.open();
|
||||||
|
sDriver.setParameters(115200, 8, UsbSerialDriver.STOPBITS_1, UsbSerialDriver.PARITY_NONE);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(TAG, "Error setting up device: " + e.getMessage(), e);
|
Log.e(TAG, "Error setting up device: " + e.getMessage(), e);
|
||||||
mTitleTextView.setText("Error opening device: " + e.getMessage());
|
mTitleTextView.setText("Error opening device: " + e.getMessage());
|
||||||
try {
|
try {
|
||||||
mSerialDevice.close();
|
sDriver.close();
|
||||||
} catch (IOException e2) {
|
} catch (IOException e2) {
|
||||||
// Ignore.
|
// Ignore.
|
||||||
}
|
}
|
||||||
mSerialDevice = null;
|
sDriver = null;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
mTitleTextView.setText("Serial device: " + mSerialDevice);
|
mTitleTextView.setText("Serial device: " + sDriver.getClass().getSimpleName());
|
||||||
}
|
}
|
||||||
onDeviceStateChange();
|
onDeviceStateChange();
|
||||||
}
|
}
|
||||||
@ -142,9 +144,9 @@ public class DemoActivity extends Activity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void startIoManager() {
|
private void startIoManager() {
|
||||||
if (mSerialDevice != null) {
|
if (sDriver != null) {
|
||||||
Log.i(TAG, "Starting io manager ..");
|
Log.i(TAG, "Starting io manager ..");
|
||||||
mSerialIoManager = new SerialInputOutputManager(mSerialDevice, mListener);
|
mSerialIoManager = new SerialInputOutputManager(sDriver, mListener);
|
||||||
mExecutor.submit(mSerialIoManager);
|
mExecutor.submit(mSerialIoManager);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -161,4 +163,17 @@ public class DemoActivity extends Activity {
|
|||||||
mScrollView.smoothScrollTo(0, mDumpTextView.getBottom());
|
mScrollView.smoothScrollTo(0, mDumpTextView.getBottom());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts the activity, using the supplied driver instance.
|
||||||
|
*
|
||||||
|
* @param context
|
||||||
|
* @param driver
|
||||||
|
*/
|
||||||
|
static void show(Context context, UsbSerialDriver driver) {
|
||||||
|
sDriver = driver;
|
||||||
|
final Intent intent = new Intent(context, SerialConsoleActivity.class);
|
||||||
|
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY);
|
||||||
|
context.startActivity(intent);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -107,6 +107,10 @@ public class HexDump {
|
|||||||
return toHexString(toByteArray(i));
|
return toHexString(toByteArray(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String toHexString(short i) {
|
||||||
|
return toHexString(toByteArray(i));
|
||||||
|
}
|
||||||
|
|
||||||
public static byte[] toByteArray(byte b) {
|
public static byte[] toByteArray(byte b) {
|
||||||
byte[] array = new byte[1];
|
byte[] array = new byte[1];
|
||||||
array[0] = b;
|
array[0] = b;
|
||||||
@ -124,6 +128,15 @@ public class HexDump {
|
|||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static byte[] toByteArray(short i) {
|
||||||
|
byte[] array = new byte[2];
|
||||||
|
|
||||||
|
array[1] = (byte) (i & 0xFF);
|
||||||
|
array[0] = (byte) ((i >> 8) & 0xFF);
|
||||||
|
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
private static int toByte(char c) {
|
private static int toByte(char c) {
|
||||||
if (c >= '0' && c <= '9')
|
if (c >= '0' && c <= '9')
|
||||||
return (c - '0');
|
return (c - '0');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user