diff --git a/src/ios/Location/TripDiaryDelegate.m b/src/ios/Location/TripDiaryDelegate.m index a6038fd..3c40c46 100644 --- a/src/ios/Location/TripDiaryDelegate.m +++ b/src/ios/Location/TripDiaryDelegate.m @@ -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" @@ -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]; @@ -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]; @@ -351,7 +361,12 @@ - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *) - (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)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]; diff --git a/src/ios/Wrapper/BluetoothBLE.h b/src/ios/Wrapper/BluetoothBLE.h index e4b1d8b..e483475 100644 --- a/src/ios/Wrapper/BluetoothBLE.h +++ b/src/ios/Wrapper/BluetoothBLE.h @@ -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 diff --git a/src/ios/Wrapper/BluetoothBLE.m b/src/ios/Wrapper/BluetoothBLE.m index 2de61cf..e9ae8cb 100644 --- a/src/ios/Wrapper/BluetoothBLE.m +++ b/src/ios/Wrapper/BluetoothBLE.m @@ -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; }