安卓Socket連接實現連接實現發送接收數據,openwrt wifi轉串口連接單片機實現控制 socket 連接採用流的方式進行發送接收數據,採用thread線程的方式。 什麼是線程? 詳細代碼介紹: 博文來源:http://blog.csdn.net/zhaocundang 如果你對代碼有些疑問 ...
安卓Socket連接實現連接實現發送接收數據,openwrt wifi轉串口連接單片機實現控制
socket 連接採用流的方式進行發送接收數據,採用thread線程的方式。
什麼是線程?
詳細代碼介紹:
博文來源:http://blog.csdn.net/zhaocundang 如果你對代碼有些疑問,請在下麵留言來探討,或者加我的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成功連接";//消息換行
// Toast.makeText(getActivity(),"wifi連接成功",Toast.LENGTH_SHORT).show();
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);
}
}
catch (Exception e)
{
recvMessageClient = "接收異常:" + e.getMessage() + "\n";//消息換行
Message msg = new Message();
msg.what = 0;
mHandler.sendMessage(msg);
}
}
}
//handler用於線程中更新界面數據
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(); //接收消息的
String [] results = recvMessageClient.split("\\.");;
// Toast.makeText(getActivity(),results[3],Toast.LENGTH_SHORT).show(); //接收消息的
/*
Intent intent=new Intent();
intent.putExtra("temp",results[0]);
intent.putExtra("humi",results[2]);
intent.setAction("zcd.netanything.temp");
getActivity().sendBroadcast(intent);
*/
}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.print("t");
mPrintWriterClient.flush();//清空
Toast.makeText(getActivity(),"前進",Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
mPrintWriterClient.print("b");
// mPrintWriterClient.print("t");
mPrintWriterClient.flush();
Toast.makeText(getActivity(),"後退",Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
mPrintWriterClient.print("l");
// mPrintWriterClient.print("t");
mPrintWriterClient.flush();
Toast.makeText(getActivity(),"向左",Toast.LENGTH_SHORT).show();
break;
case R.id.button4:
mPrintWriterClient.print("r");
// mPrintWriterClient.print("t");
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;
}
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;
}
}
項目演示: