Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-native-kontaktio

Driversnote-Dev785MIT4.1.0TypeScript support: included

React-native module for detecting Kontakt.io beacons. You have to own some Kontakt.io beacons, configure them via their managment console and have your api-key handy.

react-native, react-component, kontakt.io, beacon, beacons, ibeacon, eddystone, BLE, bluetooth

readme

react-native-kontaktio npm version

Cross-platform React Native module for detecting beacons with Android and iOS devices.

Kontakt.io SDK Versions of newest release:

OS SDK Version
Android 7.0.6
iOS 3.0.25

Advantages

  • Works with any beacon (because the Kontakt.io SDK wraps the native beacon libraries (while adding more) - no Kontakt.io SDK API key is necessary.
  • Especially useful with Kontakt.io beacons because additional information like the unique id (on the back of each beacon), the battery power level and others are available and get synchronized with your Kontakt.io online panel.
  • Highly customizable configurations (e.g. for setting arbitrary monitoring intervals on Android)

Setup

API Documentation

Examples

Extensive Example

Minimal TypeScript Example

A minimal example (created with React Native v0.69.5 and TypeScript) with the default configuration and no specifically set regions. Thus, the default region everywhere (i.e. all beacons) is automatically used.

  1. Follow the setup instructions carefully for iOS and Android to install react-native-kontaktio for both platforms.
  2. Start a new React Native TypeScript project (npx react-native init BeaconTest --template react-native-template-typescript) and replace App.tsx with the example code below.
  3. Run the app on a real device (iOS or Android - not a simulator)
  4. Check the React Native logs to see console statements containing incoming beacon signals.
import React, { useEffect } from 'react';
import {
  Alert,
  DeviceEventEmitter,
  NativeEventEmitter,
  PermissionsAndroid,
  Platform,
  SafeAreaView,
  StatusBar,
  StyleSheet,
  Text,
  View,
} from 'react-native';

import Kontakt, { KontaktModule } from 'react-native-kontaktio';
const {
  connect,
  init,
  startDiscovery,
  startRangingBeaconsInRegion,
  startScanning,
} = Kontakt;

const kontaktEmitter = new NativeEventEmitter(KontaktModule);

const isAndroid = Platform.OS === 'android';

/**
 * Android Marshmallow (6.0) and above need to ask the user to grant certain permissions.
 * This function does just that.
 */
const requestLocationPermission = async () => {
  try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
      {
        title: 'Location Permission',
        message:
          'This example app needs to access your location in order to use bluetooth beacons.',
        buttonNeutral: 'Ask Me Later',
        buttonNegative: 'Cancel',
        buttonPositive: 'OK',
      }
    );
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      return true;
    } else {
      // permission denied
      return false;
    }
  } catch (err) {
    console.warn(err);
    return false;
  }
};

const beaconSetup = async () => {
  if (isAndroid) {
    // Android
    const granted = await requestLocationPermission();
    if (granted) {
      await connect();
      await startScanning();
    } else {
      Alert.alert(
        'Permission error',
        'Location permission not granted. Cannot scan for beacons',
        [{ text: 'OK', onPress: () => console.log('OK Pressed') }],
        { cancelable: false }
      );
    }
  } else {
    // iOS
    await init();

    /**
     * Will discover Kontakt.io beacons only
     */
    await startDiscovery();

    /**
     * Works with any beacon(also virtual beacon, e.g. https://github.com/timd/MactsAsBeacon)
     * Requires user to allow GPS Location (at least while in use)
     *
     * change to match your beacon values
     */
    await startRangingBeaconsInRegion({
      identifier: '',
      uuid: 'A4826DE4-1EA9-4E47-8321-CB7A61E4667E',
      major: 1,
      minor: 34,
    });
  }

  // Add beacon listener
  if (isAndroid) {
    /* works with any beacon */
    DeviceEventEmitter.addListener(
      'beaconsDidUpdate',
      ({ beacons, region }) => {
        console.log('beaconsDidUpdate', { beacons, region });
      },
    );
  } else {
    /* works with Kontakt.io beacons only */
    kontaktEmitter.addListener('didDiscoverDevices', ({ beacons }) => {
      console.log('didDiscoverDevices', { beacons });
    });

    /* works with any beacon */
    kontaktEmitter.addListener('didRangeBeacons', ({ beacons, region }) => {
      console.log('didRangeBeacons', { beacons, region });
    });
  }
};

