Home » Android » java – Android dealing with IoT devices with NO Internet connection

java – Android dealing with IoT devices with NO Internet connection

Posted by: admin May 14, 2020 Leave a comment


I’m trying to build a project where i must pilot an IoT device from smartphone via wifi.

This device has the SPWF01 Wifi Module integrated, and is configured as an access point(that has no Internet access) with security type WEP. On this access point configuration we also have a TCP Socket Server that intercepts smartphone communications.

On the smartphone side, we have the part which scans and connects to our device’s access point(which works, although i get the esclamation point on the wifi icon since it has no Internet access). After we’ve connected, we start the Client Socket which connects to the server on our IoT device(the ip address of the server socket is actually the gateway of the access point). And here is where the trouble starts, because the client socket won’t start. Here is the code:

public void SocketInit(String ip, int port) throws IOException {
    InetAddress addr = InetAddress.getByName(ip);
    SocketAddress sockaddr = new InetSocketAddress(addr, port);
    nsocket = new Socket();
    nsocket.setReceiveBufferSize(700);   //Must be less than 730byte witch is the module buffer
    nsocket.connect(sockaddr, 5000);     //5 second connection timeout

And here is the exception i get:

java.net.SocketException: socket failed: ENONET (Machine is not on the network)

And i get that error even before reaching nsocket.connect(), precisely on setReuseAddress.

Since the exception i get is ENONET, i thought that it must be because the access point has no internet access so i used the solution proposed here for testing purpose:

adb shell settings put global captive_portal_detection_enabled 0

This is a solution that can’t be done programmatically without having root access, but i wanted to test if that was the problem. But although the exclamation mark on the wifi icon had disappeared, the client socket still gave me the same exception error.

Does anybody have a solution for this behaviour? Thank you in advance!

Sometimes the client socket manages to open, with a success rate of 1 out of 20 times. But when it does, i usually get another exception after a couple of messages sent:

java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)

Here is the code i used to connect to the access point from the smartphone:

    WifiConfiguration wc=new WifiConfiguration();
    wc.SSID= host;
    wc.status = WifiConfiguration.Status.ENABLED;
    wc.priority = 40;
    wc.wepKeys[0] = password;
    wc.wepTxKeyIndex = 0;

    int netId=mainWifi.addNetwork(wc);
    try {
        mainWifi.enableNetwork(netId, true);

        startConnectionCheck = true;
        System.out.println("enabled network");
    } catch (Exception e) {

The access point’s security type is WEP. That is because the wifi module can’t implement WPA.

Tests done on Marshmallow.

How to&Answers: