Search code examples
c#androidxamarin.formstcpesp-idf

Unable to open ESP32 socket from Xamarin.Forms app after upgrading to Android 9


Opening the TCP socket which is listening on port 3000 on the ESP32 from the Xamarin.Forms app does not work anymore after updating the phone to Android 9. On Android 8 this worked but after updating the socket can not be opened.

The following exception is thrown by the app:

System.InvalidOperationException: The operation is not allowed on non-connected sockets.

I've already tried some methods for fixing this problem:

  • Trying the app on an older phone (Android 6) which works
  • Trying opening the socket on the ESP32 from a PC using netcat which works
  • Trying opening a socket on a pc from the app which works
  • Trying opening the socket on the ESP32 from a different TCP client app on the smartphone which works

All of the above work, but trying to open the TCP socket on the ESP32 from the xamarin forms app doesn't.

I have also tried

  • enabeling cleartextTrafficPermitted which doesn't work
  • adding the "INTERNET" permission to the android manifest which doesn't work

For the Xamarin.Forms app this code is used

var client = new TcpClient();
            var result = client.BeginConnect("192.168.1.1", 3000, null, null);

            var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1));

            try
            {
                NetworkStream nwStream = client.GetStream();
                byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(message);

                nwStream.Write(bytesToSend, 0, bytesToSend.Length);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);                
            }

            client.EndConnect(result);

On the listener side I use this for listening:

client_socket = accept(soc, (struct sockaddr *)&remote_addr, &socklen);

read(client_socket, recv_buf, sizeof(recv_buf));

It seems like Android can't open the socket on the ESP32, the question is why?


Solution

  • I had a socket connection issue too when upgrading to Android 9. I had a ionic-cordova and a Xamarin form app to try basic tcp socket connection on local wifi (hosted on a raspbery)

    It was all working on previous android version, and from laptop but i was getting timeout in android pie.

    Turns out, since my Raspberry had no internet connection, when trying to open a socket by IP, Android 9 was not using the wlan network but tried to used its 4G connection to Internet and failed. After I turned off 4G, it managed to connect to the Rasp.

    my simple code sample for opening connection in Xamarin:

     private TcpClient m_client;
    
    async public void connect(string address, int port)
            {
                if(m_client.Connected)
                {
                    m_client.Close();
                }
    
                try
                {
                    await m_client.ConnectAsync(address, port);
                }catch(Exception e)
                {
                    Console.WriteLine($"error while connecting: {e.Message}");
                }
                Console.WriteLine($"is connected : {m_client.Connected}");
    
            }