我在esp32 wifi模块(arduino框架)中创建了一个DNS服务器来与flutter应用通信。它在dart和flutter ios应用程序中正常工作,但在flutter andoid应用程序中不起作用。我在flutter android中收到以下错误“ flutter(操作系统错误:没有与主机名相关的地址,errno = 7)”。由于它与dart和ios flutter应用程序完美配合,我希望它不是esp32 wifi程序错误。我认为其与颤振类人动物有关。我尝试了“ https://github.com/flutter/flutter/issues/27883”中发布的方法,但在我的情况下也不起作用。因此,请帮助我解决此问题。
https://github.com/flutter/flutter/issues/27883
ESP32代码
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiClient.h>
WiFiServer wifiServer(80);
String hostname="esp32";
const char* ssid = "ssid";
const char* password = "password";
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_AP);
WiFi.begin(ssid, password);
WiFi.setHostname("toyama");
Serial.printf("Connecting to ssid '%s'", ssid);
Serial.println();
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.printf("\nWiFi connected to '%s'", WiFi.SSID().c_str());
Serial.println();
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (!MDNS.begin(hostname.c_str())) {
Serial.println("Error setting up MDNS responder!");
while (1) {
delay(1000);
}
}
Serial.printf("mDNS responder started at %s.local:80\n",hostname.c_str());
// Start TCP (HTTP) server
wifiServer.begin();
// Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
Serial.print("Server started at ");
Serial.print(WiFi.localIP());
Serial.println(":80");
}
void loop()
{
WiFiClient client = wifiServer.available();
if (client) {
Serial.println("Client connected");
while (client.connected()) {
if (client.available() > 0) {
String c = client.readString();
Serial.print(client.remoteIP().toString());
Serial.print(":");
Serial.print(c);
}
if (Serial.available() > 0)
{
String ch = Serial.readString();
client.println(ch);
Serial.print(WiFi.localIP());
Serial.print(":");
Serial.println(ch);
}
}
client.stop();
Serial.println("Client disconnected");
}
}
颤振代码
import 'package:flutter/foundation.dart';
import 'dart:io';
import 'package:flutter/material.dart';
void main() async {
// modify with your true address/port
Socket sock = await Socket.connect("esp32.local", 80);
runApp(MyApp(sock));
}
class MyApp extends StatelessWidget {
Socket socket;
MyApp(Socket s) {
this.socket = s;
}
@override
Widget build(BuildContext context) {
final title = 'TcpSocket Demo';
return MaterialApp(
title: title,
home: MyHomePage(
title: title,
channel: socket,
),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
final Socket channel;
MyHomePage({Key key, @required this.title, @required this.channel})
: super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
TextEditingController _controller = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Form(
child: TextFormField(
controller: _controller,
decoration: InputDecoration(labelText: 'Send a message'),
),
),
StreamBuilder(
stream: widget.channel,
builder: (context, snapshot) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 24.0),
child: Text(snapshot.hasData
? '${String.fromCharCodes(snapshot.data)}'
: ''),
);
},
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _sendMessage,
tooltip: 'Send message',
child: Icon(Icons.send),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
void _sendMessage() {
if (_controller.text.isNotEmpty) {
widget.channel.writeln(_controller.text);
}
// widget.channel.write('\n');
}
@override
void dispose() {
widget.channel.close();
super.dispose();
}
}
答案 0 :(得分:0)
将
答案 1 :(得分:0)
如果 < uses-permission android:name="android.permission.INTERNET"/> 被添加到你的 Manifest.xml 文件中,你仍然得到这个错误,
请放心,当您用来运行 AVD 的笔记本电脑网络速度较慢时,会抛出此错误