Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android beacon library - multiple regions monitoring - unable remove regions #1171

Open
Saroja-T opened this issue Nov 9, 2023 · 2 comments

Comments

@Saroja-T
Copy link

Saroja-T commented Nov 9, 2023

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.

@davidgyoung
Copy link
Member

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?

@Saroja-T
Copy link
Author

Saroja-T commented Dec 8, 2023

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()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants