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

4

1 回答 1

0

保存发现的CBPeripheral实例。

var activePeripheral: CBPeripheral!

或将其添加到数组中。如果您发现多个外围设备。

var knownPeripherals = [CBPeripheral]()

这样你就可以重新连接到CBPeripheral你需要的任何一个(如果它可用,显然)。

当外围设备断开连接时调用此委托方法optional func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error error: NSError?)。您可以通过尝试重新连接到外围设备来尝试使用它。

最后,您应该查看 Apple 参考以在后台执行长期操作

希望能帮助到你!

于 2015-06-17T03:35:37.650 回答