RadiusNetworks / android-ibeacon-service

An Android library providing APIs to interact with iBeacons
http://developer.radiusnetworks.com/ibeacon/android/index.html
681 stars 244 forks source link

Problems with unbinding #21

Closed cosminstefanxp closed 10 years ago

cosminstefanxp commented 10 years ago

There seem to be some issues with properly unbinding: even after calling the IBeaconManager.unbind() method, the background service still does scans and does not always stop (probably race condition).

Situation:

  1. In onPause() I set the background mode:
if (mBeaconManager.isBound(this)) mBeaconManager.setBackgroundMode(this, true);
  1. In onStop() I unbind:
 mBeaconManager.unBind(this);

Due to the fact that when stopping the app, it first starts the background mode, probably goes to sleep for 1s and does not do any checks for state or existing Consumers.

davidgyoung commented 10 years ago

I will try to reproduce.

diegotori commented 10 years ago

I too am experiencing the same thing, where it still lives even after onDestroy is called on IBeaconService. What i've noticed is that it's trying to call stopLeScan twice in that method, first via scanLeDevice(false), and then by directly invoking it through the BluetoothAdapter member variable. Also, when using a Handler, you always remove callbacks and messages from it when destroying your activity/service, or else it'll prevent the class from fully finishing/stopping. Right now, it's not clearing it in onDestroy(), so perhaps that might be it.

Edit: I was able to successfully stop the service after unbinding by calling " handler.removeCallbacksAndMessages(null)" before stopping the LE device:

@Override
public void onDestroy() {
    Log.i(TAG, "onDestory called.  stopping scanning");
    handler.removeCallbacksAndMessages(null);
    scanLeDevice(false);
    if (bluetoothAdapter != null) {
        bluetoothAdapter.stopLeScan(leScanCallback);
        lastScanEndTime = new Date().getTime();
    }
}
davidgyoung commented 10 years ago

I have not been able to reproduce these issues using the reference application on github. Each time I hit the back button, it stops the service successfully. That said, the code proposal above sounds reasonable, so I have applied the change in ccaf2f8f9a7723275ab1808da3052480ab14d92f and put this into version 0.7.5

davidgyoung commented 10 years ago

I am closing this issue since there have not been any additional reports or feedback since the 0.7.5 release which implemented changes to address this. Please re-open a related issue if specific problems remain.