// // BeaconDetector.m // WMWallet // // Created by Friedrich Ewald on 18.02.16. // Copyright © 2016 fewald. All rights reserved. // #import #import "BeaconDetector.h" @interface BeaconDetector () @property (strong, nonatomic) CLBeaconRegion *region; @property (strong, nonatomic) NSTimer *timer; @end @implementation BeaconDetector - (id) init { self = [super init]; if (self != nil) { // additional initialization NSUUID *beaconUUID = [[NSUUID alloc] initWithUUIDString:@"2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6"]; self.region = [[CLBeaconRegion alloc] initWithProximityUUID:beaconUUID major:0 minor:1 identifier:@"test"]; self.region.notifyOnEntry = YES; self.region.notifyOnExit = YES; self.region.notifyEntryStateOnDisplay = YES; self.locationManager = [[CLLocationManager alloc] init]; //self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; self.locationManager.delegate = self; NSLog(@"init"); } return self; } - (void) startRangingBeacon { NSLog(@"Starting ranging beacon"); [self.locationManager startMonitoringForRegion:self.region]; [self.locationManager startRangingBeaconsInRegion:self.region]; } - (void) stopRangingBeacon { NSLog(@"Stopping ranging beacon"); [self.locationManager stopRangingBeaconsInRegion:self.region]; } #pragma mark - CLLocationManagerDelegate - (void) locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region { NSLog(@"Started monitoring region"); [self.locationManager requestStateForRegion:self.region]; } - (void) locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region { NSLog(@"did determine state"); if (state == CLRegionStateInside) { NSLog(@"Found beacon"); if (self.timer != nil) { NSLog(@"Invalidating timer"); [self.timer invalidate]; self.timer = nil; } } else if (state == CLRegionStateOutside) { NSLog(@"Lost beacon"); [self notification]; } else if (state == CLRegionStateUnknown) { NSLog(@"State unknown"); } } - (void) locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error { NSLog(@"error"); } - (void) locationManager:(CLLocationManager *)manager rangingBeaconsDidFailForRegion:(CLBeaconRegion *)region withError:(NSError *)error { NSLog(@"error"); } - (void) locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region { //NSLog(@"ranging"); } - (void) notification { NSLog(@"Timer fired"); // Create a local notification UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.fireDate = [NSDate date]; notification.alertBody = @"Lost the connection to the wallet."; notification.timeZone = [NSTimeZone defaultTimeZone]; notification.soundName = UILocalNotificationDefaultSoundName; //notification.applicationIconBadgeNumber = 1; [[UIApplication sharedApplication] scheduleLocalNotification:notification]; } @end