Friday, February 9, 2018

Android programmatically connect to a wifi network without Internet

Android doesn't like to connect to wifi without Internet. But what about when you want to use a local wifi connection to quickly move large volumes of data between nearby devices? The secret is that you need to obtain the Network object that corresponds to the wifi, and then use it to open URL connections, create a SocketFactory, or even bind the application. How to find out which object that is? The big (undocumented) secret is that the NetworkInfo's getExtraInfo method will return a string that matches the SSID. With thanks to this blogpost by Nishkarsh Sharma.
With

1 comment:

  1. I'm using Android SDK 28 (Pie) and NetworkInfo.getExtraInfo always returns null. Looks like Android contributors reverted this feature. Seems like they are going out of their way to NOT support programatically changing network to a Wifi Access Point to configure an IoT device.

    ReplyDelete