-
Notifications
You must be signed in to change notification settings - Fork 829
Expand file tree
/
Copy pathBeaconManager.java
More file actions
1020 lines (937 loc) · 38.7 KB
/
Copy pathBeaconManager.java
File metadata and controls
1020 lines (937 loc) · 38.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Radius Networks, Inc.
* http://www.radiusnetworks.com
*
* @author David G. Young
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.altbeacon.beacon;
import android.annotation.TargetApi;
import android.bluetooth.BluetoothManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import org.altbeacon.beacon.logging.LogManager;
import org.altbeacon.beacon.logging.Loggers;
import org.altbeacon.beacon.service.BeaconService;
import org.altbeacon.beacon.service.MonitoringStatus;
import org.altbeacon.beacon.service.RangeState;
import org.altbeacon.beacon.service.RangedBeacon;
import org.altbeacon.beacon.service.RegionMonitoringState;
import org.altbeacon.beacon.service.RunningAverageRssiFilter;
import org.altbeacon.beacon.service.StartRMData;
import org.altbeacon.beacon.service.scanner.CycledLeScanner;
import org.altbeacon.beacon.service.scanner.NonBeaconLeScanCallback;
import org.altbeacon.beacon.simulator.BeaconSimulator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* A class used to set up interaction with beacons from an <code>Activity</code> or <code>Service</code>.
* This class is used in conjunction with <code>BeaconConsumer</code> interface, which provides a callback
* when the <code>BeaconService</code> is ready to use. Until this callback is made, ranging and monitoring
* of beacons is not possible.
*
* In the example below, an Activity implements the <code>BeaconConsumer</code> interface, binds
* to the service, then when it gets the callback saying the service is ready, it starts ranging.
*
* <pre><code>
* public class RangingActivity extends Activity implements BeaconConsumer {
* protected static final String TAG = "RangingActivity";
* private BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
* {@literal @}Override
* protected void onCreate(Bundle savedInstanceState) {
* super.onCreate(savedInstanceState);
* setContentView(R.layout.activity_ranging);
* beaconManager.bind(this);
* }
* {@literal @}Override
* protected void onDestroy() {
* super.onDestroy();
* beaconManager.unbind(this);
* }
* {@literal @}Override
* public void onBeaconServiceConnect() {
* beaconManager.setRangeNotifier(new RangeNotifier() {
* {@literal @}Override
* public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
* if (beacons.size() > 0) {
* Log.i(TAG, "The first beacon I see is about "+beacons.iterator().next().getDistance()+" meters away.");
* }
* }
* });
*
* try {
* beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
* } catch (RemoteException e) {
* e.printStackTrace();
* }
* }
* }
* </code></pre>
*
* @author David G. Young
* @author Andrew Reitz <andrew@andrewreitz.com>
*/
@TargetApi(4)
public class BeaconManager {
private static final String TAG = "BeaconManager";
private Context mContext;
protected static BeaconManager client = null;
private final ConcurrentMap<BeaconConsumer, ConsumerInfo> consumers = new ConcurrentHashMap<BeaconConsumer,ConsumerInfo>();
private Messenger serviceMessenger = null;
protected final Set<RangeNotifier> rangeNotifiers = new CopyOnWriteArraySet<>();
protected RangeNotifier dataRequestNotifier = null;
protected final Set<MonitorNotifier> monitorNotifiers = new CopyOnWriteArraySet<>();
private final ArrayList<Region> rangedRegions = new ArrayList<Region>();
private final List<BeaconParser> beaconParsers = new CopyOnWriteArrayList<>();
private CycledLeScanner cycledLeScanner;
private NonBeaconLeScanCallback mNonBeaconLeScanCallback;
private boolean mBackgroundMode = false;
private boolean mBackgroundModeUninitialized = true;
private static boolean sAndroidLScanningDisabled = false;
private static boolean sManifestCheckingDisabled = false;
/**
* Set to true if you want to show library debugging.
*
* @param debug True turn on all logs for this library to be printed out to logcat. False turns
* off all logging.
* @deprecated To be removed in a future release. Use
* {@link org.altbeacon.beacon.logging.LogManager#setLogger(org.altbeacon.beacon.logging.Logger)}
* instead.
*/
@Deprecated
public static void setDebug(boolean debug) {
if (debug) {
LogManager.setLogger(Loggers.verboseLogger());
LogManager.setVerboseLoggingEnabled(true);
} else {
LogManager.setLogger(Loggers.empty());
LogManager.setVerboseLoggingEnabled(false);
}
}
/**
* The default duration in milliseconds of the Bluetooth scan cycle
*/
public static final long DEFAULT_FOREGROUND_SCAN_PERIOD = 1100;
/**
* The default duration in milliseconds spent not scanning between each Bluetooth scan cycle
*/
public static final long DEFAULT_FOREGROUND_BETWEEN_SCAN_PERIOD = 0;
/**
* The default duration in milliseconds of the Bluetooth scan cycle when no ranging/monitoring clients are in the foreground
*/
public static final long DEFAULT_BACKGROUND_SCAN_PERIOD = 10000;
/**
* The default duration in milliseconds spent not scanning between each Bluetooth scan cycle when no ranging/monitoring clients are in the foreground
*/
public static final long DEFAULT_BACKGROUND_BETWEEN_SCAN_PERIOD = 5 * 60 * 1000;
/**
* The default duration in milliseconds of region exit time
*/
public static final long DEFAULT_EXIT_PERIOD = 10000L;
private static long sExitRegionPeriod = DEFAULT_EXIT_PERIOD;
private long foregroundScanPeriod = DEFAULT_FOREGROUND_SCAN_PERIOD;
private long foregroundBetweenScanPeriod = DEFAULT_FOREGROUND_BETWEEN_SCAN_PERIOD;
private long backgroundScanPeriod = DEFAULT_BACKGROUND_SCAN_PERIOD;
private long backgroundBetweenScanPeriod = DEFAULT_BACKGROUND_BETWEEN_SCAN_PERIOD;
/**
* Sets the duration in milliseconds of each Bluetooth LE scan cycle to look for beacons.
* This function is used to setup the period before calling {@link #bind} or when switching
* between background/foreground. To have it effect on an already running scan (when the next
* cycle starts), call {@link #updateScanPeriods}
*
* @param p
*/
public void setForegroundScanPeriod(long p) {
foregroundScanPeriod = p;
}
/**
* Sets the duration in milliseconds between each Bluetooth LE scan cycle to look for beacons.
* This function is used to setup the period before calling {@link #bind} or when switching
* between background/foreground. To have it effect on an already running scan (when the next
* cycle starts), call {@link #updateScanPeriods}
*
* @param p
*/
public void setForegroundBetweenScanPeriod(long p) {
foregroundBetweenScanPeriod = p;
}
/**
* Sets the duration in milliseconds of each Bluetooth LE scan cycle to look for beacons.
* This function is used to setup the period before calling {@link #bind} or when switching
* between background/foreground. To have it effect on an already running scan (when the next
* cycle starts), call {@link #updateScanPeriods}
*
* @param p
*/
public void setBackgroundScanPeriod(long p) {
backgroundScanPeriod = p;
}
/**
* Sets the duration in milliseconds spent not scanning between each Bluetooth LE scan cycle when no ranging/monitoring clients are in the foreground
*
* @param p
*/
public void setBackgroundBetweenScanPeriod(long p) {
backgroundBetweenScanPeriod = p;
}
/**
* Set region exit period in milliseconds
*
* @param regionExitPeriod
*/
public static void setRegionExitPeriod(long regionExitPeriod){
sExitRegionPeriod = regionExitPeriod;
}
/**
* Get region exit milliseconds
*
* @return exit region period in milliseconds
*/
public static long getRegionExitPeriod(){
return sExitRegionPeriod;
}
/**
* An accessor for the singleton instance of this class. A context must be provided, but if you need to use it from a non-Activity
* or non-Service class, you can attach it to another singleton or a subclass of the Android Application class.
*/
public static BeaconManager getInstanceForApplication(Context context) {
if (client == null) {
LogManager.d(TAG, "BeaconManager instance creation");
client = new BeaconManager(context);
}
return client;
}
protected BeaconManager(Context context) {
mContext = context.getApplicationContext();
if (!sManifestCheckingDisabled) {
verifyServiceDeclaration();
}
this.beaconParsers.add(new AltBeaconParser());
cycledLeScanner = new CycledLeScanner(BeaconManager.DEFAULT_FOREGROUND_SCAN_PERIOD,
BeaconManager.DEFAULT_FOREGROUND_BETWEEN_SCAN_PERIOD, false);
}
/**
* Gets a list of the active beaconParsers.
*
* @return list of active BeaconParsers
*/
public List<BeaconParser> getBeaconParsers() {
return beaconParsers;
}
/**
* Check if Bluetooth LE is supported by this Android device, and if so, make sure it is enabled.
*
* @return false if it is supported and not enabled
* @throws BleNotAvailableException if Bluetooth LE is not supported. (Note: The Android emulator will do this)
*/
@TargetApi(18)
public boolean checkAvailability() throws BleNotAvailableException {
if (android.os.Build.VERSION.SDK_INT < 18) {
throw new BleNotAvailableException("Bluetooth LE not supported by this device");
}
if (!mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
throw new BleNotAvailableException("Bluetooth LE not supported by this device");
} else {
if (((BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter().isEnabled()) {
return true;
}
}
return false;
}
/**
* Binds an Android <code>Activity</code> or <code>Service</code> to the <code>BeaconService</code>. The
* <code>Activity</code> or <code>Service</code> must implement the <code>beaconConsumer</code> interface so
* that it can get a callback when the service is ready to use.
*
* @param consumer the <code>Activity</code> or <code>Service</code> that will receive the callback when the service is ready.
*/
public void bind(BeaconConsumer consumer) {
if (android.os.Build.VERSION.SDK_INT < 18) {
LogManager.w(TAG, "Not supported prior to API 18. Method invocation will be ignored");
return;
}
synchronized (consumers) {
ConsumerInfo newConsumerInfo = new ConsumerInfo();
ConsumerInfo alreadyBoundConsumerInfo = consumers.putIfAbsent(consumer, newConsumerInfo);
if (alreadyBoundConsumerInfo != null) {
LogManager.d(TAG, "This consumer is already bound");
}
else {
LogManager.d(TAG, "This consumer is not bound. binding: %s", consumer);
Intent intent = new Intent(consumer.getApplicationContext(), BeaconService.class);
consumer.bindService(intent, newConsumerInfo.beaconServiceConnection, Context.BIND_AUTO_CREATE);
LogManager.d(TAG, "consumer count is now: %s", consumers.size());
}
}
}
/**
* Unbinds an Android <code>Activity</code> or <code>Service</code> to the <code>BeaconService</code>. This should
* typically be called in the onDestroy() method.
*
* @param consumer the <code>Activity</code> or <code>Service</code> that no longer needs to use the service.
*/
public void unbind(BeaconConsumer consumer) {
if (android.os.Build.VERSION.SDK_INT < 18) {
LogManager.w(TAG, "Not supported prior to API 18. Method invocation will be ignored");
return;
}
synchronized (consumers) {
if (consumers.containsKey(consumer)) {
LogManager.d(TAG, "Unbinding");
consumer.unbindService(consumers.get(consumer).beaconServiceConnection);
consumers.remove(consumer);
if (consumers.size() == 0) {
// If this is the last consumer to disconnect, the service will exit
// release the serviceMessenger.
serviceMessenger = null;
// Reset the mBackgroundMode to false, which is the default value
// This way when we restart ranging or monitoring it will always be in
// foreground mode
mBackgroundMode = false;
}
}
else {
LogManager.d(TAG, "This consumer is not bound to: %s", consumer);
LogManager.d(TAG, "Bound consumers: ");
Set<Map.Entry<BeaconConsumer, ConsumerInfo>> consumers = this.consumers.entrySet();
for (Map.Entry<BeaconConsumer, ConsumerInfo> consumerEntry : consumers) {
LogManager.d(TAG, String.valueOf(consumerEntry.getValue()));
}
}
}
}
/**
* Tells you if the passed beacon consumer is bound to the service
*
* @param consumer
* @return
*/
public boolean isBound(BeaconConsumer consumer) {
synchronized(consumers) {
return consumer != null && consumers.get(consumer) != null && (serviceMessenger != null);
}
}
/**
* Tells you if the any beacon consumer is bound to the service
*
* @return
*/
public boolean isAnyConsumerBound() {
synchronized(consumers) {
return consumers.size() > 0 && (serviceMessenger != null);
}
}
/**
* This method notifies the beacon service that the application is either moving to background
* mode or foreground mode. When in background mode, BluetoothLE scans to look for beacons are
* executed less frequently in order to save battery life. The specific scan rates for
* background and foreground operation are set by the defaults below, but may be customized.
* When ranging in the background, the time between updates will be much less frequent than in
* the foreground. Updates will come every time interval equal to the sum total of the
* BackgroundScanPeriod and the BackgroundBetweenScanPeriod.
*
* @param backgroundMode true indicates the app is in the background
* @see #DEFAULT_FOREGROUND_SCAN_PERIOD
* @see #DEFAULT_FOREGROUND_BETWEEN_SCAN_PERIOD;
* @see #DEFAULT_BACKGROUND_SCAN_PERIOD;
* @see #DEFAULT_BACKGROUND_BETWEEN_SCAN_PERIOD;
* @see #setForegroundScanPeriod(long p)
* @see #setForegroundBetweenScanPeriod(long p)
* @see #setBackgroundScanPeriod(long p)
* @see #setBackgroundBetweenScanPeriod(long p)
*/
public void setBackgroundMode(boolean backgroundMode) {
if (android.os.Build.VERSION.SDK_INT < 18) {
LogManager.w(TAG, "Not supported prior to API 18. Method invocation will be ignored");
}
mBackgroundModeUninitialized = false;
if (backgroundMode != mBackgroundMode) {
mBackgroundMode = backgroundMode;
try {
this.updateScanPeriods();
} catch (RemoteException e) {
LogManager.e(TAG, "Cannot contact service to set scan periods");
}
}
}
/**
* @return indicator of whether any calls have yet been made to set the
* background mode
*/
public boolean isBackgroundModeUninitialized() {
return mBackgroundModeUninitialized;
}
/**
* Specifies a class that should be called each time the <code>BeaconService</code> gets ranging
* data, which is nominally once per second when beacons are detected.
* <p/>
* IMPORTANT: Only one RangeNotifier may be active for a given application. If two different
* activities or services set different RangeNotifier instances, the last one set will receive
* all the notifications.
*
* @param notifier The {@link RangeNotifier} to register.
* @see RangeNotifier
* @deprecated replaced by (@link #addRangeNotifier)
*/
@Deprecated
public void setRangeNotifier(RangeNotifier notifier) {
synchronized (rangeNotifiers) {
rangeNotifiers.clear();
}
addRangeNotifier(notifier);
}
/**
* Specifies a class that should be called each time the <code>BeaconService</code> gets ranging
* data, which is nominally once per second when beacons are detected.
* <p/>
* Permits to register several <code>RangeNotifier</code> objects.
* <p/>
* The notifier must be unregistered using (@link #removeRangeNotifier)
*
* @param notifier The {@link RangeNotifier} to register.
* @see RangeNotifier
*/
public void addRangeNotifier(RangeNotifier notifier) {
if (notifier != null) {
synchronized (rangeNotifiers) {
rangeNotifiers.add(notifier);
}
}
}
/**
* Specifies a class to remove from the array of <code>RangeNotifier</code>
*
* @param notifier The {@link RangeNotifier} to unregister.
* @see RangeNotifier
*/
public boolean removeRangeNotifier(RangeNotifier notifier) {
synchronized (rangeNotifiers) {
return rangeNotifiers.remove(notifier);
}
}
/**
* Remove all the Range Notifiers.
*/
public void removeAllRangeNotifiers() {
synchronized (rangeNotifiers) {
rangeNotifiers.clear();
}
}
/**
* Specifies a class that should be called each time the <code>BeaconService</code> sees
* or stops seeing a Region of beacons.
* <p/>
* IMPORTANT: Only one MonitorNotifier may be active for a given application. If two different
* activities or services set different MonitorNotifier instances, the last one set will receive
* all the notifications.
*
* @param notifier The {@link MonitorNotifier} to register.
* @see MonitorNotifier
* @see #startMonitoringBeaconsInRegion(Region)
* @see Region
* @deprecated replaced by {@link #addMonitorNotifier}
*/
@Deprecated
public void setMonitorNotifier(MonitorNotifier notifier) {
synchronized (monitorNotifiers) {
monitorNotifiers.clear();
}
addMonitorNotifier(notifier);
}
/**
* Specifies a class that should be called each time the <code>BeaconService</code> sees or
* stops seeing a Region of beacons.
* <p/>
* Permits to register several <code>MonitorNotifier</code> objects.
* <p/>
* Unregister the notifier using {@link #removeMonitoreNotifier}
*
* @param notifier The {@link MonitorNotifier} to register.
* @see MonitorNotifier
* @see #startMonitoringBeaconsInRegion(Region)
* @see Region
*/
public void addMonitorNotifier(MonitorNotifier notifier) {
if (notifier != null) {
synchronized (monitorNotifiers) {
monitorNotifiers.add(notifier);
}
}
}
/**
* @see #removeMonitorNotifier
* @deprecated Misspelled. Replaced by {@link #removeMonitorNotifier}
*/
@Deprecated
public boolean removeMonitoreNotifier(MonitorNotifier notifier) {
return removeMonitorNotifier(notifier);
}
/**
* Specifies a class to remove from the array of <code>MonitorNotifier</code>.
*
* @param notifier The {@link MonitorNotifier} to unregister.
* @see MonitorNotifier
* @see #startMonitoringBeaconsInRegion(Region)
* @see Region
*/
public boolean removeMonitorNotifier(MonitorNotifier notifier) {
synchronized (monitorNotifiers) {
return monitorNotifiers.remove(notifier);
}
}
/**
* Remove all the Monitor Notifiers.
*/
public void removeAllMonitorNotifiers() {
synchronized (monitorNotifiers) {
monitorNotifiers.clear();
}
}
/**
* @see #setRegionStatePersistenceEnabled
* @deprecated Misspelled. Replaced by {@link #setRegionStatePersistenceEnabled}
*/
@Deprecated
public void setRegionStatePeristenceEnabled(boolean enabled) {
setRegionStatePersistenceEnabled(enabled);
}
/**
* Turns off saving the state of monitored regions to persistent storage so it is retained over
* app restarts. Defaults to enabled. When enabled, there will not be an "extra" region entry
* event when the app starts up and a beacon for a monitored region was previously visible
* within the past 15 minutes. Note that there is a limit to 50 monitored regions that may be
* persisted. If more than 50 regions are monitored, state is not persisted for any.
*
* @param enabled true to enable the region state persistence, false to disable it.
*/
public void setRegionStatePersistenceEnabled(boolean enabled) {
if (enabled) {
MonitoringStatus.getInstanceForApplication(mContext).startStatusPreservation();
} else {
MonitoringStatus.getInstanceForApplication(mContext).stopStatusPreservation();
}
}
/**
* Requests the current in/out state on the specified region. If the region is being monitored,
* this will cause an asynchronous callback on the `MonitorNotifier`'s `didDetermineStateForRegion`
* method. If it is not a monitored region, it will be ignored.
* @param region
*/
public void requestStateForRegion(Region region) {
MonitoringStatus status = MonitoringStatus.getInstanceForApplication(mContext);
RegionMonitoringState stateObj = status.stateOf(region);
int state = MonitorNotifier.OUTSIDE;
if (stateObj != null && stateObj.getInside()) {
state = MonitorNotifier.INSIDE;
}
synchronized (monitorNotifiers) {
for (MonitorNotifier notifier: monitorNotifiers) {
notifier.didDetermineStateForRegion(state, region);
}
}
}
/**
* Tells the <code>BeaconService</code> to start looking for beacons that match the passed
* <code>Region</code> object, and providing updates on the estimated mDistance every seconds while
* beacons in the Region are visible. Note that the Region's unique identifier must be retained to
* later call the stopRangingBeaconsInRegion method.
*
* @param region
* @see BeaconManager#setRangeNotifier(RangeNotifier)
* @see BeaconManager#stopRangingBeaconsInRegion(Region region)
* @see RangeNotifier
* @see Region
*/
@TargetApi(18)
public void startRangingBeaconsInRegion(Region region) throws RemoteException {
if (android.os.Build.VERSION.SDK_INT < 18) {
LogManager.w(TAG, "Not supported prior to API 18. Method invocation will be ignored");
return;
}
if (serviceMessenger == null) {
throw new RemoteException("The BeaconManager is not bound to the service. Call beaconManager.bind(BeaconConsumer consumer) and wait for a callback to onBeaconServiceConnect()");
}
Message msg = Message.obtain(null, BeaconService.MSG_START_RANGING, 0, 0);
StartRMData obj = new StartRMData(region, callbackPackageName(), this.getScanPeriod(), this.getBetweenScanPeriod(), this.mBackgroundMode);
msg.obj = obj;
serviceMessenger.send(msg);
synchronized (rangedRegions) {
rangedRegions.add(region);
}
}
/**
* Tells the <code>BeaconService</code> to stop looking for beacons that match the passed
* <code>Region</code> object and providing mDistance information for them.
*
* @param region
* @see #setMonitorNotifier(MonitorNotifier notifier)
* @see #startMonitoringBeaconsInRegion(Region region)
* @see MonitorNotifier
* @see Region
*/
@TargetApi(18)
public void stopRangingBeaconsInRegion(Region region) throws RemoteException {
if (android.os.Build.VERSION.SDK_INT < 18) {
LogManager.w(TAG, "Not supported prior to API 18. Method invocation will be ignored");
return;
}
if (serviceMessenger == null) {
throw new RemoteException("The BeaconManager is not bound to the service. Call beaconManager.bind(BeaconConsumer consumer) and wait for a callback to onBeaconServiceConnect()");
}
Message msg = Message.obtain(null, BeaconService.MSG_STOP_RANGING, 0, 0);
StartRMData obj = new StartRMData(region, callbackPackageName(), this.getScanPeriod(), this.getBetweenScanPeriod(), this.mBackgroundMode);
msg.obj = obj;
serviceMessenger.send(msg);
synchronized (rangedRegions) {
Region regionToRemove = null;
for (Region rangedRegion : rangedRegions) {
if (region.getUniqueId().equals(rangedRegion.getUniqueId())) {
regionToRemove = rangedRegion;
}
}
rangedRegions.remove(regionToRemove);
}
}
/**
* Tells the <code>BeaconService</code> to start looking for beacons that match the passed
* <code>Region</code> object. Note that the Region's unique identifier must be retained to
* later call the stopMonitoringBeaconsInRegion method.
*
* @param region
* @see BeaconManager#setMonitorNotifier(MonitorNotifier)
* @see BeaconManager#stopMonitoringBeaconsInRegion(Region region)
* @see MonitorNotifier
* @see Region
*/
@TargetApi(18)
public void startMonitoringBeaconsInRegion(Region region) throws RemoteException {
if (android.os.Build.VERSION.SDK_INT < 18) {
LogManager.w(TAG, "Not supported prior to API 18. Method invocation will be ignored");
return;
}
if (serviceMessenger == null) {
throw new RemoteException("The BeaconManager is not bound to the service. Call beaconManager.bind(BeaconConsumer consumer) and wait for a callback to onBeaconServiceConnect()");
}
LogManager.d(TAG, "Starting monitoring region "+region+" with uniqueID: "+region.getUniqueId());
Message msg = Message.obtain(null, BeaconService.MSG_START_MONITORING, 0, 0);
StartRMData obj = new StartRMData(region, callbackPackageName(), this.getScanPeriod(), this.getBetweenScanPeriod(), this.mBackgroundMode);
msg.obj = obj;
serviceMessenger.send(msg);
this.requestStateForRegion(region);
}
/**
* Tells the <code>BeaconService</code> to stop looking for beacons that match the passed
* <code>Region</code> object. Note that the Region's unique identifier is used to match it to
* an existing monitored Region.
*
* @param region
* @see BeaconManager#setMonitorNotifier(MonitorNotifier)
* @see BeaconManager#startMonitoringBeaconsInRegion(Region region)
* @see MonitorNotifier
* @see Region
*/
@TargetApi(18)
public void stopMonitoringBeaconsInRegion(Region region) throws RemoteException {
if (android.os.Build.VERSION.SDK_INT < 18) {
LogManager.w(TAG, "Not supported prior to API 18. Method invocation will be ignored");
return;
}
if (serviceMessenger == null) {
throw new RemoteException("The BeaconManager is not bound to the service. Call beaconManager.bind(BeaconConsumer consumer) and wait for a callback to onBeaconServiceConnect()");
}
Message msg = Message.obtain(null, BeaconService.MSG_STOP_MONITORING, 0, 0);
StartRMData obj = new StartRMData(region, callbackPackageName(), this.getScanPeriod(), this.getBetweenScanPeriod(), this.mBackgroundMode);
msg.obj = obj;
serviceMessenger.send(msg);
}
/**
* Updates an already running scan with scanPeriod/betweenScanPeriod according to Background/Foreground state.
* Change will take effect on the start of the next scan cycle.
*
* @throws RemoteException - If the BeaconManager is not bound to the service.
*/
@TargetApi(18)
public void updateScanPeriods() throws RemoteException {
if (android.os.Build.VERSION.SDK_INT < 18) {
LogManager.w(TAG, "Not supported prior to API 18. Method invocation will be ignored");
return;
}
if (serviceMessenger == null) {
throw new RemoteException("The BeaconManager is not bound to the service. Call beaconManager.bind(BeaconConsumer consumer) and wait for a callback to onBeaconServiceConnect()");
}
Message msg = Message.obtain(null, BeaconService.MSG_SET_SCAN_PERIODS, 0, 0);
LogManager.d(TAG, "updating background flag to %s", mBackgroundMode);
LogManager.d(TAG, "updating scan period to %s, %s", this.getScanPeriod(), this.getBetweenScanPeriod());
StartRMData obj = new StartRMData(this.getScanPeriod(), this.getBetweenScanPeriod(), this.mBackgroundMode);
msg.obj = obj;
serviceMessenger.send(msg);
}
private String callbackPackageName() {
String packageName = mContext.getPackageName();
LogManager.d(TAG, "callback packageName: %s", packageName);
return packageName;
}
/**
* @return the first registered monitorNotifier
* @deprecated replaced by (@link #getMonitorNotifiers)
*/
@Deprecated
public MonitorNotifier getMonitoringNotifier() {
synchronized (monitorNotifiers) {
if (monitorNotifiers.size() > 0) {
return monitorNotifiers.iterator().next();
}
return null;
}
}
/**
* @return the list of registered monitorNotifier
*/
public Set<MonitorNotifier> getMonitoringNotifiers(){
return monitorNotifiers;
}
/**
* @return the first registered rangeNotifier
* @deprecated replaced by (@link #getRangeNotifiers)
*/
@Deprecated
public RangeNotifier getRangingNotifier() {
synchronized (rangeNotifiers) {
if (rangeNotifiers.size() > 0) {
return rangeNotifiers.iterator().next();
}
return null;
}
}
/**
* @return the list of registered rangeNotifier
*/
public Set<RangeNotifier> getRangingNotifiers() {
return rangeNotifiers;
}
/**
* @return the list of regions currently being monitored
*/
public Collection<Region> getMonitoredRegions() {
return MonitoringStatus.getInstanceForApplication(mContext).regions();
}
/**
* @return the list of regions currently being ranged
*/
public Collection<Region> getRangedRegions() {
synchronized(this.rangedRegions) {
return new ArrayList<Region>(this.rangedRegions);
}
}
/**
* Convenience method for logging debug by the library
*
* @param tag
* @param message
* @deprecated This will be removed in a later release. Use
* {@link org.altbeacon.beacon.logging.LogManager#d(String, String, Object...)} instead.
*/
@Deprecated
public static void logDebug(String tag, String message) {
LogManager.d(tag, message);
}
/**
* Convenience method for logging debug by the library
*
* @param tag
* @param message
* @param t
* @deprecated This will be removed in a later release. Use
* {@link org.altbeacon.beacon.logging.LogManager#d(Throwable, String, String, Object...)}
* instead.
*/
@Deprecated
public static void logDebug(String tag, String message, Throwable t) {
LogManager.d(t, tag, message);
}
protected static BeaconSimulator beaconSimulator;
protected static String distanceModelUpdateUrl = "http://data.altbeacon.org/android-distance.json";
public static String getDistanceModelUpdateUrl() {
return distanceModelUpdateUrl;
}
public static void setDistanceModelUpdateUrl(String url) {
distanceModelUpdateUrl = url;
}
/**
* Default class for rssi filter/calculation implementation
*/
protected static Class rssiFilterImplClass = RunningAverageRssiFilter.class;
public static void setRssiFilterImplClass(Class c) {
rssiFilterImplClass = c;
}
public static Class getRssiFilterImplClass() {
return rssiFilterImplClass;
}
/**
* Allow the library to use a tracking cache
* @param useTrackingCache
*/
public static void setUseTrackingCache(boolean useTrackingCache) {
RangeState.setUseTrackingCache(useTrackingCache);
}
/**
* Set the period of time, in which a beacon did not receive new
* measurements
* @param maxTrackingAge in milliseconds
*/
public void setMaxTrackingAge(int maxTrackingAge) {
RangedBeacon.setMaxTrackinAge(maxTrackingAge);
}
public static void setBeaconSimulator(BeaconSimulator beaconSimulator) {
BeaconManager.beaconSimulator = beaconSimulator;
}
public static BeaconSimulator getBeaconSimulator() {
return BeaconManager.beaconSimulator;
}
protected void setDataRequestNotifier(RangeNotifier notifier) {
this.dataRequestNotifier = notifier;
}
protected RangeNotifier getDataRequestNotifier() {
return this.dataRequestNotifier;
}
public NonBeaconLeScanCallback getNonBeaconLeScanCallback() {
return mNonBeaconLeScanCallback;
}
public void setNonBeaconLeScanCallback(NonBeaconLeScanCallback callback) {
mNonBeaconLeScanCallback = callback;
}
private long getScanPeriod() {
if (mBackgroundMode) {
return backgroundScanPeriod;
} else {
return foregroundScanPeriod;
}
}
private long getBetweenScanPeriod() {
if (mBackgroundMode) {
return backgroundBetweenScanPeriod;
} else {
return foregroundBetweenScanPeriod;
}
}
private void verifyServiceDeclaration() {
final PackageManager packageManager = mContext.getPackageManager();
final Intent intent = new Intent(mContext, BeaconService.class);
List resolveInfo =
packageManager.queryIntentServices(intent,
PackageManager.MATCH_DEFAULT_ONLY);
if (resolveInfo.size() == 0) {
throw new ServiceNotDeclaredException();
}
}
private class ConsumerInfo {
public boolean isConnected = false;
public BeaconServiceConnection beaconServiceConnection;
public ConsumerInfo() {
this.isConnected = false;
this.beaconServiceConnection= new BeaconServiceConnection();
}
}
private class BeaconServiceConnection implements ServiceConnection {
private BeaconServiceConnection() {
}
// Called when the connection with the service is established
public void onServiceConnected(ComponentName className, IBinder service) {
LogManager.d(TAG, "we have a connection to the service now");
serviceMessenger = new Messenger(service);
synchronized(consumers) {
Iterator<Map.Entry<BeaconConsumer, ConsumerInfo>> iter = consumers.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<BeaconConsumer, ConsumerInfo> entry = iter.next();
if (!entry.getValue().isConnected) {
entry.getKey().onBeaconServiceConnect();
entry.getValue().isConnected = true;
}
}
}
}
// Called when the connection with the service disconnects
public void onServiceDisconnected(ComponentName className) {
LogManager.e(TAG, "onServiceDisconnected");
serviceMessenger = null;
}
}
public class ServiceNotDeclaredException extends RuntimeException {
public ServiceNotDeclaredException() {
super("The BeaconService is not properly declared in AndroidManifest.xml. If using Eclipse," +
" please verify that your project.properties has manifestmerger.enabled=true");
}
}
/**
* Determines if Android L Scanning is disabled by user selection
*
* @return
*/
public static boolean isAndroidLScanningDisabled() {
return sAndroidLScanningDisabled;
}
/**
* Allows disabling use of Android L BLE Scanning APIs on devices with API 21+
* If set to false (default), devices with API 21+ will use the Android L APIs to
* scan for beacons
*
* @param disabled
*/
public static void setAndroidLScanningDisabled(boolean disabled) {
sAndroidLScanningDisabled = disabled;
}
/**
* Allows disabling check of manifest for proper configuration of service. Useful for unit
* testing
*
* @param disabled
*/