I now have an Android development issues, is that I am using Android's USB Host function, through the Android phone and microcontroller program to communicate using PL2303HX USB to RS232 or TTL to achieve this function , the current problem is I can find on the Android tablet USB devices , but can not open , and set the baud rate and other operations , tried for a long time will not work, I do not know what the brothers have done this development , pointing , thank you !
------ Solution ---------------------------------------- ----
/ / look here , http://www.2cto.com/kf/201212/174105.html
/ / put the code directly
package com.android.usb;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Iterator;
import com.hoho.android.usbserial.driver.FtdiSerialDriver;
import com.hoho.android.usbserial.driver.UsbSerialDriver;
import com.hoho.android.usbserial.util.SerialInputOutputManager;
import com.hoho.android.usbserial.util.SerialInputOutputManager.Listener;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget. *;
public class UsbtestActivity extends Activity {
/ ** Called when the activity is first created. * /
protected TextView txtTips;
protected EditText edSend;
protected EditText edReceive;
protected Button btnSend;
private UsbManager managerme;
private FtdiSerialDriver driver;
/ / private SerialInputOutputManager rs232ReadWrite;
private ReadThread readThread;
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
/ / receive RS232 data event
public class ReadListener {
public void onRunError (Exception e) {
/ / TODO Auto-generated method stub
}
public void onNewData (final byte [] data) {
/ / TODO Auto-generated method stub
UsbtestActivity.this.runOnUiThread (new Runnable () {
public void run () {
UsbtestActivity.this.updateReceivedData (data);
}
});
}
};
private ReadListener readListener = new ReadListener ();
/ * private Listener readListener = new Listener ()
{
public void onNewData (final byte [] data)
{
UsbtestActivity.this.runOnUiThread (new Runnable () {
public void run () {
String recString = data.toString ();
edReceive.setText (recString);
}
});
}
public void onRunError (Exception e)
{
}
};
* /
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
txtTips = (TextView) this.findViewById (R.id.txtTips);
edSend = (EditText) this.findViewById (R.id.edSend);
edReceive = (EditText) this.findViewById (R.id.edReceive);
btnSend = (Button) this.findViewById (R.id.btnSend);
managerme = (UsbManager) getSystemService (Context.USB_SERVICE);
}
@ Override
protected void onDestroy () {
btnCloseClick (null);
super.onDestroy ();
}
/ / calling getUsb () when the application is fired when a USB device is authorized
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver () {
public void onReceive (Context context, Intent intent) {
String action = intent.getAction ();
/ / get authorization
if (ACTION_USB_PERMISSION.equals (action)) {
synchronized (this) {
UsbDevice device = (UsbDevice) intent.getParcelableExtra ( UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra (UsbManager.EXTRA_PERMISSION_GRANTED, false ) ) {
if (device! = null) {
/ / call method to set up device communication
UsbDeviceConnection connection = ; managerme.openDevice (device);
if (connection == null)
{
txtTips.setText (txtTips.getText () + "/ connection is null");
}
else {
driver = new FtdiSerialDriver (device, connection);
open232Port ();
txtTips.setText (txtTips.getText () + "/ success connect 2");
}
}
}
else {
/ / Log.d ( TAG, "permission denied for device" + device);
txtTips.setText (txtTips.getText () + "/ permission ; denied ");
}
}
}
/ / USB device is unplugged
if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals (action)) {
UsbDevice device = (UsbDevice) intent.getParcelableExtra (UsbManager.EXTRA_DEVICE);
if (device! = null) {
/ / call your method that cleans up and closes communication with the device
btnCloseClick (null);
}
}
}
};
private class ReadThread extends Thread {
/ / private ArrayList
private Boolean IsRun = true;
private ReadListener mListener;
private static final int BUFSIZ = 4096;
private static final int READ_WAIT_MILLIS = 200;
private final ByteBuffer mReadBuffer = ByteBuffer.allocate (BUFSIZ);
/ / private final byte [] mReadBuffer = new byte [BUFSIZ];
public ReadThread (ReadListener listener) {
super ();
mListener = listener;
}
public void Stop ()
{
IsRun = false;
}
public synchronized ReadListener getListener () {
return mListener;
}
public void run () {
while (IsRun)
{
if (driver! = null)
{
int len = 0;
mReadBuffer.clear ();
try {
len = driver.read (mReadBuffer.array (), READ_WAIT_MILLIS);
} catch (Exception e) {
/ / TODO: handle exception
}
if (len> 0) {
final ReadListener listener = getListener ();
if (listener! = null) {
try {
final byte [] data = mReadBuffer.toString (). getBytes (" gbk ");
listener.onNewData (data);
} catch (UnsupportedEncodingException e) {
/ / TODO Auto-generated catch block
/ / e.printStackTrace ();
}
}
mReadBuffer.clear ();
}
}
}
}
};
/ / Connect to USB devices, and open RS-232 port
public void getUsb ()
{
HashMap
txtTips.setText ("size ():" + deviceList.size ());
Iterator
while (deviceIterator.hasNext ()) {
UsbDevice tmpDevice = deviceIterator.next ();
/ /
/ /
/ / if (usd.getVendorId () == 1659 && usd.getProductId () == 8963)
if (tmpDevice.getVendorId () == 1027 && tmpDevice.getProductId () == 24577)
{
txtTips.setText (txtTips.getText () + "/ found usb device");
UsbDeviceConnection connection = null;
if (managerme.hasPermission (tmpDevice))
{
connection = managerme.openDevice (tmpDevice);
}
if (connection == null)
{
/ / authorization
PendingIntent mPermissionIntent = PendingIntent.getBroadcast (this, 0, new Intent (
ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter (ACTION_USB_PERMISSION);
/ / registered USB authorized notifier , if authorized , but there is news to open the device
registerReceiver (mUsbReceiver, filter);
/ / application for authorization , until mUsbReceiver successful creation driver
managerme.requestPermission (tmpDevice, mPermissionIntent);
}
else
{
driver = new FtdiSerialDriver (tmpDevice, connection);
open232Port ();
txtTips.setText (txtTips.getText () + "/ success connect 1");
}
/ / open the serial code mUsbReceiver real
return;
}
}
}
/ / When the driver connected, open 232 , and initializes the reader thread
public void open232Port ()
{
if (driver! = null)
{
try {
driver.open ();
driver.setParameters (9600, UsbSerialDriver.DATABITS_8, UsbSerialDriver.STOPBITS_1, UsbSerialDriver.PARITY_NONE);
txtTips.setText (txtTips.getText () + "/ success open");
readThread = new ReadThread (readListener);
readThread.start ();
/ / This is an interface, no need to call the run Interface
/ / rs232ReadWrite = new SerialInputOutputManager (driver, readListener);
/ / rs232ReadWrite.run ();
/ / Thread.sleep (1000);
/ / rs232ReadWrite.run ();
} catch (Exception e) {
/ / TODO: handle exception
txtTips.setText (txtTips.getText () + "/ open error:" + e.getMessage ());
driver = null;
}
}
else
{
txtTips.setText (txtTips.getText () + "/ connection is null");
}
}
/ / open the serial port
public void btnOpenClick (View v)
{
btnCloseClick (v);
getUsb ();
}
/ / Close the serial port
public void btnCloseClick (View v)
{
if (driver! = null)
{
/ * if (rs232ReadWrite! = null)
{
rs232ReadWrite.stop ();
rs232ReadWrite = null;
} * /
if (readThread! = null)
{
readThread.Stop ();
readThread = null;
}
driver.close ();
driver = null;
}
/ / edReceive.setText ("close click");
}
public void btnSendClick (View v) throws UnsupportedEncodingException
{
/ / edReceive.setText ("buton click");
if (driver! = null)
{
byte buffer [] = edSend.getText (). toString (). getBytes ("gbk");
byte endChar [] = {0x61, 0x62, 0x63, 0x0d, 0x0a};
try {
driver.write (buffer, 500);
driver.write (endChar, 500);
/ / rs232ReadWrite.writeAsync (buffer);
/ / rs232ReadWrite.writeAsync (endChar);
} catch (Exception e) {
/ / TODO: handle exception
txtTips.setText (txtTips.getText () + e.getMessage ());
}
}
else {
edReceive.setText ("driver is null");
}
}
private void updateReceivedData (byte [] data) {
final String recString = data.toString ();
edReceive.setText (recString);
}
}
------ eference ----------------------------------- ----
Thank meegowei1 brothers you, I'll go to the next test .
------ eference --------------------------------------- < br> have to get, it was my one GND line did not answer , thank you !
没有评论:
发表评论