Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Socket 線程連接openwrt與arduino單片機串口雙向通信的實例解析

Android Socket 線程連接openwrt與arduino單片機串口雙向通信的實例解析

編輯:關於Android編程

廢話不多說了,直接給大家貼代碼了,具體代碼如下所示:

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單片機串口雙向通信,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved