devstepbcn / react-native-android-wifi

A react-native module for viewing and connecting to Wifi networks on Android devices.
ISC License
212 stars 121 forks source link

updateNetwork fails for Android 6.0+ whenever the network was created by something other than this module #34

Open charle692 opened 6 years ago

charle692 commented 6 years ago

Starting in Android 6.0 apps can now only change a network configuration if they created it in the first place. https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-network

Meaning that if the network was created by another app or the user themselves, findAndConnect will always return false.

The code in question is in connectTo specifically the loop compares current wifi configuration to the new configuration.

for(WifiConfiguration wifiConfig : mWifiConfigList){
    if(wifiConfig.SSID.equals(conf.SSID)){
        conf.networkId = wifiConfig.networkId;
        updateNetwork = wifi.updateNetwork(conf);
    }
}

wifi.updateNetwork(conf) will always return -1 in the case described above.

To fix it, one can replace updateNetwork = wifi.updateNetwork(conf); with updateNetwork = conf.networkId;.

I can create a pull request if you are interested in this fix. Is there a reason why wifi.updateNetwork is used?

ParthBarot-BoTreeConsulting commented 6 years ago

We have similar issue for Android 5.1, Micromax q392. Let me try this one.

ParthBarot-BoTreeConsulting commented 6 years ago

It does not work, actually i found that as per this link not able to access configured wifi networks. Is it possible that the wifi service is break when we try to call it?

https://stackoverflow.com/questions/22592353/wifimanager-getconfigurednetworks-always-returns-empty-list

So, it never goes inside the for loop, and so no chance to even get the network ID! :(

charle692 commented 6 years ago

@ParthBarot-BoTreeConsulting In my case the wifi list contains the proper wifi networks. So the loop actually executes. I added logging inside the loop to verify that the code was in-fact looping as expected.

The issue that I described above only occurs on Android 6 and higher. So this shouldn't affect Android 5.1. I forgot to add the following link to my first comment. It confirms the issue that I'm describing.

https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-network

ParthBarot-BoTreeConsulting commented 6 years ago

Actually, this issue happens because of " in SSID and passphrase given to the conf. As per the docs and some developer references, initially we found that " is not needed for newer versions of Android after Lollipop. And this is the same code which is done in this library.

So It is like,

    IF build >= LOLLIPOP && PHONE != MicroMax Q392 THEN 
       NO_QUOTES_IN_SSID_AND_PASS
    ELSE
       QUOTES_IN_SSID_AND_PASS
    END

Fixed it by adding quotes in both, works fine. I believe it would be helpful to someone.

Thanks