androidpasswordswifimanagerssidwifi

How to create wifihotspot in Oreo programmatically?


Hello Given link question is just showing how to turn on/off wifi hotspot but i want to add create wifi hotspot with SSID and password. I written code for creating wifihotspot(in both NONE and WPA2 PSK) in android and its working fine upto android 7 but in oreo it returning me false value.The summary of my code is-

private WifiManager wifiManager;
private Method method;
private WifiConfiguration config;
config.SSID = ssid;
config.status = WifiConfiguration.Status.ENABLED;
method = wifiManager.getClass().getMethod("setWifiApEnabled",                                           
WifiConfiguration.class, Boolean.TYPE);
boolean status = (Boolean)  method.invoke(wifiManager, config, true);

So my question is how to create wifihotspot in both NONE and WPA2 PSK format for android oreo? Is it possible?


Solution

  • Oreo doesnot support to create hotspot programmatically with no password. It always creates hotspot with unique ssid and key generated randomly.

     WifiManager manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
     WifiManager.LocalOnlyHotspotReservation mReservation;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            assert manager != null;
            manager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
    
                @SuppressLint("SetTextI18n")
                @Override
                public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
                    super.onStarted(reservation);
                    Timber.d("Wifi Hotspot is on now , reservation is : %s", reservation.toString());
                    mReservation = reservation;
                     key = mReservation.getWifiConfiguration().preSharedKey;
                     ussid = mReservation.getWifiConfiguration().SSID;
    
    
                }
    
                @Override
                public void onStopped() {
                    super.onStopped();
                    Timber.d("onStopped: ");
                }
    
                @Override
                public void onFailed(int reason) {
                    super.onFailed(reason);
                    Timber.d("onFailed: ");
                }
            }, new Handler());
        }