本帖最后由 ldxxin 于 2019-6-26 16:09 编辑
这个是主程序
package com.example.a123.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText mIp; // IP
private EditText mPort; // 端口号
private String mStrIp; // 字符串类型ip
private int miPort; // 字符类型端口
private Button mBtnConnect; // 连接
private Socket mSocket; // 套接字
private PrintStream out; // 打印输出流
private ConnectThread mConnectThread; // TCP连接线程
private Button mMotorZ; // 按钮LED灯开
private Button mMoyorF; // 按钮LED灯关
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtnConnect=(Button)findViewById(R.id.mBt1);
mMotorZ=(Button)findViewById(R.id.mBt2);
mMoyorF=(Button)findViewById(R.id.mBt3);
mIp=(EditText)findViewById(R.id.mEtIP);
mPort=(EditText)findViewById(R.id.mEtPort);
mBtnConnect.setOnClickListener(this);
mMotorZ.setOnClickListener(this);
mMoyorF.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.mBt1:
if( mSocket == null || ! mSocket.isConnected()) {
mStrIp = mIp.getText().toString();
miPort = Integer.valueOf(mPort.getText().toString());
mConnectThread = new ConnectThread(mStrIp, miPort);
mConnectThread.start();
}
if(mSocket != null && mSocket.isConnected()){
try {
mSocket.close();
mSocket=null; // 清空mSocket
mBtnConnect.setText("连接");
Toast.makeText(MainActivity.this,"连接已关闭", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
break;
case R.id.mBt2:
if(out!=null){
out.print("0");
out.flush();
}
break;
case R.id.mBt3:
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 {
mSocket=new Socket(ip,port);
out = new PrintStream(mSocket.getOutputStream());
runOnUiThread(new Runnable(){
@Override
public void run() {
mBtnConnect.setText("断开");
Toast.makeText(MainActivity.this,"连接成功",Toast.LENGTH_LONG).show();
}
});
} catch (IOException e) {
e.printStackTrace();
runOnUiThread(new Runnable(){
@Override
public void run() {
mBtnConnect.setText("断开");
Toast.makeText(MainActivity.this,"连接失败",Toast.LENGTH_LONG).show();
}
});
}
}
}
}
以下是AndroidManifest.xml文件的配置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.a123.myapplication">
添加以下的才能与esp8266通信
<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>
以下是界面的一些程序
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
tools:ignore="Deprecated">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="9dp"
android:layout_y="24dp"
android:text="服务器IP:"
android:textSize="20dp"
tools:ignore="HardcodedText,MissingConstraints,ObsoleteLayoutParam,SpUsage" />
<EditText
android:id="@+id/mEtIP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="112dp"
android:layout_y="20dp"
android:ems="10"
android:inputType="textPersonName"
android:text="192.168.4.1"
tools:ignore="HardcodedText,LabelFor,ObsoleteLayoutParam" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="36dp"
android:layout_y="86dp"
android:text="端口:"
android:textSize="20dp"
tools:ignore="HardcodedText,MissingConstraints,ObsoleteLayoutParam,SpUsage" />
<EditText
android:id="@+id/mEtPort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="113dp"
android:layout_y="66dp"
android:ems="10"
android:inputType="textPersonName"
android:text="8089"
tools:ignore="HardcodedText,LabelFor,ObsoleteLayoutParam" />
<Button
android:id="@+id/mBt1"
android:layout_width="wrap_content"
android:layout_height="59dp"
android:layout_x="155dp"
android:layout_y="173dp"
android:text=""
tools:ignore="HardcodedText,ObsoleteLayoutParam"
tools:layout_editor_absoluteX="144dp"
tools:layout_editor_absoluteY="163dp" />
<Button
android:id="@+id/mBt2"
android:layout_width="wrap_content"
android:layout_height="69dp"
android:text="开"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/mBt3"
android:layout_width="wrap_content"
android:layout_height="72dp"
android:text="关"
tools:ignore="HardcodedText" />
</LinearLayout>
|