9

My app needs to communicate with a Sony Wifi camera. The user specifies the camera Wifi SSID and password, the app connects, takes a picture and retrieves it to the android device. When the app is done taking pictures, I want it to restore the Wifi connection (if any) that existed before the app was started. E.g. the user may have been connected to to a coffee shop hotspot when she starts the wifi camera app, and so the the app should automatically reconnect to the hotspot when it's done.

To restore the pre-existing connection, the app needs to know the SSID - and also the password, I think. It's easy for the app to determine the pre-existing SSID - just call WifiManager.getConnectionInfo().getSSID() before switching to the camera Wifi SSID. But how to get the pre-existing password? I don't want to require the user to re-enter that password. Is this functionality possible?

Thanks, Tom

user604713
  • 437
  • 1
  • 6
  • 17
  • 1
    You can't get the password - that would be a security hole. –  Dec 30 '13 at 21:37
  • Thanks Mike W. Is there an alternative approach that restores the original Wifi connection without the user needing to re-enter the password? – user604713 Dec 30 '13 at 21:44
  • 1
    Let the user enter the password once (in the beginning): [How to connect to a specific Wifi network](http://stackoverflow.com/questions/8818290/how-to-connect-to-a-specific-wifi-network-in-android-programmatically) – Tobias Dec 30 '13 at 21:48
  • Anyone know of a way to "cache" the current connection and restore it later? In principle I could see how this *might* be done without revealing the password to the app level, since the Android system "remembers" Wifi passwords - but I just don't know of any provided APIs to to do it. – user604713 Dec 30 '13 at 22:01
  • I want same kind of app can you provide me some useful links so that I can get started – Android Apr 04 '16 at 06:32

1 Answers1

6

You can use the WifiInfo.getNetworkId API to get current network id and save it for future use. When you want to connect back to original network use WifiManager.enableNetwork API and pass the network ID you cached before.

ThomasW
  • 16,981
  • 4
  • 79
  • 106
vishalm
  • 477
  • 5
  • 12