简体   繁体   中英

Monitoring for more than 20 BLE beacon regions using CLLocationmanager in iOS

Currently I am using CLLocationmanager to monitor for BLE beacon regions in iOS.

I know I can range beacons if i want more than 20 regions but unfortunately ranging would not allow me to register entry(RegionDidEnter) and exit(RegionDidExit) events as far as I know.

In my use case I need to trigger actions on user's entry and user's exit in a particular beacon region even when app is in killed state or in background.

I need a efficient way to do this as if I look for significant location changes it also uses battery and also using beacons would not make much sense then if i use GPS.

When didEnter happens, iOS will launch your app into the background and give it a few seconds of execution time to handle the event. You can use that time to start ranging, receive the ranging results, and since ranging always provides full UUID/major/minor info, trigger an appropriate action based on that.

Pseudo-code:

let myUUID = x
startMonitoring(myUUID)

func onDidEnter {
    startRanging(myUUID)
}

func onDidRange(beacons) {
    if beacons.empty { return } // keep ranging until we find something

    let major = beacons.first.major
    if major == 1 { show("Welcome to X") }
    if major == 2 { show("Welcome to Y") }

    stopRanging(myUUID)
}

To ensure that your app doesn't get put back to sleep before it manages to range a beacon, you can also use a background task , then the (pseudo-)code would look something like:

func onDidEnter {
    self.task = beginBackgroundTask(expirationHadler: {
        // our background time is up, iOS requires us to finish our work
        stopRanging(myUUID)
        endBackgroundTask(self.task)
    })
    startRanging(myUUID)
}

func onDidRange(beacons) {
    if beacons.empty { return }

    let major = beacons.first.major
    if major == 1 { show("Welcome to X") }
    if major == 2 { show("Welcome to Y") }

    stopRanging(myUUID)
    endBackgroundTask(self.task)
}

You can add a workaround to this. Register only those regions near to the user location. When the location changes, you can remove regions that are now farther way and add regions coming up on the user's path.

To save battery when dealing with location, register for significant-change location updates or make use of defer location updates or use visit monitoring.

Why Core Location limited to 20

Regions are a shared system resource, and the total number of regions available systemwide is limited. For this reason, Core Location limits to 20 the number of regions that may be simultaneously monitored by a single app. To work around this limit,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM