您好,给定的link问题只是显示如何打开/关闭wifi热点,但我想添加具有SSID和密码的create wifi热点。
我编写了用于在android中创建wifihotspot(在NONE和WPA2 PSK中)的代码,并且在android 7之前都可以正常工作,但是在oreo中它返回了我错误的值。我代码的摘要是-
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);
所以我的问题是如何为Android Oreo创建NONE和WPA2 PSK格式的wifihotspot?可能吗?
最佳答案
Oreo不支持以编程方式创建没有密码的热点。它始终使用随机生成的唯一ssid和密钥创建热点。
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());
}
关于android - 如何在Oreo中以编程方式创建wifi热点?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50169004/