Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Johannes Stein
committed
Apr 21, 2015
1 parent
ff6861b
commit b0e8ab3
Showing
16 changed files
with
314 additions
and
1,053 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Stub of RNBeacon for Android. | ||
* | ||
* @providesModule RNBeacon | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
var warning = require('warning'); | ||
|
||
var RNBeacon = { | ||
test: function() { | ||
warning("Not yet implemented for Android."); | ||
} | ||
}; | ||
|
||
module.exports = RNBeacon; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* @providesModule RNBeacon | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
var NativeRNBeacon = require('NativeModules').RNBeacon; | ||
|
||
module.exports = RNBeacon; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
// | ||
// RNBeacon.m | ||
// RNBeacon | ||
// | ||
// Created by Johannes Stein on 20.04.15. | ||
// Copyright (c) 2015 Geniux Consulting. All rights reserved. | ||
// | ||
|
||
#import <CoreLocation/CoreLocation.h> | ||
|
||
#import "RCTBridge.h" | ||
#import "RCTConvert.h" | ||
#import "RCTEventDispatcher.h" | ||
|
||
#import "RNBeacon.h" | ||
|
||
@interface RNBeacon() <CLLocationManagerDelegate> | ||
|
||
@property (strong, nonatomic) CLLocationManager *locationManager; | ||
|
||
@end | ||
|
||
@implementation RNBeacon | ||
|
||
RCT_EXPORT_MODULE() | ||
|
||
@synthesize bridge = _bridge; | ||
|
||
#pragma mark Initialization | ||
|
||
- (instancetype)init | ||
{ | ||
if (self = [super init]) { | ||
self.locationManager = [[CLLocationManager alloc] init]; | ||
|
||
self.locationManager.delegate = self; | ||
self.locationManager.pausesLocationUpdatesAutomatically = NO; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
#pragma mark | ||
|
||
- (CLBeaconRegion *) createBeaconRegion: (NSString *) identifier uuid: (NSString *) uuid major: (NSInteger) major minor:(NSInteger) minor | ||
{ | ||
NSUUID *beaconUUID = [[NSUUID alloc] initWithUUIDString:uuid]; | ||
|
||
unsigned short mj = (unsigned short) major; | ||
unsigned short mi = (unsigned short) minor; | ||
|
||
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:beaconUUID major:mj minor:mi identifier:identifier]; | ||
|
||
beaconRegion.notifyEntryStateOnDisplay = YES; | ||
|
||
return beaconRegion; | ||
} | ||
|
||
- (CLBeaconRegion *) createBeaconRegion: (NSString *) identifier uuid: (NSString *) uuid major: (NSInteger) major | ||
{ | ||
NSUUID *beaconUUID = [[NSUUID alloc] initWithUUIDString:uuid]; | ||
|
||
unsigned short mj = (unsigned short) major; | ||
|
||
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:beaconUUID major:mj identifier:identifier]; | ||
|
||
beaconRegion.notifyEntryStateOnDisplay = YES; | ||
|
||
return beaconRegion; | ||
} | ||
|
||
- (CLBeaconRegion *) createBeaconRegion: (NSString *) identifier uuid: (NSString *) uuid | ||
{ | ||
NSUUID *beaconUUID = [[NSUUID alloc] initWithUUIDString:uuid]; | ||
|
||
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:beaconUUID identifier:identifier]; | ||
|
||
beaconRegion.notifyEntryStateOnDisplay = YES; | ||
|
||
return beaconRegion; | ||
} | ||
|
||
- (CLBeaconRegion *) convertDictToBeaconRegion: (NSDictionary *) dict | ||
{ | ||
if (dict[@"minor"] == nil) { | ||
if (dict[@"major"] == nil) { | ||
return [self createBeaconRegion:[RCTConvert NSString:dict[@"identifier"]] uuid:[RCTConvert NSString:dict[@"uuid"]]]; | ||
} else { | ||
return [self createBeaconRegion:[RCTConvert NSString:dict[@"identifier"]] uuid:[RCTConvert NSString:dict[@"uuid"]] major:[RCTConvert NSInteger:dict[@"major"]]]; | ||
} | ||
} else { | ||
return [self createBeaconRegion:[RCTConvert NSString:dict[@"identifier"]] uuid:[RCTConvert NSString:dict[@"uuid"]] major:[RCTConvert NSInteger:dict[@"major"]] minor:[RCTConvert NSInteger:dict[@"minor"]]]; | ||
} | ||
} | ||
|
||
- (NSString *)stringForProximity:(CLProximity)proximity { | ||
switch (proximity) { | ||
case CLProximityUnknown: return @"unknown"; | ||
case CLProximityFar: return @"far"; | ||
case CLProximityNear: return @"near"; | ||
case CLProximityImmediate: return @"immediate"; | ||
default: | ||
return @""; | ||
} | ||
} | ||
|
||
RCT_EXPORT_METHOD(requestAlwaysAuthorization) | ||
{ | ||
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { | ||
[self.locationManager requestAlwaysAuthorization]; | ||
} | ||
} | ||
|
||
RCT_EXPORT_METHOD(requestWhenInUseAuthorization) | ||
{ | ||
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { | ||
[self.locationManager requestWhenInUseAuthorization]; | ||
} | ||
} | ||
|
||
RCT_EXPORT_METHOD(startMonitoringForRegion:(NSDictionary *) dict) | ||
{ | ||
[self.locationManager startMonitoringForRegion:[self convertDictToBeaconRegion:dict]]; | ||
} | ||
|
||
RCT_EXPORT_METHOD(startRangingBeaconsInRegion:(NSDictionary *) dict) | ||
{ | ||
[self.locationManager startRangingBeaconsInRegion:[self convertDictToBeaconRegion:dict]]; | ||
} | ||
|
||
RCT_EXPORT_METHOD(stopMonitoringForRegion:(NSDictionary *) dict) | ||
{ | ||
[self.locationManager stopMonitoringForRegion:[self convertDictToBeaconRegion:dict]]; | ||
} | ||
|
||
RCT_EXPORT_METHOD(stopRangingBeaconsInRegion:(NSDictionary *) dict) | ||
{ | ||
[self.locationManager stopRangingBeaconsInRegion:[self convertDictToBeaconRegion:dict]]; | ||
} | ||
|
||
RCT_EXPORT_METHOD(startUpdatingLocation) | ||
{ | ||
[self.locationManager startUpdatingLocation]; | ||
} | ||
|
||
|
||
- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error { | ||
NSLog(@"Failed monitoring region: %@", error); | ||
} | ||
|
||
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { | ||
NSLog(@"Location manager failed: %@", error); | ||
} | ||
|
||
-(void) locationManager:(CLLocationManager *)manager didRangeBeacons: | ||
(NSArray *)beacons inRegion:(CLBeaconRegion *)region | ||
{ | ||
NSMutableArray *beaconArray = [[NSMutableArray alloc] init]; | ||
|
||
for (CLBeacon *beacon in beacons) { | ||
[beaconArray addObject:@{ | ||
@"uuid": [beacon.proximityUUID UUIDString], | ||
@"major": beacon.major, | ||
@"minor": beacon.minor, | ||
|
||
@"rssi": [NSNumber numberWithLong:beacon.rssi], | ||
@"proximity": [self stringForProximity: beacon.proximity], | ||
@"accuracy": [NSNumber numberWithDouble: beacon.accuracy] | ||
}]; | ||
} | ||
|
||
NSDictionary *event = @{ | ||
@"region": @{ | ||
@"identifier": region.identifier, | ||
@"uuid": [region.proximityUUID UUIDString], | ||
}, | ||
@"beacons": beaconArray | ||
}; | ||
|
||
[self.bridge.eventDispatcher sendDeviceEventWithName:@"beaconsDidRange" body:event]; | ||
} | ||
|
||
-(void)locationManager:(CLLocationManager *)manager | ||
didEnterRegion:(CLRegion *)region { | ||
NSDictionary *event = @{ | ||
@"region": region.identifier, | ||
}; | ||
|
||
[self.bridge.eventDispatcher sendDeviceEventWithName:@"regionDidEnter" body:event]; | ||
} | ||
|
||
-(void)locationManager:(CLLocationManager *)manager | ||
didExitRegion:(CLRegion *)region { | ||
NSDictionary *event = @{ | ||
@"region": region.identifier, | ||
}; | ||
|
||
[self.bridge.eventDispatcher sendDeviceEventWithName:@"regionDidExit" body:event]; | ||
} | ||
|
||
@end |
Oops, something went wrong.