Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

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

Open
Ventis opened this issue Sep 19, 2017 · 4 comments
Assignees

Comments

@Ventis
Copy link

Ventis commented Sep 19, 2017

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.

@Ventis Ventis changed the title App crashed when stopping the IBeaconScanner in a service that destroys itself afterwards App crashes when stopping the IBeaconScanner in a service that destroys itself afterwards Sep 19, 2017
@eliaslecomte
Copy link
Member

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
Copy link
Author

Ventis commented Sep 20, 2017

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
Copy link

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

@eliaslecomte
Copy link
Member

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).

@eliaslecomte eliaslecomte self-assigned this Nov 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants