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

Service not closing properly #26

Closed Sfavilla closed 10 years ago

Sfavilla commented 10 years ago

Hi, I am using your library to develop an Application. I have performed some current drain measurement and found the following results on Samsung SIII: Bluetooth scan in foreground 66 mA Bluetooth scan in background 16mA I have bound IBeaconManager to an intent service. I noticed that after calling the onDestroy method of the IntentService:

public void onDestroy(){

    Log.d(ConstantsDecoder.LOGTAG,"INTENT SERVICE BT ON DESTROY()");
    super.onDestroy();
    if(ConstantsDecoder.isBT_ON() && iBeaconManager!=null) {
        try {
            iBeaconManager.stopRangingBeaconsInRegion(region);
            } catch (RemoteException e) {
        // TODO Auto-generated catch block
                e.printStackTrace();
            }
        if (iBeaconManager.isBound(this))
            iBeaconManager.unBind(this);
    }
}

The Bluetooth service remains in some way active I see this log continuously :

D/BtGatt.btif(3092): btif_gattc_upstreams_evt: Event 4096

and the current consumption remains at 66mA. Please, could you help?

davidgyoung commented 10 years ago

Unbinding to the service can be tricky due to complexities of the Android lifecycle. Make sure you actually see the log line ""INTENT SERVICE BT ON DESTROY()". If the code doesn't get executed, the unbinding won't happen. You can move it elsewhere if necessary.

Also, make sure that a second application is not running on your phone that uses the service. Two applications using the service run independent instances.

There were some potential race conditions that could have stopped unbinding prior to the 0.7.4 version of the library, which have since been patched. See: https://github.com/RadiusNetworks/android-ibeacon-service/issues/21

Make sure you have the 0.7.5 release of the library. I have been unable to reproduce failures to stop the service with that version of the library. If you can provide the source code of an Android app that demonstrates a failure to stop the service with 0.7.5, along with instructions on how to reproduce, I would love to see it.