Skip to content

Commit

Permalink
try to fix stopping and starting of monitoring same beacon regions
Browse files Browse the repository at this point in the history
  • Loading branch information
lmeier committed Sep 21, 2023
1 parent d7dbfff commit ed845a4
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 4 deletions.
2 changes: 1 addition & 1 deletion RadarSDK.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'RadarSDK'
s.version = '3.8.9-beta.2'
s.version = '3.8.9-beta.3'
s.summary = 'iOS SDK for Radar, the leading geofencing and location tracking platform'
s.homepage = 'https://radar.com'
s.author = { 'Radar Labs, Inc.' => '[email protected]' }
Expand Down
2 changes: 2 additions & 0 deletions RadarSDK/RadarLocationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ NS_ASSUME_NONNULL_BEGIN
- (void)stopTracking;
- (void)replaceSyncedGeofences:(NSArray<RadarGeofence *> *)geofences;
- (void)replaceSyncedBeacons:(NSArray<RadarBeacon *> *)beacons;
- (void)updateSyncedBeacons:(NSArray<RadarBeacon *> *)beacons;
- (void)updateSyncedBeaconUUIDs:(NSArray<NSString *> *)uuids;
- (void)replaceSyncedBeaconUUIDs:(NSArray<NSString *> *)uuids;
- (void)updateTracking;
- (void)updateTrackingFromMeta:(RadarMeta *_Nullable)meta;
Expand Down
124 changes: 122 additions & 2 deletions RadarSDK/RadarLocationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,126 @@ - (void)removePendingNotificationsWithCompletionHandler:(void (^)(void))completi
}];
}

- (void)updateSyncedBeacons:(NSArray<RadarBeacon *> *)newBeacons {
BOOL tracking = [RadarSettings tracking];
RadarTrackingOptions *options = [Radar getTrackingOptions];
if (!tracking || !options.beacons || !newBeacons) {
[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelDebug message:@"Skipping updating synced beacons"];
return;
}

// Convert new beacons to set for faster lookup
NSMutableSet *newBeaconIdentifiers = [NSMutableSet set];
for (RadarBeacon *beacon in newBeacons) {
NSString *identifier = [NSString stringWithFormat:@"%@%@", kSyncBeaconIdentifierPrefix, beacon._id];
[newBeaconIdentifiers addObject:identifier];
}

// Determine current beacons being monitored
NSMutableSet *currentBeaconIdentifiers = [NSMutableSet set];
for (CLRegion *region in self.locationManager.monitoredRegions) {
if ([region isKindOfClass:[CLBeaconRegion class]] && [region.identifier hasPrefix:kSyncBeaconIdentifierPrefix]) {
[currentBeaconIdentifiers addObject:region.identifier];
}
}

// Stop monitoring beacons that are not in the new list
for (NSString *identifier in currentBeaconIdentifiers) {
if (![newBeaconIdentifiers containsObject:identifier]) {
CLRegion *regionToRemove = nil;
for (CLRegion *region in self.locationManager.monitoredRegions) {
if ([region.identifier isEqualToString:identifier]) {
regionToRemove = region;
break;
}
}
if (regionToRemove) {
[self.locationManager stopMonitoringForRegion:regionToRemove];
}
}
}

// Start monitoring new beacons not currently being monitored
NSUInteger numBeacons = MIN(newBeacons.count, 9);
for (int i = 0; i < numBeacons; i++) {
RadarBeacon *beacon = [newBeacons objectAtIndex:i];
NSString *identifier = [NSString stringWithFormat:@"%@%@", kSyncBeaconIdentifierPrefix, beacon._id];
if (![currentBeaconIdentifiers containsObject:identifier]) {
CLBeaconRegion *region = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:beacon.uuid]
major:[beacon.major intValue]
minor:[beacon.minor intValue]
identifier:identifier];

if (region) {
region.notifyEntryStateOnDisplay = YES;
[self.locationManager startMonitoringForRegion:region];
[self.locationManager requestStateForRegion:region];

[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelDebug
message:[NSString stringWithFormat:@"Synced beacon | identifier = %@; uuid = %@; major = %@; minor = %@", identifier, beacon.uuid,
beacon.major, beacon.minor]];
} else {
[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelDebug
message:[NSString stringWithFormat:@"Error syncing beacon | identifier = %@; uuid = %@; major = %@; minor = %@", identifier,
beacon.uuid, beacon.major, beacon.minor]];
}
}
}
}

