diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/README.md b/README.md index eddaa33..277fb70 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,17 @@ # React Native Heading Get device heading information on iOS or Android -##What +## What Report back device orientation in degrees, 0-360, with 0 being North. -####Example -```java -const { DeviceEventEmitter } = require('react-native'); -const ReactNativeHeading = require('react-native-heading'); +#### Example +```javascript +import { NativeEventEmitter } from 'react-native'; +import ReactNativeHeading from '@zsajjad/react-native-heading'; //.... componentDidMount() { + this.listener = new NativeEventEmitter(ReactNativeHeading) ReactNativeHeading.start(1) .then(didStart => { this.setState({ @@ -18,47 +19,48 @@ const ReactNativeHeading = require('react-native-heading'); }) }) - DeviceEventEmitter.addListener('headingUpdated', data => { - console.log('New heading is:', data.heading); + this.listener.addListener('headingUpdated', heading => { + console.log('New heading is:', heading); }); } componentWillUnmount() { ReactNativeHeading.stop(); - DeviceEventEmitter.removeAllListeners('headingUpdated'); + this.listener.removeAllListeners('headingUpdated'); } //... ``` -####API +#### API `start(filter)` - Start receiving heading updates. Accepts an optional filter param (int) to ignore heading changes less than the spcified threshold. The default value is 5. Returns a promise which can be used to determine if heading updates are suported by the device. `stop` - Stop receiving heaing updates (don't forget to remove the `headingUpdated` listener) -##Setup +## Setup -```` -npm install --save react-native-heading -```` +``` +yarn add https://github.com/zsajjad/react-native-heading.git +``` -###iOS -* Run open node_modules/react-native-heading -* Drag ReactNativeHeading.xcodeproj into your Libraries group +### iOS +* Run open node_modules/@zsajjad/react-native-heading +* Drag ReactNativeHeading.xcodeproj into your Libraries group of XCode's project navigator +* In XCode add Libraries/ReactNativeHeading.xcodeproj/Products/libReactNativeHeading.a to the "Link Binary with Libraries" section of the Build Phases -###Android -#####Step 1 - Update Gradle Settings +### Android +##### Step 1 - Update Gradle Settings ``` // file: android/settings.gradle ... include ':react-native-heading' -project(':react-native-heading').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-heading/android') +project(':react-native-heading').projectDir = new File(rootProject.projectDir, '../node_modules/@zsajjad/react-native-heading/android') ``` -#####Step 2 - Update Gradle Build +##### Step 2 - Update Gradle Build ``` // file: android/app/build.gradle @@ -69,7 +71,7 @@ dependencies { compile project(':react-native-heading') } ``` -#####Step 3 - Register React Package +##### Step 3 - Register React Package ``` ... import com.joshblour.reactnativeheading.ReactNativeHeadingPackage; // <--- import diff --git a/ReactNativeHeading.android.js b/ReactNativeHeading.android.js deleted file mode 100644 index 87dac21..0000000 --- a/ReactNativeHeading.android.js +++ /dev/null @@ -1,10 +0,0 @@ -/** * - * @providesModule ReactNativeHeading - * @flow - */ -'use strict'; - -var React = require('react-native'); -var ReactNativeHeading = React.NativeModules.ReactNativeHeading; - -module.exports = ReactNativeHeading; diff --git a/ReactNativeHeading.h b/ReactNativeHeading.h index e0b2b5c..f760db6 100644 --- a/ReactNativeHeading.h +++ b/ReactNativeHeading.h @@ -5,10 +5,9 @@ // Created by Yonah Forst on 18/02/16. // Copyright © 2016 Yonah Forst. All rights reserved. // -#import "RCTBridgeModule.h" +#import +#import -#import - -@interface ReactNativeHeading : NSObject +@interface ReactNativeHeading : RCTEventEmitter @end diff --git a/ReactNativeHeading.ios.js b/ReactNativeHeading.ios.js deleted file mode 100644 index b3e92e4..0000000 --- a/ReactNativeHeading.ios.js +++ /dev/null @@ -1,6 +0,0 @@ -'use strict'; - -var React = require('react-native'); -var Heading = React.NativeModules.ReactNativeHeading; - -module.exports = Heading; diff --git a/ReactNativeHeading.js b/ReactNativeHeading.js new file mode 100644 index 0000000..b79b49b --- /dev/null +++ b/ReactNativeHeading.js @@ -0,0 +1,12 @@ +/** + * + * @providesModule ReactNativeHeading + * + */ +'use strict'; + +import { + NativeModules +} from 'react-native'; + +export default NativeModules.ReactNativeHeading; \ No newline at end of file diff --git a/ReactNativeHeading.m b/ReactNativeHeading.m index 9df8593..399c93e 100644 --- a/ReactNativeHeading.m +++ b/ReactNativeHeading.m @@ -8,9 +8,9 @@ #import "ReactNativeHeading.h" -#import "RCTBridge.h" -#import "RCTConvert.h" -#import "RCTEventDispatcher.h" +#import +#import +#import #import @@ -54,6 +54,10 @@ - (instancetype)init [self.locManager stopUpdatingHeading]; } +- (NSArray *)supportedEvents { + return @[@"headingUpdated"]; +} + - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading { if (newHeading.headingAccuracy < 0) return; @@ -62,9 +66,7 @@ - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading CLLocationDirection heading = ((newHeading.trueHeading > 0) ? newHeading.trueHeading : newHeading.magneticHeading); - NSDictionary *headingEvent = @{@"heading": @(heading)}; - - [self.bridge.eventDispatcher sendDeviceEventWithName:@"headingUpdated" body:headingEvent]; + [self sendEventWithName:@"headingUpdated" body:@(heading)]; } @end diff --git a/android/build.gradle b/android/build.gradle index 55dc946..eb24e51 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -11,12 +11,10 @@ buildscript { apply plugin: 'com.android.library' android { - compileSdkVersion 23 - buildToolsVersion "23.0.1" - + compileSdkVersion 26 defaultConfig { - minSdkVersion 18 - targetSdkVersion 22 + minSdkVersion 16 + targetSdkVersion 26 versionCode 1 versionName "1.0" } @@ -30,6 +28,5 @@ repositories { } dependencies { - compile 'com.facebook.react:react-native:0.20.+' - compile "com.joshblour.discovery:discovery:0.0.3" + implementation 'com.facebook.react:react-native:+' } diff --git a/android/src/main/java/com/joshblour/reactnativeheading/ReactNativeHeadingModule.java b/android/src/main/java/com/joshblour/reactnativeheading/ReactNativeHeadingModule.java index 395ade8..5de3050 100755 --- a/android/src/main/java/com/joshblour/reactnativeheading/ReactNativeHeadingModule.java +++ b/android/src/main/java/com/joshblour/reactnativeheading/ReactNativeHeadingModule.java @@ -20,8 +20,6 @@ import com.facebook.react.bridge.WritableArray; import com.facebook.react.bridge.WritableMap; import com.facebook.react.modules.core.DeviceEventManagerModule; -import com.joshblour.discovery.BLEUser; -import com.joshblour.discovery.Discovery; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -36,6 +34,7 @@ public class ReactNativeHeadingModule extends ReactContextBaseJavaModule impleme private static Context mApplicationContext; private int mAzimuth = 0; // degree + private int newAzimuth = 0; // degree private int mFilter = 5; private SensorManager mSensorManager; private Sensor mSensor; @@ -52,9 +51,6 @@ public String getName() { return "ReactNativeHeading"; } - - - @ReactMethod public void start(int filter, Promise promise) { @@ -82,21 +78,21 @@ public void onSensorChanged(SensorEvent event) { // calculate th rotation matrix SensorManager.getRotationMatrixFromVector(rMat, event.values); // get the azimuth value (orientation[0]) in degree - int newAzimuth = (int) ( Math.toDegrees( SensorManager.getOrientation( rMat, orientation )[0] ) + 360 ) % 360; - + newAzimuth = (int) (((( Math.toDegrees( SensorManager.getOrientation( rMat, orientation )[0] ) + 360 ) % 360) - + ( Math.toDegrees( SensorManager.getOrientation( rMat, orientation )[2] ))) +360) % 360; //dont react to changes smaller than the filter value if (Math.abs(mAzimuth - newAzimuth) < mFilter) { return; } - mAzimuth = newAzimuth; - Log.e("TAG", String.valueOf(newAzimuth)); - WritableMap params = Arguments.createMap(); - params.putInt("heading", mAzimuth); - getReactApplicationContext() .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) - .emit("headingUpdated", params); + .emit("headingUpdated", (int) newAzimuth); + + mAzimuth = newAzimuth; + // Log.e("Azimuth", + // String.valueOf(newAzimuth)); + } } diff --git a/android/src/main/java/com/joshblour/reactnativeheading/ReactNativeHeadingPackage.java b/android/src/main/java/com/joshblour/reactnativeheading/ReactNativeHeadingPackage.java index df0e4be..24e0bf7 100755 --- a/android/src/main/java/com/joshblour/reactnativeheading/ReactNativeHeadingPackage.java +++ b/android/src/main/java/com/joshblour/reactnativeheading/ReactNativeHeadingPackage.java @@ -24,11 +24,6 @@ public List createNativeModules(ReactApplicationContext reactConte modules.add(new ReactNativeHeadingModule(reactContext)); return modules; } - @Override - public List> createJSModules() { - return Collections.emptyList(); - } - @Override public List createViewManagers(ReactApplicationContext reactContext) { return Arrays.asList(); diff --git a/package.json b/package.json index 77c3edf..16d999d 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,14 @@ { - "name": "react-native-heading", - "version": "1.0.0", + "name": "@zsajjad/react-native-heading", + "version": "1.1.0", "repository": { "type": "git", - "url": "https://github.com/joshblour/react-native-heading.git" + "url": "https://github.com/zsajjad/react-native-heading.git" }, + "homepage": "https://github.com/zsajjad/react-native-heading.git", + "summary": "RN Package for head sensor detection", "license": "MIT", - "keywords": ["react-native", "react-component"], + "keywords": ["react-native", "react-component", "heading", "azimuth"], "main": "ReactNativeHeading", - "author": "Yonah Forst " + "author": "Zain Sajjad " } diff --git a/react-native-heading.podspec b/react-native-heading.podspec new file mode 100644 index 0000000..212c0ee --- /dev/null +++ b/react-native-heading.podspec @@ -0,0 +1,22 @@ +require 'json' + +package = JSON.parse(File.read(File.join(__dir__, 'package.json'))) + +Pod::Spec.new do |s| + s.name = 'react-native-heading' + s.version = package['version'] + s.summary = package['summary'] + s.description = package['description'] + s.license = package['license'] + s.author = package['author'] + s.homepage = package['homepage'] + s.source = { :git => 'https://github.com/zsajjad/react-native-heading', :tag => s.version } + + s.requires_arc = true + s.platform = :ios, '8.0' + + s.preserve_paths = 'LICENSE', 'README.md', 'package.json', 'ReactNativeHeading.ios.js' + s.source_files = '*.{h,m}' + + s.dependency 'React' +end