MacKentoch / react-native-beacons-manager

React-Native library for detecting beacons (iOS and Android)
MIT License
579 stars 318 forks source link

Crash on launch with 1.1.0 (master) #66

Open EliotAndres opened 6 years ago

EliotAndres commented 6 years ago

First of all, thank you for this plugin !

The current master branch (5f0d44e850e607bd2629476bbf701beec3c4d093) crashes on Android. I think it is because the beaconServiceConnected event is sent too early.

A quick and dirty fix is to comment the line: sendEvent(mReactContext, "beaconServiceConnected", null); in BeaconsAndroidModule.java

Version

1.1.0

Platform

Android

OS version

android 6.0

Steps to reproduce

  1. Launch demo App on Android. Sometimes it takes several launches to crash
11-21 17:39:58.957 22955 22955 V BeaconsAndroidModule: onBeaconServiceConnect
11-21 17:39:58.957 22955 22955 D AndroidRuntime: Shutting down VM
11-21 17:39:58.957 22955 23790 D ReactNative: Initializing React Xplat Bridge after initializeBridge
11-21 17:39:58.957 22955 23790 D ReactNative: CatalystInstanceImpl.runJSBundle()
11-21 17:39:58.957 22955 22955 E AndroidRuntime: FATAL EXCEPTION: main
11-21 17:39:58.957 22955 22955 E AndroidRuntime: Process: com.demo, PID: 22955
11-21 17:39:58.957 22955 22955 E AndroidRuntime: java.lang.RuntimeException: Tried to access a JS module before the React instance was fully set up. Calls to ReactContext#getJSModule should only happen once initialize() has been called on your native module.
11-21 17:39:58.957 22955 22955 E AndroidRuntime:    at com.facebook.react.bridge.ReactContext.getJSModule(ReactContext.java:105)
11-21 17:39:58.957 22955 22955 E AndroidRuntime:    at com.mackentoch.beaconsandroid.BeaconsAndroidModule.sendEvent(BeaconsAndroidModule.java:404)
11-21 17:39:58.957 22955 22955 E AndroidRuntime:    at com.mackentoch.beaconsandroid.BeaconsAndroidModule.onBeaconServiceConnect(BeaconsAndroidModule.java:243)
11-21 17:39:58.957 22955 22955 E AndroidRuntime:    at org.altbeacon.beacon.BeaconManager$BeaconServiceConnection.onServiceConnected(BeaconManager.java:933)
ashemah commented 6 years ago

Hi, just FYI I am also seeing this issue.

mojotti commented 6 years ago

Hi,

This issue can be avoided by adding this to 'node_modules/react-native-beacons-manager/com/mackentoch/beaconsandroid/BeaconsAndroidModule.java':

  if (reactContext.hasActiveCatalystInstance()) {   <---- add this
      reactContext
                .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit(eventName, params);
  }      <---- and this
brayanAP commented 5 years ago

Hi, Could you provide me with an example of how you solved this problem?

Pacn91 commented 5 years ago

Hi @brayanAP, I did what @mojotti wrote and it works.

Go to: node_modules/react-native-beacons-manager/android/src/main/java/com/mackentoch/beaconsandroid/BeaconsAndroidModule.java'

Then change this method from this (line 404 for me):

private void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
      reactContext
              .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
              .emit(eventName, params);
  }

to this:

private void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
    if (reactContext.hasActiveCatalystInstance()) {
      reactContext
              .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
              .emit(eventName, params);
    }
  }

Restart app and it should work ;)