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

Barometer support #32

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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ DeviceEventEmitter.addListener('Thermometer', function (data) {
SensorManager.stopThermometer();
```

### Barometer
```js
SensorManager.startBarometer(1000);
DeviceEventEmitter.addListener('Barometer', function (data) {
/**
* data.press
**/
});
SensorManager.stopBarometer();
```

### LightSensor
```js
SensorManager.startLightSensor(100);
Expand Down
82 changes: 82 additions & 0 deletions android/src/main/java/com/sensormanager/BarometerRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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 BarometerRecord implements SensorEventListener {

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

private ReactContext mReactContext;
private Arguments mArguments;

public BarometerRecord(ReactApplicationContext reactContext) {
mSensorManager = (SensorManager)reactContext.getSystemService(reactContext.SENSOR_SERVICE);
mReactContext = reactContext;
}

public int start(int delay) {
this.delay = delay;
if ((mBarometer = mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE)) != null) {
mSensorManager.registerListener(this, mBarometer, SensorManager.SENSOR_DELAY_FASTEST);
} else {
return (0);
}
return (1);
}

public void stop() {
mSensorManager.unregisterListener(this);
}

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_PRESSURE) {
long curTime = System.currentTimeMillis();
i++;
if ((curTime - lastUpdate) > delay) {
i = 0;
map.putDouble("press", sensorEvent.values[0]);
sendEvent("Barometer", map);
lastUpdate = curTime;
}
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
14 changes: 14 additions & 0 deletions android/src/main/java/com/sensormanager/SensorManagerModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class SensorManagerModule extends ReactContextBaseJavaModule {
private MagnetometerRecord mMagnetometerRecord = null;
private StepCounterRecord mStepCounterRecord = null;
private ThermometerRecord mThermometerRecord = null;
private BarometerRecord mBarometerRecord = null;
private MotionValueRecord mMotionValueRecord = null;
private OrientationRecord mOrientationRecord = null;
private ProximityRecord mProximityRecord = null;
Expand Down Expand Up @@ -98,6 +99,19 @@ public void stopThermometer() {
mThermometerRecord.stop();
}

@ReactMethod
public int startBarometer(int delay) {
if (mBarometerRecord == null)
mBarometerRecord = new BarometerRecord(mReactContext);
return (mBarometerRecord.start(delay));
}

@ReactMethod
public void stopBarometer() {
if (mBarometerRecord != null)
mBarometerRecord.stop();
}

@ReactMethod
public int startMotionValue(int delay) {
if (mMotionValueRecord == null)
Expand Down