简体   繁体   English

Android WifiLock WIFI_MODE_SCAN_ONLY不起作用

[英]Android WifiLock WIFI_MODE_SCAN_ONLY not working

I'm trying to block the wifi connections. 我正在尝试阻止wifi连接。 I want my application to turn on the wifi, but does not connect to any network that is already stored on the smartphone. 我希望我的应用程序打开wifi,但不连接到智能手机上已存储的任何网络。 But even after I use the SCAN_ONLY mode, he continues to connect to networks that already "know". 但是,即使在我使用SCAN_ONLY模式后,他仍然继续连接到已经“知道”的网络。

    .....
    wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    .....
    wifiManager.setWifiEnabled(true);
    WifiLock scanOnly = wifiManager.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY, "scanOnly");      
scanOnly.acquire(); 

Already in despair i tried to disconnect after to make sure that the state is WIFI_STATE_ENABLED wifi. 我已经绝望了,尝试确保状态为WIFI_STATE_ENABLED wifi后断开连接。 That the app can not connect for a few seconds, but after some time it connects to a network in the same ... 该应用程序无法连接几秒钟,但过了一段时间后,它连接到同一网络中...

    wifiManager.setWifiEnabled(true);
    ....
    WHEN (WIFI STATE == WIFI_STATE_ENABLED)
    {wifiManager.disconnect();
scanOnly.acquire();} 

Can someone help me? 有人能帮我吗? Tks ks

WifiLock not for this. WifiLock不适用于此。 Only for "Allows an application to keep the Wi-Fi radio awake". 仅适用于“允许应用程序保持Wi-Fi无线电处于唤醒状态”。 This does not forbid anyone to connect to wifi. 这并不禁止任何人连接到wifi。 Only says "I want to be able to do at least this in sleep mode". 只说“我希望至少能够在睡眠模式下做到这一点”。 You have to do what you said Peanut above: get the list of all the stored networks and then disable wifi in BroadcastReceiver. 您必须执行上面所说的Peanut:获取所有存储网络的列表,然后在BroadcastReceiver中禁用wifi。

@Override
public void onReceive(Context c, Intent intent) {
    saveWifiList(mainWifi.getScanResults());

    unregisterReceiver(this);
    mainWifi.setWifiEnabled(false);

    showWifiList();     
}

One way would be to get the list of all the stored networks and then disable them. 一种方法是获取所有已存储网络的列表,然后禁用它们。 You might want to store their state somewhere so you can restore the initial state after you're done. 您可能希望将其状态存储在某个位置,以便在完成后可以恢复其初始状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM