WEB开发网
开发学院手机开发Android 开发 Android 串行接口(serial port)编程 阅读

Android 串行接口(serial port)编程

 2013-03-12 15:26:28 来源:开发学院   
核心提示:MainActivity.javapackage com.whyonly.serial;import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.Menu;public class M

MainActivity.java

package com.whyonly.serial;


import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

	private static final String TAG = "MainActivity";
	
	private USBSerialHelper usbSerialHelper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		usbSerialHelper = new USBSerialHelper(MainActivity.this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void flag(int onoff) {
		//the message returned from serial port, do anything you want.
		Log.d(TAG,"onoff:"+onoff);
		
	}
	
	@Override
	protected void onDestroy() {
    	super.onDestroy();
    	Log.d(TAG,"onDestroy()");
    	stopUSBSerial();
	}
	
	private void stopUSBSerial() {
		if(usbSerialHelper != null){
			usbSerialHelper.onDestroy();
		}
		
	}

}

USBSerialHelper.java

package com.whyonly.serial;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidParameterException;


import android_serialport_api.SerialPort;




public class USBSerialHelper {
	
	private MainActivity activity;
	protected SerialApplication mApplication;
	protected SerialPort mSerialPort;
	protected OutputStream mOutputStream;
	private InputStream mInputStream;
	private ReadThread mReadThread;
	
	public USBSerialHelper(final MainActivity mActivity){
		this.activity = mActivity;
		mApplication = (SerialApplication) mActivity.getApplication();
		Preferences.saveUSBSetting(mActivity);
		start();
	}
	
	public void start(){
		try {
			mSerialPort = mApplication.getSerialPort();
			mOutputStream = mSerialPort.getOutputStream();
			mInputStream = mSerialPort.getInputStream();

			/* Create a receiving thread */
			mReadThread = new ReadThread();
			mReadThread.start();
		} catch (SecurityException e) {
			DisplayError(R.string.error_security);
		} catch (IOException e) {
			DisplayError(R.string.error_unknown);
		} catch (InvalidParameterException e) {
			DisplayError(R.string.error_configuration);
		}
	}
	
	private void DisplayError(int resourceId) {
		DialogUtil.showWarningDialog(activity, resourceId);
	}
	
	private String lastStatus = "";//1:flag off;2:flag on;0 power off

	private void onDataReceived(final byte[] buffer, final int size) {
		activity.runOnUiThread(new Runnable() {
			public void run() {
				//receive serial number from usb serial device.
				String signal = new String(buffer, 0, size);
				if(signal.length()>0){
					String status = signal.substring(0,1);
					if(!status.equals("0") && !lastStatus.equals(status)){
						lastStatus = status;
						if(status.equals("1")){//flag on status
							activity.flag(1);
						}else if(status.equals("2")){//flag off status
							activity.flag(0);
						}
					}
				}
			}
		});
	}
	
	public void onDestroy() {
		if (mReadThread != null)
			mReadThread.interrupt();
		mApplication.closeSerialPort();
		mSerialPort = null;
	}
	
	private class ReadThread extends Thread {

		@Override
		public void run() {
			super.run();
			while(!isInterrupted()) {
				int size;
				try {
					byte[] buffer = new byte[64];
					if (mInputStream == null) return;
					size = mInputStream.read(buffer);
					if (size > 0) {
						onDataReceived(buffer, size);
					}
				} catch (IOException e) {
					e.printStackTrace();
					return;
				}
			}
		}
	}
}

Tags:Android 串行 接口

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接