Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add proximity support #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 };