你好鉴于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格式的wifihothot?有可能吗?
答案 0 :(得分:2)
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());
}
答案 1 :(得分:1)
谢谢,兄弟,我终于在您的答案中找到了解决方法,
首先添加此内容(启用修改设置)
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + this.getPackageName()));
startActivity(intent);
粘贴代码后 然后添加权限
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
主要需要位置
<uses-feature android:name="android.hardware.wifi" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
答案 2 :(得分:0)
{@ 3}}将被弃用。查看源代码,它总是返回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;
}
您可以尝试使用javadoc中所述的setWifiApEnabled
。我个人从未尝试过。