Created
January 16, 2019 15:39
-
-
Save ger86/df97cf6a46fbf8b14b59319c4e505f98 to your computer and use it in GitHub Desktop.
React Native Beacons (1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // @flow | |
| import { | |
| type BeaconRegion, | |
| type AuthorizationStatus, | |
| type BeaconsManagerIOS | |
| } from './module.types'; | |
| const RN = require('react-native'); | |
| const BeaconsManager: BeaconsManagerIOS = RN.NativeModules.RNiBeacon; | |
| const BeaconsEventEmitter = new RN.NativeEventEmitter(BeaconsManager); | |
| /** | |
| * request always authorization (mandatory when ranging beacons but energy drain) | |
| * IMPORTANT: To be effective your info.plist file should have 'Privacy - Location Always Usage Description' key defined | |
| */ | |
| function requestAlwaysAuthorization(): void { | |
| BeaconsManager.requestAlwaysAuthorization(); | |
| } | |
| /** | |
| * request when app in use authorization (bare minimum for beacons) | |
| * IMPORTANT: To be effective your info.plist file should have 'Privacy - Location When In Use Usage Description' key defined (hopefully 'react-native init' should have set it for you) | |
| */ | |
| function requestWhenInUseAuthorization(): void { | |
| BeaconsManager.requestWhenInUseAuthorization(); | |
| } | |
| /** | |
| * set background location updates to ensure monitoring when app is killed or in background mode | |
| * | |
| * @param {boolean} [allow=false] allow or disallow background modes | |
| */ | |
| function allowsBackgroundLocationUpdates(allow: boolean = false): void { | |
| BeaconsManager.allowsBackgroundLocationUpdates(allow); | |
| } | |
| /** | |
| * get authorization status | |
| * | |
| * @returns {() => AuthorizationStatus} instant callback (not async) | |
| */ | |
| function getAuthorizationStatus( | |
| callback: (status: AuthorizationStatus) => any | |
| ): any { | |
| return BeaconsManager.getAuthorizationStatus(callback); | |
| } | |
| /** | |
| * get monitored regions | |
| * | |
| * @returns {Promise<Array<BeaconRegion>>} promise resolve to an array of monitored regions | |
| */ | |
| function getMonitoredRegions(): Promise<Array<BeaconRegion>> { | |
| return new Promise(resolve => { | |
| BeaconsManager.getMonitoredRegions(resolve); | |
| }); | |
| } | |
| /** | |
| * call is needed for monitoring beacons and gets the initial position of the device. | |
| * | |
| */ | |
| function startUpdatingLocation(): void { | |
| BeaconsManager.startUpdatingLocation(); | |
| } | |
| /** | |
| * This method should be called when you don't need to receive location-based information and want to save battery power. | |
| * | |
| */ | |
| function stopUpdatingLocation(): void { | |
| BeaconsManager.stopUpdatingLocation(); | |
| } | |
| function shouldDropEmptyRanges(drop: boolean): void { | |
| BeaconsManager.shouldDropEmptyRanges(drop); | |
| } | |
| /** | |
| * start monitoring for a region | |
| * | |
| * @param {BeaconRegion} region region to monitor (identifier + uuid -> major and minor are optional) | |
| * @returns {Promise<any>} promise resolves to void or error | |
| */ | |
| function startMonitoringForRegion(region: BeaconRegion): Promise<any> { | |
| return new Promise((resolve, reject) => { | |
| try { | |
| BeaconsManager.startMonitoringForRegion(region).then(lastEvent => | |
| resolve(lastEvent) | |
| ); | |
| } catch (error) { | |
| reject(error); | |
| } | |
| }); | |
| } | |
| /** | |
| * stop monitoring for a region | |
| * | |
| * @param {BeaconRegion} region region (identifier + uuid -> major and minor are optional) | |
| * @returns {Promise<any>} promise resolves to void or error | |
| */ | |
| function stopMonitoringForRegion(region: BeaconRegion): Promise<any> { | |
| return new Promise((resolve, reject) => { | |
| try { | |
| BeaconsManager.stopMonitoringForRegion(region); | |
| resolve(); | |
| } catch (error) { | |
| reject(error); | |
| } | |
| }); | |
| } | |
| /** | |
| * start ranging for a region | |
| * | |
| * @param {BeaconRegion} region region to scan (identifier + uuid -> major and minor are optional) | |
| * @returns {Promise<any>} promise resolves to void or error | |
| */ | |
| function startRangingBeaconsInRegion(region: BeaconRegion): Promise<any> { | |
| return new Promise((resolve, reject) => { | |
| try { | |
| BeaconsManager.startRangingBeaconsInRegion(region); | |
| resolve(); | |
| } catch (error) { | |
| reject(error); | |
| } | |
| }); | |
| } | |
| /** | |
| * stop ranging for a region | |
| * | |
| * @param {BeaconRegion} region region (identifier + uuid -> major and minor are optional) | |
| * @returns {Promise<any>} promise: resolves to void when successful | |
| */ | |
| function stopRangingBeaconsInRegion(region: BeaconRegion): Promise<any> { | |
| return new Promise((resolve, reject) => { | |
| try { | |
| BeaconsManager.stopRangingBeaconsInRegion(region); | |
| resolve(); | |
| } catch (error) { | |
| reject(error); | |
| } | |
| }); | |
| } | |
| module.exports = { | |
| BeaconsEventEmitter, | |
| requestAlwaysAuthorization, | |
| requestWhenInUseAuthorization, | |
| allowsBackgroundLocationUpdates, | |
| getAuthorizationStatus, | |
| getMonitoredRegions, | |
| startUpdatingLocation, | |
| stopUpdatingLocation, | |
| shouldDropEmptyRanges, | |
| // common with android: | |
| startMonitoringForRegion, | |
| startRangingBeaconsInRegion, | |
| stopMonitoringForRegion, | |
| stopRangingBeaconsInRegion | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment