如何以编程方式在 Oreo 中创建 wifihotspot?

How to create wifihotspot in Oreo programmatically?

你好鉴于问题只是展示如何打开on/offwifi热点,但我想添加使用SSID和密码创建wifi热点。 我编写了用于在 android 中创建 wifihotspot(在 NONE 和 WPA2 PSK 中)的代码,它在 android 7 之前工作正常,但在 oreo 中它返回错误的 value.The 我的总结代码是-

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?可能吗?

setWifiApEnabled 将被弃用。查看源代码,总是 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;
}

您可以按照 javadoc 中的说明尝试使用 ConnectivityManager#startTethering(int, boolean, ConnectivityManager#OnStartTetheringCallback)。我个人从未尝试过。

专业版不支持无密码编程创建热点。它总是使用随机生成的唯一 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());
    }

谢谢,兄弟,我终于根据你的回答找到了解决方案,

首先添加这个(启用修改设置)

 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"/>