const App: React.FC = () => {
  useEffect(() => {
    Promise.resolve().then(beaconSetup);

    return () => {
      // remove event listeners
      if (isAndroid) {
        kontaktEmitter.removeAllListeners('beaconsDidUpdate');
      } else {
        kontaktEmitter.removeAllListeners('didDiscoverDevices');
        kontaktEmitter.removeAllListeners('didRangeBeacons');
      }
    };
  }, []);

  return (
    <SafeAreaView>
      <StatusBar barStyle="dark-content" />
      <View style={styles.wrapper}>
        <Text style={styles.title}>react-native-kontaktio Example</Text>
        <Text>Check console.log statements</Text>
      </View>
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  wrapper: {
    height: '100%',
    justifyContent: 'center',
    alignItems: 'center',
  },
  title: {
    paddingVertical: 10,
    fontSize: 30,
  },
});

export default App;

Note (March 2020): The example in the Example/ folder is a bit outdated. If you want to try to run the example app anyway, here are some instructions to do so:

  1. Clone this repository, connect an Android and/or Apple device to your computer and have some (Kontakt.io) beacons nearby.

  2. Open a terminal window, bash to the Example/ folder, run npm install and start the react-native server

    $ cd react-native-kontaktio/Example
    $ npm install
    $ npm start
  3. Build the example and run it on your device. The app will appear under the name KontaktIoSimpleTest:

    • Android:

      $ react-native run-android
    • iOS

      $ react-native run-ios

Further notes

  • Beacons support is part of Android versions 4.3 and up. * So far the lowest Android version this library was tested on was a device with Android 4.4.2.
  • A physical device must be used for testing and some beacons (Kontakt.io beacons to be able to use all features).

ToDo:

  • Update Android Eddystone feature:

    • Add multiple Eddystone namespaces, i.e. add function setEddystoneNamespaces
    • Add Eddystone Frames Selection configuration option

Contribute

Test library changes locally

  1. Fork and clone this repository
  2. Run yarn to install the dependencies
  3. Make code changes
  4. Delete lib folder if it exists and run yarn tsc to compile the TypeScript files in the the lib folder.
  5. In the package.json file of an example app point to the this directory, e.g.

     "dependencies": {
       ...
       "react-native-kontaktio": "../react-native-kontaktio"
     },
  6. Build and run on a real device

Upgrade to a new version of the Kontakt.io SDK

Android

In build.gradle file change the version in the following line

implementation "io.kontakt.mvn:sdk:7.0.6"

iOS

  1. Go to the Kontakt.io SDK releases page and download the newest version of KontaktSDK.framework.zip.
  2. Replace the ios/KontaktSDK.framework folder of this library with the Cocoapods/KontaktSDK/iOS/KontaktSDK.xcframework/ios-arm64_armv7/KontaktSDK.framework folder which you find in the unzipped folder structure.

changelog

Changelog

All notable changes to this project will be documented in this file. If a contribution does not have a mention next to it, @andrekovac, @JonasWho or @martinpoulsen did it.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

Unreleased

Added

Changed

v4.1.0 - 2022-10-21

Changed

  • Update Setup documentation, add formerly undocumented secure profile information
  • fix new NativeEventEmitter warning
  • README: New minimal example code, new Contribute section
  • iOS: Upgrade Kontakt.io iOS SDK to version 3.0.25

v4.0.0 - 2022-09-26

Changed

  • Android: Update Kontakt.io SDK to version 7.0.6
  • Android: Update target SDK to 31 and add BLUETOOTH_SCAN and BLUETOOTH_CONNECT to permissions used.

v3.1.0 - 2022-02-17

Changed

  • Improve TypeScript types for secure profiles.
  • Android: Update Kontakt.io SDK to version 5.0.25-pre-31

v3.0.1 - 2022-02-16

Changed

  • Migrated library to TypeScript
  • Upgraded Example app to use React Native 0.65.2

