B4A Library RSProximityAlert - Enter or exit surrounding area

Hi folks

Yet again a new library called RSProximityAlert.
There is already one in esLocationManager, but mine will let you add multiple Proximity Alerts to locations instead of 1.

A proximity alert is a broadcast you receive when the device detects that it has entered or exited the area surrounding the location you entered.

API Documentation

RSProximityAlert
Author:
XverhelstX
Version: 1
  • RSProximityAlert
    Fields:
    • EVENT_ID_INTENT_EXTRA As String
    • KEY_PROXIMITY_ENTERING As String
    • PROXIMITY_INTENT_ACTION As String
    Methods:
    • AddProximityAlert (Longitude As Double, Latitude As Double, Radius As Float, Expiration As Long, EventID As Long, RequestCode As Int) As PendingIntent
      Set a proximity alert for the location given by the position (latitude, longitude) and the given radius.

      When the device detects that it has entered or exited the area surrounding the location,
      the given PendingIntent will be used to create an Intent to be fired.
      The fired Intent will have a boolean extra added with key KEY_PROXIMITY_ENTERING.
      If the value is true, the device is entering the proximity region; if false, it is exiting.

      Returns the pending intent. Save this intent in a map with the Eventid if you want to remove this ProximityAlert later.
    • Initialize
      Initializes this RSProximityAlert library
    • IsInitialized As Boolean
    • RemoveProximityAlert (intent As PendingIntent)
      Removes the Proximity Alert.
    Permissions:
    • android.permission.ACCESS_COARSE_LOCATION
    • android.permission.ACCESS_FINE_LOCATION

Manifest Editor

B4X:
AddReceiverText(VirtualService,
<intent-filter>
    <action android:name="com.rootsoft.rslocationmanager.PROXIMITY_ALERT" />
</intent-filter>)

Initialize

B4X:
dim mProximityAlert as RSProximityAlert
mProximityAlert.Initialize

Add a Proximity alert to a location

B4X:
Dim pIntent As Object
pIntent = mProximityAlert.AddProximityAlert(Longitude, Latitude, Radius, PROXIMITY_DEFAULT_EXPIRATION, EventId, EventId)
        mapProximityAlerts.Put(EventId, pIntent)

Handle the intent in your StartService

B4X:
'Handles the intent.
Private Sub HandleIntent(StartingIntent As Intent)

If StartingIntent.Action = mProximityAlert.PROXIMITY_INTENT_ACTION Then

        EventId = StartingIntent.GetExtra(mProximityAlert.EVENT_ID_INTENT_EXTRA)
        Entering = StartingIntent.GetExtra(mProximityAlert.KEY_PROXIMITY_ENTERING)
      
        If Entering Then
            ProximityEntered(EventId)
        Else
            ProximityLeft(EventId)
        End If
      
    End If

End Sub

B4X:
'The user is entering a spot.
Public Sub ProximityEntered (EventId As Long)
    Log("Entering Proximity Area: " & EventId)
  
End Sub

'The user is leaving a spot.
Public Sub ProximityLeft (EventId As Long)
    Log("Leaving a Proximity Area: " & EventId)
  

End Sub

Removing a Proximity alert can be done by setting the experation or removing the PendingIntent.
This is why you should save the PendingIntent together with the EventId in a map.


Kind regards,
Tomas
 

Attachments

  • RSProximityAlert.zip
    3.4 KB · Views: 529

bluedude

Well-Known Member
Licensed User
Longtime User
Interesting, a few questions:

- What do you mean by handle intent in StartService? Do you mean in a service in the service creation part?
- how many proximity events can it handle?
- how to remove a pending intent?

Would this work with the Fluffy location stuff?

Cheers,
 

XverhelstX

Well-Known Member
Licensed User
Longtime User
Interesting, a few questions:

- What do you mean by handle intent in StartService? Do you mean in a service in the service creation part?
- how many proximity events can it handle?
- how to remove a pending intent?

Would this work with the Fluffy location stuff?

Cheers,