- (void)updateSyncedBeaconUUIDs:(NSArray<NSString *> *)uuids {
NSMutableArray<NSString *> *existingUUIDs = [NSMutableArray array];
for (CLRegion *region in self.locationManager.monitoredRegions) {
if ([region.identifier hasPrefix:kSyncBeaconUUIDIdentifierPrefix]) {
NSString *existingUUID = [(CLBeaconRegion *)region proximityUUID].UUIDString;
[existingUUIDs addObject:existingUUID];
}
}

BOOL tracking = [RadarSettings tracking];
RadarTrackingOptions *options = [Radar getTrackingOptions];
if (!tracking || !options.beacons || !uuids) {
return;
}

NSUInteger numUUIDs = MIN(uuids.count, 9);

for (int i = 0; i < numUUIDs; i++) {
NSString *uuid = uuids[i];

// If UUID is already being monitored, skip
if ([existingUUIDs containsObject:uuid]) {
continue;
}

NSString *identifier = [NSString stringWithFormat:@"%@%@", kSyncBeaconUUIDIdentifierPrefix, uuid];
CLBeaconRegion *region = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:uuid] identifier:identifier];

if (region) {
region.notifyEntryStateOnDisplay = YES;
[self.locationManager startMonitoringForRegion:region];
[self.locationManager requestStateForRegion:region];

[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelDebug message:[NSString stringWithFormat:@"Synced UUID | identifier = %@; uuid = %@", identifier, uuid]];
} else {
[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelDebug message:[NSString stringWithFormat:@"Error syncing UUID | identifier = %@; uuid = %@", identifier, uuid]];
}
}

// Remove any old UUIDs that aren't in the new list
for (NSString *existingUUID in existingUUIDs) {
if (![uuids containsObject:existingUUID]) {
NSString *identifierToRemove = [NSString stringWithFormat:@"%@%@", kSyncBeaconUUIDIdentifierPrefix, existingUUID];
for (CLRegion *region in self.locationManager.monitoredRegions) {
if ([region.identifier isEqualToString:identifierToRemove]) {
[self.locationManager stopMonitoringForRegion:region];
}
}
}
}
}


- (void)replaceSyncedBeacons:(NSArray<RadarBeacon *> *)beacons {
[self removeSyncedBeacons];

Expand Down Expand Up @@ -838,9 +958,9 @@ - (void)sendLocation:(CLLocation *)location stopped:(BOOL)stopped source:(RadarL
limit:10
completionHandler:^(RadarStatus status, NSDictionary *_Nullable res, NSArray<RadarBeacon *> *_Nullable beacons, NSArray<NSString *> *_Nullable beaconUUIDs) {
if (beaconUUIDs && beaconUUIDs.count) {
[self replaceSyncedBeaconUUIDs:beaconUUIDs];
[self updateSyncedBeaconUUIDs:beaconUUIDs];
} else if (beacons && beacons.count) {
[self replaceSyncedBeacons:beacons];
[self updateSyncedBeacons:beacons];
}
}];
}
Expand Down
2 changes: 1 addition & 1 deletion RadarSDK/RadarUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ + (NSNumber *)timeZoneOffset {
}

+ (NSString *)sdkVersion {
return @"3.8.9-beta.2";
return @"3.8.9-beta.3";
}

+ (NSString *)deviceId {
Expand Down

0 comments on commit ed845a4

Please sign in to comment.