|
| 1 | +// |
| 2 | +// Proximity.m |
| 3 | +// |
| 4 | +// Created by zxcpoiu. |
| 5 | +// |
| 6 | + |
| 7 | +#import "RCTBridge.h" |
| 8 | +#import "RCTEventDispatcher.h" |
| 9 | +#import "Proximity.h" |
| 10 | + |
| 11 | +@implementation Proximity |
| 12 | + |
| 13 | +@synthesize bridge = _bridge; |
| 14 | + |
| 15 | +RCT_EXPORT_MODULE(); |
| 16 | + |
| 17 | +- (id) init { |
| 18 | + self = [super init]; |
| 19 | + NSLog(@"Proximity"); |
| 20 | + |
| 21 | + if (self) { |
| 22 | + |
| 23 | + self->_currentDevice = [UIDevice currentDevice]; |
| 24 | + [self->_currentDevice setProximityMonitoringEnabled:YES]; |
| 25 | + |
| 26 | + if ([self->_currentDevice isProximityMonitoringEnabled] == YES) |
| 27 | + { |
| 28 | + NSLog(@"Proximity available"); |
| 29 | + } |
| 30 | + else |
| 31 | + { |
| 32 | + NSLog(@"Proximity not Available!"); |
| 33 | + } |
| 34 | + [self->_currentDevice setProximityMonitoringEnabled:NO]; |
| 35 | + } |
| 36 | + return self; |
| 37 | +} |
| 38 | + |
| 39 | +RCT_EXPORT_METHOD(getProximityData:(RCTResponseSenderBlock) cb) { |
| 40 | + BOOL state = self->_currentDevice.proximityState; |
| 41 | + |
| 42 | + NSLog(@"getProximityData: %@", (state) ? @"YES" : @"NO"); |
| 43 | + |
| 44 | + cb(@[[NSNull null], @{ |
| 45 | + @"isNear" : [NSNumber numberWithBool:state] |
| 46 | + }] |
| 47 | + ); |
| 48 | +} |
| 49 | + |
| 50 | +RCT_EXPORT_METHOD(startProximityUpdates) { |
| 51 | + NSLog(@"startProximityUpdates"); |
| 52 | + [self->_currentDevice setProximityMonitoringEnabled:YES]; |
| 53 | + [self->_currentDevice addObserver:self forKeyPath:@"proximityState" options:NSKeyValueObservingOptionNew context:nil]; |
| 54 | +} |
| 55 | + |
| 56 | +RCT_EXPORT_METHOD(stopProximityUpdates) { |
| 57 | + NSLog(@"stopProximityUpdates"); |
| 58 | + [self->_currentDevice setProximityMonitoringEnabled:NO]; |
| 59 | + [self->_currentDevice removeObserver:self forKeyPath:@"proximityState"]; |
| 60 | +} |
| 61 | + |
| 62 | +- (void)observeValueForKeyPath:(NSString *)keyPath |
| 63 | + ofObject:(id)object |
| 64 | + change:(NSDictionary *)change |
| 65 | + context:(void *)context |
| 66 | +{ |
| 67 | + if ([keyPath isEqualToString:@"proximityState"]) { |
| 68 | + BOOL state = self->_currentDevice.proximityState; |
| 69 | + [self.bridge.eventDispatcher sendDeviceEventWithName:@"Proximity" body:@{ |
| 70 | + @"isNear" : [NSNumber numberWithBool:state] |
| 71 | + }]; |
| 72 | + |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +@end |
0 commit comments