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

feat: add accuracy support for android #48

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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ project.xcworkspace
.settings
local.properties
android.iml
android/gradlew
android/gradlew.bat

# Cocoapods
#
Expand Down
Binary file added android/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
7 changes: 7 additions & 0 deletions android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
24 changes: 22 additions & 2 deletions android/src/main/java/com/compassheading/CompassHeadingModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public class CompassHeadingModule extends ReactContextBaseJavaModule implements
private int mFilter = 1;

private SensorManager sensorManager;
/**
* Possible values.
* 0 -> sensorManager.SENSOR_STATUS_UNRELIABLE
* 1 -> sensorManager.SENSOR_STATUS_ACCURACY_LOW
* 2 -> sensorManager.SENSOR_STATUS_ACCURACY_MEDIUM
* 3 -> sensorManager.SENSOR_STATUS_ACCURACY_HIGH
*/
private int mAccuracy = SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM;

private final float[] mGravity = new float[3];
private final float[] mGeomagnetic = new float[3];
Expand Down Expand Up @@ -174,7 +182,7 @@ else if(rotation == Surface.ROTATION_180){

WritableMap params = Arguments.createMap();
params.putDouble("heading", mAzimuth);
params.putDouble("accuracy", 1.0);
params.putDouble("accuracy", mAccuracy);

getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
Expand All @@ -185,5 +193,17 @@ else if(rotation == Surface.ROTATION_180){
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
public void onAccuracyChanged(Sensor sensor, int accuracy) {

if (sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
mAccuracy = accuracy;
WritableMap params = Arguments.createMap();
params.putDouble("heading", mAzimuth);
params.putDouble("accuracy", mAccuracy);

getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("HeadingUpdated", params);
}
}
}
12 changes: 12 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ let _start = CompassHeading.start;

type dataType = {
heading: number;
/**
* for Android:
* 0 -> sensorManager.SENSOR_STATUS_UNRELIABLE
* 1 -> sensorManager.SENSOR_STATUS_ACCURACY_LOW
* 2 -> sensorManager.SENSOR_STATUS_ACCURACY_MEDIUM
* 3 -> sensorManager.SENSOR_STATUS_ACCURACY_HIGH
* https://developer.android.com/reference/android/hardware/SensorManager
*
* for iOS:
* in degree of the offset
* https://developer.apple.com/documentation/corelocation/clheading
*/
accuracy: number;
};

Expand Down