1. Copy the HandleIntent to your Service_Start sub and pass the StartingIntent parameter.
A broadcast intent is sent when entering or leaving the proximity.

2. I have no idea. I think unlimited.

3. Store the pendingintent in a map with EventId as the key. You can call ProximityAlert.RemoveProximityAlert to remove one.
Just pass the PendingIntent from the map as parameter

4. Yes, this is why I ported it as i'm using it in a app of my own.
 

bluedude

Well-Known Member
Licensed User
Longtime User
Mmmm, still a little clueless about how to implement this. The service is started by a broadcast? What is the VirtualService in the manifest? Is this the service I create myself?

What location solution needs to be implemented to get this working? GPS, WIFI or?

A sample would help :)
 

XverhelstX

Well-Known Member
Licensed User
Longtime User
The service is started by a broadcast?

Yes, whenever the OS finds that you are in that proximity, it will send a broadcast to you own service and starts it (Here VirtualService)
You can use any location library, and just for any location you find (with GPS for instance _LocationChanged) you can add a proximityalert on that location.

Just change your manifest.
Add the handleintent sub in your servce_create sub and adds the ProximityEntered and ProximityLeft subs. These are events when called when leaving or entering the area.
 

Theera

Well-Known Member
Licensed User
Longtime User
Hi Tomas,or others
Where is virtualservice_br Module? I cann't see it.
 

Attachments

  • TestRSProximityAlert.zip
    5.9 KB · Views: 360
Last edited:
D

Deleted member 103

Guest
Hi XverhelstX,

do you have an example that works?
 

merlin2049er

Well-Known Member
Licensed User
Longtime User
Hey, cool. I guess this would work if you were to pair it up with a bluetooth beacon.
 

Croïd

Active Member
Licensed User
Longtime User
Thomas,

Please, you have an example to understand the process ?

How possible put notification near to a destination ?

Thanks for your help
 

Attachments

  • rs2.zip
    7.7 KB · Views: 282

coslad

Well-Known Member
Licensed User
Longtime User
HI XverhelstX

in the add function what do you mean for :

Expiration As Long ?
EventID As Long ? We decide the eventid number ?
RequestCode As Int ?
Radius As Float ? Is radius in centimeter , meter, km ?

Thanks !!
 

coslad

Well-Known Member
Licensed User
Longtime User
It seems that this library is based on google service? May I wrong?
 

coslad

Well-Known Member
Licensed User
Longtime User
Thanks for the reply, but seems that whit exlocationmanager you can add a single Geofence location
 

b4xscripter

Member
Licensed User
Longtime User
Hi!
How is everything? Is updated the RSProximityAlert library?
I would like to use it for my next project.

Best regards
 

Danbicky

Member
Licensed User
Longtime User
This sounds Awesome, Please can you post an Example app showing us how to use this. I have an idea to use this for when I arrive home in the car to then send an MQTT message to disarm my alarm system and arm when I leave for work in the morning based on proximity. I have already developed the hardware and can control this through MQTT commands.

Great work

Dans
 

b4xscripter

Member
Licensed User
Longtime User
Hi all,
I resolved it and implemented it in my new project using Fused Location and Node.js (with MongoDB). Works perfectly.
By basic concerns to not implement all the "proximity logic" in the Android device but on the server are:
- I am not sure that the multiple alarms are working
- Sometimes the device be disconnected or even switched off. So the alert would "die". So I implemented the logic on the server and used the Android to "ping" to my server providing the geolocation coordinates...

If anyone is interested, I would be happy to share my solution.

Best regards
 

Danbicky

Member
Licensed User
Longtime User
Hi XverhelstX

Do you have a simple example demonstrating how this code works. I would really like to try this out with a Geo zone of 100 metres around my location and to control some device I have based on entering and exiting the Geo zone.

Big thanks in advance for all your assistance.

Dans
 

DonManfred

Expert
Licensed User
Longtime User
Dont expect an answer from Xverhelst as he is no longer an active user.
 
Top