1. 程式人生 > 實用技巧 >製作連線ESP8266 WiFi模組WiFi的Android客戶端APP

製作連線ESP8266 WiFi模組WiFi的Android客戶端APP

一、連線ESP8266 WiFi模組WiFi的Android客戶端Java程式碼

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客戶端介面佈局程式碼

<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模組WiFi的Android客戶端屬性許可權程式碼

<?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模組WiFi的Android客戶端字串資原始碼

<?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模組WiFi的Android客戶端APP介面