Skip to content

Commit

Permalink
🗃️ Save the BLE callbacks as sensor data
Browse files Browse the repository at this point in the history
Consistent with
e-mission/e-mission-docs#1062 (comment)
store the BLE scan results so that we can use them while matching the mapping

While storing the changes, I realized that the region enter and exit calls
returned a CLBeaconRegion object and the range scans returned a CLRegion
object.
e-mission#220 (comment)

This needed us to change the wrapper class as well:
- Add a new initFromCLBeaconRegion that only copies non-range values and sets
  default values for the range values (proximity, accuracy and rssi)
- Change the existing initFromCLBeacon to not need an event type, since it is
  only called for the scans, and the event type is always `RANGE_UPDATE` in
  that case.

Testing done:
- Compiled
- Launched the app
- Note that we cannot test this code without a physical beacon, but it is very
  straightforward and compiles. The only reason why it may fail is if values
  are not filled out in unexpected ways.
  • Loading branch information
shankari committed Apr 15, 2024
1 parent 752e415 commit 3e9383e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
17 changes: 16 additions & 1 deletion src/ios/Location/TripDiaryDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import "TripDiaryActions.h"
#import "LocalNotificationManager.h"
#import "SimpleLocation.h"
#import "BluetoothBLE.h"
#import "LocationTrackingConfig.h"
#import "ConfigManager.h"
#import "BEMAppDelegate.h"
Expand Down Expand Up @@ -136,6 +137,10 @@ - (void)locationManager:(CLLocationManager *)manager
}
// FOR FLEET VERSION: Check BLE Region exit
if([region.identifier compare:OpenPATHBeaconIdentifier] == NSOrderedSame) {
// Save the exit data before stopping ranging and generating event
BluetoothBLE* currBeaconRegion = [[BluetoothBLE alloc] initWithCLBeaconRegion:(CLBeaconRegion*) region andEventType:@"REGION_EXIT"];
[[BuiltinUserCache database] putSensorData:@"key.usercache.bluetooth_ble" value:currBeaconRegion];

NSUUID *UUID = [[NSUUID alloc] initWithUUIDString:OpenPATHBeaconUUID];
CLBeaconIdentityConstraint *constraint = [[CLBeaconIdentityConstraint alloc] initWithUUID:UUID];

Expand Down Expand Up @@ -323,7 +328,12 @@ - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
// We only monitor the entrance of BLE Regions...
if([region.identifier compare:OpenPATHBeaconIdentifier] == NSOrderedSame) {
// Save the entry data before starting ranging and generating event
// TODO: unify the checks here and in the exit region. Why are we checking for isKindOFClass here but not in the exit code?
if ([region isKindOfClass:[CLBeaconRegion class]]) {
BluetoothBLE* currBeaconRegion = [[BluetoothBLE alloc] initWithCLBeaconRegion:(CLBeaconRegion*) region andEventType:@"REGION_ENTER"];
[[BuiltinUserCache database] putSensorData:@"key.usercache.bluetooth_ble" value:currBeaconRegion];

if ([CLLocationManager isRangingAvailable]) {
NSUUID *UUID = [[NSUUID alloc] initWithUUIDString:OpenPATHBeaconUUID];
CLBeaconIdentityConstraint *constraint = [[CLBeaconIdentityConstraint alloc] initWithUUID:UUID];
Expand Down Expand Up @@ -351,7 +361,12 @@ - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)
- (void)locationManager:(CLLocationManager *)manager
didRangeBeacons:(NSArray<CLBeacon *> *)beacons
satisfyingConstraint:(CLBeaconIdentityConstraint *)beaconConstraint {


for (int i = 0; i < beacons.count; i++) {
BluetoothBLE* currBeaconRegion = [[BluetoothBLE alloc] initWithCLBeacon:beacons[i]];
[[BuiltinUserCache database] putSensorData:@"key.usercache.bluetooth_ble" value:currBeaconRegion];
}

[[NSNotificationCenter defaultCenter] postNotificationName:CFCTransitionNotificationName
object:CFCTransitionBeaconFound];

Expand Down
3 changes: 2 additions & 1 deletion src/ios/Wrapper/BluetoothBLE.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

@interface BluetoothBLE : NSObject

- (instancetype)initWithCLBeacon:(CLBeacon *)beacon andEventType:(NSString *) eventType;
- (instancetype)initWithCLBeaconRegion:(CLBeaconRegion*) beaconRegion andEventType:(NSString*) eventType;
- (instancetype)initWithCLBeacon:(CLBeacon *)beacon;
- (instancetype)initFake:(NSString *)eventType anduuid:(NSString*) uuid andmajor:(int) major andminor:(int) minor;

// fields from CLBeacon, modified to be easy to serialize and restore
Expand Down
21 changes: 18 additions & 3 deletions src/ios/Wrapper/BluetoothBLE.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,31 @@

@implementation BluetoothBLE

-(id) initWithCLBeacon:(CLBeacon*) beacon andEventType:(NSString*) eventType {
-(id) initWithCLBeaconRegion:(CLBeaconRegion*) beaconRegion andEventType:(NSString*) eventType {
assert([eventType isEqualToString:@"REGION_ENTER"] || [eventType isEqualToString:@"REGION_EXIT"]);
self = [super init];
self.uuid = beacon.UUID;
self.uuid = beaconRegion.UUID.UUIDString;
self.major = beaconRegion.major.integerValue;
self.minor = beaconRegion.minor.integerValue;
self.proximity = [BluetoothBLE proximityToString:CLProximityUnknown];
self.accuracy = -1;
self.rssi = -1;

self.eventType = eventType;
self.ts = [DataUtils dateToTs:[NSDate now]];
return self;
}

-(id) initWithCLBeacon:(CLBeacon*) beacon {
self = [super init];
self.uuid = beacon.UUID.UUIDString;
self.major = beacon.major.integerValue;
self.minor = beacon.minor.integerValue;
self.proximity = [BluetoothBLE proximityToString:beacon.proximity];
self.accuracy = beacon.accuracy;
self.rssi = beacon.rssi;

self.eventType = eventType;
self.eventType = @"RANGE_UPDATE"; // we only get CLBeacon objects on range updates
self.ts = [DataUtils dateToTs:beacon.timestamp];
return self;
}
Expand Down

0 comments on commit 3e9383e

Please sign in to comment.