Wednesday, 16 June 2021

Connect to WiFi network without DHCP and private IP address in 169.254

I have a device that broadcasts a WiFi AP which doesn't have a DHCP server. Computers and iPhones can connect to it and end up with an IP in 169.254.x.y. Then, mDNS such as device-name.local are resolved and allow to use the web interface of the device to configure it (notably, send it the SSID/PSK of the target WiFi network to connect to).

For some reason, my Android devices (tested on Moto G6 Android 9 and Pixel 3 XL Android 11) fail to reach the IP state. They can associate with the AP but don't get a 169.254.x.y. IP. They usually keep mobile data active, or try to switch to another working AP.

When I disable mobile data and remove all known AP, they still won't get an IP after association.

When I manually set an IP in the same subnet (obtained by a computer that managed to connect to the IP and land in 169.254.x.y.), then it works! Name resolution doesn't work, but the Android device is able to reach the other device by using its IP address.

Now this is very bad, especially because I didn't find any stable API that didn't use reflection to set a static IP address when establishing a connection to a new AP.

Is there a way to connect to the AP and be able to reach the device using whatever.local without any interaction from the user?

My code basically does these steps, with a 15-time retry on each step if it fails (and start over from #1):

  1. connect to SSID with

     WiFiForIoTPlugin.connect(ssid,
         password: 'XXXX', security: NetworkSecurity.WPA, joinOnce: true, withInternet: true))
    
  2. get SSID with WiFiForIoTPlugin.getSSID(), check it matches the target

  3. get RSSI and check it's not null or == -127

  4. try to resolve myhostname.local (which works on Linux, MacOS, iOS but not on Android)

  5. try to communicate with the device now that its host is resolved and we have IP connectivity



from Connect to WiFi network without DHCP and private IP address in 169.254

No comments:

Post a Comment