In my Android application I'm using the following code snippet:
@RequiresApi(api = Build.VERSION_CODES.O)
private void turnOnHotspot(){
WifiManager manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
manager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback(){
@Override
public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
super.onStarted(reservation);
Log.d(TAG, "Wifi Hotspot is on now");
}
@Override
public void onStopped() {
super.onStopped();
Log.d(TAG, "onStopped: ");
}
@Override
public void onFailed(int reason) {
super.onFailed(reason);
Log.d(TAG, "onFailed: ");
}
},new Handler());
}
This piece of code creates a hotspot named something like "AndroidShare_1234". For a project of mine I need to be able to set a password and SSID to this hotspot, however I can't find a way to do this. I would like to create a hotspot with an SSID like MyHotspot
and a custom password.
Note that the setWifiApEnabled
is not supported anymore in Android O, this is how it's done in older versions of Android. However, I still need to programmatically make a wifi hotspot with a SSID and a password. I can't figure out how to do this. Thanks in advance!
For who cares...:
For a school project I made a locker that unlocks whenever it can connect to a wireless network with certain cridentials, hence the need of setting a hotspot programmatically.
I have only a partial solution to the problem. Hopefully, it will be sufficient for the application you are designing.
The SSID and the password are hard-coded by the android system when you start the Hotspot. By looking over at the AOSP code, I see that the same hotspot can be shared by multiple applications. The configuration for this hotspot(class name is
WifiConfiguration
) is also shared with all the requesting applications. This configuration is passed back to the application in the callbackonStarted(LocalOnlyHotspotReservation reservation)
. You can get theWifiConfiguration
by callingreservation.getWifiConfiguration()
. You will get all the information you need from theWifiConfiguration
object. So you can read the Pre-Shared Key and the access point name. But I don't think you can change themFYI, The relevant code that sets up the wifi configuration (including the hard-coded SSID and WPA2-PSK key) is done by the following piece of code