Page 1 of 1

Wifi Geofencing

Posted: 02 Apr 2018 17:58
by Desmanto
Wifi Geofencing

After posting in several thread discussing geofencing, I am going to share my own method of geofencing. As the general concept of my geofencing
Entering : Should be precise, doing something that always happen. In this case I use wifi connected.
Exiting : Should be triggered by something precise, but buffered/delayed by another wifi scanning or location scanning with some timer.

I have simplified the process and combine 2 of the flows into one (wifi connect/disconnect). The buffer still need to be on other flow, since it needs to be enabled/disabled on demand.
Download and import this : http://automagic4android.com/flow.php?i ... 7e31776ee4

Flow 1 : Wifi Home Enter-Exit
1. Wifi Home Enter-Exit.png
1. Wifi Home Enter-Exit.png (128.58 KiB) Viewed 10721 times
This flow consist of 2 Triggers. One is Wifi connected, the other is Wifi disconnected. Both pointed to the same wifi, which is the name of you Home Wifi. If you have more than one wifi name, you can choose 2 or 3 names here. Just make sure you change it to your own Home Wifi name.

The next condition split the trigger to 2 branch. I actually should use expression. But in this case I want to reduce the possibility of flopping when connecting. So I just use Wifi connected. If true, means it is entering. False means exiting.

True branch
I use Wifi available condition to provide me detail on more variables. Next expression is to check the mac address (bssid). Replace this with your wifi AP mac address. You can find it easily by attaching a debug dialog after the condition wifi available, when you are near your home wifi (not necessary to be connected, just near is enough). The reason we check on this is to reduce the risk of same wifi name. You don't want to go to some coffee shop or restaurant where coincidencely someone has the same wifi name as yours (especially if you use generic name); and suddenly your flow activated out of nowhere. By checking against the mac address, we reduce the possibility of that risk. Yes, someone still can spoof their mac to match yours wifi AP, but the chance is much lower now. (since they need to "coincidencely" have the same wifi name and same mac address). In other word, you want to be sure that the flow only activated when you are really at home.

The next 2 condition start at true branch from this mac address checking. After we ensure we are at home (mac address matches), then we need to do additional checking. The first is to disable the Wifi Home Exit Buffer flow (Flow 2) if it is enabled. We need this since if we lose our wifi connection temporary, and we reconnect within the time, we need to be sure the buffer flow disabled and not checking anymore. The second one is the your main action. You need something to assure that you are at home. That's why we save it in global_geofencing. If this is not at "home", means this is the first you connected to your home wifi after you arrived home. If true, execute your main action. You can replace this with anything you want, from turning on bluetooth, disabling lock screen or enabling some flows. In my case, I will turn on my Remote Server flow. (Remote Server flow is not included in the xml). So I make sure I only use Remote server flow when I am at home. If this is just a reconnect (the global_geofencing is already "home"), then it will end in false (doing nothing). You don't want to execute your main action twice. In my case, enabling Remote Server won't make difference. But if your action is something like sending sms/email, or playing some music; you don't want it keeps resending sms/email everytime you lose connection and reconnect to your wifi while still at home.

False branch
The trigger comes from Wifi Disconnected. It can be you are leaving house, or just roaming around and hit the place where there is blank spot. Or it can be you are just restarting the router to reset the connection. There will be sleep for 5 minutes before finally enabling the Wifi Home Exit Buffer flow. Adjust the sleep to your need. I find 5 minutes should be reasonable.

This flow use Flow Execution Policy Stop (find more info about FEP at my index). So if within that 5 minutes, you are connected back to your wifi, the sleep will be cancelled. The flow execute the true branch as above. Since we have protected the true branch with additional condition, it won't repeat your main action anymore, as long as the glovar still contain "home".


Flow 2 : Wifi Home Exit Buffer
2. Wifi Home Exit Buffer.png
2. Wifi Home Exit Buffer.png (97.78 KiB) Viewed 10721 times
This flow only use 1 trigger, Wifi Scan Results Available. It is trigger by your native android wifi scanning. Make sure android setting "wifi scanning always available" is checked. On higher android version (nougat above), and some certain ROM. You might need to enable Location in order for the flow to work properly. Wifi scanning usually occur periodically 1-5 minutes (depend on your ROM, power saving option and several other factors).

After it is triggered by the wifi scanning result, we do extra check for wifi available. But this time we add our neighbour wifi too inside this list. Probably we just visit them to say hello, and too far away from our main wifi, thus get connected. By checking for their wifi name, you ensure you are still around the house. Simply change the condition to your nearby wifi you want to mark as "home" and neighbour. The condition split to true branch to do additional check again on the mac address. It will check against the available bssid from the scanning, similar to the main flow. Except in here, put your neighbour mac address too inside. The expression will check if any one of the bssids match with any one of the neighbour listed mac address, then it will evaluate to true. Means you are still nearby to home; don't do anything.

If any of this wifi available or expression result in false (neighbour wifi not detected or no mac address matches), then it means you are far from home already. Proceed to disable itself (this flow). Set the global_geofencing to "outside". And then do the main action which you need when you leave home. Again, replace this with the opposite of the action you put in the entering flow. At my case, the opposite is Disable Remote Server flow (I enabled it when entering). If your entering is disable lock security; then this exiting will be enable back the lock security.

Actually, if you don't mind about the neighbour mac address, and you don't mind if the neighbour wifi name, you can just put only your wifi name here. So anytime it doesn't detect your wifi, it will be Exiting event, much more secure. But probably it will trigger more often, when the wifi signal is flopping (between connect and disconnect). So there is also a tradeoff. You can also remove the additional expression which check the mac address at this Exit Buffer flow. Because sometimes, our neighbour can change their devices too frequently, or change the wifi name too. Removing the Expression at the true branch, only reduce the security a bit. Since now you don't check anymore for the mac address, only for wifi name. There will be higher chance there is a coincidence same wifi name that appear during the interval wifi scan (about 1-2 minutes). Anyway, just remove this expression if your exiting action execution unintentionally too many times.


Import instruction
1. Import this 2 flows (single xml).
2. Edit the home wifi name, scan for the mac address and your neighbour wifi mac address.
3. Change the mac address to your wifi and your neighbour wifi.
4. Change the sleep timer to your need
5. Replace the main action (Set flow state Enable/disable Remote Server) with the set of actions you need.
6. After done, you only need to enable the flow 1 only (Wifi Home Enter-Exit). Flow 2 doesn't need to be enabled, as it will be temporary enabled as buffer timer only.
7. Then connect to your main wifi (you can disconnect/turn on wifi and reconnect again). The flow 1 will be trigger and do all the entering main action, indicating you are at home.

Testing
- Try to walk far away from your wifi or go outside until got disconnected from any of your wifi. You should the flow 1 executing the sleep in the false branch.
- Try to wait until the time out and see how flow 2 got enabled.
- When it scan the wifi and doesn't get any of your neighbour wifi, see how it will execute the exit main action.
- After confirmed, try to go back inside and connect to your main wifi and see how it enable back the entering main action.

More complex option
For a more complex flow that include location, you can check Scott12 thread about geofencing. Mine is a simpler solution using only wifi as the check. But you can always add another flow to check for period location update after the Wifi Home Exit Buffer. So you have first buffer at wifi available, second buffer at location periodic check.