Android Socket 线程连接openwrt与arduino单片机串口双向通信的实例解析
程序员文章站
2024-02-29 16:52:40
废话不多说了,直接给大家贴代码了,具体代码如下所示:
package zcd.netanything;
import java.io.bufferedreade...
废话不多说了,直接给大家贴代码了,具体代码如下所示:
package zcd.netanything; import java.io.bufferedreader; import java.io.inputstreamreader; import java.io.printwriter; import java.net.socket; import android.app.fragment; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.os.bundle; import android.os.handler; import android.os.message; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.toast; public class mycar extends fragment implements onclicklistener{ private static string wifiurl; private static string wificom; private static string wificamera; private thread mthreadclient = null; private socket msocketclient = null; //视频线程 private thread mthreadvideo = null; private string recvmessageclient ; //mysurfaceview r; private boolean isconnect=false; //指令发出 数据缓存 static printwriter mprintwriterclient = null; static bufferedreader mbufferedreaderclient = null; public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // todo auto-generated method stub view view = inflater.inflate(r.layout.mycar,container, false); view.findviewbyid(r.id.button1).setonclicklistener(this); view.findviewbyid(r.id.button2).setonclicklistener(this); view.findviewbyid(r.id.button3).setonclicklistener(this); view.findviewbyid(r.id.button4).setonclicklistener(this); view.findviewbyid(r.id.button5).setonclicklistener(this); return view; } public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } //内部类 广播接收 public static class myreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { // todo auto-generated method stub wifiurl = intent.getstringextra("wifiurl"); wificom = intent.getstringextra("wificom"); wificamera = intent.getstringextra("wificamera"); toast.maketext(context, "广播已经接收", toast.length_short).show(); } } //来源 http://www.cnblogs.com/xiaobo-linux/ 赵存档 qq463431476 //线程mrunnable启动 private runnable mrunnable = new runnable() { public void run() { try { //连接服务器 msocketclient = new socket(wifiurl,integer.parseint(wificom)); //取得输入、输出流 mbufferedreaderclient = new bufferedreader(new inputstreamreader(msocketclient.getinputstream())); mprintwriterclient = new printwriter(msocketclient.getoutputstream(), true); recvmessageclient = "wifi成功连接";//消息换行 message msg = new message(); msg.what = 1; mhandler.sendmessage(msg); } catch (exception e) { recvmessageclient = "连接错误!";//消息换行 message msg = new message(); msg.what = 1; mhandler.sendmessage(msg); return; } setrecvmessage(); } }; public void setrecvmessage(){ char[] buffer = new char[256]; int count = 0; while (true) { try { if((count = mbufferedreaderclient.read(buffer))>0) { recvmessageclient = getinfobuff(buffer, count);//消息换行 //thread.sleep(500); message msg = new message(); msg.what = 1; mhandler.sendmessage(msg); //toast.maketext(getactivity(),recvmessageclient,toast.length_short).show(); } } catch (exception e) { recvmessageclient = "接收异常:" + e.getmessage() + "\n";//消息换行 message msg = new message(); msg.what = 0; mhandler.sendmessage(msg); } } } handler mhandler = new handler() { public void handlemessage(message msg) { super.handlemessage(msg); if(msg.what == 0) { toast.maketext(getactivity(),recvmessageclient,toast.length_short).show();//刷新消息机制 } else if(msg.what == 1) { try { string result = recvmessageclient; toast.maketext(getactivity(),result,toast.length_short).show(); }catch(exception e) { //toast.maketext(getactivity(),"无法获取数据,检查网络是否连接!",toast.length_short).show();//刷新消息机制 } } } }; //接收处理 private string getinfobuff(char[] buff, int count) { char[] temp = new char[count]; for(int i=0; i<count; i++) { temp[i] = buff[i]; } return new string(temp); } /*public void ondestroy() { super.ondestroy(); if (isconnect) { isconnect = false; try { if(msocketclient!=null) { msocketclient.close(); msocketclient = null; //mthreadvideo.destroy(); mprintwriterclient.close(); mprintwriterclient = null; recvmessageclient = "网络端口成功";//消息换行 message msg = new message(); msg.what = 0; mhandler.sendmessage(msg); } } catch (ioexception e) { e.printstacktrace(); } mthreadclient.interrupt(); } } */ public void onclick(view v) { // todo auto-generated method stub switch(v.getid()){ case r.id.button1: mprintwriterclient.print("f"); mprintwriterclient.flush();//清空 toast.maketext(getactivity(),"前进",toast.length_short).show(); break; case r.id.button2: mprintwriterclient.print("b"); mprintwriterclient.flush(); toast.maketext(getactivity(),"后退",toast.length_short).show(); break; case r.id.button3: mprintwriterclient.print("l"); mprintwriterclient.flush(); toast.maketext(getactivity(),"向左",toast.length_short).show(); break; case r.id.button4: mprintwriterclient.print("r"); mprintwriterclient.flush(); toast.maketext(getactivity(),"向右",toast.length_short).show(); break; case r.id.button5: if(!isconnect) { //开启mthreadclient线程 mthreadclient = new thread(mrunnable); mthreadclient.start(); toast.maketext(getactivity(),"尝试连接网络",500).show(); } else { ondestroy(); isconnect=false; // btn_openwifi.setbackgroundresource(r.drawable.disconnect); } //开启mthreadvideo线程 //mthreadvideo = new thread(mrunvideo); //mthreadvideo.start();//开启视频监听 break; //case r.id.button6: //关闭wifi /* if (isconnect) { isconnect = false; try { if(msocketclient!=null) { msocketclient.close(); msocketclient = null; //mthreadvideo.destroy(); mprintwriterclient.close(); mprintwriterclient = null; recvmessageclient = "网络端口成功";//消息换行 message msg = new message(); msg.what = 0; mhandler.sendmessage(msg); } } catch (ioexception e) { e.printstacktrace(); } mthreadclient.interrupt(); } */ // break; } } }
以上所述是小编给大家介绍的android socket 线程连接openwrt与arduino单片机串口双向通信,希望对大家有所帮助