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

我知道如何使用以下方法在 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;
    }

但上面的方法不适用于Android 8.0(Oreo).
当我在Android 8.0中执行上面的方法时,我在logcat中得到以下语句.

com.gck.dummy W/WifiManager: com.gck.dummy attempted call to setWifiApEnabled: enabled = true

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

解决方法

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

在Oreo中,hot-spotting / tethering移动到ConnectionManager,并注释了@SystemApi,因此(名义上)无法访问.

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

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

相关的代码位于:

> MyOreoWifiManager和;
> CallbackMaker

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

相关文章

Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...
Android实现自定义带文字和图片的Button 在Android开发中经常...
Android 关于长按back键退出应用程序的实现最近在做一个Andr...
android自带的时间选择器只能精确到分,但是对于某些应用要求...