-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
this sensor doesn't need to set UpdateInterval. will fire event when detected changes. isNear: true or false
- Loading branch information
Showing
4 changed files
with
109 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |