1

I am working on an app that scans for a specific peripheral, ones peripheral is found it should send the small amount of data.

App works in the foreground and also in background. I have also add this code in the plist

UIBackgroundModes bluetooth-central When iPhone is locked and peripheral starts advertising, it doesn't call

func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {

I have done some research but couldn't find an answer.

Any help is appreciated.

Thanks

Paragon
  • 982
  • 2
  • 10
  • 37
  • Does your app work only as a central? – Fernando Reynoso Jun 16 '15 at 15:29
  • @FernandoReynoso yes – Paragon Jun 16 '15 at 19:21
  • I think It would be useful if you explain a little more the scenario. For example, if the device acting as peripheral was already discovered by the central. In this particular case maybe you aren´t specifying `CBCentralManagerScanOptionAllowDuplicatesKey:YES` as an option in the `func scanForPeripheralsWithServices(_ serviceUUIDs: [CBUUID]?, options options: [String : AnyObject]?)` method. – Fernando Reynoso Jun 16 '15 at 19:37
  • @FernandoReynoso Yes central has already discovered the device in the foreground mode. My problem is when screen is locked, i don't get call back from iOS unless i unlock the screen. According to Apple documentation CBCentralManagerScanOptionAllowDuplicatesKey:YES is ignored in the background. – Paragon Jun 17 '15 at 02:42
  • You´re right. Then what you need to do is to make the connection yourself. If you need to reconnect to the peripheral try adding the `CBPeripheral` that you discovered into an array eg.`var knownPeripherals = [CBPeripheral]()` so you can reconnect to whichever known peripheral you need (if it´s available, obviously). – Fernando Reynoso Jun 17 '15 at 02:52
  • where should i reconnect to the peripheral? didDiscoverPeripheral is called after unlocking the screen. – Paragon Jun 17 '15 at 03:06
  • Also the `CBCentralManager` has this `func retrievePeripheralsWithIdentifiers(_ identifiers: [NSUUID]) -> [CBPeripheral]`. But anyway an array of identifiers is needed. – Fernando Reynoso Jun 17 '15 at 03:06
  • I am creating a variable var usingPeripheral:CBPeripheral! Do i need an array? i have only on peripheral to connect. Thanks – Paragon Jun 17 '15 at 03:10
  • 1
    If the peripheral for any reason disconnects from the central this delegate method is invoked: `optional func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error error: NSError?)` I think you can try to reconnect there. Also check the Apple reference for long performing long-term actions. Here the link: https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html#//apple_ref/doc/uid/TP40013257-CH7-SW5 – Fernando Reynoso Jun 17 '15 at 03:12
  • 1
    No, you don´t need an array if you are connecting with only one peripheral. Hope it helps! – Fernando Reynoso Jun 17 '15 at 03:13
  • What if you simply want to scan for devices of a specific UUID while the device is locked? I have encountered a similar issue but do not need to connect; simply need to detect a peripheral via scanning https://stackoverflow.com/questions/42775327/scan-for-peripherals-when-device-is-locked – vikzilla Mar 16 '17 at 23:37

1 Answers1

0

Save the discovered CBPeripheral instance.

var activePeripheral: CBPeripheral!

Or adding it into an array. If you are discovering more than one peripherals.

var knownPeripherals = [CBPeripheral]()

This way you can reconnect to whichever CBPeripheral you need (if it is available, obviously).

This delegate method is invoked when a peripheral is disconnected optional func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error error: NSError?). You can try using it in your benefit by trying to reconnect to your peripheral.

And finally you should check the Apple reference for performing long-term actions in background.

Hope it helps!

Fernando Reynoso
  • 545
  • 1
  • 6
  • 22
  • Yes, but is there a way to continue to scan for new peripherals while the device is locked? – vikzilla Mar 16 '17 at 23:17
  • I think you should go to a different threat. Try this one: http://stackoverflow.com/questions/42775327/scan-for-peripherals-when-device-is-locked?noredirect=1&lq=1 hope it helps! – Fernando Reynoso Mar 17 '17 at 19:33
  • I can connect to a peripheral while device is locked because i had already connected to it. iOS wakes up my app and passes me the peripheral – Paragon Apr 05 '17 at 17:57