• 编写连接ESP8266 WiFi模块WiFi的Android客户端APP安卓程序


    一、编写连接ESP8266 WiFi模块(ESP8266-01S芯片)WiFi的Android客户端安卓主程序

    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模块(ESP8266-01S芯片)WiFi的Android客户端APP安卓界面布局程序

    <RelativeLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context="com.example.esp8266androidclient.ESP8266AndroidClientMainActivity" >
      <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
        <TextView
          android:id="@+id/textView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="服务器地址:"
          android:textSize="20dp" />
        <EditText
          android:id="@+id/editextip"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:ems="10"
          android:hint="ESP8266 WiFi模块WiFi地址号"
          android:inputType="textPersonName" />
       </LinearLayout>
       <LinearLayout
         android:id="@+id/linearLayout2"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/linearLayout1"
         android:layout_below="@+id/linearLayout1" >
         <TextView
           android:id="@+id/textView2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="服务器端口:"
           android:textSize="20dp" />
         <EditText
           android:id="@+id/editextport"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:ems="10"
           android:hint="ESP8266 WiFi模块WiFi端口号"
           android:inputType="textPersonName" />
        </LinearLayout>
        <Button
          android:id="@+id/button1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/linearLayout2"
          android:layout_below="@+id/linearLayout2"
          android:layout_marginTop="30dp" 
          android:text="连接"/>
        <Button
          android:id="@+id/button2"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/button1"
          android:layout_below="@+id/button1"
          android:layout_marginTop="30dp"
          android:text="打开" />
        <Button
          android:id="@+id/button3"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/button2"
          android:layout_below="@+id/button2"
          android:layout_marginTop="30dp"
          android:text="关闭" />
    </RelativeLayout>

    三、编写连接ESP8266 WiFi模块(ESP8266-01S芯片)WiFi的Android客户端APP安卓属性权限程序

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.esp8266androidclient"
      android:versionCode="1"
      android:versionName="1.0" >
      <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
      <uses-permission android:name="android.permission.INTERNET"></uses-permission>
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
      <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
      <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
          android:name=".ESP8266AndroidClientMainActivity"
          android:label="@string/app_name" >
          <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
        </activity>
       </application>
    </manifest>

    四、编写连接ESP8266 WiFi模块(ESP8266-01S芯片)WiFi的Android客户端APP字符串资源程序

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <string name="app_name">ESP8266AndroidClient</string>
      <string name="hello_world">Hello world!</string>
      <string name="action_settings">Settings</string>
    </resources>

    五、连接ESP8266 WiFi模块(ESP8266-01S芯片)WiFi的Android客户端APP界面

  • 相关阅读:
    《影响力》 一个活生生的例子(转载)
    《激荡三十年》
    百度编辑器UEditor ASP.NET示例Demo 分类: ASP.NET 2015-01-12 11:18 347人阅读 评论(0) 收藏
    网站通用登录模块代码 分类: ASP.NET 2014-12-06 10:49 614人阅读 评论(0) 收藏
    网站通用登录模块代码 分类: ASP.NET 2014-12-06 10:49 615人阅读 评论(0) 收藏
    JqueryDemoTools-用于整理jQueryDemo 分类: C# 公共资源 2014-12-02 16:50 223人阅读 评论(1) 收藏
    JqueryDemoTools-用于整理jQueryDemo 分类: C# 公共资源 2014-12-02 16:50 224人阅读 评论(1) 收藏
    visual studio2010复制粘贴源代码到Word时乱码问题 分类: C# 2014-11-28 09:25 686人阅读 评论(0) 收藏
    visual studio2010复制粘贴源代码到Word时乱码问题 分类: C# 2014-11-28 09:25 687人阅读 评论(0) 收藏
    不定义JQuery插件,不要说会JQuery 分类: JavaScript 2014-11-24 14:18 154人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/AChenWeiqiangA/p/13337558.html
Copyright © 2020-2023  润新知