From d1dda43839002de5bce712befe70721215d56426 Mon Sep 17 00:00:00 2001 From: "David G. Young" Date: Tue, 19 May 2015 00:10:23 -0400 Subject: [PATCH] Avoid 500 alarm limit by rescheduling alarms. --- .../service/scanner/CycledLeScanner.java | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScanner.java b/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScanner.java index 7e9e73faf..8c5270240 100644 --- a/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScanner.java +++ b/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScanner.java @@ -268,7 +268,6 @@ private void finishScanCycle() { } } - protected BluetoothAdapter getBluetoothAdapter() { if (mBluetoothAdapter == null) { // Initializes Bluetooth adapter. @@ -296,22 +295,33 @@ protected void setWakeUpAlarm() { if (milliseconds < mScanPeriod) { milliseconds = mScanPeriod; } + AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); - Intent intent = new Intent(); - intent.setClassName(mContext, StartupBroadcastReceiver.class.getName()); - intent.putExtra("wakeup", true); - cancelWakeUpAlarm(); - mWakeUpOperation = PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); - alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + milliseconds, mWakeUpOperation); - LogManager.d(TAG, "Set a wakeup alarm to go off in %s ms: %s", milliseconds, mWakeUpOperation); + alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + milliseconds, getWakeUpOperation()); + LogManager.d(TAG, "Set a wakeup alarm to go off in %s ms: %s", milliseconds, getWakeUpOperation()); + } + + protected PendingIntent getWakeUpOperation() { + if (mWakeUpOperation == null) { + Intent wakeupIntent = new Intent(); + //intent.setFlags(Intent.FLAG_UPDATE_CURRENT); + wakeupIntent.setClassName(mContext, StartupBroadcastReceiver.class.getName()); + wakeupIntent.putExtra("wakeup", true); + mWakeUpOperation = PendingIntent.getBroadcast(mContext, 0, wakeupIntent, PendingIntent.FLAG_UPDATE_CURRENT); + } + return mWakeUpOperation; } protected void cancelWakeUpAlarm() { LogManager.d(TAG, "cancel wakeup alarm: %s", mWakeUpOperation); - if (mWakeUpOperation != null) { - AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); - alarmManager.cancel(mWakeUpOperation); - } + // We actually don't cancel the wakup alarm... we just reschedule for a long time in the + // future. This is to get around a limit on 500 alarms you can start per app on Samsung + // devices. + long milliseconds = Long.MAX_VALUE; // 2.9 million years from now + AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); + alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + milliseconds, getWakeUpOperation()); + LogManager.d(TAG, "Set a wakeup alarm to go off in %s ms: %s", milliseconds, getWakeUpOperation()); + } private long getNextScanStartTime() {