目标的简短描述:
1)启动ESP8266 Wifi模块作为侦听特定端口的TCP服务器 - >到目前为止完成了
2)从服务器向客户端发送一个int或String
2)编程Android App(Android Studios),作为客户端,连接到服务器并接收发送的数据。
问题:
1)我的客户端应用程序没有收到任何数据
2)我也无法从我的MainActivity类访问消息以在TextView中显示它,变量在Try,Catch Block中。
Android Studio
<小时/> 客户代码:
package com.example.petarmaric.client;
import android.util.Log;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;
/**
* Created by petarmaric on 04/02/2017.
*/
public class TCP_Client {
// Variables and Constants
public static final String SERVERIP = "192.168.1.2";
public static final int SERVERPORT = 3030;
DataInputStream in;
Integer serverMessage;
// Run the Client
public void run(){
try {
InetAddress serverAddress = InetAddress.getByName(SERVERIP);
Log.e("Connection", "connecting to server...");
//Create the Socket
Socket socket = new Socket(serverAddress, SERVERPORT);
Log.e("Socket", "Socket was created");
try {
//receive the message from the Server
//in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
in = new DataInputStream(socket.getInputStream());
Log.e("InputStream","DataInputStream created");
serverMessage = in.read();
Log.e("Message from Server", "Maybe");
} catch (Exception e) {
Log.e("InputERROR", "Nachricht wurde nicht empfangen");
} finally {
socket.close();
Log.e("Socket","Socket was closed");
}
}catch (Exception e){
Log.e("ConnectionERROR","Nicht connected");
}
Log.e("End","End of Run Method");
}
}
应用程序启动后Logcat:
Markdown and HTML are turned off in code blocks:
<i>This is not italic</i>, and [this is not a link](http://example.com)
02-04 18:36:17.475 25858-25858/com.example.petarmaric.client W/System: ClassLoader referenced unknown path: /data/app/com.example.petarmaric.client-2/lib/x86_64
02-04 18:36:17.788 25858-25858/com.example.petarmaric.client W/System: ClassLoader referenced unknown path: /data/app/com.example.petarmaric.client-2/lib/x86_64
02-04 18:36:18.157 25858-25858/com.example.petarmaric.client W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
02-04 18:36:18.213 25858-25895/com.example.petarmaric.client E/Connection: connecting to server...
02-04 18:36:18.217 25858-25896/com.example.petarmaric.client D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
[ 02-04 18:36:18.219 25858:25858 D/ ]
HostConnection::get() New Host Connection established 0x7fa68eb72bc0, tid 25858
[ 02-04 18:36:18.278 25858:25896 D/ ]
HostConnection::get() New Host Connection established 0x7fa68eb72e80, tid 25896
02-04 18:36:18.295 25858-25896/com.example.petarmaric.client I/OpenGLRenderer: Initialized EGL, version 1.4
02-04 18:36:18.367 25858-25895/com.example.petarmaric.client E/Socket: Socket was created
02-04 18:36:18.367 25858-25895/com.example.petarmaric.client E/InputStream: DataInputStream created
MainAcitvity代码:
Markdown and HTML are turned off in code blocks:
<i>This is not italic</i>, and [this is not a link](http://example.com)
package com.example.petarmaric.client;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
//Declaration of Client object
private TCP_Client mTcpClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Connect to the Server
new ConnectTask().execute("");
}
//ConnectClass definition
public class ConnectTask extends AsyncTask
{
@Override
protected TCP_Client doInBackground(Object[] params) {
//We create the TCP client object and run the Task
mTcpClient = new TCP_Client();
mTcpClient.run();
Log.e("Main Thread", "Client run initiated");
return null;
}
}
}
Arduino IDE
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <SPI.h>
// declare the Variables
char* ssid = "NETGEAR54";
char* password = "calmpiano742";
char thisChar;
//create wifi server listen on a given port
WiFiServer server(3030);
//create Wifi client
WiFiClient client;
void setup() {
//Connnect to WiFi Network
Serial.begin(115200);
WiFi.begin(ssid,password);
Serial.println("");
//Wait for connection
while (WiFi.status()!=WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
//Print status to Serial Monitor
Serial.print("connected to: "); Serial.println(ssid);
Serial.print("IP Address: "); Serial.println(WiFi.localIP());
//Start the TCP server
server.begin();
}
void loop() {
client = server.available();
if(client){
if(client.connected()){
Serial.println("connected to client");
while(client.connected()){
server.write(1);
}
}
}
}
串行监视器:
1384, room 16
tail 8
chksum ��
......connected to: NETGEAR54
IP Address: 192.168.1.2
connected to client
.....................................................................
Soft WDT reset
ctx: cont
sp: 3ffef2c0 end: 3ffef4f0 offset: 01b0
>>>stack>>>
3ffef470: 3ffe8644 3ffee49c 3ffee49c 40202d25
3ffef480: 3ffe8444 00000002 3ffe84ff 40203380
3ffef490: 00000001 3ffee388 3ffee49c 3ffee4c8
3ffef4a0: 3ffee388 3ffee49c 3ffee370 40201d40
3ffef4b0: 3ffe8480 00000000 000003e8 40201c89
3ffef4c0: 00000000 3fff0424 00000001 40202fc9
3ffef4d0: 3fffdad0 00000000 3ffee4c0 40202ff4
3ffef4e0: feefeffe feefeffe 3ffee4d0 40100718
<<<stack<<<
ets Jan 8 2013,rst cause:2, boot mode:(1,7)
在此事先感谢您的帮助,如果您需要任何进一步的信息,请在下面写下您的问题。