Android IBeaconManager not connecting from activit

2019-08-30 15:34发布

问题:

I'm using the IBeacon SDK from radius networks on the android. Im currently using the IBeaconManager in one Activity and one Service. When the app first launches, The activity binds its iBeaconManager object however onIBeaconServiceConnect never gets called. Calling any ranging or monitoring functions ex startRangingBeaconsInRegion(region) is throwing a RemoteException:

 The IBeaconManager is not bound to the service.  Call iBeaconManager.bind(IBeaconConsumer consumer) and wait for a callback to onIBeaconServiceConnect()

Surprisingly, iBeaconMananger.isBound(myActivity) is returning true.

Now when I start my service (unbind from my activity and bind to my service), the binding works fine and I can start monitoring for beacons. If then, I open the activity again (after unbinding from my Service and binding to my Activity), it now works in my Activity too.

So only when the app first launches, the onIBeaconServiceConnect() is not being called for my Activity.

any help is greatly appreciated.


Update

MyActivity (BaseActivity is a subclass of Activity):

public class MyActivity extends BaseActivity implements IBeaconConsumer {

    private IBeaconManager iBeaconManager;
    LocationManager locationManager;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_survey);

        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        ...

        iBeaconManager = IBeaconManager.getInstanceForApplication(this);
        iBeaconManager.bind(this);
    }

    @Override
    public void onIBeaconServiceConnect() {
        Log.d("mytag","beacon service connected");
        iBeaconManager.setRangeNotifier(new RangeNotifier() {
            @Override
            public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
                Log.d("mytag","did range beacons");

        });
    }
}

回答1:

I ended up moving the beaconManager to my Application subclass instead of using it in my activity.