-
-
Save anonymous/285e732521e3ee14acb4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void onCreate() { | |
if (beaconManager == null) | |
{ | |
Log.d(APP_TAG,"create new beaconmanager"); | |
try{ | |
beaconManager = new BeaconManager(getBaseContext()); | |
} | |
catch (Exception e) | |
{ | |
Log.d(APP_TAG,e.toString()); | |
} | |
} | |
beaconManager.setBackgroundScanPeriod(5000,2000); | |
beaconManager.setMonitoringListener(new BeaconManager.MonitoringListener() { | |
@Override public void onEnteredRegion(Region region, List<Beacon> beacons) { | |
//Log.d(TAG, "entered"); | |
Toast.makeText(SchedulerEventService.this, "Entered", Toast.LENGTH_LONG).show(); | |
Intent notificationIntent = new Intent(SchedulerEventService.this, MainActivity.class); | |
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | |
| Intent.FLAG_ACTIVITY_SINGLE_TOP); | |
PendingIntent intent = PendingIntent.getActivity(SchedulerEventService.this, 0, | |
notificationIntent, 0); | |
Notification noti = new Notification.Builder(SchedulerEventService.this) | |
.setContentTitle("Entered") | |
.setContentText("You're home!") | |
.setSmallIcon(R.drawable.ic_launcher) | |
.setContentIntent(intent) | |
.build(); | |
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
mNotificationManager.cancel(2); | |
mNotificationManager.notify(1, noti); | |
} | |
@Override public void onExitedRegion(Region region) { | |
//Log.d(TAG, "exited"); | |
Toast.makeText(SchedulerEventService.this, "Exited", Toast.LENGTH_LONG).show(); | |
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
mNotificationManager.cancel(1); | |
Notification noti = new Notification.Builder(SchedulerEventService.this) | |
.setContentTitle("Exited") | |
.setContentText("See you!") | |
.setSmallIcon(R.drawable.ic_launcher) | |
.build(); | |
mNotificationManager.notify(2, noti); | |
} | |
}); | |
//this is thread, for loop calling beaconScanning | |
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() { | |
public void run() { | |
Log.d(APP_TAG,"Call beacon Scan"); | |
BeaconScanning(); | |
} | |
}, 0, 35, TimeUnit.SECONDS); | |
} | |
//this method will execute a thread for startMonitoring for 30s and then stopMonitoring + cancel thread itself. | |
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); | |
public void BeaconScanning() { | |
final Runnable starMonitoring = new Runnable() { | |
public void run() { | |
System.out.println("StartMonitoring"); | |
beaconManager.connect(new BeaconManager.ServiceReadyCallback() { | |
@Override public void onServiceReady() { | |
try | |
{ | |
com.estimote.sdk.utils.L.enableDebugLogging(true); | |
beaconManager.startMonitoring(ALL_ESTIMOTE_BEACONS); | |
} | |
catch (RemoteException e) | |
{ | |
//Log.e(TAG, "Cannot start ranging", e); | |
} | |
} | |
}); | |
} | |
}; | |
final ScheduledFuture<?> monitoringHandle = scheduler.scheduleAtFixedRate(starMonitoring, 0, 60, TimeUnit.SECONDS); | |
scheduler.schedule(new Runnable() { | |
public void run() { | |
System.out.println("StopMonitoring"); | |
try | |
{ | |
com.estimote.sdk.utils.L.enableDebugLogging(true); | |
beaconManager.stopMonitoring(ALL_ESTIMOTE_BEACONS); | |
} | |
catch (RemoteException e) | |
{ | |
//Log.e(TAG, "Cannot start ranging", e); | |
} | |
// cancel the running thread | |
monitoringHandle.cancel(true); } | |
}, 30, TimeUnit.SECONDS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment