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; } } }

 

posted @ 2016-11-28 12:27  zhaocundang  阅读(1160)  评论(0编辑  收藏  举报