【问题标题】:Android app to dynamically turn on/off a wifi hotspotAndroid应用程序动态打开/关闭wifi热点
【发布时间】:2019-12-01 09:27:16
【问题描述】:

我想在我的 Android 应用项目中动态控制 wifi 热点。我已经厌倦了 Reflection(在 Android Oreo 和更高版本中不起作用)、startLocalOnyNetwork(但我想要特定的 SSIDPASSWORD ,这是无法配置的)。

然后我root了我的手机,如果设备已经root了可以吗?

期望一个 api 打开/关闭具有特定 SSIDPASSWORD 的 wifi 热点或使用前一个。

任何可能性或解决方法?

提前致谢。

【问题讨论】:

  • 我认为它不可能,因为它应该留给用户。您可以通过将用户引导至设置屏幕来引导用户打开 wifi。注意:可能有解决方法,我不知道有任何解决方法

标签: android android-wifi personal-hotspot


【解决方案1】:

要开启 Wifi 热点,需要一些权限

<uses-permission android:name="android.permission.WRITE_SETTINGS"
    tools:ignore="ProtectedPermissions" />

并且权限应该由用户动态授予

在应用程序高级设置 -> 修改系统设置

/**
 * This enables tethering using the ssid/password defined in Settings App>Hotspot & tethering
 * Does not require app to have system/privileged access
 * Credit: Vishal Sharma - https://stackoverflow.com/a/52219887
 */
public boolean startTethering() {
    File outputDir = mContext.getCodeCacheDir();
    Object proxy;
    try {
        proxy = ProxyBuilder.forClass(OnStartTetheringCallbackClass())
                .dexCache(outputDir).handler(new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        return null;
                    }

                }).build();
    } catch (Exception e) {
        Log.e(TAG, "Error in enableTethering ProxyBuilder");
        e.printStackTrace();
        return false;
    }

    Method method = null;
    try {
        method = mConnectivityManager.getClass().getDeclaredMethod("startTethering", int.class, boolean.class, OnStartTetheringCallbackClass(), Handler.class);
        if (method == null) {
            Log.e(TAG, "startTetheringMethod is null");
        } else {
            method.invoke(mConnectivityManager, ConnectivityManager.TYPE_MOBILE, false, proxy, null);
            Log.d(TAG, "startTethering invoked");
        }
        return true;
    } catch (Exception e) {
        Log.e(TAG, "Error in enableTethering");
        e.printStackTrace();
    }
    return false;
}

public void stopTethering() {
    try {
        Method method = mConnectivityManager.getClass().getDeclaredMethod("stopTethering", int.class);
        if (method == null) {
            Log.e(TAG, "stopTetheringMethod is null");
        } else {
            method.invoke(mConnectivityManager, ConnectivityManager.TYPE_MOBILE);
            Log.d(TAG, "stopTethering invoked");
        }
    } catch (Exception e) {
        Log.e(TAG, "stopTethering error: " + e.toString());
        e.printStackTrace();
    }
}

使用上述方法通过设置中定义的 SSID 和密码打开/关闭 Wifi 热点。

private int AP_STATE_DISABLED = 11;
private int AP_STATE_ENABLING = 12;
private int AP_STATE_ENABLED = 13;
private int AP_STATE_ERROR = 14;

/**
 * @return status hot spot enabled or not
 */
public boolean isHotSpotEnabled(Context context) {
    Method method = null;
    int actualState = 0;
    try {
        WifiManager mWifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        method = mWifiManager.getClass().getDeclaredMethod("getWifiApState");
        method.setAccessible(true);
        actualState = (Integer) method.invoke(mWifiManager, (Object[]) null);
        if (actualState == AP_STATE_ENABLING ||actualState ==  AP_STATE_ENABLED) {
            return true;
        }
    } catch (IllegalArgumentException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

以上方法可用于获取热点当前状态

【讨论】:

    猜你喜欢
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多