Skip to content Skip to sidebar Skip to footer

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 b

Solution 1:

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

WifiManagermanager= (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
 WifiManager.LocalOnlyHotspotReservation mReservation;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        assert manager != null;
        manager.startLocalOnlyHotspot(newWifiManager.LocalOnlyHotspotCallback() {

            @SuppressLint("SetTextI18n")@OverridepublicvoidonStarted(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;


            }

            @OverridepublicvoidonStopped() {
                super.onStopped();
                Timber.d("onStopped: ");
            }

            @OverridepublicvoidonFailed(int reason) {
                super.onFailed(reason);
                Timber.d("onFailed: ");
            }
        }, newHandler());
    }

Solution 2:

thanks, bro finally I found the solution on your answer,

first adding this (enable the modify setting)

Intentintent=newIntent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
        intent.setData(Uri.parse("package:" + this.getPackageName()));
        startActivity(intent);

After pasting your codethen adding permissions in manifast

<uses-permissionandroid:name="android.permission.CHANGE_WIFI_STATE" /><uses-permissionandroid:name="android.permission.INTERNET" /><uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE" /><uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE" />

location mainly needs

<uses-featureandroid:name="android.hardware.wifi" /><uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/><uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>

Solution 3:

The setWifiApEnabled will be deprecated. Looking at the source code, it always returns false :

/**
 * This call will be deprecated and removed in an upcoming release.  It is no longer used to
 * start WiFi Tethering.  Please use {@link ConnectivityManager#startTethering(int, boolean,
 * ConnectivityManager#OnStartTetheringCallback)} if
 * the caller has proper permissions.  Callers can also use the LocalOnlyHotspot feature for a
 * hotspot capable of communicating with co-located devices {@link
 * WifiManager#startLocalOnlyHotspot(LocalOnlyHotspotCallback)}.
 *
 * @param wifiConfig SSID, security and channel details as
 *        part of WifiConfiguration
 * @return {@code false}
 *
 * @hide
 */@SystemApi@RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)publicbooleansetWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
    StringpackageName= mContext.getOpPackageName();
    Log.w(TAG, packageName + " attempted call to setWifiApEnabled: enabled = " + enabled);
    returnfalse;
}

You can try using ConnectivityManager#startTethering(int, boolean, ConnectivityManager#OnStartTetheringCallback) as said in the javadoc. I personnally never tried it.

Post a Comment for "How To Create Wifihotspot In Oreo Programmatically?"