SerialPortHandsetActivity.java 3.36 KB
/*
 * Copyright 2009 Cedric Priscal
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */

package com.ectrip.cyt.ui;

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

import android.os.Bundle;
import android_serialport_api.SerialPortHandset;

import com.ectrip.cyt.config.DevicTool;
import com.ectrip.cyt.config.MyApp;

public abstract class SerialPortHandsetActivity extends BaseActivity {

	protected MyApp mApplication;
	protected SerialPortHandset mSerialPort;
	protected OutputStream mOutputStream;
	private InputStream mInputStream;
	public ReadThread mReadThread=null;

	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);
//					int len = -1;
//					byte[] buff = new byte[1024];
//					String res = "";
//					while (-1 != (len = mInputStream.read(buff))) {
//						// 将字节数组转换为字符串
//						// String username = new
//						// String(request.getParamter("userName").getBytes("ISO8859-1"),"GBK")
//						res += new String(buff, 0, len, "ISO8859-1");
//					}
//					System.out.println(res);
					if (size > 0&&size!=1) {
						onDataReceived(buffer, size);
					}
				} catch (IOException e) {
					e.printStackTrace();
					return;
				}
			}
		}
	}

//	private void DisplayError(int resourceId) {
//		AlertDialog.Builder b = new AlertDialog.Builder(this);
//		b.setTitle("Error");
//		b.setMessage(resourceId);
//		b.setPositiveButton("OK", new OnClickListener() {
//			public void onClick(DialogInterface dialog, int which) {
//				SerialPortHandsetActivity.this.finish();
//			}
//		});
//		b.show();
//	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mApplication = (MyApp) getApplication();
		try {
			mSerialPort = DevicTool.getInstance().getSerialPortHandset();
			mOutputStream = mSerialPort.getOutputStream();
			mInputStream = mSerialPort.getInputStream();

			/* Create a receiving thread */
			mReadThread = new ReadThread();
			mReadThread.start();
		} catch (SecurityException e) {
			// You do not have read/write permission to the serial port.
			e.printStackTrace();
		} catch (IOException e) {
			// The serial port can not be opened for an unknown reason.
			e.printStackTrace();
		} catch (InvalidParameterException e) {
			// Please configure your serial port first.
			e.printStackTrace();
		}
	}

	/**
	 * 返回数据
	 *
	 * @param buffer
	 * @param size
	 */
	protected abstract void onDataReceived(final byte[] buffer, final int size);

	@Override
	protected void onDestroy() {
		if (mReadThread != null)
			mReadThread.interrupt();
		DevicTool.getInstance().closeSerialPort();
		mSerialPort = null;
		super.onDestroy();
	}
}