Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 安卓Socket連接實現連接實現發送接收數據,openwrt wifi轉串口連接單片機實現控制,安卓openwrt

安卓Socket連接實現連接實現發送接收數據,openwrt wifi轉串口連接單片機實現控制,安卓openwrt

編輯:關於android開發

安卓Socket連接實現連接實現發送接收數據,openwrt wifi轉串口連接單片機實現控制,安卓openwrt


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

 

項目演示:

APP控制小車視頻演示

APP物聯系統視頻演示

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