Greetings, I am working on the Ranging Example from the airport app. The application works fine when it is in specified beacon regions (Major Minor provided). However, as soon as the phone is placed outside the beacon region, it crashes the application.
Following is my Main activity:
package com.example.test.shoppingcart;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;
import com.estimote.sdk.Beacon;
import com.estimote.sdk.BeaconManager;
import com.estimote.sdk.Region;
import com.estimote.sdk.SystemRequirementsChecker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import static com.example.test.shoppingcart.R.id.ImageView;
public class MainActivity extends AppCompatActivity {
private static final Map<String, List<String>> PLACES_BY_BEACONS;
// TODO: replace "<major>:<minor>" strings to match your own beacons.
static {
Map<String, List<String>> placesByBeacons = new HashMap<>();
placesByBeacons.put("xxx", new ArrayList<String>() {{
add("m1");
}});
placesByBeacons.put("xxx", new ArrayList<String>() {{
add("m2");
}});
PLACES_BY_BEACONS = Collections.unmodifiableMap(placesByBeacons);
}
private List<String> placesNearBeacon(Beacon beacon) {
String beaconKey = String.format("%d:%d", beacon.getMajor(), beacon.getMinor());
if (PLACES_BY_BEACONS.containsKey(beaconKey)) {
return PLACES_BY_BEACONS.get(beaconKey);
}
return Collections.emptyList();
}
private BeaconManager beaconManager;
private Region region;
//TextView textView;
public static ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// textView= (TextView)findViewById(R.id.tv_test);
imageView = (ImageView)findViewById(ImageView);
// imageView.setImageResource(R.drawable.img4);
beaconManager = new BeaconManager(this);
beaconManager.setRangingListener(new BeaconManager.RangingListener() {
@Override
public void onBeaconsDiscovered(Region region, List<Beacon> list) {
if (!list.isEmpty()) {
Beacon nearestBeacon = list.get(0);
List<String> places = placesNearBeacon(nearestBeacon);
// textView.setText(places.get(0));
if((places.get(0)).equals("m1")){
imageView.setImageResource(R.drawable.img2);
}
else if((places.get(0)).equals("m2")){
imageView.setImageResource(R.drawable.img3);
}
// TODO: update the UI here
Log.d("Airport", "Nearest places: " + places);
}
}
});
region = new Region("ranged region", UUID.fromString("xxxxxx"), null, null);
}
@Override
protected void onResume() {
super.onResume();
SystemRequirementsChecker.checkWithDefaultDialogs(this);
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
beaconManager.startRanging(region);
}
});
}
@Override
protected void onPause() {
beaconManager.stopRanging(region);
super.onPause();
}
}