0

I have one Smart Door unlock hardware device which acts as iBeacon and CoreBluetooth Peripheral simultaneously!

What my requirement is when my app is launched first-time, the app will start monitoring the iBeacon Region and when the user's distance is less than 10 meters, the application should start scanning the BLE Peripheral using specific CBService.

When a user comes within a 0.5-meter distance, the app should automatically connect with the scanned CBPeripheral device & process door open operation.

You can imagine it as the app will get activated when you're nearer (Approx 10 meters away) from the entrance of the building & start scanning the nearest Bluetooth H/w. When you're reaching to the entrance, the door will get opened automatically by performing some Bluetooth operation.

Can anyone help me with how can I achieve similar behavior using CoreBluetooth tech?

This app should work in background mode as well.

What I have done till now is, started iBeacon region monitoring and getting callback in

func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
        // I am using beacon's accuracy as distance calculation.
        // When accuracy is < 10 meter, I am starting Peripheral scanning using: centralManager.scanForPeripherals(withServices: [{My Custom Service UUID}], options: [CBCentralManagerScanOptionAllowDuplicatesKey: false])
}

My Bluetooth manager gets a callback about

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
}

Now how can I map with my iBeacon and Discovered Peripheral & make an automatic connection with Peripheral when the distance is < 0.5 meters.

In short, my mobile should work as an RFID card for Authentication & Attendance management

I want to achieve similar behavior as displayed in the following video: https://www.youtube.com/watch?v=Y6XlVE7UKp0

iCoder
  • 1,298
  • 1
  • 9
  • 25
  • You probably don't really need the iBeacon functionality. Just lower the transmit power of the Bluetooth chip in the lock so that it is only detected when you are 0.5m away from it – Paulw11 Jan 15 '21 at 07:48
  • @Paulw11 But without iBeacon functionality, will my app be activated when I am not using the app or user/OS has killed it. I need some guidance about how to handle these iBeacon + BLE scenario – iCoder Jan 15 '21 at 08:28
  • You can use CoreBluetooth background mode and state restoration. If you have a pending `connect` to your peripheral then iOS will either deliver the connection event in the background (if your app is suspended) or launch it in the background and then deliver the connection event (if your app was terminated by iOS). If the app is terminated by the user then it won't be relaunched, but that is a user choice to terminate your app and they should not expect it to work if they have terminated it. – Paulw11 Jan 15 '21 at 09:09
  • Even if you use iBeacon you will need a pending Core Bluetooth connect since there is no link between an iBeacon and a CoreBluetooth peripheral. Using iBeacon also means that you will need to ask for (and receive) "always" location permission – Paulw11 Jan 15 '21 at 09:11
  • @Paulw11 That's true that It can work in Background mode if there is a pending 'connect' call. However, I will have multiple devices (e.g. Multiple doors) that can be opened whenever I am near. This case may not get handled when multiple peripherals are there. That's why we had to add an iBeacon chip. When a user is in range of this chip, the app is activated, scan for nearest BLE Peripheral > Makes a connection > Performs BLE Operation > In Last, it drops a connection. – iCoder Jan 15 '21 at 09:24
  • You can have multiple pending connects. If you do want to use iBeacon then you will need your app to maintain a record of the peripheral identifier that corresponds to a given iBeacon and use `retrievePeripheralsWithIdentifier` to get the `CBPeripheral` to `connect` to it since each iOS device will see a different `identifier` for a given peripheral – Paulw11 Jan 15 '21 at 09:57

1 Answers1

1

If you have an iBeacon and based on the unique Major/Minor value, you are searching for the BLE peripheral, then you will have only one peripheral scanned. Thus iBeacon and BLE peripheral will be matched.

You can manage the array of detected peripheral and make connection when iBeacon delegate didRangeBeacon responds the distance as 0.5 meter (value of Beacon Accuracy is in meter. You can use that too)

iLoveIOS
  • 33
  • 7