BlueSword BlueSword - 4 months ago 14
Android Question

Gimbal "BeaconSighting" listener working erratically

I am trying to obtain the Gimbal beacon distance changes run-time in Android. I am able to connect to my beacon and same is reflected in the android app.

But I am not able to obtain the changed RSSI in the app. Below is my code.

private PlaceManager placeManager;
private PlaceEventListener placeEventListener;
private ArrayAdapter<String> listAdapter;
private ListView listView;
private BeaconManager bm;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1);
listView = (ListView) findViewById(R.id.list);
listView.setAdapter(listAdapter);

listAdapter.add("Setting API Key");
listAdapter.notifyDataSetChanged();
Gimbal.setApiKey(this.getApplication(), "my key");

bm = new BeaconManager();
bm.addListener(new BeaconEventListener() {
@Override
public void onBeaconSighting(BeaconSighting beaconSighting) {

super.onBeaconSighting(beaconSighting);

Log.d("beacon1", beaconSighting.getBeacon().toString());
Log.d("beacon2", String.format("", beaconSighting.getRSSI()));
}
});
bm.startListening();


placeEventListener = new PlaceEventListener() {

@Override
public void onVisitStart(Visit visit) {

super.onVisitStart(visit);

listAdapter.add(String.format("Start Visit for %s", visit.getPlace().getName()));
listAdapter.notifyDataSetChanged();

Log.d("1", String.valueOf(visit.getArrivalTimeInMillis()));
Log.d("2", String.valueOf(visit.getDepartureTimeInMillis()));
Log.d("3", String.valueOf(visit.getPlace()));
Log.d("4", String.valueOf(visit.getDwellTimeInMillis()));
/*BeaconSighting demo = new BeaconSighting();
Beacon beac = demo.getBeacon();
Log.d("", beac.getUuid());*/
}

@Override
public void onVisitEnd(Visit visit) {

super.onVisitEnd(visit);

listAdapter.add(String.format("End Visit for %s", visit.getPlace().getName()));
listAdapter.notifyDataSetChanged();

Log.d("ending","ending");
}
};

placeManager = PlaceManager.getInstance();
placeManager.addListener(placeEventListener);
placeManager.startMonitoring();

CommunicationManager.getInstance().startReceivingCommunications();


Below is my log output.

11-24 00:18:49.128 9801-9801/com.gimbal.hello_gimbal_android D/1: 1448353129095
11-24 00:18:49.128 9801-9801/com.gimbal.hello_gimbal_android D/2: 1448353076508
11-24 00:18:49.128 9801-9801/com.gimbal.hello_gimbal_android D/3: Place [identifier=D4786F227DA24C56D46884FD8A33F97E, name=abc, attributes=com.gimbal.android.a.a@123]
11-24 00:18:49.128 9801-9801/com.gimbal.hello_gimbal_android D/4: -52587
11-24 00:18:49.208 9801-9801/com.gimbal.hello_gimbal_android D/1: 1448353129095
11-24 00:18:49.208 9801-9801/com.gimbal.hello_gimbal_android D/2: 1448353076508
11-24 00:18:49.208 9801-9801/com.gimbal.hello_gimbal_android D/3: Place [identifier=6D63654565EA48D38D2D1F81E1D558F4, name=xyz, attributes=com.gimbal.android.a.a@456]
11-24 00:18:49.208 9801-9801/com.gimbal.hello_gimbal_android D/4: -52587
11-24 00:18:49.226 9801-9801/com.gimbal.hello_gimbal_android D/1: 1448353129095
11-24 00:18:49.226 9801-9801/com.gimbal.hello_gimbal_android D/2: 1448353076508
11-24 00:18:49.226 9801-9801/com.gimbal.hello_gimbal_android D/3: Place [identifier=A6E32B4B5FBF5849F7ED78FBF02D701, name=lmn, attributes=null]
11-24 00:18:49.226 9801-9801/com.gimbal.hello_gimbal_android D/4: -52587
11-24 01:33:39.383 21329-21329/com.gimbal.hello_gimbal_android D/beacon1: Beacon [identifier=uid123, name=abc, iconURL=null, batteryLevel=HIGH, temperature=74]
11-24 01:33:39.383 21329-21329/com.gimbal.hello_gimbal_android D/beacon2: -62


Here lies problem.

Log "beacon1" and "beacon2" gets printed randomly/erratically. I am not able to understand why "BeaconEventListener" has such behavior. Most of the times its not printed at all. Due to this I am not roadblocked as I am not able to get the distance between my mobile and beacon, when moving.

Edit

Was able to extract more error log. Added last 2 lines in logcat output.

Any help is appreciated.

Answer

Gimbal team pretty much resolved this issue, so anyone having this issue can get help in future too.

As in my OP, you could see below is original part of my code.

bm = new BeaconManager();
bm.addListener(new BeaconEventListener() {
    @Override
    public void onBeaconSighting(BeaconSighting beaconSighting) {

        super.onBeaconSighting(beaconSighting);

        Log.d("beacon1", beaconSighting.getBeacon().toString());
        Log.d("beacon2", String.format("", beaconSighting.getRSSI()));
    }
});
bm.startListening();

Now we have to add onBeaconSighting() in PlaceEventListener() too. So the new part of the code would be as below.

 placeEventListener = new PlaceEventListener() {

    @Override
    public void onVisitStart(Visit visit) {

        super.onVisitStart(visit);
        //do anything
    }

     @Override
     public void onVisitEnd(Visit visit) {

        super.onVisitEnd(visit);
        //do anything
     }

     public void onBeaconSighting(BeaconSighting sighting, List<Visit> visits) { 
     // This will be invoked when a beacon assigned to a place within a current visit is sighted. 
    }
  }

The reason why we added a duplicate or rather another listener was replied by Gimbal team as below.

Me : Can you inform me as why adding a duplicate listener made the code work ? As this listener is already handled in BeaconManger, it should have worked.

Gimbal Team : onBeaconSighting in PlaceManager will will be invoked when a beacon assigned to a place within a current visit is sighted. onBeaconSighting in BeaconManager will be invoked when a beacon is sighted.

I think I made pretty much clear as where the issue was and how it was resolved. Hope it helps you too.

Cheers.