android串口通信可以实现设备与设备之间通过设备线连接进行数据(消息)传递
(一)导入so库
(二)在moudle的build中添加jnilibs
buildtypes { sourcets { main { jni.srcdirs = [] } } }12345
(三)添加google的rialport
添加的是google的所以必须创建android_rialport_api包
如需要更改rialport、rialportfinder我的父亲母亲影评位置需要重新生成so库
(四)创建串口通信工具类rialportutils
package com.demo.rialport;import android.util.log;import java.io.file;import java.io.ioexception;import java.io.inputstream;import java.io.outputstream;import android_rialport_api.rialport;/** * @author renquan */public class rialportutils { private final string tag = "rialportutils";// private string path = "/dev/ttys1";// private int baudrate = 9600; pub隶书的特点lic boolean rialportstatus = fal; //是否打开串口标志 public string data_; public boolean threadstatus; //线程状态,为了安全终止线程 public rialport rialport = null; public inputstream inputstream = null; public outputstream outputstream = null; public changetool changetool = new changetool(); /** * 打开串口 * @return rialport串口对象 */ public rialport openrialport(string path,int baudrate){ try { rialport = new rialport(new file(path),baudrate,0); this.rialportstatus = true; threadstatus = fal; //线程状态 //获取打开的串口中的输入输出流,以便于串口数据的收发 inputstream = rialport.getinputstream(); outputstream = rialport.getoutputstream(); new readthread().start(); //开始线程监控是否有数据要接收 } catch (ioexception e) { log.e(tag, "openrialport: 打开串口异常:" + e.tostring()); return rialport; } log.d(tag, "openrialport: 打开串口"); return rialport; } /** * 关闭串口 */ public void clorialport(){ try { inputstream.clo(); outputstream.clo(); this.rialportstatus = fal; this.threadstatus = true; //线程状态 rialport.clo(); } catch (ioexception e) { log.e(tag, "clorialport: 关闭串口异常:"+e.tostring()); return; } log.d(tag, "clorialport: 关闭串口成功"); } /** * 发送串口指令(字符串) * @param data string数据指令 */ public void ndrialport(string data){ log.d(tag, "ndrialport: 发送数据"); try { byte[] nddata = data.getbytes(); //string转byte[] this.data_ = new string(nddata); //byte[]转string if (nddata.length > 0) { outputstream.write(nddata); outputstream.write('n'); //outputstream.write('r'+'n'); outputstream.flush(); log.d(tag, "ndrialport: 串口数据发送成功"); } } catch (ioexception e) { log.e(tag, "ndrialport: 串口数据发送失败:"+e.tostring()); } } /** * 单开一线程,来读数据 */ private class readthread extends thread{ @override public void run() { super.run(); //判断进程是否在运行,更安全的结束进程 while (!threadstatus){ log.d(tag, "进入线程run"); //64 1024 byte[] buffer = new byte[64]; int size; //读取数据的大小 try { size = inputstream.read(buffer); if (size > 0){ log.d(tag, "run: 接收到了数据:" + changetool.bytearrtohex(buffer)); log.d(tag, "run: 接收到了数据大小:" + string.valueof(size)); ondatareceivelistener.ondatareceive(buffer,size); } } catch (ioexception e) { log.e(tag, "run: 数据读取异常:" +e.tostring()); } } } } //数据回调 public ondatareceivelistener ondatareceivelistener = null; public static interface ondatareceivelistener { public void ondatareceive(byte[] buffer, int size); } public void tondatareceivelistener(ondatareceivelistener datareceivelistener) { ondatareceivelistener = datareceivelistener; }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
package com.demo.rialport;/** * @author renquan */public class changetool { /** * byte数组转16进制字符串 * * @param bytes byte数组 * @return 16进制字符串 */ public string bytearrtohex(byte[] bytes) { string strhex; stringbuilder sb = new stringbuilder(); for (byte abyte : bytes) { strhex = integer.tohexstring(abyte & 0xff); sb.append(" ").append((strhex.length() == 1) ? "0" : "").append(strhex); // 每个字节由两个字符表示,位数不够,高位补0 } return sb.tostring().trim(); } /** * byte字节转int * * @param b byte字节 * @return int */ public static int bytetoint(byte b) { int x = b & 0xff; if (x == 127) { return 0; } return x; }}123456789101112131415161718192021222324252627282930313233343536373839
package com.demo.rialport;import android.os.bundle;import android.util.log;import android.view.view;import android.widget.button;import android.widget.edittext;import androidx.appcompat.app.appcompatactivity;/** * @author renquan */public class mainactivity extends appcompatactivity implements view.onclicklistener { private edittext mmessage; private button mopen; private button mnd; private button mclo; private rialportutils rialportutils; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); tcontentview(r.layout.activity_main); init(); //串口数据监听事件 rialportutils.tondatareceivelistener(new rialportutils.ondatareceivelistener() { @override public void ondatareceive(byte[] buffer, int size) { log.d("tag", "进入数据监听事件中。。。" + new string(buffer)); } }); } private void init() { initview(); rialportutils = new rialportutils(); } private void initview() { mmessage = (edittext) findviewbyid(r.id.message); mopen = (button) findviewbyid(r.id.open); mopen.tonclicklistener(this); mnd = (button) findviewbyid(r.id.nd); mnd.tonclicklistener(this); mclo = (button) findviewbyid(r.id.clo); mclo自由创业.tonclicklistener(this); } @override public void onclick(view v) { switch (v.getid()) { ca r.id.open: // todo 20/12/28 rialportutils.openrialport("/dev/ttys9",9600); break; ca r.id.nd: // todo 20/12/28 rialportutils.ndrialport(mnd.gettext().tostring()); 隋朝李密 break; ca r.id.clo: rialportutils.clorialport(); 大唐三绝 // todo 20/12/28 break; default: break; } }}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
demo——github地址
本文发布于:2023-04-05 05:28:03,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/be0ecaf740e755f3449cf41d40bf4c24.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:android即时通讯demo开源(android studio实现聊天功能).doc
本文 PDF 下载地址:android即时通讯demo开源(android studio实现聊天功能).pdf
留言与评论(共有 0 条评论) |