Closed kababu007 closed 5 years ago
public class BeaconLiveData extends MutableLiveData
private Context context;
private BeaconManager beaconManager;
private List<String> beaconUuidList;
private Region region;
private boolean isInitialized;
@Inject
public BeaconLiveData(Context context) {
this.context = context;
beaconUuidList = getBeaconUuidList();
if (!isInitialized) {
initializeBeaconManager();
}
}
@Override
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<Beacon> observer) {
super.observe(owner, observer);
}
@Override
protected void onActive() {
super.onActive();
startMonitoringAndRanging();
}
@Override
protected void onInactive() {
super.onInactive();
stopMonitoringAndRanging();
}
private void initializeBeaconManager() {
beaconManager = BeaconManager.getInstanceForApplication(context);
BeaconManager.setAndroidLScanningDisabled(true);
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(context.getString(R.string.beacon_layout)));
region = new Region("backgroundRegion", Identifier.parse(StringConstants.ENTRY_BEACON_UUID), null, null);
beaconManager.setBackgroundBetweenScanPeriod(ApplicationConstants.SCAN_PERIOD);
beaconManager.setForegroundBetweenScanPeriod(ApplicationConstants.SCAN_PERIOD);
beaconManager.bind(this);
isInitialized = true;
}
private List<String> getBeaconUuidList() {
return Arrays.asList(StringConstants.test_POS,
StringConstants.test_ADHOC,
StringConstants.test_GAS,
StringConstants.test_LOC,
StringConstants.test_ENTRY_BEACON_UUID);
}
private boolean istestBeacon(Beacon beacon) {
if (beaconUuidList != null && !beaconUuidList.isEmpty()) {
for (String uuid : beaconUuidList) {
if (beacon.getId1().toString().equals(uuid)) {
return true;
}
}
}
return false;
}
private void startMonitoringAndRanging() {
if (beaconManager != null && isInitialized) {
beaconManager.addRangeNotifier(this);
beaconManager.addMonitorNotifier(this);
} else {
initializeBeaconManager();
}
}
private void stopMonitoringAndRanging() {
if (beaconManager != null) {
beaconManager.removeRangeNotifier(this);
beaconManager.removeMonitorNotifier(this);
}
}
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) {
if (collection != null && collection.size() > 0) {
//beacons were detected;
for (Beacon beacon : collection) {
if (beacon != null && istestBeacon(beacon)) {
setValue(beacon);
return;
}
}
} else {
//no beacons in the region;
setValue(null);
Logger.d("Beacon has fallen out of range");
}
}
@Override
public void onBeaconServiceConnect() {
if (beaconManager != null) {
beaconManager.addRangeNotifier(this);
beaconManager.addMonitorNotifier(this);
try {
beaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException ex) {
Logger.e(ex.getMessage());
}
}
}
@Override
public void didEnterRegion(Region region) {
}
@Override
public Context getApplicationContext() {
return context;
}
@Override
public void unbindService(ServiceConnection serviceConnection) {
}
@Override
public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
return false;
}
@Override
public void didExitRegion(Region region) {
}
@Override
public void didDetermineStateForRegion(int i, Region region) {
}
}
Sorry I mentioned the function name wrong. onBeaconServiceConnect() is the one NOT getting called in 6.0 and 7.0.
Expected behavior
didRangeBeaconsInRegion() Should get called
Actual behavior
didRangeBeaconsInRegion() does not get called
Steps to reproduce this behavior
Use MVVM with LiveData instead of implementing BeaconConsumer in fragment / activity [Code attached]. It works fine when we implement BeaconConsumer in fragment or activity.
Mobile device model and OS version
Motorola Android 7.0 API 24 Tango Lenova 6.0
Android Beacon Library version
2.15.2
IMPORTANT: This forum is reserved for feature requests or reproducible bugs with the library itself. If you need help with using the library with your project, please open a new question on StackOverflow.com.
BeaconLiveData.pdf