Skip to content

Commit c256e1b

Browse files
committed
add proximity support
this sensor doesn't need to set UpdateInterval. will fire event when detected changes. isNear: true or false
1 parent 40e2f11 commit c256e1b

4 files changed

Lines changed: 109 additions & 3 deletions

File tree

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ If you get stuck, take a look at [Brent Vatne's blog](http://brentvatne.ca/packa
2222
var {
2323
Accelerometer,
2424
Gyroscope,
25-
Magnetometer
25+
Magnetometer,
26+
Proximity
2627
} = require('NativeModules');
2728
var {
2829
DeviceEventEmitter // will emit events that you can listen to
@@ -72,6 +73,18 @@ Magnetometer.startMagnetometerUpdates(); // you'll start getting AccelerationDat
7273
Magnetometer.stopMagnetometerUpdates();
7374
```
7475

76+
### Proximity
77+
```js
78+
// no need to set UpdateInterval, will fire event when detected changes.
79+
DeviceEventEmitter.addListener('Proximity', function (data) {
80+
/**
81+
* data.isNear
82+
**/
83+
});
84+
Proximity.startProximityUpdates(); // you'll start getting Proximity events above
85+
Proximity.stopProximityUpdates();
86+
```
87+
7588
# Example
7689

7790
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)

RNMotionManager/Proximity.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// Proximity.h
3+
//
4+
// Created by zxcpoiu.
5+
//
6+
7+
#import "RCTBridgeModule.h"
8+
#import <UIKit/UIKit.h>
9+
10+
@interface Proximity : NSObject <RCTBridgeModule> {
11+
UIDevice *_currentDevice;
12+
}
13+
- (void) getProximityData:(RCTResponseSenderBlock) cb;
14+
- (void) startProximityUpdates;
15+
- (void) stopProximityUpdates;
16+
17+
@end

RNMotionManager/Proximity.m

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
var { Accelerometer, Gyroscope, Magnetometer } = require('react-native').NativeModules;
1+
var { Accelerometer, Gyroscope, Magnetometer, Proximity } = require('react-native').NativeModules;
22

3-
module.exports = { Accelerometer, Gyroscope, Magnetometer };
3+
module.exports = { Accelerometer, Gyroscope, Magnetometer, Proximity };

0 commit comments

Comments
 (0)