我想要一些简短的想法/链接作为引用,以开始如何使用 Android 应用程序连接 esp8266 路由器/接入点。在 esp8266 静态 ip 是 192.168.4.1 想要使用 Android 应用程序控制 led 闪烁或其他功能。 如何在esp8266和android应用程序之间建立连接。
最佳答案
Android端只是网络通信,没有任何功能。看看Official Documentation以及类似 this 的教程。一切都取决于 esp8266 固件:
如果它实现
HTTP web server
您可以使用HttpUrlConnection Android端的GET或POST请求以及esp8266端的相应脚本;如果它实现
ServerSocket
您可以使用Socket连接工具Socket Client Android 端。
更新:
与 esp8266
进行套接字通信您应该在单独的(而不是 UI)线程中执行此操作。完整的例子是这样的:
class SocketClientThread implements Runnable {
DataInputStream dis;
DataOutputStream dos;
String strResponseData;
@Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName("<address>");
clientSocket = new Socket(serverAddr, <port_number - 80 in your example>);
dos = new DataOutputStream(clientSocket.getOutputStream());
dis = new DataInputStream(clientSocket.getInputStream());
// now you can write data to stream
dos.writeUTF("Hello");
// you can also read data from stream
strResponseData = dis.readUTF();
} catch (UnknownHostException ignore) {
} catch (IOException ignore) {
}
finally{
if (clientSocket != null){
try {
clientSocket.close();
}
catch (IOException ignore) {
}
}
}
}
}
您可以这样使用 SocketClientThread:
Thread socketClientThread;
socketClientThread = new Thread(new SocketClientThread());
socketClientThread.start();
关于java - 如何将esp8266 softAp与android应用程序连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40691721/