Skip to content

Commit

Permalink
add proximity support
Browse files Browse the repository at this point in the history
this sensor doesn't need to set UpdateInterval.
will fire event when detected changes.
isNear: true or false
  • Loading branch information
zxcpoiu committed Apr 28, 2016
1 parent 40e2f11 commit c256e1b
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 3 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions RNMotionManager/Proximity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Proximity.h
//
// Created by zxcpoiu.
//

#import "RCTBridgeModule.h"
#import <UIKit/UIKit.h>

@interface Proximity : NSObject <RCTBridgeModule> {
UIDevice *_currentDevice;
}
- (void) getProximityData:(RCTResponseSenderBlock) cb;
- (void) startProximityUpdates;
- (void) stopProximityUpdates;

@end
76 changes: 76 additions & 0 deletions RNMotionManager/Proximity.m
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -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 };

0 comments on commit c256e1b

Please sign in to comment.