0
votes

There is one requirement in which I need to allow iOS App to communicate with the BLE Hardware when App is Killed by users or OS ( May or May not be forcefully).

This Hardware works as an iBeacon and BLE Peripheral simultaneously!

What we have done is:

  • App Launch First Time --> Then Ask for AlwaysUsage Permission
  • Start Scanning for iBeacon Region with UUID(uuidString: "{Provided UUID}") only (Minor value will be different)
  • When func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in the region: CLBeaconRegion) callback is received, we are extracting Major & Minor value from the detected beacon, preparing a Service UUID string based on this value e.g. if Major value is : 00 & Minor value is "11", concacting this will be "0011" and adding this final concated value as a prefix of device service UUID e.g. "0011-12345-12345-123452" to start scanning for BLE Peripheral.

So my BLECentralManager object would start scanning as

self?.bleCentralManager.scanForPeripherals(withServices: "0011-12345-12345-123452", options: [CBCentralManagerScanOptionAllowDuplicatesKey: false])

Once the Peripheral is detected, rest of the BLE Operations are performed on this peripheral (i.e. Making Connection, Sending data to Specific Characteristic, and at last Drop the connection). This works good in foreground mode.

Now, I need to achieve the same mechanism in Background (i.e. App not running state). How can I achieve it?

NOTE:

  • Monitoring for iBeacon region is never stopped. So I am able to awake my app even when it is not launched.
  • But my query is, how can I start preparing the Major + Minor value string for the detected Beacon in background
1
You can't range beacons in the background but you can monitor for beacon region entry. When you enter the region you can start scanning for the BLE service. If the user terminates your app then you probably won't get any further region notifications.Paulw11
Beacon Ranging won't work. That's true! But I can get UserDidEnter/Exit the beacon range (For testing purpose I am sending local notification when user enters/exits the beacon range) if it is forcefully killed from "App Switcher > Swipe Up"!iCoder
Ok. That's all you need then.Paulw11
@Paulw11 My query is How can I retrieve the Major & Minor Value when didEnterRegion callback is received. Right now I can see "nil" when printing the region's major/minor value in func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) due to this, I am unable to define my DeviceService UUID to start scanning! Can you please tell me what am I missing?iCoder
Ok. I understand now. Yeah, you can't do it. It would be much simpler if your BLE device just advertised a known service, that you can scan for, connect to and interrogate a characteristic to see if it is the peripheral you are interested in.Paulw11

1 Answers

1
votes

iOS will launch your app upon beacon detection and you will get a didEnter(region:) callback. At this time, you can immediately start beacon ranging with locationManager.startRangingBeacons(...) and you will get didRangeBeacons(...)callbacks for a few seconds -- long enough to read the major/minor identifiers and do the work you need.