-
Notifications
You must be signed in to change notification settings - Fork 54
/
RNBeacon.m
253 lines (194 loc) · 8.11 KB
/
RNBeacon.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//
// 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;
@property (assign, nonatomic) BOOL dropEmptyRanges;
@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;
self.dropEmptyRanges = 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(getAuthorizationStatus:(RCTResponseSenderBlock)callback)
{
callback(@[[self nameForAuthorizationStatus:[CLLocationManager authorizationStatus]]]);
}
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];
}
RCT_EXPORT_METHOD(stopUpdatingLocation)
{
[self.locationManager stopUpdatingLocation];
}
RCT_EXPORT_METHOD(shouldDropEmptyRanges:(BOOL)drop)
{
self.dropEmptyRanges = drop;
}
-(NSString *)nameForAuthorizationStatus:(CLAuthorizationStatus)authorizationStatus
{
switch (authorizationStatus) {
case kCLAuthorizationStatusAuthorizedAlways:
return @"authorizedAlways";
case kCLAuthorizationStatusAuthorizedWhenInUse:
return @"authorizedWhenInUse";
case kCLAuthorizationStatusDenied:
return @"denied";
case kCLAuthorizationStatusNotDetermined:
return @"notDetermined";
case kCLAuthorizationStatusRestricted:
return @"restricted";
}
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
NSString *statusName = [self nameForAuthorizationStatus:status];
[self.bridge.eventDispatcher sendDeviceEventWithName:@"authorizationStatusDidChange" body:statusName];
}
- (void)locationManager:(CLLocationManager *)manager rangingBeaconsDidFailForRegion:(CLBeaconRegion *)region withError:(NSError *)error
{
NSLog(@"Failed ranging region: %@", error);
}
- (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
{
if (self.dropEmptyRanges && beacons.count == 0) {
return;
}
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:(CLBeaconRegion *)region {
NSDictionary *event = @{
@"region": region.identifier,
@"uuid": [region.proximityUUID UUIDString],
};
[self.bridge.eventDispatcher sendDeviceEventWithName:@"regionDidEnter" body:event];
}
-(void)locationManager:(CLLocationManager *)manager
didExitRegion:(CLBeaconRegion *)region {
NSDictionary *event = @{
@"region": region.identifier,
@"uuid": [region.proximityUUID UUIDString],
};
[self.bridge.eventDispatcher sendDeviceEventWithName:@"regionDidExit" body:event];
}
@end