iOS CoreBluetooth无法在后台接收通知 [英] iOS CoreBluetooth can't receive notifications in background

查看:90
本文介绍了iOS CoreBluetooth无法在后台接收通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应该能够连接到Ble设备并接收其中一个特征的更新的应用程序.

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.

每次应用程序收到针对此特性的更新时,它将通过http调用将该值发送到服务器.

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>

到我的plist文件. 我正在运行iOS 9.3的iPhone6上进行测试.使用Xcode 8和Swift 2.3开发. Ble设备每10秒钟发送一次通知,但该应用不会在后台处理通知.

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.

推荐答案

我发现了问题.

我正在实例化对象,负责我的AppDelegate中的所有扫描,连接,读取等工作. 一旦将其移至ViewController,它就可以在后台正常运行.不知道为什么.

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.

这篇关于iOS CoreBluetooth无法在后台接收通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