如何在Android 8.0(Oreo)中以编程方式打开/关闭wifi热点

Cha*_*nth 17 android android-wifi hotspot android-8.0-oreo

我知道如何使用以下方法在android中使用反射打开/关闭wifi热点.

private static boolean changeWifiHotspotState(Context context,boolean enable) {
        try {
            WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            Method method = manager.getClass().getDeclaredMethod("setWifiApEnabled", WifiConfiguration.class,
                    Boolean.TYPE);
            method.setAccessible(true);
            WifiConfiguration configuration = enable ? getWifiApConfiguration(manager) : null;
            boolean isSuccess = (Boolean) method.invoke(manager, configuration, enable);
            return isSuccess;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
Run Code Online (Sandbox Code Playgroud)

但上面的方法不适用于Android 8.0(Oreo).

当我在Android 8.0中执行上面的方法时,我在logcat中得到以下语句.

com.gck.dummy W/WifiManager: com.gck.dummy attempted call to setWifiApEnabled: enabled = true
Run Code Online (Sandbox Code Playgroud)

在Android 8.0上有没有其他方法来开/关热点

Jon*_*Jon 17

我认为这LocalOnlyHotspot条路是可行的,但正如@ edsappfactory.com在评论中所说 - 它只提供封闭的网络,没有互联网接入.

在奥利奥热点/束缚移动ConnectionManager,及其注释@SystemApi,所以(名义上)无法进入.

作为我正在做的其他事情的一部分,我制作了一个应用程序并将其放在github上.它使用反射来获取函数,并使用DexMaker生成一个子类ConnectionManager.OnStartTetheringCallback(也是不可访问的).

认为一切正常 - 边缘粗糙,所以请随意做得更好!

相关的代码位于:

我失去了耐心试图让我的DexMaker生成的回调触发,MyOnStartTetheringCallback所以所有代码都处于混乱状态并被注释掉.


Cha*_*nth 16

最后我得到了解决方案.Android 8.0,他们提供公共API来打开/关闭热点.WifiManager

以下是打开热点的代码

private WifiManager.LocalOnlyHotspotReservation mReservation;

@RequiresApi(api = Build.VERSION_CODES.O)
private void turnOnHotspot() {
    WifiManager manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

    manager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {

        @Override
        public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
            super.onStarted(reservation);
            Log.d(TAG, "Wifi Hotspot is on now");
            mReservation = reservation;
        }

        @Override
        public void onStopped() {
            super.onStopped();
            Log.d(TAG, "onStopped: ");
        }

        @Override
        public void onFailed(int reason) {
            super.onFailed(reason);
            Log.d(TAG, "onFailed: ");
        }
    }, new Handler());
}

private void turnOffHotspot() {
    if (mReservation != null) {
        mReservation.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

onStarted(WifiManager.LocalOnlyHotspotReservation reservation)如果打开热点,将WifiManager.LocalOnlyHotspotReservation调用close()方法.使用引用调用方法关闭热点.

注意: 要打开热点,Location(GPS)应在设备中启用.否则,它会抛出SecurityException

  • 如何使用此方法设置SSID名称?现在我得到类似"AndroidShare_xxxx"的东西 (7认同)
  • 当您想要创建一个封闭的网络时,这很好.您的热点将无法访问互联网.参见[https://developer.android.com/reference/android/net/wifi/WifiManager.html#startLocalOnlyHotspot(android.net.wifi.WifiManager.LocalOnlyHotspotCallback,android.os.Handler)] (3认同)