diff --git a/README.md b/README.md index a063e64..30fe249 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/android/src/main/java/com/reactlibrary/RNSimpleCompassModule.java b/android/src/main/java/com/reactlibrary/RNSimpleCompassModule.java index 5e27cba..507fb22 100644 --- a/android/src/main/java/com/reactlibrary/RNSimpleCompassModule.java +++ b/android/src/main/java/com/reactlibrary/RNSimpleCompassModule.java @@ -1,4 +1,3 @@ - package com.reactlibrary; import android.hardware.Sensor; @@ -14,6 +13,7 @@ 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 { @@ -21,6 +21,7 @@ public class RNSimpleCompassModule extends ReactContextBaseJavaModule implements private static Context mApplicationContext; private int mAzimuth = 0; // degree + private int mAccuracy = 0; private int mFilter = 1; private SensorManager mSensorManager; private Sensor mSensor; @@ -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 @@ -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; + } } } diff --git a/index.js b/index.js index 644bc31..13140b6 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,3 @@ - import { NativeModules, NativeEventEmitter } from 'react-native'; const { RNSimpleCompass } = NativeModules; @@ -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); diff --git a/ios/RNSimpleCompass.m b/ios/RNSimpleCompass.m index 17c6655..a5e92b0 100644 --- a/ios/RNSimpleCompass.m +++ b/ios/RNSimpleCompass.m @@ -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"); @@ -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 { diff --git a/react-native-simple-compass.podspec b/react-native-simple-compass.podspec new file mode 100644 index 0000000..8f55757 --- /dev/null +++ b/react-native-simple-compass.podspec @@ -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" => "some.guy@internett.com" } + + # ――― 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