7

I created a DNS server in esp32 wifi module (arduino framework) to communicate with flutter app. It working correctly in dart and flutter ios app but its not working with flutter andoid app. I am getting the following error in flutter android "flutter (os error: no address associated with hostname, errno = 7)". Since it is working perfectly with dart and ios flutter app I hope its not the esp32 wifi program error. I think its problem with flutter andoid. I tried the methods posted in "https://github.com/flutter/flutter/issues/27883" and is also not working in my case. So please help me to solve this issue.

https://github.com/flutter/flutter/issues/27883

ESP32 code


#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");

  }
}

Flutter code


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();
  }
}
Yatheesha S
  • 71
  • 1
  • 1
  • 4

4 Answers4

10

Add < uses-permission android:name="android.permission.INTERNET"/> to your Manifest.xml file. In your AVD Manager select the device and wipe out data by going on options (Arrow down button). Restart your emulator. Rerun the application.

  • Never thought that could be related to AVD. The error message doesn't seem to be related though. Above solution worked for me. Surprised! – nesimtunc Jun 03 '22 at 05:28
4

if < uses-permission android:name="android.permission.INTERNET"/> is added to your Manifest.xml file and you still get this error,

then be assured that this error is thrown when there is a slow network on the laptop you are using to run the AVD

Nibha Jain
  • 7,742
  • 11
  • 47
  • 71
1

I had the same problem. Follow these steps: In android studio go to AVD manager and then from actions tab, click on wipe data image

Ata
  • 11
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 04 '22 at 21:59
0

For me, the problem probably came from the VPN.

I also disable Wi-Fi on the emulator, then re-enable it.

Guillem Puche
  • 1,199
  • 13
  • 16