Beacon Ranging in Background on iOS

I understand the difference between monitoring and ranking, and I understand that iOS restriction in this range of beacons can only happen in the foreground or in the background when entering and leaving regions, as described here ( http://developer.radiusnetworks.com/ 2013/11/13 / ibeacon-monitoring-in-the-background-and-foreground.html ). But I'm trying to figure out how to solve the general scenario.

If I had a lot of lighthouses installed in a department store, how should I detect when a person moves within the range of these lighthouses? According to how it is currently running, the application will receive an event when the user enters the repository ( didEnterRegion ), because the collection of all beacons acts as one large area. But there is no way to know that the user is moving between different sections of the store if the beacons are not located far enough so that the user can exit and enter the area again, which is probably impractical.

The reason I want to settle for beacons in the background is because I may need to know that the user is in a particular section / product in the store in order to display specific offers / information (via notification) for this section without need to open the application.

It seems to me that this is a very common scenario for shopping centers and museums, etc. I am wondering how other developers have decided this or is there any other way to achieve what I want.

I did not include code snippets here because the problem is not in the code, it is just a conceptual problem. If any clarification or code is needed, I can add this too.

thanks

+5
source share
3 answers

The biggest part of the answer is the method described by my colleague @csexton in another answer to this question.

To solve the second problem, having received only 10 seconds of ranking time after the transition, you can request additional time to save. iOS allows you to navigate in the background for 180 seconds. This does not require background modes and special permission from the AppStore.

Here's how you install it:

 - (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region { if (_inBackground) { [self extendBackgroundRunningTime]; } } - (void)applicationDidEnterBackground:(UIApplication *)application { [self logString: [NSString stringWithFormat:@"applicationDidEnterBackground"]]; [self extendBackgroundRunningTime]; _inBackground = YES; } - (void)extendBackgroundRunningTime { if (_backgroundTask != UIBackgroundTaskInvalid) { // if we are in here, that means the background task is already running. // don't restart it. return; } NSLog(@"Attempting to extend background running time"); __block Boolean self_terminate = YES; _backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"DummyTask" expirationHandler:^{ NSLog(@"Background task expired by iOS"); if (self_terminate) { [[UIApplication sharedApplication] endBackgroundTask:_backgroundTask]; _backgroundTask = UIBackgroundTaskInvalid; } }]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSLog(@"Background task started"); while (true) { NSLog(@"background time remaining: %8.2f", [UIApplication sharedApplication].backgroundTimeRemaining); [NSThread sleepForTimeInterval:1]; } }); } - (void)applicationDidBecomeActive:(UIApplication *)application { [self logString: [NSString stringWithFormat:@"applicationDidBecomeActive"]]; _inBackground = NO; } 

Getting 180 seconds for a range in the background is not a silver bullet, but it solves many use cases that are missing 10 seconds.

You can read the full record of how this works, as well as the test results here: https://github.com/RadiusNetworks/ibeacon-background-demo/tree/background-task

+7
source

I would probably simulate this by breaking the store into several regions. How they will be modeled will be based on the use cases that you have when the notification fires.

I would do this by giving them all the same UUID, but different basic values. Then use small values ​​to distinguish between stores. Thus, when moving from a region, the application will register the didEnter event.

You can have about 20 regions registered, so I will be careful when grouping beacons.

Example:

  • UUID: 754A5D70-C59E-4E39-AA56-ED646903EF5B Major: 1 β†’ Input
  • UUID: 754A5D70-C59E-4E39-AA56-ED646903EF5B Main: 2 β†’ Cash registers
  • UUID: 754A5D70-C59E-4E39-AA56-ED646903EF5B Major: 3 β†’ Clothing
  • UUID: 754A5D70-C59E-4E39-AA56-ED646903EF5B Major: 4 β†’ Cookware
  • ...

Then, when application users move around the store, you can start actions when crossing the borders of regions.

That way you can:

  • Get input and output callback
  • Start ranking to find out Small values ​​(which may be for each store or unique to each beacon)
  • Send local notification
+3
source

What you need to do is make sure all your beacons have the same UUID. Then register one CLBeaconRegion for monitoring, which indicates ONLY this UUID. Do not include primary or secondary value. This will call didEnterRegion: when any beacon matching the UUID is entered (primary and minor values ​​are wildcard). However, the returned CLBeaconRegion will only have a UUID, not a major / minor, just like you registered it so that you don’t know exactly which beacon was launched. To determine exactly which beacon your device displays when didEnterRegion: is didEnterRegion: , tell the location manager startRangingBeaconsInRegion: with the area entered. Then, the location manager will call you back with didRangeBeacons: passing the CLBeacons array. These CLBeacons will know their primary and secondary values, from which you can determine exactly where the user is located in the repository (since you know where the beacon with this major / minor is used). This can be done when the application is in the background.

This way, you register only one CLBeaconRegion, but you can still interact with any beacon matching the registered UUID.

I used this method to successfully deploy 80+ beacons in one area, which all successfully run in the foreground and in the background. To do this, the user does not need to open the application.

+2
source

Source: https://habr.com/ru/post/1204668/


All Articles