How to create wifihotspot in Oreo programmatically?

二次信任 提交于 2019-12-31 04:14:09

问题


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?


回答1:


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());
    }



回答2:


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)
public boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
    String packageName = mContext.getOpPackageName();
    Log.w(TAG, packageName + " attempted call to setWifiApEnabled: enabled = " + enabled);
    return false;
}

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



来源:https://stackoverflow.com/questions/50169004/how-to-create-wifihotspot-in-oreo-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!