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