-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathRNSimpleCompassModule.java
More file actions
95 lines (72 loc) · 2.82 KB
/
RNSimpleCompassModule.java
File metadata and controls
95 lines (72 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.reactlibrary;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.content.Context;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
public class RNSimpleCompassModule extends ReactContextBaseJavaModule implements SensorEventListener {
private final ReactApplicationContext reactContext;
private static Context mApplicationContext;
private int mAzimuth = 0; // degree
private int mFilter = 1;
private SensorManager mSensorManager;
private Sensor mSensor;
private float[] orientation = new float[3];
private float[] rMat = new float[9];
public RNSimpleCompassModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
mApplicationContext = reactContext.getApplicationContext();
}
@Override
public String getName() {
return "RNSimpleCompass";
}
@ReactMethod
public void start(int filter) {
if (mSensorManager == null) {
mSensorManager = (SensorManager) mApplicationContext.getSystemService(Context.SENSOR_SERVICE);
}
if (mSensor == null) {
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
}
mFilter = filter;
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_UI);
}
@ReactMethod
public void stop() {
if (mSensorManager != null) {
mSensorManager.unregisterListener(this);
}
}
@Override
public void onSensorChanged(SensorEvent event) {
if( event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR ){
// calculate th rotation matrix
SensorManager.getRotationMatrixFromVector(rMat, event.values);
// get the azimuth value (orientation[0]) in degree
int newAzimuth = (int) ( Math.toDegrees( SensorManager.getOrientation( rMat, orientation )[0] ) + 360 ) % 360;
//dont react to changes smaller than the filter value
if (Math.abs(mAzimuth - newAzimuth) < mFilter) {
return;
}
mAzimuth = newAzimuth;
WritableMap map = Arguments.createMap();
map.putDouble("heading", mAzimuth);
map.putDouble("accuracy",event.accuracy);
getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("HeadingUpdated", map);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}