Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit e33b074

Browse files
authored
feat: raw gravity added (react-native-sensors#397)
* raw gravity added for android * raw gravity added for ios * prefixes * ignore vscode config * prefixes
1 parent beb2aee commit e33b074

File tree

10 files changed

+190
-3
lines changed

10 files changed

+190
-3
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,5 @@ android/gradle/
4545
android/gradlew
4646
android/gradlew.bat
4747
yarn.lock
48+
49+
.vscode

android/src/main/java/com/sensors/RNSensor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public void onSensorChanged(SensorEvent sensorEvent) {
107107
switch (currentType)
108108
{
109109
case Sensor.TYPE_ACCELEROMETER:
110+
case Sensor.TYPE_GRAVITY:
110111
case Sensor.TYPE_GYROSCOPE:
111112
case Sensor.TYPE_MAGNETIC_FIELD:
112113
map.putDouble("x", sensorEvent.values[0]);

android/src/main/java/com/sensors/RNSensorsPackage.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public List<NativeModule> createNativeModules(ReactApplicationContext reactConte
1717
return Arrays.<NativeModule>asList(
1818
new RNSensor(reactContext, "RNSensorsGyroscope", Sensor.TYPE_GYROSCOPE),
1919
new RNSensor(reactContext, "RNSensorsAccelerometer", Sensor.TYPE_ACCELEROMETER),
20+
new RNSensor(reactContext, "RNSensorsGravity", Sensor.TYPE_GRAVITY),
2021
new RNSensor(reactContext, "RNSensorsMagnetometer", Sensor.TYPE_MAGNETIC_FIELD),
2122
new RNSensor(reactContext, "RNSensorsBarometer", Sensor.TYPE_PRESSURE),
2223
new RNSensor(reactContext, "RNSensorsOrientation", Sensor.TYPE_ROTATION_VECTOR)

index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ declare module "react-native-sensors" {
77
magnetometer: "magnetometer";
88
barometer: "barometer";
99
orientation: "orientation";
10+
gravity: "gravity";
1011
};
1112

1213
export const SensorTypes: Sensors;
@@ -44,9 +45,10 @@ declare module "react-native-sensors" {
4445
magnetometer: Observable<SensorData>;
4546
barometer: Observable<BarometerData>;
4647
orientation: Observable<OrientationData>;
48+
gravity: Observable<SensorData>
4749
};
4850

49-
export const { accelerometer, gyroscope, magnetometer, barometer, orientation }: SensorsBase;
51+
export const { accelerometer, gyroscope, magnetometer, barometer, orientation, gravity }: SensorsBase;
5052

5153
const sensors: SensorsBase;
5254

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export const SensorTypes = {
77
magnetometer: "magnetometer",
88
barometer: "barometer",
99
orientation: "orientation",
10+
gravity: "gravity"
1011
};
1112

12-
export const { accelerometer, gyroscope, magnetometer, barometer, orientation } = sensors;
13+
export const { accelerometer, gyroscope, magnetometer, barometer, orientation, gravity } = sensors;
1314
export default sensors;

ios/RNSensorsGravity.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Inspired by https://github.com/pwmckenna/react-native-motion-manager
2+
3+
#import <React/RCTBridgeModule.h>
4+
#import <CoreMotion/CoreMotion.h>
5+
#import <React/RCTEventEmitter.h>
6+
7+
@interface RNSensorsGravity : RCTEventEmitter <RCTBridgeModule> {
8+
CMMotionManager *_motionManager;
9+
int logLevel;
10+
}
11+
12+
- (void) isAvailableWithResolver:(RCTPromiseResolveBlock) resolve
13+
rejecter:(RCTPromiseRejectBlock) reject;
14+
- (void) setUpdateInterval:(double) interval;
15+
- (void) getUpdateInterval:(RCTResponseSenderBlock) cb;
16+
- (void) setLogLevel:(int) level;
17+
- (void) getData:(RCTResponseSenderBlock) cb;
18+
- (void) startUpdates;
19+
- (void) stopUpdates;
20+
21+
@end

ios/RNSensorsGravity.m

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
2+
// Orientation.m
3+
4+
5+
#import <React/RCTBridge.h>
6+
#import <React/RCTEventDispatcher.h>
7+
#import "RNSensorsGravity.h"
8+
#import "Utils.h"
9+
10+
@implementation RNSensorsGravity
11+
12+
@synthesize bridge = _bridge;
13+
14+
RCT_EXPORT_MODULE();
15+
16+
- (id) init {
17+
self = [super init];
18+
NSLog(@"Gravity");
19+
20+
if (self) {
21+
self->_motionManager = [[CMMotionManager alloc] init];
22+
self->logLevel = 0;
23+
}
24+
return self;
25+
}
26+
27+
- (NSArray<NSString *> *)supportedEvents
28+
{
29+
return @[@"Gravity"];
30+
}
31+
32+
+ (BOOL)requiresMainQueueSetup
33+
{
34+
return NO;
35+
}
36+
37+
RCT_REMAP_METHOD(isAvailable,
38+
resolver:(RCTPromiseResolveBlock)resolve
39+
rejecter:(RCTPromiseRejectBlock)reject) {
40+
return [self isAvailableWithResolver:resolve
41+
rejecter:reject];
42+
}
43+
44+
- (void) isAvailableWithResolver:(RCTPromiseResolveBlock) resolve
45+
rejecter:(RCTPromiseRejectBlock) reject {
46+
if([self->_motionManager isDeviceMotionAvailable])
47+
{
48+
/* Start the accelerometer if it is not active already */
49+
if([self->_motionManager isDeviceMotionActive] == NO)
50+
{
51+
resolve(@YES);
52+
} else {
53+
reject(@"-1", @"Gravity is not active", nil);
54+
}
55+
}
56+
else
57+
{
58+
reject(@"-1", @"Gravity is not available", nil);
59+
}
60+
}
61+
62+
RCT_EXPORT_METHOD(setUpdateInterval:(double) interval) {
63+
if (self->logLevel > 0) {
64+
NSLog(@"setUpdateInterval: %f", interval);
65+
}
66+
67+
double intervalInSeconds = interval / 1000;
68+
69+
[self->_motionManager setDeviceMotionUpdateInterval:intervalInSeconds];
70+
}
71+
72+
RCT_EXPORT_METHOD(setLogLevel:(int) level) {
73+
if (level > 0) {
74+
NSLog(@"setLogLevel: %f", level);
75+
}
76+
77+
self->logLevel = level;
78+
}
79+
80+
RCT_EXPORT_METHOD(getUpdateInterval:(RCTResponseSenderBlock) cb) {
81+
double interval = self->_motionManager.deviceMotionUpdateInterval;
82+
83+
if (self->logLevel > 0) {
84+
NSLog(@"getUpdateInterval: %f", interval);
85+
}
86+
87+
cb(@[[NSNull null], [NSNumber numberWithDouble:interval]]);
88+
}
89+
90+
RCT_EXPORT_METHOD(getData:(RCTResponseSenderBlock) cb) {
91+
double x = self->_motionManager.deviceMotion.gravity.x;
92+
double y = self->_motionManager.deviceMotion.gravity.y;
93+
double z = self->_motionManager.deviceMotion.gravity.z;
94+
double timestamp = [Utils sensorTimestampToEpochMilliseconds:self->_motionManager.deviceMotion.timestamp];
95+
96+
if (self->logLevel > 0) {
97+
NSLog(@"getData: %f, %f, %f, %f", x, y, z, timestamp);
98+
}
99+
100+
cb(@[[NSNull null], @{
101+
@"x" : [NSNumber numberWithDouble:x],
102+
@"y" : [NSNumber numberWithDouble:y],
103+
@"z" : [NSNumber numberWithDouble:z],
104+
@"timestamp" : [NSNumber numberWithDouble:timestamp]
105+
}]
106+
);
107+
}
108+
109+
RCT_EXPORT_METHOD(startUpdates) {
110+
if (self->logLevel > 0) {
111+
NSLog(@"startUpdates/startGravityUpdates");
112+
}
113+
114+
[self->_motionManager setShowsDeviceMovementDisplay:YES];
115+
116+
/* Receive the orientation data on this block */
117+
[self->_motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
118+
withHandler:^(CMDeviceMotion *deviceMotion, NSError *error)
119+
{
120+
121+
double x = deviceMotion.gravity.x;
122+
double y = deviceMotion.gravity.y;
123+
double z = deviceMotion.gravity.z;
124+
125+
double timestamp = [Utils sensorTimestampToEpochMilliseconds:deviceMotion.timestamp];
126+
127+
if (self->logLevel > 1) {
128+
NSLog(@"Updated gravity values: %f, %f, %f, %f", x, y, z, timestamp);
129+
}
130+
131+
[self sendEventWithName:@"Gravity" body:@{
132+
@"x" : [NSNumber numberWithDouble:x],
133+
@"y" : [NSNumber numberWithDouble:y],
134+
@"z" : [NSNumber numberWithDouble:z],
135+
@"timestamp" : [NSNumber numberWithDouble:timestamp]
136+
}];
137+
}];
138+
139+
}
140+
141+
RCT_EXPORT_METHOD(stopUpdates) {
142+
if (self->logLevel > 0) {
143+
NSLog(@"stopUpdates");
144+
}
145+
146+
[self->_motionManager stopDeviceMotionUpdates];
147+
}
148+
149+
@end

mock.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ const rnSensors = {
1919
gyroscope: 'gyroscope',
2020
magnetometer: 'magnetometer',
2121
barometer: 'barometer',
22+
gravity: 'gravity'
2223
},
2324

2425
accelerometer: sensorMock(),
2526
gyroscope: sensorMock(),
2627
magnetometer: sensorMock(),
2728
barometer: sensorMock({ pressure: 0 }),
29+
gravity: sensorMock(),
2830

2931
setLogLevelForType: jest.fn(),
3032
setUpdateIntervalForType: jest.fn(),

src/rnsensors.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ const {
55
RNSensorsMagnetometer: MagnNative,
66
RNSensorsBarometer: BarNative,
77
RNSensorsOrientation: OrientNative,
8+
RNSensorsGravity: GravNative
89
} = NativeModules;
910

10-
if (!GyroNative && !AccNative && !MagnNative && !BarNative && !OrientNative) {
11+
if (!GyroNative && !AccNative && !MagnNative && !BarNative && !OrientNative && !GravNative) {
1112
throw new Error("Native modules for sensors not available. Did react-native link run successfully?");
1213
}
1314

@@ -17,6 +18,7 @@ const nativeApis = new Map([
1718
["magnetometer", MagnNative],
1819
["barometer", BarNative],
1920
["orientation", OrientNative],
21+
["gravity", GravNative]
2022
]);
2123

2224
// Cache the availability of sensors

src/sensors.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
RNSensorsMagnetometer: MagnNative,
1010
RNSensorsBarometer: BarNative,
1111
RNSensorsOrientation: OrientNative,
12+
RNSensorsGravity: GravNative
1213
} = NativeModules;
1314

1415
const listenerKeys = new Map([
@@ -17,6 +18,7 @@ const listenerKeys = new Map([
1718
["magnetometer", "Magnetometer"],
1819
["barometer", "Barometer"],
1920
["orientation", "Orientation"],
21+
["gravity", "Gravity"]
2022
]);
2123

2224
const nativeApis = new Map([
@@ -25,6 +27,7 @@ const nativeApis = new Map([
2527
["magnetometer", MagnNative],
2628
["barometer", BarNative],
2729
["orientation", OrientNative],
30+
["gravity", GravNative]
2831
]);
2932

3033
const eventEmitterSubscription = new Map([
@@ -33,6 +36,7 @@ const eventEmitterSubscription = new Map([
3336
["magnetometer", null],
3437
["barometer", null],
3538
["orientation", null],
39+
["gravity", null]
3640
]);
3741

3842
function createSensorObservable(sensorType) {
@@ -81,11 +85,13 @@ const gyroscope = createSensorObservable("gyroscope");
8185
const magnetometer = createSensorObservable("magnetometer");
8286
const barometer = createSensorObservable("barometer");
8387
const orientation = createSensorObservable("orientation");
88+
const gravity = createSensorObservable("gravity");
8489

8590
export default {
8691
gyroscope,
8792
accelerometer,
8893
magnetometer,
8994
barometer,
9095
orientation,
96+
gravity
9197
};

0 commit comments

Comments
 (0)