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

问题描述 投票:16回答:3

我知道如何使用以下方法在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上有没有其他方法来开/关热点

android android-wifi hotspot android-8.0-oreo
3个回答
17
投票

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

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

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

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

相关的代码位于:

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


14
投票

最后我得到了解决方案。 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();
    }
}

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

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


3
投票

根据John的建议,我有另一种方法在Android Oreo及更高版本中启用Wifi HotSpot。

public boolean enableTetheringNew(MyTetheringCallback callback) {
    File outputDir = mContext.getCodeCacheDir();
    try {
        proxy = ProxyBuilder.forClass(classOnStartTetheringCallback())
                .dexCache(outputDir).handler(new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                       switch (method.getName()) {
                            case "onTetheringStarted":
                                callback.onTetheringStarted();
                                break;
                            case "onTetheringFailed":
                                callback.onTetheringFailed();
                                break;
                            default:
                                ProxyBuilder.callSuper(proxy, method, args);
                        }
                        return null;
                    }

                }).build();
    } catch (IOException e) {
        e.printStackTrace();
    }
    ConnectivityManager manager = (ConnectivityManager) mContext.getApplicationContext().getSystemService(ConnectivityManager.class);

    Method method = null;
    try {
        method = manager.getClass().getDeclaredMethod("startTethering", int.class, boolean.class, classOnStartTetheringCallback(), Handler.class);
        if (method == null) {
            Log.e(TAG, "startTetheringMethod is null");
        } else {
            method.invoke(manager, TETHERING_WIFI, false, proxy, null);

        }
        return true;
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
    return false;
}

private Class classOnStartTetheringCallback() {
    try {
        return Class.forName("android.net.ConnectivityManager$OnStartTetheringCallback");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}
© www.soinside.com 2019 - 2024. All rights reserved.