Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
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;

Expand Down Expand Up @@ -74,10 +76,14 @@ public void onSensorChanged(SensorEvent event) {
}

mAzimuth = newAzimuth;
WritableMap map = Arguments.createMap();

map.putDouble("heading", mAzimuth);
map.putDouble("accuracy",event.accuracy);

getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("HeadingUpdated", mAzimuth);
.emit("HeadingUpdated", map);
}
}

Expand Down
14 changes: 8 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

import { NativeModules, NativeEventEmitter } from 'react-native';
import { NativeModules, NativeEventEmitter, Dimensions } from "react-native";
const { RNSimpleCompass } = NativeModules;

let listener;
Expand All @@ -12,18 +11,21 @@ RNSimpleCompass.start = (update_rate, callback) => {
}

const compassEventEmitter = new NativeEventEmitter(RNSimpleCompass);
listener = compassEventEmitter.addListener('HeadingUpdated', (degree) => {
callback(degree);
listener = compassEventEmitter.addListener("HeadingUpdated", course => {
const correctedCourse = { ...course };
const { height, width } = Dimensions.get("window");
if (width > height) correctedCourse.heading += 90;
callback(correctedCourse);
});

_start(update_rate === null ? 0 : update_rate);
}
};

let _stop = RNSimpleCompass.stop;
RNSimpleCompass.stop = () => {
listener && listener.remove();
listener = null;
_stop();
}
};

export default RNSimpleCompass;
16 changes: 10 additions & 6 deletions ios/RNSimpleCompass.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ - (instancetype)init {
if ([CLLocationManager headingAvailable]) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
NSLog(@"Requesting permission");
[self.locationManager requestWhenInUseAuthorization];
}
}
else {
NSLog(@"Heading not available");
}
}

return self;
}

Expand All @@ -40,7 +36,10 @@ - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading
if (newHeading.headingAccuracy < 0) {
return;
}
[self sendEventWithName:kHeadingUpdated body:@(newHeading.trueHeading)];
[self sendEventWithName:kHeadingUpdated body:(@{
@"heading": @(newHeading.trueHeading),
@"accuracy": @(newHeading.headingAccuracy)
})];
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
Expand All @@ -60,6 +59,10 @@ - (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)mana
#pragma mark - React

RCT_EXPORT_METHOD(start: (NSInteger) headingFilter) {
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
NSLog(@"Requesting permission");
[self.locationManager requestWhenInUseAuthorization];
}
self.locationManager.headingFilter = headingFilter;
[self.locationManager startUpdatingHeading];
}
Expand All @@ -71,3 +74,4 @@ - (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)mana
RCT_EXPORT_MODULE()

@end

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-simple-compass",
"version": "1.0.0",
"version": "1.0.1",
"description": "Simple module exposing the compass on iOS and Android",
"main": "index.js",
"scripts": {
Expand Down