Skip to content

Commit

Permalink
Merge pull request #189 from AltBeacon/workaround-500-alarm-limit
Browse files Browse the repository at this point in the history
Avoid 500 alarm limit by rescheduling alarms
  • Loading branch information
davidgyoung committed May 19, 2015
2 parents 14b737d + d1dda43 commit b7536cc
Showing 1 changed file with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ private void finishScanCycle() {
}
}


protected BluetoothAdapter getBluetoothAdapter() {
if (mBluetoothAdapter == null) {
// Initializes Bluetooth adapter.
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit b7536cc

Please sign in to comment.