1

I have a decive which reads data from serial and tranmits it over WiFi. (there is no global internet connection). It has an IP and port to which it can forward the read data over socket connection. Now I have an android application which needs to connect to the device (lets call it D) using its ssid and presharedkey so that I can initiate socket connection and read data from device D.

This works fine if there is only one WiFi AP (i.e. device D) but in case mobile is connected to another WiFi then when I try to connect (via code similar to https://stackoverflow.com/a/8818490/504133) mobile connects to Device D WiFi for a moment and then gets connected to previously connected WiFi automatically.

How can I auto Connect to specific WiFi AP in presence of other remembered strong WiFi connections via code ? (I have tried setting priority for WiFiConfiguration but it still does not work)

nits.kk
  • 5,204
  • 4
  • 33
  • 55
  • Have you checked, that you haven't created a wifi configuration to "D" via the system settings already? If the user created a configuration there, apps are NOT allowed to connect to that wifi programmatically. – Denis Loh Jun 21 '18 at 07:32
  • Yes the wifi is configured via code for device D not by user. – nits.kk Jun 21 '18 at 07:33
  • 1
    Does D have an internet connection itself? Android has some limitations, that it disconnects from wifis with no internet connection automatically and reconnects to another known wifi. you can overcome this by setting a static IP for that wifi: https://android.stackexchange.com/questions/130265/stay-connected-to-specific-wifi-which-has-no-internet. Have you checked that already? – Denis Loh Jun 21 '18 at 07:46
  • modifying attributes of Device D is not possible. It does not have internet connection itself – nits.kk Jun 21 '18 at 07:50
  • You don't have to modify D, however your smartphone requires a static IP to "think" that D is a valid connection. So: check what IP range you'll get from D's DHCP, pick one of the IPs of that range, setup a static IP configuration via reflection in your code and connect with that wifi configuration. I do that in multiple projects and it's working on most (or even all of our tested devices). edit: Reflection is required as Android has no public API at the moment to support static IPs from code. I raised a feature request to support that as Android P will strictly limit non-SDK API calls. – Denis Loh Jun 21 '18 at 07:59

0 Answers0