DeviceEventEmitter listener not firing in detached app

#1

Hi, great project! It would be awesome if Bluetooth beacons and geo fences would be implemented. I noticed it’s on the radar (iBeacon support).

I’ve did some testing where I created an Expo project and detached it with $ exp detach.

Then, I’ve installed $ npm install react-native-beacons-manager --save, ran $ react-native link and updated the build gradle as described here, https://docs.expo.io/versions/v15.0.0/guides/expokit.html:

Note: You may have to update android/app/build.gradle after running react-native link. Change the line added by react-native link from compile project(’:library-name’) to compile(project(’:library-name’)) { exclude module: ‘react-native’ }.

In main.js I’ve added DeviceEventEmitter to the third line:

import { Platform, StatusBar, StyleSheet, View, DeviceEventEmitter } from 'react-native';

And below the imports I’ve added this code:

import Beacons from 'react-native-beacons-manager';

// Tells the library to detect iBeacons
Beacons.detectIBeacons();

// Monitoring
try {
  const myRegion = {
    identifier: 'BEACON',
    uuid: 'FDA50693-A4E2-4FB1-AFCF-C6EB07647825',
    minor: 54480,
    major: 10004
  };

  Beacons.startMonitoringForRegion(myRegion);

  console.log('Beacons monitoring started successfully');
} catch (err) {
  console.log('Beacons monitoring not started, error: ${err}');
}

DeviceEventEmitter.addListener('regionDidEnter', (region) => {
  console.log('regionDidEnter!', region);
});

DeviceEventEmitter.addListener('regionDidExit', (region) => {
  console.log('regionDidExit!', region);
});

All seems to work fine and I get the console.log Beacons monitoring started successfully, however, DeviceEventEmitter.addListener('regionDidEnter', ...); is never fired.

When I run the same code in a fresh non-Expo project (with react-native init) the listeners are triggered as expected. I understand detach is an alpha feature, so maybe it’s an issue or maybe I’m missing something :smiley:

#2

Hm i’m not sure why that wouldn’t work. Is your code uploaded somewhere?

#3

I’ve double checked by creating a blank project with the Expo XDE.

Added the android package to exp.json:

  "android": {
    "package": "com.xxx.beacontest"
  }

Ran these commands with the CLI:

$ exp detach

$ npm install react-native-beacons-manager --save

$ react-native link

Updated android/app/build.gradle: https://gist.github.com/sem-nowsquare/bf88b53d6de297be654d1691e034e368#file-build-gradle

Replaced:

compile project(':react-native-beacons-manager')

with:

compile(project(':react-native-beacons-manager')) { exclude module: 'react-native' }

Edited main.js: https://gist.github.com/sem-nowsquare/bf88b53d6de297be654d1691e034e368#file-main-js

Openend the project in Expo XDE and ran the app with $ react-native run-android in the CLI.

The monitoring starts according to console, but DeviceEventEmitter isn’t triggered. Same beacon with a non-Expo project works.

#4

Thanks for the details! Can you upload both of those to Github? There are a bunch of small details that could be breaking this and it’ll be easier if I can just run the projects myself.

#5

Got it: https://github.com/sem-nowsquare/RN-Expo-Beacon-Test

It works fine btw with CRNA and npm run eject.

#6

Forgot to add the working repo: https://github.com/sem-nowsquare/RN-CRNA-Beacon-Test

#7

Great. I probably don’t have time to look at this today but maybe this weekend.

#8

Hi there,

Just getting started with expo but it looks great. Wanted to follow up on this thread to see if there’s any progress on having beacon support through an SDK in expo, instead of having to eject the app and built it outside of expo. Thanks!

#9

We recently released some changes to detach that should fix this. If you detach with SDK 16 it should work. Sorry for the delay @eugyev and @s3ms3m

#10

Thank you for the update. We will likely use this soon.

I did have one question. Do you have plans to have an SDK that supports beacons without ejecting the app? My understanding is that once we eject we lose a lot of the benefits expo offers, including the code push and push notifications.

#11

Thanks, I did some quick testing but ran into issues probably related to outdated versions. I will test in more detail when I have time.

#12

We want to support beacons eventually but probably not in the next few months.