如何将 esp8266 softAp 与 android 应用连接
how to connect esp8266 softAp with android app
我想要一些简短的idea/links参考开始如何连接esp8266router/access点一个androidapp.Inesp8266静态ip是192.168.4.1想控制led blink 或其他带有 android 应用程序的功能。
如何在 esp8266 和 android app .
之间建立连接
在Android方面,它只是没有任何功能的网络通信。看看Official Documentation and tutorials like this。一切都取决于 esp8266 固件:
如果它实现了HTTP web server
,你可以在Android端使用HttpUrlConnection和GET或POST请求,在esp8266端使用相应的脚本;
如果它实现了ServerSocket
你可以在Android端使用Socket connection an implement Socket Client。
更新:
与 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();
我想要一些简短的idea/links参考开始如何连接esp8266router/access点一个androidapp.Inesp8266静态ip是192.168.4.1想控制led blink 或其他带有 android 应用程序的功能。 如何在 esp8266 和 android app .
之间建立连接在Android方面,它只是没有任何功能的网络通信。看看Official Documentation and tutorials like this。一切都取决于 esp8266 固件:
如果它实现了
HTTP web server
,你可以在Android端使用HttpUrlConnection和GET或POST请求,在esp8266端使用相应的脚本;如果它实现了
ServerSocket
你可以在Android端使用Socket connection an implement Socket Client。
更新:
与 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();