Change local-only Wi-Fi hotspot's SSID and password in Android Oreo 8.x
Asked Answered
B

1

32

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.

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 Wi-Fi hotspot with an 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 credentials hence the need of setting a hotspot programmatically.

Becalm answered 7/12, 2017 at 17:36 Comment(5)
I don't have an answer for you, but have you considered using Bluetooth to either perform the authentication or to publish the network SSID?Sciatica
Note that according to the documentation of startLocalOnlyHotspot, the hotspot may be shared between several apps. This indicates that modifying the SSID is unlikely to be officially supported.Sciatica
Hey, did you ever find a solution to this problem?Thorazine
@Thorazine nope :(Becalm
@Markinson did you found the solution yet ?Lingenfelter
C
17

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 callback onStarted(LocalOnlyHotspotReservation reservation). You can get the WifiConfiguration by calling reservation.getWifiConfiguration(). You will get all the information you need from the WifiConfiguration object. So you can read the Pre-Shared Key and the access point name. But I don't think you can change them


FYI, 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

  /**
   * Generate a temporary WPA2 based configuration for use by the local only hotspot.
   * This config is not persisted and will not be stored by the WifiApConfigStore.
   */
   public static WifiConfiguration generateLocalOnlyHotspotConfig(Context context) {
       WifiConfiguration config = new WifiConfiguration();
       config.SSID = context.getResources().getString(
              R.string.wifi_localhotspot_configure_ssid_default) + "_"
                      + getRandomIntForDefaultSsid();
       config.allowedKeyManagement.set(KeyMgmt.WPA2_PSK);
       config.networkId = WifiConfiguration.LOCAL_ONLY_NETWORK_ID;
       String randomUUID = UUID.randomUUID().toString();
       // first 12 chars from xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
       config.preSharedKey = randomUUID.substring(0, 8) + randomUUID.substring(9, 13);
       return config;
   }
Curch answered 13/12, 2017 at 20:45 Comment(7)
Thanks for your answe......context.getResources().getString( R.string.wifi_localhotspot_configure_ssid_default) + "_" + getRandomIntForDefaultSsid(); can give us the format please..Hypercorrection
please help me sirHypercorrection
It is not possible to change the SSIDCurch
is there any workaround solution for that....I want to create hotspot name with some specific name sir..Hypercorrection
@Gowthamam, As far as I know, there isnt a way to set a specific name.Curch
Still same not effect on name !Delaryd
For our use case we also need to set ssid and password programitically. Any solution appreciated.Mammet

© 2022 - 2024 — McMap. All rights reserved.