• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.serialchat;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.hardware.SerialManager;
22 import android.hardware.SerialPort;
23 import android.os.Bundle;
24 import android.os.Handler;
25 import android.os.Message;
26 import android.os.ParcelFileDescriptor;
27 import android.view.KeyEvent;
28 import android.view.View;
29 import android.view.inputmethod.EditorInfo;
30 import android.util.Log;
31 import android.widget.EditText;
32 import android.widget.TextView;
33 
34 import java.nio.ByteBuffer;
35 import java.io.IOException;
36 
37 public class SerialChat extends Activity implements Runnable, TextView.OnEditorActionListener {
38 
39     private static final String TAG = "SerialChat";
40 
41     private TextView mLog;
42     private EditText mEditText;
43     private ByteBuffer mInputBuffer;
44     private ByteBuffer mOutputBuffer;
45     private SerialManager mSerialManager;
46     private SerialPort mSerialPort;
47     private boolean mPermissionRequestPending;
48 
49     private static final int MESSAGE_LOG = 1;
50 
51     @Override
onCreate(Bundle savedInstanceState)52     public void onCreate(Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54 
55         mSerialManager = (SerialManager)getSystemService(Context.SERIAL_SERVICE);
56         setContentView(R.layout.serial_chat);
57         mLog = (TextView)findViewById(R.id.log);
58         mEditText = (EditText)findViewById(R.id.message);
59         mEditText.setOnEditorActionListener(this);
60 
61         if (false) {
62             mInputBuffer = ByteBuffer.allocateDirect(1024);
63             mOutputBuffer = ByteBuffer.allocateDirect(1024);
64         } else {
65             mInputBuffer = ByteBuffer.allocate(1024);
66             mOutputBuffer = ByteBuffer.allocate(1024);
67         }
68     }
69 
70     @Override
onResume()71     public void onResume() {
72         super.onResume();
73 
74         String[] ports = mSerialManager.getSerialPorts();
75         if (ports != null && ports.length > 0) {
76             try {
77                 mSerialPort = mSerialManager.openSerialPort(ports[0], 115200);
78                 if (mSerialPort != null) {
79                     new Thread(this).start();
80                 }
81             } catch (IOException e) {
82             }
83         }
84 
85     }
86 
87     @Override
onPause()88     public void onPause() {
89         super.onPause();
90 
91     }
92 
93     @Override
onDestroy()94     public void onDestroy() {
95         if (mSerialPort != null) {
96             try {
97                 mSerialPort.close();
98             } catch (IOException e) {
99             }
100             mSerialPort = null;
101         }
102         super.onDestroy();
103     }
104 
onEditorAction(TextView v, int actionId, KeyEvent event)105     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
106         if (/* actionId == EditorInfo.IME_ACTION_DONE && */ mSerialPort != null) {
107             try {
108                 String text = v.getText().toString();
109                 Log.d(TAG, "write: " + text);
110                 byte[] bytes = text.getBytes();
111                 mOutputBuffer.clear();
112                 mOutputBuffer.put(bytes);
113                 mSerialPort.write(mOutputBuffer, bytes.length);
114             } catch (IOException e) {
115                 Log.e(TAG, "write failed", e);
116             }
117             v.setText("");
118             return true;
119         }
120         Log.d(TAG, "onEditorAction " + actionId + " event: " + event);
121         return false;
122     }
123 
run()124     public void run() {
125         Log.d(TAG, "run");
126         int ret = 0;
127         byte[] buffer = new byte[1024];
128         while (ret >= 0) {
129             try {
130                 Log.d(TAG, "calling read");
131                 mInputBuffer.clear();
132                 ret = mSerialPort.read(mInputBuffer);
133                 Log.d(TAG, "read returned " + ret);
134                 mInputBuffer.get(buffer, 0, ret);
135             } catch (IOException e) {
136                 Log.e(TAG, "read failed", e);
137                 break;
138             }
139 
140             if (ret > 0) {
141                 Message m = Message.obtain(mHandler, MESSAGE_LOG);
142                 String text = new String(buffer, 0, ret);
143                 Log.d(TAG, "chat: " + text);
144                 m.obj = text;
145                 mHandler.sendMessage(m);
146             }
147         }
148         Log.d(TAG, "thread out");
149     }
150 
151    Handler mHandler = new Handler() {
152         @Override
153         public void handleMessage(Message msg) {
154             switch (msg.what) {
155                 case MESSAGE_LOG:
156                     mLog.setText(mLog.getText() + (String)msg.obj);
157                     break;
158              }
159         }
160     };
161 }
162 
163 
164