- /*
- * 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.huangcheng.serial;
- import java.io.IOException;
- import com.huangcheng.zigbeeview.R;
- import Android.os.Bundle;
- import android.view.KeyEvent;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.TextView.OnEditorActionListener;
- public class ConsoleActivity extends SerialPortActivity {
- EditText mReception;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.console);
- // setTitle("Loopback test");
- mReception = (EditText) findViewById(R.id.EditTextReception);
- EditText Emission = (EditText) findViewById(R.id.EditTextEmission);
- Emission.setOnEditorActionListener(new OnEditorActionListener() {
- public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
- int i;
- CharSequence t = v.getText();
- char[] text = new char[t.length()];
- for (i=0; i<t.length(); i++) {
- text[i] = t.charAt(i);
- }
- try {
- mOutputStream.write(new String(text).getBytes());
- mOutputStream.write('\n');
- } catch (IOException e) {
- e.printStackTrace();
- }
- return false;
- }
- });
- }
- @Override
- protected void onDataReceived(final byte[] buffer, final int size) {
- runOnUiThread(new Runnable() {
- public void run() {
- if (mReception != null) {
- mReception.append(new String(buffer, 0, size));
- }
- }
- });
- }
- }
我会把android-serialport-api源码上传,大家可以根据我的这篇文章自己简化一下,就可以直接移植到需要到程序上去了,绝对可用!
免费下载地址在 http://linux.linuxidc.com/
用户名与密码都是www.linuxidc.com
具体下载目录在 /2012年资料/5月/27日/Android串口通信实例分析【附源码】/
更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11
