0

I want to connect to the BLE sensor every hour to my iOS device for transmission of data from the sensor to an iOS device. If the app is in foreground everything is fine from Discovery to Connection to Read/Write of data. After Reading/Writing to my BLE sensor, I disconnect the iOS device from the sensor and sensor goes to sleep.

Now when the next sync cycle comes (i.e. next hour) and the app is in the background I need to again scan for the peripheral and read data from it. Is it possible to give a command to scan for peripheral when the app is in the background?

  1. I have enabled background modes as bluetooth-central.
  2. I have implemented all delegates of CBCentralManager but did not get any callback when app is in background and peripheral is advertising data. (-didDiscoverPeripheral, or -didConnectPeripheral)

So my question is:

  1. When my app is in the background where should I pass the command (scanForPeripheralsWithServices) by providing respective service UUIDs?
  2. Is there a way by which I can scan BLE devices in the background continuously or on a certain schedule like every hour?
Shashank Agarwal
  • 1,122
  • 1
  • 8
  • 24
  • There should be no reason to scan for a peripheral once you have initially scanned and retrieved its `identifier`; you can simply issue a `connect`. Your problem is that there is no way to execute code in the background after a specific interval in iOS. You could use background refresh, but the actual period for refresh is set by iOS. Really your system would work better if the iOS device simply remained connected to the device and the device just notified when it had new data, or the device itself use a timer to only notify once per hour. – Paulw11 Sep 23 '18 at 10:00
  • @Paulw11 Thanks for answering. Yes, my BLE device will only transmit advertisement packets for **1 minute in every 1 hour**, but how will my iOS device come to know that somebody is advertising if it is not connected with that BLE sensor at that point of time? Also, where should I issue a `connect` command when the app is already in the background? – Shashank Agarwal Sep 23 '18 at 10:25
  • 1
    Generally you would issue the `connect` in the `didDisconnect` method; iOS will then connect to the device and call your `didConnect` delegate method when it is able to re-establish connectivity to the device. – Paulw11 Sep 23 '18 at 10:50
  • @Paulw11 Oh Nice, but that would drain my iPhone battery but looks like the only solution for now. I think the better place for me to place `connect()` code would be `-applicationDidEnterBackground`. – Shashank Agarwal Sep 23 '18 at 11:30
  • You could try that. A pending connect shouldn't drain the battery – Paulw11 Sep 23 '18 at 12:56
  • @Paulw11 Thanks I will try this out. Also, have you tried this out or it's just theoretical? – Shashank Agarwal Sep 23 '18 at 13:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/180618/discussion-between-shashank-agarwal-and-paulw11). – Shashank Agarwal Sep 23 '18 at 13:59

0 Answers0