1

i can't figure it out how to do this and all other tutorials are outdated and i have not some options they do (like a check box next to the time to set variable). so please don't mark this question as duplicate.

with that said; i have a profile which turn WiFi on when i unlock my phone. i want that to also ping Google DNS and if it get time out turn WiFi off if not keep the wifi connected.

3 Answers 3

1

You can create a task to ping Google DNS as follows:

Task → Code → Run Shell

Command: ping -c 2 8.8.8.8 [Store Output In: %stdout] [Continue Task After Error: checked]

If %stdout ~ 100% packet loss

google servers unreachable

Else

connectivity established

End If

IMG:

Add this to your main task with either keeps wi-fi conection or switches it off.

Credits

Run shell 'ping' with tasker

3
  • when wifi is on but not connected it give me the message "device connected" how cam i fix that? Commented Jul 29, 2018 at 3:21
  • 07.48.07/E Run Shell: %stdout -> %stdout 07.48.07/E Run Shell: -> 07.48.07/E Run Shell: -> 07.48.07/Shell runBackground ping -c 2 8.8.8.8 root: false timeout: -1 07.48.07/Shell start process-thread ID 813 07.48.07/E add wait type Shell1 time 2147483647 07.48.07/E add wait type Shell1 done 07.48.07/E add wait task 07.48.07/Variables doreplresult: |%stdout| -> |%stdout| 07.48.07/E Error: 2 Commented Jul 29, 2018 at 3:22
  • This task, produces an error when the device can;t ping, thus you have to tick "continue on error, in the task to prevent this...The flash message "Devie connected"is a customised text just to indicate that it has successfully pinge to google DNS you could replace this text with any useful message to indicate a succssful ping Commented Jul 30, 2018 at 16:47
0

You don't need the complexity and performance hit of "shelling out", you can do this with Tasker native commands. Use command "http get", then check the HTTP return code in %HTTPRC (or sonething like that:-).

2
  • 3
    It would help if you could add a bit more steps making it easy for new Tasker users to follow too. :-)
    – Firelord
    Commented Nov 30, 2018 at 18:28
  • Entirely unnecessary; Android has alreadly done an internet connectivity check, plus, your method doesn't account for Captive Portal logins. See my answer. Commented Jan 20 at 14:02
0

The Quick, Dirty, and 99% Reliable Method

Better yet, simply use AutoTools' Connectivity event or action. While I generally don't recommend plug-ins, unless absolutely necessary, AutoTools is definitely an essential.

The Right Way™

If you want to do it, "the right way," although considerably more convoluted, use the new Get Network Info action and check the capabilities, like so:

Task: Disconnect Wi-Fi if No Internet

<Pause and allow for network conditions and checks to settle>
A1: Wait [
     MS: 0
     Seconds: 3
     Minutes: 0
     Hours: 0
     Days: 0
     Continue Task After Error:On ]

A2: Get Network Info [
     Continue Task After Error:On ]

<Wi-Fi Connected to AP<br />
<b>AND</b><br />
(Does NOT have internet<br />
<b>OR</b><br />
Has NOT been validated<br />
<b>OR</b><br />
Captive Portal PRESENT)>
A3: If [ %ni_active_types(#?wifi) > 0 & %ni_active_capabilities(#?internet) < 1 | %ni_active_capabilities(#?validated) < 1 | %ni_active_capabilities(#?captive_portal) > 0 ]

    A4: WiFi [
         Set: Off
         Continue Task After Error:On ]

A5: End If

(Scroll to the right at the bottom, in the If conditions of A3, of the above code block to see where the magic really happens.)

The above allows for a variety of network scenarios to occur without prematurely judging the state of internet connectivity; namely, Captive Portal logins. It also prevents you from having to manually ping Google.com (which has its own pitfalls).

... One Step Further

You could make this much more robust too by associating the above task with a profile that handles things on an event-driven basis when network conditions change, like so:

Profile: Wi-Fi: Network Change Detected
Settings: Priority: 20 Enforce: no
    Event: Intent Received [ Action:android.net.conn.CONNECTIVITY_CHANGE Cat:None Cat:None Scheme:* Mime Type:* ]


Enter Task: Anon
Settings: Abort Existing Task

Task: Disconnect Wi-Fi if No Internet

<Pause and allow for network conditions and checks to settle>
A1: Wait [
     MS: 0
     Seconds: 3
     Minutes: 0
     Hours: 0
     Days: 0
     Continue Task After Error:On ]

A2: Get Network Info [
     Continue Task After Error:On ]

<Wi-Fi Connected to AP<br />
<b>AND</b><br />
(Does NOT have internet<br />
<b>OR</b><br />
Has NOT been validated<br />
<b>OR</b><br />
Captive Portal PRESENT)>
A3: If [ %ni_active_types(#?wifi) > 0 & %ni_active_capabilities(#?internet) < 1 | %ni_active_capabilities(#?validated) < 1 | %ni_active_capabilities(#?captive_portal) > 0 ]

    A4: WiFi [
         Set: Off
         Continue Task After Error:On ]

A5: End If

(Take special note of the Task Setting of, Abort Existing Task since this intent gets fired about 6-12 times whenever there is a network state change and you don't want your Task to be executing at the very beginning of those intents, otherwise network states won't be correct yet.)

Also, make sure you're not in beginner mode or in Tasky, and ensure all steps have, Continue on Error checked.

That is the proper way to detect internet connectivity: by allowing Android to do the heavy-lifting so that ALL scenarios are accounted for.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .