Android-WiFi关联禁用隐藏WiFi配置

Android-WiFi关联禁用隐藏WiFi配置,android,networking,hidden,ssid,Android,Networking,Hidden,Ssid,在我的应用程序中,我有一个网络选择屏幕,显示所有可见的网络,包括配置的隐藏SSID 但是,当用户选择一个不是隐藏SSID的可见网络,并使用以下代码与之关联时 public boolean associate(ScanResultWrapper scanResult){ WifiConfiguration wc = getWifiConfiguration(scanResult.scanResult); int id = -1; if (wc == null )

在我的应用程序中,我有一个网络选择屏幕,显示所有可见的网络,包括配置的隐藏SSID

但是,当用户选择一个不是隐藏SSID的可见网络,并使用以下代码与之关联时

    public boolean associate(ScanResultWrapper scanResult){

    WifiConfiguration wc = getWifiConfiguration(scanResult.scanResult);
    int id = -1;
    if (wc == null ) {
        wc = new WifiConfiguration();
        wc.SSID = "\"" + scanResult.SSID + "\"";
        wc.BSSID = scanResult.scanResult.BSSID;
        wc.status = WifiConfiguration.Status.ENABLED;

        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);

        wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

        id = mWifiManager.addNetwork(wc);

        if (!mWifiManager.saveConfiguration()){
            return false;
        }

    } else{
        id = wc.networkId;
    }

    boolean result;

    try {
        result = mWifiManager.enableNetwork(id, true);

        return result;
    } catch (Throwable t) {
        t.printStackTrace();
        return false;
    }

}
因此,与此处所示网络关联的方法为布尔值)

禁用所有其他配置。这对于非隐藏SSID是可以的,但这意味着我的隐藏SSID配置被禁用,并且不再包含在扫描结果中。这意味着,如果用户在隐藏网络上并加入另一个网络,则除非启动设备Wifi设置,否则他们无法返回到隐藏网络

我发现上述以编程方式更改Wifi网络的方法必须与设备Wifi设置使用的方法不同。如果您以编程方式关联,然后转到Wifi设置屏幕,您将看到所有其他配置的网络都已设置为“禁用”。但是,如果您从设备Wifi设置屏幕关联到网络,则所有其他Wifi配置将保持为“已保存”

是否有人有其他方法可以通过编程方式关联到保留隐藏SSID配置而不禁用它们的网络


谢谢,这真的很痛。

好的,我现在好像已经治好了

这是我修改后的关联逻辑——我实际上已经将其拆分为不同的方法,但为了便于阅读,我将在这里将其全部发布在一个方法中

简单地说,在执行关联之前,我会找到所有可见的隐藏网络,对这些网络进行精确的引用,然后关联到用户选择的网络,然后在不禁用任何其他网络的情况下重新启用隐藏的SSID配置

