Skip to content

Instantly share code, notes, and snippets.

@f-ewald
Created February 19, 2016 18:03
Show Gist options
  • Save f-ewald/c329e744facee21d61ac to your computer and use it in GitHub Desktop.
Save f-ewald/c329e744facee21d61ac to your computer and use it in GitHub Desktop.
Beacon Detector
//
// BeaconDetector.h
// WMWallet
//
// Created by Friedrich Ewald on 18.02.16.
// Copyright © 2016 fewald. All rights reserved.
//
#import <Foundation/Foundation.h>
@import CoreLocation;
@interface BeaconDetector : NSObject <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
/**
* Starts ranging the beacon. In the foreground and background.
*/
- (void) startRangingBeacon;
/**
* Stops ranging the beacon, in the foreground and background.
*/
- (void) stopRangingBeacon;
@end
@protocol BeaconDetectorDelegate <NSObject>
- (void) foundBeacon;
- (void) lostBeacon;
@end
//
// BeaconDetector.m
// WMWallet
//
// Created by Friedrich Ewald on 18.02.16.
// Copyright © 2016 fewald. All rights reserved.
//
#import <UIKit/UIKit.h>
#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<CLBeacon *> *)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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment