Skip to content

Commit 53d0dd9

Browse files
author
Viktor Nilsson
committed
Add android support and changed API a bit
1 parent 1e5c4b7 commit 53d0dd9

File tree

5 files changed

+78
-16
lines changed

5 files changed

+78
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
ios/RNSimpleCompass.xcodeproj/project.xcworkspace/
22
ios/RNSimpleCompass.xcodeproj/xcuserdata/
3+
.DS_Store
Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,89 @@
11

22
package com.reactlibrary;
33

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+
413
import com.facebook.react.bridge.ReactApplicationContext;
514
import com.facebook.react.bridge.ReactContextBaseJavaModule;
615
import com.facebook.react.bridge.ReactMethod;
716
import com.facebook.react.bridge.Callback;
817

9-
public class RNSimpleCompassModule extends ReactContextBaseJavaModule {
18+
public class RNSimpleCompassModule extends ReactContextBaseJavaModule implements SensorEventListener {
1019

1120
private final ReactApplicationContext reactContext;
1221

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+
1330
public RNSimpleCompassModule(ReactApplicationContext reactContext) {
1431
super(reactContext);
1532
this.reactContext = reactContext;
33+
mApplicationContext = reactContext.getApplicationContext();
1634
}
1735

1836
@Override
1937
public String getName() {
2038
return "RNSimpleCompass";
2139
}
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+
}

ios/RNSimpleCompass.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11

2-
#import "RCTBridgeModule.h"
3-
#import "RCTEventEmitter.h"
2+
#import <React/RCTBridgeModule.h>
3+
#import <React/RCTEventEmitter.h>
44

55
@interface RNSimpleCompass : RCTEventEmitter <RCTBridgeModule>
66

77
@end
8-

ios/RNSimpleCompass.m

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
2-
#import "RNSimpleCompass.h"
3-
#import "RCTEventDispatcher.h"
4-
#import "RCTLog.h"
1+
#import <React/RNSimpleCompass.h>
2+
#import <React/RCTEventDispatcher.h>
53
#import <Corelocation/CoreLocation.h>
64

75
#define kHeadingUpdated @"HeadingUpdated"
8-
#define kDefauktHeadingFilter 1
96

107
@interface RNSimpleCompass() <CLLocationManagerDelegate>
118
@property (strong, nonatomic) CLLocationManager *locationManager;
@@ -27,7 +24,7 @@ - (instancetype)init {
2724
NSLog(@"Heading not available");
2825
}
2926
}
30-
27+
3128
return self;
3229
}
3330

@@ -62,9 +59,7 @@ - (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)mana
6259

6360
#pragma mark - React
6461

65-
RCT_EXPORT_METHOD(start: (NSDictionary *)params) {
66-
NSNumber *accuracy = [params objectForKey:@"accuracy"];
67-
NSInteger headingFilter = accuracy ? [accuracy doubleValue] : kDefauktHeadingFilter;
62+
RCT_EXPORT_METHOD(start: (NSInteger) headingFilter) {
6863
self.locationManager.headingFilter = headingFilter;
6964
[self.locationManager startUpdatingHeading];
7065
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
{
33
"name": "react-native-simple-compass",
4-
"version": "0.0.1",
5-
"description": "Simple module exposing the compass on iOS",
4+
"version": "0.0.2",
5+
"description": "Simple module exposing the compass on iOS and Android",
66
"main": "index.js",
77
"scripts": {
88
"test": "echo \"Error: no test specified\" && exit 1"

0 commit comments

Comments
 (0)