Android Socket Thread Connection OpenWrt and Arduino single-chip computer serial port two-way communication case analysis _android

Source: Internet
Author: User
Tags flush static class stub

Nonsense not to say more, directly to everyone to paste the code, the specific code as follows:

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 wific
Om
private static String Wificamera;
Private Thread mthreadclient = null;
Private Socket msocketclient = null;
Video thread private thread mthreadvideo = null;
Private String recvmessageclient;
Mysurfaceview R; 
Private Boolean isconnect=false;
Instruction emits data cache static PrintWriter mprintwriterclient = null;
static BufferedReader mbufferedreaderclient = null; PublIC View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {//TODO auto-generated met
Hod Stub 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);}//internal class broadcast receives public static class M Yreceiver extends Broadcastreceiver {@Override public void onreceive (context context, Intent Intent) {//TODO Auto-gener
ated method Stub wifiurl = Intent.getstringextra ("Wifiurl");
wificom = Intent.getstringextra ("wificom");
Wificamera = Intent.getstringextra ("Wificamera");
Toast.maketext (Context, "broadcast has received", Toast.length_short). Show (); }//Source Http://www.cnblogs.com/xiaobo-Linux/Zhao Archive QQ463431476//thread mrunnable start private Runnable mrunnable = new Runnable () {public void run () {try {//Connect server MS 
Ocketclient = new Socket (Wifiurl,integer.parseint (wificom));
Get input, output stream mbufferedreaderclient = new BufferedReader (New InputStreamReader (Msocketclient.getinputstream ()));
Mprintwriterclient = new PrintWriter (Msocketclient.getoutputstream (), true);
recvmessageclient = "WiFi connection is successful";//message newline msg = new messages ();
Msg.what = 1; 
Mhandler.sendmessage (msg); catch (Exception e) {recvmessageclient = "Connection Error!)
";//message newline msg = new messages ();
Msg.what = 1;
Mhandler.sendmessage (msg);
Return
} setrecvmessage ();
}
}; public void Setrecvmessage () {char[] buffer = new CHAR[256]; int count = 0. while (true) {try {if (count = Mbufferedrea
Derclient.read (buffer)) >0 {recvmessageclient = getinfobuff (buffer, count);//Message Wrap//thread.sleep (500);
msg = new Message ();
Msg.what = 1; 
Mhandler.sendmessage (msg); Toast.maketext (Getactivity (), Recvmessageclient,toast.Length_short). Show ();
The catch (Exception e) {recvmessageclient = "Receive exception:" + e.getmessage () + "\ n";//message Wrap msg = new messages ();
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 ();//refresh message mechanism} else if (
Msg.what = = 1) {try {String result = recvmessageclient; 
Toast.maketext (Getactivity (), Result,toast.length_short). Show (); }catch (Exception e) {//toast.maketext (getactivity (), "Unable to get data, check whether the network is connected!")
", Toast.length_short). Show ();//Refresh Message Mechanism}}}; 
Receive processing private String getinfobuff (char[] buff, int count) {char[] temp = new Char[count]; for (int i=0; i<count; i++) {
Temp[i] = Buff[i];
Return to New String (temp); }/*public void OnDestroy () {Super.ondestroy (); if (isconnect) {isconnect = false; try {if (msocketclient!=null) {MSo
Cketclient.close ();
Msocketclient = null; Mthreadvideo.destroy ();
Mprintwriterclient.close ();
Mprintwriterclient = null;
recvmessageclient = "Network port success";//message newline msg = new messages ();
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:mprintwriter
Client.print ("F");
Mprintwriterclient.flush ()//Empty Toast.maketext (Getactivity (), "forward", Toast.length_short). Show ();
Break
Case R.id.button2:mprintwriterclient.print ("B");
Mprintwriterclient.flush ();
Toast.maketext (Getactivity (), "Back", Toast.length_short). Show ();
Break
Case R.id.button3:mprintwriterclient.print ("L");
Mprintwriterclient.flush ();
Toast.maketext (Getactivity (), "left", Toast.length_short). Show ();
Break
Case R.id.button4:mprintwriterclient.print ("R"); 
Mprintwriterclient.flush ();
Toast.maketext (Getactivity (), "right", Toast.length_short). Show ();
Break Case R.id.button5:if (!isconnect) {//Open mthreadclient thread mthreadclient = NEW Thread (mrunnable); 
Mthreadclient.start ();
Toast.maketext (Getactivity (), "Try to connect to the network"). Show (); else {OnDestroy (); isconnect=false;//Btn_openwifi.setbackgroundresource (R.drawable.disconnect);}//
Open the Mthreadvideo thread//mthreadvideo = The new Thread (Mrunvideo);
Mthreadvideo.start ()//open video monitor break; Case R.ID.BUTTON6://Turn off WiFi//if (isconnect) {isconnect = false; try {if (msocketclient!=null) {Msocketclient.close
();
Msocketclient = null;
Mthreadvideo.destroy ();
Mprintwriterclient.close ();
Mprintwriterclient = null;
recvmessageclient = "Network port success";//message newline msg = new messages ();
Msg.what = 0;
Mhandler.sendmessage (msg);
} catch (IOException e) {e.printstacktrace ();} mthreadclient.interrupt ();
} *//break; }
} 
}

The above is a small series to introduce the Android Socket thread connection OpenWrt and Arduino SCM serial port Two-way communication, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.