Android Socket 线程连接openwrt与arduino单片机串口双向通信的实例解析
更新时间:2016年11月28日 17:04:41 作者:小波Linux
这篇文章主要介绍了Android Socket 线程连接openwrt与arduino单片机串口双向通信的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
废话不多说了,直接给大家贴代码了,具体代码如下所示:
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单片机串口双向通信,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
相关文章
Android UI系列-----ScrollView和HorizontalScrollView的详解
本篇文章主要是介绍的Android UI系列-----ScrollView和HorizontalScrollView,ScrollView和HorizontalScrollView都是布局容器,有需要的可以了解一下。2016-11-11Android Jetpack组件DataBinding详解
这篇文章主要介绍了Android Jetpack组件DataBinding,DataBinding有很多优势,其中最明显是代码更加简洁,可读性会更高。部分和UI控件有关的代码可以在布局文件当中完成,本文给大家详细讲解,需要的朋友可以参考下2022-10-10
最新评论