inthepocket / ibeacon-scanner-android

Android library to scan for iBeacons.
MIT License
70 stars 21 forks source link

App crashes when stopping the IBeaconScanner in a service that destroys itself afterwards #18

Open Ventis opened 7 years ago

Ventis commented 7 years ago

Hi,

I'm using your library in a react-native project where on user logout we want to stop the ibeaconscanner. We do this by sending a broadcast which we catch in a service called BeaconService:

public class UserStopServiceReceiver extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            BeaconService.this.destroyIntentional = true;
            IBeaconScanner.getInstance().stop();
            BeaconService.this.stopSelf();
        }
    }

However, not long after logout this crashes the app with the follow error:

09-19 17:38:50.008 19149-19149/be.imec.apt.wappr.debug E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: be.imec.apt.wappr.debug, PID: 19149
                                                                         java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.le.BluetoothLeScanner android.bluetooth.BluetoothAdapter.getBluetoothLeScanner()' on a null object reference
                                                                             at mobi.inthepocket.android.beacons.ibeaconscanner.DefaultBluetoothFactory.canAttachBluetoothAdapter(DefaultBluetoothFactory.java:46)
                                                                             at mobi.inthepocket.android.beacons.ibeaconscanner.DefaultScanService.timedOut(DefaultScanService.java:133)
                                                                             at mobi.inthepocket.android.beacons.ibeaconscanner.handlers.TimeoutHandler$1.run(TimeoutHandler.java:45)
                                                                             at android.os.Handler.handleCallback(Handler.java:751)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                             at android.os.Looper.loop(Looper.java:154)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

This is how we init the iBeaconScanner in the service:

@Override
    public void onCreate()
    {
        super.onCreate();

        Log.i(TAG, "Beacon service is initializing");

        IBeaconScanner.initialize(IBeaconScanner.newInitializer(this).build());
        IBeaconScanner.getInstance().setCallback(this);

        userStopServiceReceiver = new UserStopServiceReceiver();
        registerReceiver(userStopServiceReceiver,  new IntentFilter(INTENT_USER_STOP_SERVICE));
    }

After a logout the timedOutHandler seems to be called and this triggers a null pointer exception because bluetoothAdapter is null.

eliaslecomte commented 7 years ago

Hey,

I understand that the issue is that, when you are inside a beacon, and stop monitoring, that the timeout (to trigger the exit callback) encounters a nullpointer exception as you stopped the service.

I'll have to fix the behavior. When you stop, the exit timeouts should be removed.

This is part of our next release, https://github.com/inthepocket/ibeacon-scanner-android/projects/1, however the problem is that I don't now exactly when this release is going to happen :/.

Ventis commented 7 years ago

Yes, that is correct. Unfortunately it seems to trigger every time on the Android Emulator, causing a crash. Even though we don't start monitoring if EmulatorUtils.isEmulator() is true.

wdalbakri commented 6 years ago

hello , I will product special I beacon, I need to virtual I beacon connect with android app . please help me !

eliaslecomte commented 4 years ago

Two years later...

The problem is a simple null pointer exception that only occurs on emulators or more recent Android versions (9+ I suppose) when bluetooth is turned off.

BluetoothAdapter.getDefaultAdapter();

I will fix this bug (pr coming soon).