AltBeacon / android-beacon-library

Allows Android apps to interact with BLE beacons

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Android beacon library - multiple regions monitoring - unable remove regions

Saroja-T opened this issue · comments

Expected behavior

In user mode, I've created four regions using the RegionMonitor class. Upon logout, I stop monitoring all regions using the "stopMonitoring" method. For instance, in user mode, add four regions and find one of them, then in guest mode, remove all regions from the monitored.

Actual behavior

In user mode, I've created four regions using the RegionMonitor class. Upon logout, I stop monitoring all regions using the "stopMonitoring" method. However, when logging in as a guest, I still observe that a region is found, despite having set the number of regions to 0 using bootstrapRegion.addRegion. Is it possible to add functions to stop all processes? For instance, in user mode, add four regions and find one of them, then in guest mode, remove all regions from the monitored and ranging lists?

Steps to reproduce this behavior

for (macAddress in macAddressList) {
Log.e("Before loop", "regionlist size: $macAddress")
val region = Region(macAddress, macAddress)
MonitorBeaconsRegion(
macAddress,
macAddress,
beaconManager,
).createRegion()
MonitorBeaconsRegion(
macAddress,
macAddress,
beaconManager
).startMonitoring()
}


class MonitorBeaconsRegion(
regionNameParam: String,
regionAddressParam: String,
beaconManagerParam: BeaconManager,
apicontext: Context,
) {
private val regionName: String = regionNameParam
private val regionAddress: String = regionAddressParam
private val beaconManager: BeaconManager = beaconManagerParam
private val pagecontext: Context =apicontext
private var count: Int = 0
private var objSharedPref: PrefUtils? = null

@RequiresApi(Build.VERSION_CODES.R)
fun createRegion() {
    objSharedPref = PrefUtils(pagecontext)
    val region = Region(regionName, regionAddress)

    // These two lines set up a Live Data observer so this Activity can get beacon data from the Application class
    val regionViewModel = beaconManager.getRegionViewModel(region)
    // observer will be called each time the monitored regionState changes (inside vs. outside region)
    regionViewModel.regionState.observeForever(regionMonitoringObserver)
    // observer will be called each time a new list of beacons is ranged (typically ~1 second in the foreground)
    regionViewModel.rangedBeacons.observeForever(regionRangingObserver)
}

fun getUserModel(): UserModel? {
    val gson = Gson()
    return gson.fromJson(
        objSharedPref?.getString("user_response"), UserModel::class.java
    )
}

fun startMonitoring() {
    val region = Region(regionName, regionAddress)
    beaconManager.startMonitoring(region)
    beaconManager.startRangingBeacons(region)
}

fun stopMonitoring() {
    val region = Region(regionName, regionAddress)
    beaconManager.stopRangingBeacons(region)
    beaconManager.stopMonitoring(region)
}

@RequiresApi(Build.VERSION_CODES.R)
val regionMonitoringObserver = Observer<Int> { state ->
    if (state == MonitorNotifier.OUTSIDE) {
        Log.d("sendNotification", "You are OUTSIDE the premises: $regionAddress")
        sendNotification(regionAddress.toString())
    } else {
        Log.d("sendNotification", "You are * INSIDE * the premises: $regionAddress")
        sendNotification(regionAddress.toString())
    }
}

private val regionRangingObserver = Observer<Collection<Beacon>> { beacons ->
    Log.d("regionRangingObserver", "Ranged: ${beacons.count()} beacons")
    for (beacon: Beacon in beacons) {
        Log.d(BDApplication.TAG, "$beacon about ${beacon.distance} meters away")
    }
}

private fun sendNotification(notificationText: String) {
    count += 1
    val contentText = notificationText + count.toString()
    val builder = NotificationCompat.Builder(pagecontext, "bdapp-notification-id")
        .setContentTitle("bzBoss")
        .setContentText(contentText)
        .setSmallIcon(R.drawable.ic_app)
    val stackBuilder = TaskStackBuilder.create(pagecontext)
    stackBuilder.addNextIntent(Intent(pagecontext, SplashActivity::class.java))
    val resultPendingIntent = stackBuilder.getPendingIntent(
        0,
        PendingIntent.FLAG_UPDATE_CURRENT + PendingIntent.FLAG_IMMUTABLE
    )
    builder.setContentIntent(resultPendingIntent)
    val notificationManager =
        pagecontext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.notify(1, builder.build())
}

}

Mobile device model and OS

version all devices

Android Beacon Library version

2.19.6

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.

In the "Actual Behavior" section you say " I still observe that a region is found, despite having set the number of regions to 0 using bootstrapRegion.addRegion. Is it possible to add functions to stop all processes? " but I don't see anything in the code you showed that calls bootstrapRegion.addRegion. Can you please share the code that does this?

By using this function am adding regions
for (macAddress in macAddressList) {
Log.e("Before loop", "regionlist size: $macAddress")
val region = Region(macAddress, macAddress)
MonitorBeaconsRegion(
macAddress,
macAddress,
beaconManager,
).createRegion()
MonitorBeaconsRegion(
macAddress,
macAddress,
beaconManager
).startMonitoring()
}