iOS CoreBluetooth can't receive notifications in background

我与影子孤独终老i 提交于 2019-12-25 09:14:19

问题


I am writing an app that should be able to connect to a Ble device and receive update for one of the characteristics.

I am able to discover the device I want to connect to, connect to it, and register for notifications on the characteristic I am interested in.

Every time the app receives an update for this characteristic it will send the value to a server via an http call.

Everything is working fine in the foreground but as soon as I background my app, it stops receiving the updates. I added

<key>UIBackgroundModes</key>
<array>
    <string>bluetooth-central</string>
    <string>bluetooth-peripheral</string>
</array>

to my plist file. I am testing on an iPhone6 plus running iOS 9.3. Developing with Xcode 8 and swift 2.3. Notifications are being sent every 10 seconds by the Ble device but the app won't handle the notifications in the bakground.


回答1:


I found the issue.

I was instantiating the object taking care of all the scanning, connecting, reading, etc in my AppDelegate. As soon as I moved it to a ViewController it started working just fine in the background. Not sure why.




回答2:


You will need to add capabilities of Required background mode for Bluetooth in Project/capabilities Since you are able to use the blue tooth while foreground, I believe you have added necessary entitlements



来源:https://stackoverflow.com/questions/39923926/ios-corebluetooth-cant-receive-notifications-in-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!