手机版
你好,游客 登录 注册
背景:
阅读新闻

Android串口通信实例分析【附源码】

[日期:2012-05-27] 来源:Linux社区  作者:hc260164797 [字体:大 中 小]
最后,是一个测试Activity实现串口收发,ConsoleActivity.Java
  1. /* 
  2.  * Copyright 2009 Cedric Priscal 
  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.huangcheng.serial;  
  18.   
  19. import java.io.IOException;  
  20.   
  21. import com.huangcheng.zigbeeview.R;  
  22.   
  23. import Android.os.Bundle;  
  24. import android.view.KeyEvent;  
  25. import android.widget.EditText;  
  26. import android.widget.TextView;  
  27. import android.widget.TextView.OnEditorActionListener;  
  28.   
  29. public class ConsoleActivity extends SerialPortActivity {  
  30.   
  31.     EditText mReception;  
  32.   
  33.     @Override  
  34.     protected void onCreate(Bundle savedInstanceState) {  
  35.         super.onCreate(savedInstanceState);  
  36.         setContentView(R.layout.console);  
  37.   
  38. //      setTitle("Loopback test");   
  39.         mReception = (EditText) findViewById(R.id.EditTextReception);  
  40.   
  41.         EditText Emission = (EditText) findViewById(R.id.EditTextEmission);  
  42.         Emission.setOnEditorActionListener(new OnEditorActionListener() {  
  43.             public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
  44.                 int i;  
  45.                 CharSequence t = v.getText();  
  46.                 char[] text = new char[t.length()];  
  47.                 for (i=0; i<t.length(); i++) {  
  48.                     text[i] = t.charAt(i);  
  49.                 }  
  50.                 try {  
  51.                     mOutputStream.write(new String(text).getBytes());  
  52.                     mOutputStream.write('\n');  
  53.                 } catch (IOException e) {  
  54.                     e.printStackTrace();  
  55.                 }  
  56.                 return false;  
  57.             }  
  58.         });  
  59.     }  
  60.   
  61.     @Override  
  62.     protected void onDataReceived(final byte[] buffer, final int size) {  
  63.         runOnUiThread(new Runnable() {  
  64.             public void run() {  
  65.                 if (mReception != null) {  
  66.                     mReception.append(new String(buffer, 0, size));  
  67.                 }  
  68.             }  
  69.         });  
  70.     }  
  71. }  

我会把android-serialport-api源码上传,大家可以根据我的这篇文章自己简化一下,就可以直接移植到需要到程序上去了,绝对可用!

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2012年资料/5月/27日/Android串口通信实例分析【附源码】/

更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11

linux
相关资讯       Android编程入门 
本文评论   查看全部评论 (6)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 6 楼
* 匿名 发表于 2017/1/22 16:34:13
请你们写点有用的。
第 5 楼
* 匿名 发表于 2013/7/15 15:30:14
从未见过如此垃圾的代码
第 4 楼
* 匿名 发表于 2013/4/27 17:32:14
看不懂
第 3 楼
* 匿名 发表于 2012/10/24 10:36:04
这个android-serial-api,纯粹太烂了,很多人浪费了很多时间在上面,但是没法用起,有这个时间去看这个代码,还不如自己去编呢,效率反而高
第 2 楼
* 匿名 发表于 2012/10/24 10:33:07
根本没法用,瞎扯