问题
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