15

this has many similar questions (google for: "no internet access detected. won't automatically reconnect." or: android force wifi connection programmatically).

i thought i had a answer here, but it stopped working after installing 6.0.1 updates (i have may 1 security patches).

seems like this is a behaviour change.

i have some 2013 nexus 7's with 6.0.1 that run a kiosk type app and want to connect programmatically to a specific wireless network that has no internet connection. each tablet has a unique static ip address of the form: 192.168.0.xx. i use the normal java socket constructors and check to see if the interface is up using: NetworkInterface.getNetworkInterfaces().

a manual connection has been made. sometimes there is a dialog that asks whether or not you want to always connect. i always check yes.

but the wifi says: "no internet access detected. won't automatically reconnect" after the router cycles power.

doing a disconnect, enable, reconnect does not work. at best it gets: ip6-localhost/::1.

has anyone had any luck using a request object, or bindProcessToNetwork?

edit: related.

edit: the problem seems to be with: CAPTIVE_PORTAL_DETECTION_ENABLED - this string seems to be defined in the source:

public static final String
        CAPTIVE_PORTAL_DETECTION_ENABLED = "captive_portal_detection_enabled";
    ...
    MOVED_TO_GLOBAL.add(Settings.Global.CAPTIVE_PORTAL_DETECTION_ENABLED);

but throws" android.provider.Settings$SettingNotFoundException: captive_portal_detection_enabled when used explicitly and is not visible to android studio.

also, doing a settings list global does not contain the constant.

edit doing a adb shell settings put global captive_portal_detection_enabled 0 does seem to work, but this can not be done in the field when the router cycles power. this value seems to persist when the tablet cycles power. and now this value shows up in a settings list global. also, using the raw string: Settings.Global.getInt(getContentResolver(),"captive_portal_detection_enabled"); now returns 0.

edit: looks like setting it requires: android.permission.WRITE_SECURE_SETTINGS, but of course this fails when put into the manifest since we are not a system app.

edit: trying to exec the shell command throws: java.lang.SecurityException, so it looks like you need to issue the command from adb :(

thanks

Community
  • 1
  • 1
Ray Tayek
  • 9,841
  • 8
  • 50
  • 90

2 Answers2

15

Could you try and set the global setting captive_portal_detection_enabled to 0 (false).

What's actually happening is that by default, everytime you connect to a wifi, the FW will test against a server (typically google) to see if it's a captive wifi (needs login). So if your wifi is not connected to google, this check will fail. After that, the device knows that wifi has no internet connection and simply will not autoconnect to it.

Setting this setting to 0, will avoid this check.

Programatically Settings.Global.putInt(getContentResolver(), Settings.Global.CAPTIVE_PORTAL_DETECTION_ENABLED, 0);

You can do it through adb for testing purposes:

adb shell settings put global captive_portal_detection_enabled 0

And retrieve it's value like this:

adb shell settings list global | grep "captive"

IMHO this is not very nice thing to do, since you are changing a setting for the user and many FWs don't provide even an advanced setting to enable/disable this by the user itself. (Google doesn't). But maybe it suits your needs.

Hope it helps!

Armand
  • 23,463
  • 20
  • 90
  • 119
Olaia
  • 2,172
  • 25
  • 27
  • yes, i have heard of that, but anddroid studio says: can not resolve symbol: Settings.Global.CAPTIVE_PORTAL_DETECTION_ENABLED – Ray Tayek May 19 '16 at 18:02
  • and adb shell settings list global does not contain captive on 6.0.1 – Ray Tayek May 19 '16 at 22:43
  • 1
    You might need root access for that, also CAPTIVE_PORTAL_DETECTION_ENABLED is hidden, so there is no easy way to tweak it, try using the string "captive_portal_detection_enabled" directly, instead of the constant that's not visible. Also, in 6.0.1, for sure captive is there. It's been there before 6.0.1 also. check this link: http://android.stackexchange.com/questions/100657/how-to-disable-captive-portal-detection-how-to-remove-exclamation-mark-on-wi-fi – Olaia May 20 '16 at 10:20
  • maybe gone: android.provider.Settings$SettingNotFoundException: captive_portal_detection_enabled – Ray Tayek May 20 '16 at 14:19
  • 1
    it's in there : public static final String CAPTIVE_PORTAL_DETECTION_ENABLED = "captive_portal_detection_enabled";. and MOVED_TO_GLOBAL.add(Settings.Global.CAPTIVE_PORTAL_DETECTION_ENABLED); – Ray Tayek May 20 '16 at 14:30
  • Yes it's there, I know for sure, it's just hidden in the API, that's why I proposed you use the string directly. That's why I suggested you may need root access for it to persist. What do you mean "router cycles power"? You could make receiver for BOOT_COMPLETE and set the setting right away. Please consider accepting the answer since it's actually helping you needs? – Olaia May 20 '16 at 16:20
  • both the tablet and the router can lose power in the field. you can't even get the value unless you have set it with a shell command. so that needs to be done once before the app is installed. – Ray Tayek May 20 '16 at 16:43
  • Yes, it's quite a hassel how this works. It's a downside of how they've come up with this setting. Keeping in mind there are countries with limited access to google servers, I don't think it should be so hidden in the API and even don't provide a way for users to disable it. – Olaia May 21 '16 at 00:35
  • 2
    Is there a workaround that doesn't involve root access or adb? – user2489122 Jan 23 '17 at 11:06
0

A non-root solution which is a kind of hack tech. :P

  1. Reboot phone, connect to the non-Internet Wifi;
  2. Go to Settings and create a new user;
  3. Continue, continue, and continue until you see "Checking connection";
  4. As soon as you see "checking connection", switch off your phone;
  5. Switch on your phone again, you will be in "Owner" user, keep it;
  6. Toggle Wifi, and the exclamation mark should disappear quickly :)
  7. Remove that new user or just leave it there;

I don't know why, but it works...

MewX
  • 4,232
  • 1
  • 29
  • 38
  • does it work if you cycle power on the phone and the router? – Ray Tayek Nov 23 '16 at 07:38
  • @RayTayek Sure, it works here perfectly though I don't know why. In my case, it should be connected/disconnected with PC, and AP should be turned on/off for thousands of times. :D – MewX Nov 23 '16 at 13:54
  • @RayTayek Two devices: Sony XZ and Nexus 9 – MewX Nov 24 '16 at 03:01