问题描述
我正在尝试开发一个简单的 android 应用程序,该应用程序能够在 AP 模式下从 ESP8266 接收字符串。 该项目旨在与 ESP8266 建立本地通信(无需互联网连接)。该组件将发送 RFID 识别码。 但是,根据 [1] 和 [2] 等几个指南和示例,我无法在 Android 中的 TCP 客户端上接收任何数据。
以下是我的 Arduino 代码:
服务器AP
```
/* ----------------- Include Libraries -------------------- */
#include <ESP8266WiFi.h>
/* ----------- Create a WiFi access point and provide a web server on it. ----------- */
#ifndef APSSID
#define APSSID "ESPsoftAP"
#define APPSK "pass-to-ESP"
#endif
// Set these to your desired credentials.
const char *ssid = APSSID;
const char *password = APPSK;
WiFiServer server(80);
WiFiClient client;
/* -------------------------------- Other variables --------------------------------- */
int status = WL_IDLE_STATUS;
char data[1500];
int ind = 0;
/* ------------------------------- Setup Method ------------------------------- */
void setup() {
delay(1000);
Serial.begin(115200);
while(!Serial) delay(1);
delay(3000);
Serial.print("Configuring access point...");
WiFi.mode(WIFI_AP);
Serial.print(WiFi.softAP(ssid,password) ? "Ready" : "Failed");
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();
Serial.println("HTTP server started");
}
/* ------------------------------- Loop Method -------------------------------- */
void loop() {
// wait for a new client:
WiFiClient client = server.available();
// when the client sends the first byte,say hello:
while (client.connected()) {
// echo the bytes back to the client:
server.write("Hello Android");
}
}
```
以下是我的 Android 代码:
主活动
```
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private final static String TAG = MainActivity.class.getSimpleName();
private boolean isConnected = false;
private Runnable socketThread;
private ExecutorService executorService;
private final Map<Runnable,Future<?>> taskFutureMap = new HashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// serviceIntent = new Intent(this,ConnectionService.class);
executorService = Executors.newSingleThreadExecutor();
socketThread = new SocketClientThread();
FloatingActionButton btn = findViewById(R.id.conn_btn);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v instanceof FloatingActionButton) {
if (!isConnected) {
Log.i(TAG,"Connection started");
taskFutureMap.put(socketThread,executorService.submit(socketThread));
isConnected = true;
} else {
Log.i(TAG,"Connection stopped");
taskFutureMap.get(socketThread).cancel(true);
isConnected = false;
}
}
}
}
```
SocketClientThread
```
public class SocketClientThread implements Runnable {
private final static String TAG = SocketClientThread.class.getSimpleName();
private final static String IP_ADDRESS = "192.168.4.1";
private final static int PORT = 80;
private Socket clientSocket;
@Override
public void run() {
try {
Log.i(TAG,"Connecting to server...");
final InetAddress serverAddress = InetAddress.getByName(IP_ADDRESS);
clientSocket = new Socket(serverAddress,PORT);
Log.i(TAG,"Connection status: " + clientSocket.isConnected());
Log.i(TAG,"Data received: " + clientSocket.getInputStream().available());
if (clientSocket.getInputStream().available() > 0) {
DataInputStream dataInputStream =
new DataInputStream(clientSocket.getInputStream());
String strResponse = dataInputStream.readUTF();
Log.i(TAG,"Response: " + strResponse);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (clientSocket != null){
try {
clientSocket.close();
}
catch (IOException ignore) {}
}
}
}
}
```
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)