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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@

#### iOS

##### Using CocoaPods

To install with CocoaPods, add the following to your Podfile:

```
pod 'react-native-simple-compass', :path => '../node_modules/react-native-simple-compass'
```

Then run pod install and rebuild your project.

##### Manual installation
1. In XCode, in the project navigator, right click `Libraries` ➜ `Add Files to [your project's name]`
2. Go to `node_modules` ➜ `react-native-simple-compass` and add `RNSimpleCompass.xcodeproj`
3. In XCode, in the project navigator, select your project. Add `libRNSimpleCompass.a` to your project's `Build Phases` ➜ `Link Binary With Libraries`
Expand Down
16 changes: 11 additions & 5 deletions android/src/main/java/com/reactlibrary/RNSimpleCompassModule.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package com.reactlibrary;

import android.hardware.Sensor;
Expand All @@ -14,13 +13,15 @@
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.WritableMap;

public class RNSimpleCompassModule extends ReactContextBaseJavaModule implements SensorEventListener {

private final ReactApplicationContext reactContext;

private static Context mApplicationContext;
private int mAzimuth = 0; // degree
private int mAccuracy = 0;
private int mFilter = 1;
private SensorManager mSensorManager;
private Sensor mSensor;
Expand Down Expand Up @@ -62,7 +63,7 @@ public void stop() {

@Override
public void onSensorChanged(SensorEvent event) {
if( event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR ){
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
Expand All @@ -75,15 +76,20 @@ public void onSensorChanged(SensorEvent event) {

mAzimuth = newAzimuth;

WritableMap params = Arguments.createMap();
params.putInt("degree", mAzimuth);
params.putInt("accuracy", mAccuracy);

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


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

if (sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
mAccuracy = accuracy;
}
}
}
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

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

Expand All @@ -12,8 +11,8 @@ RNSimpleCompass.start = (update_rate, callback) => {
}

const compassEventEmitter = new NativeEventEmitter(RNSimpleCompass);
listener = compassEventEmitter.addListener('HeadingUpdated', (degree) => {
callback(degree);
listener = compassEventEmitter.addListener('HeadingUpdated', (degree, accuracy) => {
callback(degree, accuracy);
});

_start(update_rate === null ? 0 : update_rate);
Expand Down
6 changes: 1 addition & 5 deletions ios/RNSimpleCompass.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ - (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");
Expand All @@ -40,7 +36,7 @@ - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading
if (newHeading.headingAccuracy < 0) {
return;
}
[self sendEventWithName:kHeadingUpdated body:@(newHeading.trueHeading)];
[self sendEventWithName:kHeadingUpdated body:@{@"degree": @(newHeading.trueHeading), @"accuracy": @(newHeading.headingAccuracy)}];
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
Expand Down
38 changes: 38 additions & 0 deletions react-native-simple-compass.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Pod::Spec.new do |s|

# ――― Spec Metadata ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

s.name = "react-native-simple-compass"
s.version = "1.0.0"
s.summary = "Some summary"
s.description = <<-DESC
This is a great description
DESC

s.homepage = "https://github.com/vnil/react-native-simple-compass"

# ――― Spec License ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

s.license = "MIT"

# ――― Author Metadata ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

s.author = { "Some guy" => "[email protected]" }

# ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

s.platform = :ios, "9.0"

# ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

s.source = { :git => "https://github.com/vnil/react-native-simple-compass" }

# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

s.source_files = 'ios/**/*.{h,m}'

# ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

s.dependency "React"

end