public boolean associate(ScanResultWrapper scanResult){

    WifiConfiguration wc = getWifiConfiguration(scanResult._scanResult);
    int id = -1;
    if (wc == null ) {
        wc = new WifiConfiguration();
        wc.SSID = "\"" + scanResult._Ssid + "\"";
        wc.BSSID = scanResult._scanResult.BSSID;
        wc.status = WifiConfiguration.Status.ENABLED;

        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);

        wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

        id = mWifiManager.addNetwork(wc);

        if (!mWifiManager.saveConfiguration()){
            return false;
        }

    } else{
        id = wc.networkId;
    }

    boolean result;

           //Get reference to all hidden visible networks
    ArrayList<ScanResultWrapper> hiddenScanResults = new ArrayList<ScanResultWrapper>();
    for (ScanResultWrapper wrapper : mScanResults){

        if (wrapper._isHidden){
            hiddenScanResults.add(wrapper);
        }

    }

    try {

            //Enable the user network - disabling all others
        result = mWifiManager.enableNetwork(id, true);


              //re-enable all hidden networks, leaving all other networks enabled
        for (ScanResultWrapper wrapper : hiddenScanResults){

            WifiConfiguration wcHidden = getWifiConfiguration(wrapper._scanResult);

                            //the false is the important part here
            mWifiManager.enableNetwork(wcHidden.networkId, false);

        }

        return result;
    } catch (Throwable t) {
        t.printStackTrace();
        return false;
    }

}
公共布尔关联(ScanResultRapper scanResult){
WifiConfiguration wc=getWifiConfiguration(scanResult.\u scanResult);
int id=-1;
如果(wc==null){
wc=新的接线配置();
wc.SSID=“\”+扫描结果。\ u SSID+“\”;
wc.BSSID=scanResult.\u scanResult.BSSID;
wc.status=WifiConfiguration.status.ENABLED;
AlloweGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
AlloweGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
AlloweGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedKeyManagement.set(WifiConfiguration.keymanagement.NONE);
wc.allowedPairwisecipher.set(WifiConfiguration.Pairwiseciper.TKIP);
wc.allowedPairwisecipher.set(WifiConfiguration.Pairwiseciper.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
id=mWifiManager.addNetwork(wc);
如果(!mWifiManager.saveConfiguration()){
返回false;
}
}否则{
id=wc.networkId;
}
布尔结果;
//获取对所有隐藏可见网络的引用
ArrayList hiddenScanResults=新建ArrayList();
用于(ScanResultRapper包装器:mScanResults){
如果(包装器){
hiddenScanResults.add(包装器);
}
}
试一试{
//启用用户网络-禁用所有其他网络
结果=mWifiManager.enableNetwork(id,true);
//重新启用所有隐藏的网络,保留所有其他网络处于启用状态
for(ScanResultRapper包装器:hiddenScanResults){
WifiConfiguration wcHidden=getWifiConfiguration(wrapper.\u scanResult);
//错误是这里的重要部分
mWifiManager.enableNetwork(wcHidden.networkId,false);
}
返回结果;
}捕获(可丢弃的t){
t、 printStackTrace();
返回false;
}
}
ScanResultsWrapper类是一个简单的类,它允许我保留一些额外的信息,并且易于访问。下面是一个构造函数,它解释了_ishiden变量是由什么设置的

    public ScanResultWrapper(ScanResult scanResult, Context c) {
    this._Ssid = scanResult.SSID;
    this._scanResult = scanResult;
    this.context = c;
    WifiManager mWifiManager = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);

    List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();

    WifiConfiguration config = getWifiConfiguration(scanResult, configs) ;

    this._isKnown = config != null;
    if (config!=null){

        _isHidden = config.hiddenSSID;

    }else{
        _isHidden = false;
    }

    this._isAssociated = _Ssid.equals(SSIDUtils.checkSSIDForEnclosingQuotes(mWifiManager.getConnectionInfo().getSSID()));

    if (_isAssociated){
        this._isAssociated = mWifiManager.getConnectionInfo().getNetworkId()!= -1 ? true : false;
    }

    this._isSecure = scanResult.capabilities.contains("WEP") || scanResult.capabilities.contains("PSK") || scanResult.capabilities.contains("EAP");

}

/**
 * returns the wifi configuration for a given ScanResult.  It compares the ssid AND the bssid
 * @param ssid
 * @param configs
 * @return
 */
