为 WiFi 连接禁用三星“自动网络切换”

Posted

技术标签:

【中文标题】为 WiFi 连接禁用三星“自动网络切换”【英文标题】:Disabling Samsung "Auto Network Switch" for WiFi Connection 【发布时间】:2013-12-31 08:17:44 【问题描述】:

在我们的应用程序中,我们处理启动与设备的 WiFi 连接,该设备广播自己的无线接入点(没有互联网连接)以进行直接通信。

它在我们所有的测试设备上都运行良好;但是,我们收到用户的报告,在某些三星设备(Galaxy S4、Galaxy Note 3)上,三星添加了一个名为“自动网络切换”的设置,用于查找“不稳定”网络,并且将自动断开连接并恢复为移动数据。不幸的是,由于我们的设备没有互联网连接,三星将其报告为不稳定的网络并立即断开连接。

我没有可用于测试的这些设备,所以我很好奇是否有其他人知道此问题或知道以编程方式禁用此设置或解决此设置的方法?

我们用于连接的代码是:

/**
 * Attempt to connect to an open wifi network with the given SSID
 * @param ssid the SSID of the unsecured wireless network to connect to
 */
public static void connectToOpenNetwork (String ssid) 
    WifiManager mgr = getManager();
    WifiConfiguration configuration = getOpenWifiConfiguration(ssid);
    mgr.addNetwork(configuration);
    mgr.saveConfiguration();

    int res = findOpenNetworkId(ssid);
    if (res != INVALID_NETWORK_ID) 
        mgr.enableNetwork(res, true);
        mgr.reconnect();
     else 
        Log.e(TAG, "Received request to connect to network " + ssid + " but the network was not found in the configurations.");
    


/**
 * Get a WifiConfiguration object configured for an unsecured wireless network with the
 * given SSID.
 * @param ssid the SSID of the network to configure
 * @return a WifiConfiguration object that can be passed to
 * @link WifiManager#addNetwork(android.net.wifi.WifiConfiguration)
 */
private static WifiConfiguration getOpenWifiConfiguration (String ssid) 
    WifiConfiguration config = new WifiConfiguration();

    config.SSID = "\"" + ssid + "\"";
    config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

    return config;

【问题讨论】:

如果您还没有尝试过三星自己的论坛,您可能会有更好的运气:developer.samsung.com/forum/en 会做的,谢谢你的链接。 :) 【参考方案1】:

编辑:因此,对于那些感兴趣的人,进一步研究,这似乎是基于 4.3 的三星 Touchwiz 版本中添加的一项功能。在内部,该设置被命名为“wifi_watchdog_connectivity_check”。我仍然使用下面的代码来查看是否能够检查 sure 是否启用了该设置,否则我必须假设它已启用。

所以我发现,在您尝试建立连接并且操作系统从网络切换后,Wi-Fi 配置处于“禁用”状态。所以问题发生后,您可以通过检查WifiManager的配置状态来确定问题是否发生。

WifiManager m = (WifiManger) getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> networks = m.getConfiguredNetworks();
String mySsid = "My Network";
mySsid = "\"" + mySsid + "\"";

boolean isDisabled = false;
for (WifiConfiguration config : networks) 
    if (mySsid.equals(config.SSID)) 
        if (config.status = WifiConfiguration.Status.DISABLED) 
            isDisabled = true;
            break;
        
    


//If isDisabled is true, the network was disabled by the OS

然后您可以尝试从系统设置应用程序中解析设置的名称:

/** Gets the resources of another installed application */
private static Resources getExternalResources(Context ctx, String namespace) 
    PackageManager pm = ctx.getPackageManager();
    try 
        return (pm == null) ? null : pm.getResourcesForApplication(namespace);
     catch (PackageManager.NameNotFoundException ex) 
        return null;
    


/** Gets a resource ID from another installed application */
private static int getExternalIdentifier(Context ctx, String namespace, 
        String key, String type) 
    Resources res = getExternalResources(ctx, namespace);
    return (res == null) ? 0 : res.getIdentifier(key, type, namespace);


/** Gets a String resource from another installed application */
public static String getExternalString(Context ctx, String namespace, 
        String key, String defVal) 
    int resId = getExternalIdentifier(ctx, namespace, key, "string");
    if (resId != 0) 
        Resources res = getExternalResources(ctx, namespace);
        return res.getString(resId);
     else 
        return defVal;
    

然后用它来获取字符串:

String autoNetworkSwitch = getExternalString(this, "com.android.settings",
        "wifi_watchdog_connectivity_check", "Unknown");

如果字符串存在,这将返回当前用户语言的本地化字符串。


对于任何对此结果感兴趣的人,事实证明,这个选项实际上是一个普通的 Android 设置,但在这些三星设备上似乎更具侵略性。该设置是在android.provider.Settings.java中找到的隐藏设置:

/**
 * Setting to turn off poor network avoidance on Wi-Fi. Feature is enabled by default and
 * the setting needs to be set to 0 to disable it.
 * @hide
 */
public static final String WIFI_WATCHDOG_POOR_NETWORK_TEST_ENABLED =
       "wifi_watchdog_poor_network_test_enabled";

Settings$Secure 中的 API == 15 || API == 16Settings$Global 中的 API &gt;= 17。这不是第三方应用程序可以启用或禁用的设置;但是,它可以被检测到并发出警告。我的解决方案是这样的:

import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1;
import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;

/**
 * Checks whether the "Avoid poor networks" setting (named "Auto network switch" on 
 * some Samsung devices) is enabled, which can in some instances interfere with Wi-Fi.
 *
 * @return true if the "Avoid poor networks" or "Auto network switch" setting is enabled
 */
public static boolean isPoorNetworkAvoidanceEnabled (Context ctx) 
    final int SETTING_UNKNOWN = -1;
    final int SETTING_ENABLED = 1;
    final String AVOID_POOR = "wifi_watchdog_poor_network_test_enabled";
    final String WATCHDOG_CLASS = "android.net.wifi.WifiWatchdogStateMachine";
    final String DEFAULT_ENABLED = "DEFAULT_POOR_NETWORK_AVOIDANCE_ENABLED";
    final ContentResolver cr = ctx.getContentResolver();

    int result;

    if (SDK_INT >= JELLY_BEAN_MR1) 
        //Setting was moved from Secure to Global as of JB MR1
        result = Settings.Global.getInt(cr, AVOID_POOR, SETTING_UNKNOWN);
     else if (SDK_INT >= ICE_CREAM_SANDWICH_MR1) 
        result = Settings.Secure.getInt(cr, AVOID_POOR, SETTING_UNKNOWN);
     else 
        //Poor network avoidance not introduced until ICS MR1
        //See android.provider.Settings.java
        return false;
    

    //Exit here if the setting value is known
    if (result != SETTING_UNKNOWN) 
        return (result == SETTING_ENABLED);
    

    //Setting does not exist in database, so it has never been changed.
    //It will be initialized to the default value.
    if (SDK_INT >= JELLY_BEAN_MR1) 
        //As of JB MR1, a constant was added to WifiWatchdogStateMachine to determine 
        //the default behavior of the Avoid Poor Networks setting.
        try 
            //In the case of any failures here, take the safe route and assume the 
            //setting is disabled to avoid disrupting the user with false information
            Class wifiWatchdog = Class.forName(WATCHDOG_CLASS);
            Field defValue = wifiWatchdog.getField(DEFAULT_ENABLED);
            if (!defValue.isAccessible()) defValue.setAccessible(true);
            return defValue.getBoolean(null);
         catch (IllegalAccessException ex) 
            return false;
         catch (NoSuchFieldException ex) 
            return false;
         catch (ClassNotFoundException ex) 
            return false;
         catch (IllegalArgumentException ex) 
            return false;
        
     else 
        //Prior to JB MR1, the default for the Avoid Poor Networks setting was
        //to enable it unless explicitly disabled
        return true;
    

您可以通过Intent 将您的用户引导至高级 Wi-Fi 设置:

/**
 *  Ensure that an Activity is available to receive the given Intent
 */
public static boolean activityExists (Context ctx, Intent intent) 
    final PackageManager mgr = ctx.getPackageManager();
    final ResolveInfo info = mgr.resolveActivity(i, PackageManager.MATCH_DEFAULT_ONLY);
    return (info != null);


public static void showAdvancedWifiIfAvailable (Context ctx) 
    final Intent i = new Intent(Settings.ACTION_WIFI_IP_SETTINGS);
    if (activityExists(ctx, i)) 
        ctx.startActivity(i);
    

有趣的琐事:这个Intent 与进入设置 > Wi-Fi > 高级时显示相同的Activity,但会以不同的标题显示(IP 设置,与高级 Wi-Fi)。

【讨论】:

我需要在不打开设置 > Wi-Fi > 高级的情况下从我的本机应用程序中强制取消选中 Wifi 通知,是否可能,请给我一些想法来实现这个? @Puneet 如答案所述,这是无法做到的。 作为附录,wifi_watchdog_on 也可能是相关的。 如果我正确阅读了源代码,则此设置已在 Lollipop 5.0 中删除,但设备已升级并在升级之前更改了设置。此外,它在 5.1 中被完全删除。 android.googlesource.com/platform/frameworks/opt/net/wifi/+/… @amram99 不幸的是,您必须有权访问三星源(我们没有)——该设置仍然存在于 S6 (ee.co.uk/help/phones-and-device/samsung/samsung-galaxy-s6/…) 上,所以他们必须修改它以保持设置到位。

以上是关于为 WiFi 连接禁用三星“自动网络切换”的主要内容,如果未能解决你的问题,请参考以下文章

三星note8连接公司wifi

从多对等连接中禁用 WiFi

手机WiFi总是在连接后又马上断开之间重复是怎么回事

如何判断“移动网络数据”是启用还是禁用(即使通过 WiFi 连接)?

Android:如何以编程方式启用/禁用 Wifi 或 Internet 连接

Android:如何以编程方式启用/禁用 Wifi 或 Internet 连接