Head and toes
Head and toes

Reputation: 659

Displaying a notification only when a beacon is detected for a certain period of time

I am very new to the andriod app programming and would like to ask about beacons.

Currently, I am using AltBeacon android-beacon-library to write an app. There is one feature that I like to implement:

Ideally, I would like a notification to be displayed when a user is near a particular beacon. However, I would only like the notification to be displayed if the user is around that beacon for more than 30 seconds. If the user simply walk past the beacon, then I don't want the notification to be displayed.

May I ask if there is any existing methods for this? I understand there is a method called "startMonitoringBeaconsInRegion" but I dont really know if this is a suitable method. Any help would be much appreciated.

Upvotes: 0

Views: 477

Answers (2)

davidgyoung
davidgyoung

Reputation: 64941

You can implement this easily with the Android Beacon Library using ranging. Follow the regular tutorials on setting up ranging. Then in your ranging callback add code like this:

HashMap<String,Long> beaconDetections = new HashMap<String,Long>();

public void didRangeBeaconsInRegion(Region region, Collection<Beacon> beacons) {
  Long now = System.currentTimeMillis();
  for (Beacon beacon : beacons) {
    Long firstDetectionTime = beaconDetections.get(beacon.toString());
    if (firstDetectionTime != null) {
      if (now-firstDetectionTime > 30000l) {
        // Put logic here for if beacon seen for 30 secs or more
      }
    }
    else {
      beaconDetections.put(beacon.toString(), now);   
    }
  }
}

The code above uses a HashMap to track when each beacon was first seen, then executes special logic on each callback if it has been seen for 30 secs or more.

Upvotes: 0

Arbaz Ali Rizvi
Arbaz Ali Rizvi

Reputation: 253

Use Background services to get beacon availability with timer. Start Timer when you get beacon presence and trigger notification after 30 second if beacon is available at location for 30 second else reset the timer.

Beacon background search : http://developer.estimote.com/android/tutorial/part-2-background-monitoring/

Above tutorial link looks promising

    //start when found beacon in background

   Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {

                        getActivity().runOnUiThread(new Runnable() {
                            public void run() {
                                try {
                                 //send notification
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                    }
                }, 30000);

//Reset if beacon status false or unavailable

   if (timer != null) {
   timer.cancel();
   }

Upvotes: 2

Related Questions