public WifiConfiguration getWifiConfiguration(ScanResult scanResult, List<WifiConfiguration> configs) { // ScanResult result) {


    try {
        String ssid = scanResult.SSID;
        ssid = ssid.replaceAll("\"", "");
        ssid = ssid.trim();


        String bssid = scanResult.BSSID;
        if ( bssid != null ) {
            bssid = bssid.replaceAll("\"", "");
            bssid = bssid.trim();
        }

        if (configs== null){
            return null;
        }
        for ( WifiConfiguration config : configs ) {


            String candidate = config.SSID;
            if ( BSGStringUtils.isNullOrEmpty(candidate) ) {
                continue;
            }
            candidate = candidate.replaceAll("\"", "");
            candidate = candidate.trim();
            if ( candidate.equals(ssid) ) {

                String candidateBSSID = config.BSSID;
                if ( candidateBSSID == null && bssid == null ) {

                    return config;
                }else if (candidateBSSID == null){

                    return config;
                } else if ( candidateBSSID != null && bssid != null && candidateBSSID.equals(bssid) ) {
                    return config;
                } else {

                }

            } else {

            }
        }

        return null;
    } finally {
    }
}
公共扫描结果说话者(扫描结果扫描结果,上下文c){
这是._Ssid=scanResult.Ssid;
这是。_scanResult=scanResult;
this.context=c;
WifiManager mWifiManager=(WifiManager)c.getSystemService(Context.WIFI_服务);
List configs=mWifiManager.getConfiguredNetworks();
WifiConfiguration config=getWifiConfiguration(扫描结果,配置);
这是。_isKnown=config!=null;
如果(配置!=null){
_isHidden=config.hiddenSSID;
}否则{
_isHidden=假;
}
this._isAssociated=_Ssid.equals(SSIDUtils.checkSSIDForEnclosingQuotes(mWifiManager.getConnectionInfo().getSSID());
如果(_是关联的){
此._isAssociated=mWifiManager.getConnectionInfo().getNetworkId()!=-1?true:false;
}
此.u isSecure=scanResult.capabilities.contains(“WEP”)| | scanResult.capabilities.contains(“PSK”)| | scanResult.capabilities.contains(“EAP”);
}
/**
*返回给定扫描结果的wifi配置。它比较ssid和bssid
*@param-ssid
*@param-configs
*@返回
*/
公共无线配置getWifiConfiguration(ScanResult ScanResult,列表配置){//ScanResult结果){
试一试{
字符串ssid=scanResult.ssid;
ssid=ssid.replaceAll(“\”,“);
ssid=ssid.trim();
字符串bssid=scanResult.bssid;
如果(bssid!=null){
bssid=bssid.replaceAll(“\”,“);
bssid=bssid.trim();
}
if(configs==null){
返回null;
}
用于(WifiConfiguration config:configs){
字符串候选者=config.SSID;
if(BSGStringUtils.isNullOrEmpty(候选)){
继续;
}
candidate=candidate.replaceAll(“\”,“);
candidate=candidate.trim();
if(候选者等于(ssid)){
字符串candidateBSID=config.BSSID;
if(candidateBSID==null&&bssid==null){
    public ScanResultWrapper(ScanResult scanResult, Context c) {
    this._Ssid = scanResult.SSID;
    this._scanResult = scanResult;
    this.context = c;
    WifiManager mWifiManager = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);

    List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();

    WifiConfiguration config = getWifiConfiguration(scanResult, configs) ;

    this._isKnown = config != null;
    if (config!=null){

        _isHidden = config.hiddenSSID;

    }else{
        _isHidden = false;
    }

    this._isAssociated = _Ssid.equals(SSIDUtils.checkSSIDForEnclosingQuotes(mWifiManager.getConnectionInfo().getSSID()));

    if (_isAssociated){
        this._isAssociated = mWifiManager.getConnectionInfo().getNetworkId()!= -1 ? true : false;
    }

    this._isSecure = scanResult.capabilities.contains("WEP") || scanResult.capabilities.contains("PSK") || scanResult.capabilities.contains("EAP");

}

/**
 * returns the wifi configuration for a given ScanResult.  It compares the ssid AND the bssid
 * @param ssid
 * @param configs
 * @return
 */
public WifiConfiguration getWifiConfiguration(ScanResult scanResult, List<WifiConfiguration> configs) { // ScanResult result) {


    try {
        String ssid = scanResult.SSID;
        ssid = ssid.replaceAll("\"", "");
        ssid = ssid.trim();


        String bssid = scanResult.BSSID;
        if ( bssid != null ) {
            bssid = bssid.replaceAll("\"", "");
            bssid = bssid.trim();
        }

        if (configs== null){
            return null;
        }
        for ( WifiConfiguration config : configs ) {


            String candidate = config.SSID;
            if ( BSGStringUtils.isNullOrEmpty(candidate) ) {
                continue;
            }
            candidate = candidate.replaceAll("\"", "");
            candidate = candidate.trim();
            if ( candidate.equals(ssid) ) {

                String candidateBSSID = config.BSSID;
                if ( candidateBSSID == null && bssid == null ) {

                    return config;
                }else if (candidateBSSID == null){

                    return config;
                } else if ( candidateBSSID != null && bssid != null && candidateBSSID.equals(bssid) ) {
                    return config;
                } else {

                }

            } else {

            }
        }

        return null;
    } finally {
    }
}