v2.10.0 - 2021-11-26

Changed

  • Android: Migrate to androidx.
  • Android: Downgrade the Kontakt.io SDK to version 5.0.14 since it is the last version to target Android SDK 30 and since versions >= 5.0.15 might trigger the new Android 12 bluetooth permission model.

v2.9.0 - 2021-11-13

Changed

  • Android: Change the Kontakt.io SDK source and update to version 5.0.15

v2.8.0 - 2021-01-10

Changed

  • Android: Update Kontakt.io SDK to version 5.0.0
  • iOS: Update Kontakt.io SDK to version 3.0.4

v2.7.2 - 2020-03-06

Changed

  • Updated setup docs to include instructions on auto-linking
  • Added new minimal example to README which works with RN 0.61

v2.7.1 - 2020-02-22

Added

  • Support for RN >= 0.60 and autolinking (incl. pod install section), by @pkondrashkov (see #67)

Changed

  • Update of permission information, by @nylsoo (see #63)

v2.7.0 - 2019-09-17

Added

  • iOS: Update Kontakt.io SDK to version 1.5.1

Changed

  • iOS: Eddystone support
  • iOS: Other minor code fixes

v2.6.2 - 2019-06-07

Changed

  • Update KontaktBeacons.podspec

v2.6.1 - 2019-05-22

Changed

  • Added information about the event didDetermineState and the method requestStateForRegion to the docs.

v2.6.0 - 2019-05-09

Changed

  • Android: Don't set proximityManager to null in disconnect function.
  • Android: Make calls to isConnected function safer.
  • Android: Make calls to isScanning function safer.

v2.5.4 - 2019-02-18

Changed

  • Android: Improved behavior of the isConnected function to resolve with false if connect wasn't called before and only reject in case of other errors. With this fix, isConnected may be called anytime. So far calling it before connect was called lead to a Promise rejection which is counter-intuitive because you'd expect to call this function to check whether this is the case.

v2.5.0 - 2019-02-07

Added

  • iOS: New function requestStateForRegion and corresponding listener didDetermineState.

Changed

  • Improved iOS setup documentation: Added a troubleshooting case

v2.4.0 - 2018-12-19

Added

  • Android: Add event beaconInitStatus as an alternative to the returned Promise of the connect() call.

Changed

  • Android: Remove routine to prevent repeated promise resolve or reject calls for the same promise. After the second call to the library, this case would not resolve or reject anything which caused the Promise chain on the Javascript side to halt.
  • Android: Move isConnected() implementation into BeaconProximityManager file

v2.3.0 - 2018-11-08

Changed

  • Android: Wrap every call of so-far not-error-handled methods into an if-else statement which checks whether its object which is created after the connect() call is defined to avoid null-pointer exceptions.

v2.2.0 - 2018-11-08

Changed

  • Android: Upgraded gradle build tools to Version 25

v2.1.0 - 2018-06-24

Added

  • Android: Add Secure Profile. It is used when scanning for Beacon Pros, by @jampueroc (see #36)
  • Android: Add KontaktTelemetry (with Acceleration) to Secure Profile - only works with BeaconPros, by @Andruschenko (see #36)
  • Add CHANGELOG

Changed

  • Android: Update to SDK version 4.0.1 (Important: Version 4.0.0. removes support for Kontakt.io nRF51-based devices (Beacon, Tough Beacon, USB Beacon) with firmware < 4.0)
  • Fixed and updated parts of the documentation. Added images to the iOS setup guide.

Fixed

  • Handle illegal callback invocation exception, by @JonasWho (see #21)

v2.0.4 - 2017-10-25

Changed

  • Compatibility with React Native 0.48 and up, by @exKAZUu (see #19)

v2.0.0 - 2017-06-08

Added

  • iOS support: Discovery, ranging and monitoring possible
  • Android: Basic Eddystone support
  • Android: New method getBeaconRegions() to get all regions which are set to scan.

Changed

  • Android: Rename init() to connect() and improve functionality so that disconnect() is the opposite action.
  • Updated example apps
  • Improved documentation (start-up documentation tested for react-native versions below and after 0.40.0)

Fixed

  • Several bug fixes

Full changelogs