ESP8266 WIFI模块学习之路(7)——自写Android手机APP接受单片机数据
程序员文章站
2024-03-22 18:21:40
...
上一篇是写关于自写Android手机APP给单片机下发数据的,这次我将写一下APP如何接受单片机数据。
其实使用调试助手,或者别人开的的APP同样能够实现接受单片机数据,但想学习的,我还是建议能够自己写一个Android软件,这样从底层到上层都能学到很多东西。
APP页面如图:
硬件连接如图:
单片机的硬件代码:
if(0 == P32) // 通过串口给手机发送字符串 wantin
{
ms_delay(400);
if(0 == P32)
{
ES = 0;
TI = 1;
printf("AT+CIPSEND=?\r\n");
LED();
ms_delay(1000) ;
printf("AT+CIPSEND=0,10\r\n");
LED();
ms_delay(1000) ;
printf("wangting\r\n");
LED();
ms_delay(1000) ;
LED();
ms_delay(1000) ;
LED();
ms_delay(1000) ;
while(!TI);
TI = 0;
ES = 1;
}
while(0 == P32);
}
我这里只贴了串口发送的代码,它的数据是通过按键进行发送的,以前的文章也有写到。上发的内容为wangting.
app代码部分:
package com.open_open_wt.myapplication;
import android.os.Handler;
import android.os.Message;
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.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ConnectThread mConnectThread;
private Socket mSocket;
private EditText mEIP;
private EditText mEPort;
private String mStrIP;
private int miPort;
private Button mBtnConn;
private Button mBtnRecData;
private TextView mtvRec;
private String line;
private String mstr;
private int flag=1;
BufferedReader br;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEIP=(EditText)findViewById(R.id.eTIP);
mEPort=(EditText)findViewById(R.id.eTPort);
mBtnConn=(Button)findViewById(R.id.btConn);
mBtnRecData=(Button)findViewById(R.id.bRecData);
mtvRec=(TextView)findViewById(R.id.tVRecData);
mBtnConn.setOnClickListener(this);
mBtnRecData.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btConn:
// 连接
if(mSocket == null || !mSocket.isConnected()){
mStrIP=mEIP.getText().toString();
miPort=Integer.valueOf(mEPort.getText().toString());
mConnectThread=new ConnectThread(mStrIP,miPort);
mConnectThread.start();
}
if(mSocket != null && mSocket.isConnected()){
try {
mSocket.close();
mSocket=null;
mBtnConn.setText("连接");
Toast.makeText(MainActivity.this,"连接断开",Toast.LENGTH_LONG).show();
}catch (IOException e){
e.printStackTrace();
}
}
break;
case R.id.bRecData:
if(mSocket != null && mSocket.isConnected()){
if(flag==1){
Toast.makeText(MainActivity.this,"开始接受数据",Toast.LENGTH_LONG).show();
mBtnRecData.setText("Doing recDta");
GetTCPstring();
flag=(-1)*flag;
}
else{
Toast.makeText(MainActivity.this,"停止接受数据",Toast.LENGTH_LONG).show();
mBtnRecData.setText("RecData");
try {
mSocket.getInputStream().close();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this,"停止接受数据失败",Toast.LENGTH_LONG).show();
}
flag=(-1)*flag;
}
}
else{
mBtnRecData.setText("RecData");
Toast.makeText(MainActivity.this,"开始接受数据失败",Toast.LENGTH_LONG).show();
}
break;
}
}
private void GetTCPstring(){
new Thread(){
public void run(){
try{
char[] cbuf=new char[10];
br=new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
while (true){
if(br.ready()){
br.read(cbuf,0,10);
line=String.valueOf(cbuf);
handler.sendMessage(handler.obtainMessage());
}
}
}catch(IOException e){
e.printStackTrace();
Toast.makeText(MainActivity.this,"接受数据失败",Toast.LENGTH_LONG).show();
}
}
}.start();
}
// 消息句柄
private Handler handler=new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
mstr+=line;
if(mstr.length()>500){
mstr="";
}
mtvRec.setText(mstr);
}
};
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);
runOnUiThread(new Runnable() {
@Override
public void run() {
mBtnConn.setText("断开");
Toast.makeText(MainActivity.this,"连接成功",Toast.LENGTH_LONG).show();
}
});
}catch (IOException e){
e.printStackTrace();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"连接失败",Toast.LENGTH_LONG).show();
}
});
}
}
}
}
刚开始学习Android代码,写的逻辑一定有问题,因为我测试了一下,会有闪退问题,但功能是能用的。手机连接到WIFI模块的WIFI上,然后打开我编写的APP——连接——接收数据按钮按下,接收的数据如图:
能够正确接收数据了,这就说明,手机基本能正确接收单片机上发的数据了,后期还需要不断的研究与深化。