废话不多说了,直接给大家贴代码了,具体代码如下所示:
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
以上所述是小编给大家介绍的Android Socket 线程连接openwrt与arduino单片机串口双向通信,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!