赞
踩
package com.example.esp8266_gpio;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Handler;
public class ESP8266 extends Activity
{
Handler handler = new Handler();
private final String SERVER_HOST_IP =
"192.168.4.1";
private final int SERVER_HOST_PORT =
5000;
private Button button1;
private Button Button01;
private Button Button02;
private Button Button03;
private Button Button04;
private Button Button05;
private Button Button06;
private Button Button07;
private TextView textView1;
private Socket socket;
private PrintStream output;
Runnable runnable = new Runnable() {
@Override
public void run() {
try
{
socket.sendUrgentData(0xFF);
}catch(Exception ex){
closeSocket();//断开连接
}
handler.postDelayed(this,
1000);
}
};
public void toastText(String message)
{
Toast.makeText(this,
message, Toast.LENGTH_LONG).show();
}
public void handleException(Exception e,
String prefix)
{
e.printStackTrace();
toastText(prefix +
e.toString());
}
@Override
public void onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_esp8266);
initView();
button1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
initClientSocket();
}
});
Button01.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
closeSocket();//断开连接
}
});
Button02.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
try
{
socket.sendUrgentData(0xFF);
}catch(Exception ex){
closeSocket();//断开连接
}
sendMessage("GPIO0=0;");
}
});
Button03.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
sendMessage("GPIO0=1;");
}
});
Button04.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
sendMessage("GPIO0=2;");
}
});
Button05.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
sendMessage("GPIO0=3;");
}
});
Button06.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
sendMessage("GPIO0=4;");
}
});
Button07.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
sendMessage("GPIO0=5;");
}
});
}
public void initView()
{
button1 =
(Button)findViewById(R.id.button1);
Button01 =
(Button)findViewById(R.id.Button01);
Button02 =
(Button)findViewById(R.id.Button02);
Button03 =
(Button)findViewById(R.id.Button03);
Button04 =
(Button)findViewById(R.id.Button04);
Button05 =
(Button)findViewById(R.id.Button05);
Button06 =
(Button)findViewById(R.id.Button06);
Button07 =
(Button)findViewById(R.id.Button07);
textView1=(TextView)findViewById(R.id.textView1);
Button01.setEnabled(false);
Button02.setEnabled(false);
Button03.setEnabled(false);
Button04.setEnabled(false);
Button05.setEnabled(false);
Button06.setEnabled(false);
Button07.setEnabled(false);
textView1.setText("设备未连接");
}
public void closeSocket()
{
try
{
output.close();
socket.close();
button1.setEnabled(true);
Button01.setEnabled(false);
Button02.setEnabled(false);
Button03.setEnabled(false);
Button04.setEnabled(false);
Button05.setEnabled(false);
Button06.setEnabled(false);
Button07.setEnabled(false);
textView1.setText("设备未连接");
}
catch (IOException
e)
{
}
}
private void initClientSocket()
{
try
{
socket = new Socket(SERVER_HOST_IP, SERVER_HOST_PORT);
output = new PrintStream(socket.getOutputStream(), true,
"utf-8");
button1.setEnabled(false);
Button01.setEnabled(true);
Button02.setEnabled(true);
Button03.setEnabled(true);
Button04.setEnabled(true);
Button05.setEnabled(true);
Button06.setEnabled(true);
Button07.setEnabled(true);
textView1.setText("连接成功");
handler.postDelayed(runnable, 1000);
}
catch
(UnknownHostException e)
{
handleException(e, "unknown host exception: " +
e.toString());
}
catch (IOException
e)
{
toastText("连接错误!请检查是否连接硬件的wifi");
}
}
private void sendMessage(String msg)
{
output.print(msg);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。