diff --git a/README.md b/README.md index 3bf0319..a6ba9bf 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,8 @@ If you get stuck, take a look at [Brent Vatne's blog](http://brentvatne.ca/packa var { Accelerometer, Gyroscope, - Magnetometer + Magnetometer, + Proximity } = require('NativeModules'); var { DeviceEventEmitter // will emit events that you can listen to @@ -72,6 +73,18 @@ Magnetometer.startMagnetometerUpdates(); // you'll start getting AccelerationDat Magnetometer.stopMagnetometerUpdates(); ``` +### Proximity +```js +// no need to set UpdateInterval, will fire event when detected changes. +DeviceEventEmitter.addListener('Proximity', function (data) { + /** + * data.isNear + **/ +}); +Proximity.startProximityUpdates(); // you'll start getting Proximity events above +Proximity.stopProximityUpdates(); +``` + # Example This repo contains an example react-native app to help get you started. [Source code here.](https://github.com/pwmckenna/react-native-motion-manager/tree/master/Example/MotionExample) diff --git a/RNMotionManager/Proximity.h b/RNMotionManager/Proximity.h new file mode 100644 index 0000000..066e0e3 --- /dev/null +++ b/RNMotionManager/Proximity.h @@ -0,0 +1,17 @@ +// +// Proximity.h +// +// Created by zxcpoiu. +// + +#import "RCTBridgeModule.h" +#import + +@interface Proximity : NSObject { + UIDevice *_currentDevice; +} +- (void) getProximityData:(RCTResponseSenderBlock) cb; +- (void) startProximityUpdates; +- (void) stopProximityUpdates; + +@end diff --git a/RNMotionManager/Proximity.m b/RNMotionManager/Proximity.m new file mode 100644 index 0000000..33d2ce2 --- /dev/null +++ b/RNMotionManager/Proximity.m @@ -0,0 +1,76 @@ +// +// Proximity.m +// +// Created by zxcpoiu. +// + +#import "RCTBridge.h" +#import "RCTEventDispatcher.h" +#import "Proximity.h" + +@implementation Proximity + +@synthesize bridge = _bridge; + +RCT_EXPORT_MODULE(); + +- (id) init { + self = [super init]; + NSLog(@"Proximity"); + + if (self) { + + self->_currentDevice = [UIDevice currentDevice]; + [self->_currentDevice setProximityMonitoringEnabled:YES]; + + if ([self->_currentDevice isProximityMonitoringEnabled] == YES) + { + NSLog(@"Proximity available"); + } + else + { + NSLog(@"Proximity not Available!"); + } + [self->_currentDevice setProximityMonitoringEnabled:NO]; + } + return self; +} + +RCT_EXPORT_METHOD(getProximityData:(RCTResponseSenderBlock) cb) { + BOOL state = self->_currentDevice.proximityState; + + NSLog(@"getProximityData: %@", (state) ? @"YES" : @"NO"); + + cb(@[[NSNull null], @{ + @"isNear" : [NSNumber numberWithBool:state] + }] + ); +} + +RCT_EXPORT_METHOD(startProximityUpdates) { + NSLog(@"startProximityUpdates"); + [self->_currentDevice setProximityMonitoringEnabled:YES]; + [self->_currentDevice addObserver:self forKeyPath:@"proximityState" options:NSKeyValueObservingOptionNew context:nil]; +} + +RCT_EXPORT_METHOD(stopProximityUpdates) { + NSLog(@"stopProximityUpdates"); + [self->_currentDevice setProximityMonitoringEnabled:NO]; + [self->_currentDevice removeObserver:self forKeyPath:@"proximityState"]; +} + +- (void)observeValueForKeyPath:(NSString *)keyPath + ofObject:(id)object + change:(NSDictionary *)change + context:(void *)context +{ + if ([keyPath isEqualToString:@"proximityState"]) { + BOOL state = self->_currentDevice.proximityState; + [self.bridge.eventDispatcher sendDeviceEventWithName:@"Proximity" body:@{ + @"isNear" : [NSNumber numberWithBool:state] + }]; + + } +} + +@end diff --git a/index.js b/index.js index 8bcb9ed..15fc277 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,3 @@ -var { Accelerometer, Gyroscope, Magnetometer } = require('react-native').NativeModules; +var { Accelerometer, Gyroscope, Magnetometer, Proximity } = require('react-native').NativeModules; -module.exports = { Accelerometer, Gyroscope, Magnetometer }; +module.exports = { Accelerometer, Gyroscope, Magnetometer, Proximity };