FileDocCategorySizeDatePackage
SerialChat.javaAPI DocAndroid 5.1 API4976Thu Mar 12 22:22:44 GMT 2015com.android.serialchat

SerialChat

public class SerialChat extends android.app.Activity implements Runnable, TextView.OnEditorActionListener

Fields Summary
private static final String
TAG
private android.widget.TextView
mLog
private android.widget.EditText
mEditText
private ByteBuffer
mInputBuffer
private ByteBuffer
mOutputBuffer
private android.hardware.SerialManager
mSerialManager
private android.hardware.SerialPort
mSerialPort
private boolean
mPermissionRequestPending
private static final int
MESSAGE_LOG
android.os.Handler
mHandler
Constructors Summary
Methods Summary
public voidonCreate(android.os.Bundle savedInstanceState)


    
        
        super.onCreate(savedInstanceState);

        mSerialManager = (SerialManager)getSystemService(Context.SERIAL_SERVICE);
        setContentView(R.layout.serial_chat);
        mLog = (TextView)findViewById(R.id.log);
        mEditText = (EditText)findViewById(R.id.message);
        mEditText.setOnEditorActionListener(this);

        if (false) {
            mInputBuffer = ByteBuffer.allocateDirect(1024);
            mOutputBuffer = ByteBuffer.allocateDirect(1024);
        } else {
            mInputBuffer = ByteBuffer.allocate(1024);
            mOutputBuffer = ByteBuffer.allocate(1024);
        }
    
public voidonDestroy()

        if (mSerialPort != null) {
            try {
                mSerialPort.close();
            } catch (IOException e) {
            }
            mSerialPort = null;
        }
        super.onDestroy();
    
public booleanonEditorAction(android.widget.TextView v, int actionId, android.view.KeyEvent event)

        if (/* actionId == EditorInfo.IME_ACTION_DONE && */ mSerialPort != null) {
            try {
                String text = v.getText().toString();
                Log.d(TAG, "write: " + text);
                byte[] bytes = text.getBytes();
                mOutputBuffer.clear();
                mOutputBuffer.put(bytes);
                mSerialPort.write(mOutputBuffer, bytes.length);
            } catch (IOException e) {
                Log.e(TAG, "write failed", e);
            }
            v.setText("");
            return true;
        }
        Log.d(TAG, "onEditorAction " + actionId + " event: " + event);
        return false;
    
public voidonPause()

        super.onPause();
    
    
public voidonResume()

        super.onResume();

        String[] ports = mSerialManager.getSerialPorts();
        if (ports != null && ports.length > 0) {
            try {
                mSerialPort = mSerialManager.openSerialPort(ports[0], 115200);
                if (mSerialPort != null) {
                    new Thread(this).start();
                }
            } catch (IOException e) {
            }
        }

    
public voidrun()

        Log.d(TAG, "run");
        int ret = 0;
        byte[] buffer = new byte[1024];
        while (ret >= 0) {
            try {
                Log.d(TAG, "calling read");
                mInputBuffer.clear();
                ret = mSerialPort.read(mInputBuffer);
                Log.d(TAG, "read returned " + ret);
                mInputBuffer.get(buffer, 0, ret);
            } catch (IOException e) {
                Log.e(TAG, "read failed", e);
                break;
            }

            if (ret > 0) {
                Message m = Message.obtain(mHandler, MESSAGE_LOG);
                String text = new String(buffer, 0, ret);
                Log.d(TAG, "chat: " + text);
                m.obj = text;
                mHandler.sendMessage(m);
            }
        }
        Log.d(TAG, "thread out");