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

Gravity sensor and fix for orientation #35

Open
wants to merge 2 commits 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
android/build
**/*.swp
.DS_Store
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,19 @@ DeviceEventEmitter.addListener('Magnetometer', function (data) {
SensorManager.stopMagnetometer();
```

### Gravity
```js
DeviceEventEmitter.addListener('Gravity', function (data) {
/**
* data.x
* data.y
* data.z
**/
});
SensorManager.startGravity(100);
SensorManager.stopGravity();
```

### Orientation
```js
SensorManager.startOrientation(100);
Expand Down
90 changes: 90 additions & 0 deletions android/src/main/java/com/sensormanager/GravityRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.sensormanager;

import android.os.Bundle;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.support.annotation.Nullable;

import java.io.*;
import java.util.Date;
import java.util.Timer;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.bridge.ReactApplicationContext;

public class GravityRecord implements SensorEventListener {

private SensorManager mSensorManager;
private Sensor mGravity;
private long lastUpdate = 0;
private int i = 0, n = 0;
private int delay;
private int isRegistered = 0;

private ReactContext mReactContext;
private Arguments mArguments;


public GravityRecord(ReactApplicationContext reactContext) {
mSensorManager = (SensorManager)reactContext.getSystemService(reactContext.SENSOR_SERVICE);
mGravity = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
mReactContext = reactContext;
}

public int start(int delay) {
this.delay = delay;
if (mGravity != null && isRegistered == 0) {
mSensorManager.registerListener(this, mGravity, SensorManager.SENSOR_DELAY_FASTEST);
isRegistered = 1;
return (1);
}
return (0);
}

public void stop() {
if (isRegistered == 1) {
mSensorManager.unregisterListener(this);
isRegistered = 0;
}
}

private void sendEvent(String eventName, @Nullable WritableMap params)
{
try {
mReactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
} catch (RuntimeException e) {
Log.e("ERROR", "java.lang.RuntimeException: Trying to invoke JS before CatalystInstance has been set!");
}
}

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
Sensor mySensor = sensorEvent.sensor;
WritableMap map = mArguments.createMap();

if (mySensor.getType() == Sensor.TYPE_GRAVITY) {
long curTime = System.currentTimeMillis();
i++;
if ((curTime - lastUpdate) > delay) {
i = 0;
map.putDouble("x", sensorEvent.values[0]);
map.putDouble("y", sensorEvent.values[1]);
map.putDouble("z", sensorEvent.values[2]);
sendEvent("Gravity", map);
lastUpdate = curTime;
}
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,15 @@ public void onSensorChanged(SensorEvent sensorEvent) {
roll = 360 - (0 - roll);
}

i++;
Copy link

@Kuluum Kuluum Oct 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to move that right under long curTime = System.currentTimeMillis();. Not to make excess work with geting orientation and performing data to send event.

if ((curTime - lastUpdate) > delay) {
i = 0;
map.putDouble("azimuth", heading);
map.putDouble("pitch", pitch);
map.putDouble("roll", roll);
sendEvent("Orientation", map);
lastUpdate = curTime;
}
}
}
}
Expand Down
18 changes: 16 additions & 2 deletions android/src/main/java/com/sensormanager/SensorManagerModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import android.util.Log;

public class SensorManagerModule extends ReactContextBaseJavaModule {
private static final String REACT_CLASS = "SensorManager";
private AccelerometerRecord mAccelerometerRecord = null;
private static final String REACT_CLASS = "SensorManager";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the best idea to break the code alignment)

private AccelerometerRecord mAccelerometerRecord = null;
private GyroscopeRecord mGyroscopeRecord = null;
private MagnetometerRecord mMagnetometerRecord = null;
private StepCounterRecord mStepCounterRecord = null;
Expand All @@ -20,6 +20,7 @@ public class SensorManagerModule extends ReactContextBaseJavaModule {
private OrientationRecord mOrientationRecord = null;
private ProximityRecord mProximityRecord = null;
private LightSensorRecord mLightSensorRecord = null;
private GravityRecord mGravityRecord = null;

private ReactApplicationContext mReactContext;

Expand Down Expand Up @@ -150,6 +151,19 @@ public void stopLightSensor() {
mLightSensorRecord.stop();
}

@ReactMethod
public int startGravity(int delay) {
if (mGravityRecord == null)
mGravityRecord = new GravityRecord(mReactContext);
return (mGravityRecord.start(delay));
}

@ReactMethod
public void stopGravity() {
if (mGravityRecord != null)
mGravityRecord.stop();
}

/*
@Override
public ReactBarcodeScannerView createViewInstance(ThemedReactContext context) {
Expand Down