制作连接ESP8266 WiFi模块WiFi的Android客户端APP控制单片机外围LED灯亮灭

一、制作连接ESP8266 WiFi模块WiFi的Android客户端APP控制单片机外围LED灯亮灭的原理

 

二、

三、

四、

五、

六、

七、

八、

九、

十、

package com.example.esp8266androidclient;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
  public class ESP8266AndroidClientMainActivity extends ActionBarActivity implements View.OnClickListener
{
   private EditText IP;//IP
   private EditText PORT;//端口号
   private String stringip;//字符串类型IP
   private int stringport;//字符类型端口号
   private Button connect;//连接
   private Socket socket;//套接字
   private PrintStream out;//打印输出流
   private ConnectThread connectthread;//连接线程
   private Button open;//按钮LED灯开
   private Button close;//按钮LED灯关
   @Override
   protected void onCreate(Bundle savedInstanceState) 
 {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_esp8266_android_client_main);
    connect=(Button)findViewById(R.id.button1);
    open=(Button)findViewById(R.id.button2);
    close=(Button)findViewById(R.id.button3);
    IP=(EditText)findViewById(R.id.editextip);
    PORT=(EditText)findViewById(R.id.editextport);
    connect.setOnClickListener(this);
    open.setOnClickListener(this);
    close.setOnClickListener(this);
   }
    @Override
    public void onClick(View v) 
  {
     switch(v.getId())
   {
      case R.id.button1:
                          if( socket == null || ! socket.isConnected()) 
                        {
                           stringip = IP.getText().toString();
                           stringport = Integer.valueOf(PORT.getText().toString());
                           connectthread = new ConnectThread(stringip, stringport);
                           connectthread.start();
                          }
                          if(socket != null && socket.isConnected())
                        {
                           try 
                         {
                            socket.close();
                            socket=null;   //  清空mSocket
                            connect.setText("连接");
                            Toast.makeText(ESP8266AndroidClientMainActivity.this,"连接已关闭", Toast.LENGTH_LONG).show();
                           } 
                           catch (IOException e) 
                         {
                            e.printStackTrace();
                           }
                         }
                          break;
     case R.id.button2:
                         if(out!=null)
                       {
                          out.print("0");
                          out.flush();
                         }
                         break;
     case R.id.button3:
                         if (out!=null)
                       {
                          out.print("1");
                          out.flush();
                         }
                         break;
    }
   }
   private class ConnectThread extends Thread
 {
    private String ip;
    private int port;
    public ConnectThread(String ip,int port){
    this.ip=ip;
    this.port=port;
   }
   @Override
   public void run() 
 {
    try 
  {
     socket=new Socket(ip,port);
     out = new PrintStream(socket.getOutputStream());
     runOnUiThread(new Runnable()
   {
      @Override
      public void run() 
    {
       connect.setText("断开");
       Toast.makeText(ESP8266AndroidClientMainActivity.this,"连接成功",Toast.LENGTH_LONG).show();
      }
     });
    } 
    catch (IOException e) 
  {
     e.printStackTrace();
     runOnUiThread(new Runnable()
   {
      @Override
      public void run() 
    {
       connect.setText("断开");
       Toast.makeText(ESP8266AndroidClientMainActivity.this,"连接失败",Toast.LENGTH_LONG).show();
      }
     });
    }
   }
  }    
 }

 

制作连接ESP8266 WiFi模块WiFi的Android客户端APP控制单片机外围LED灯亮灭

上一篇:vue+axios 下载后端返回的文件流


下一篇:APP移动端的开发(从无到有)——基于VUE+mint-ui