简体   繁体   English

Swift 3 CoreBluetooth后台数据接收

[英]Swift 3 CoreBluetooth Background data receive

I am trying to receive some data from an Arduino which is emitting some data when I push a button. 我试图从Arduino接收一些数据,当我按下按钮时,该数据正在发射一些数据。

Now I've noticed that it does not receive after a while. 现在,我注意到一段时间后没有收到它。 I did not kill the app yet. 我尚未杀死该应用程序。 How come? 怎么会? I've already added 我已经加了

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

My devices are both paired. 我的设备已配对。

EDIT: 编辑:

I am trying to do the same as the Tile App , it works really well there. 我正在尝试做与Tile App相同的操作,在那儿真的很好用。

EDIT 2: 编辑2:

I instantiate my manager like this: 我这样实例化我的经理:

centralManager = CBCentralManager(delegate:self, queue:nil)

you need to initialize your CBCentralManager by passing in the option parameter your restore key identifier. 您需要通过传递option参数您的还原密钥标识符来初始化CBCentralManager。

let central = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionRestoreIdentifierKey: "yourkey"])

Where "your key" can be anything you want and it is useful for you just to understand that your app was awaken to handle a BLE message. “您的密钥”可以是您想要的任何内容,这对您很有用,因为您可以了解您的应用已被唤醒以处理BLE消息。 If the app was killed by the system when a new BLE message will cause it to be resurrect in your app delegate didFinish launching this key will be passed in the options parameter. 如果当新的BLE消息导致应用程序被系统杀死时,它将在您的应用程序委托didFinish中复活,则启动此密钥将在options参数中传递。

To make it work properly you'll have to implement the delegate method 为了使其正常工作,您必须实现委托方法

optional public func centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any])

this will be called in case of app killed by the system (or by a crash.. not by the user by the way) in order to notify you that a new BLE event happened. 如果应用程序被系统杀死(或被崩溃..而不是用户撞死),则会调用此方法,以通知您发生了新的BLE事件。 In the will restore state dict you will have all the info you need to restore your BLE session. 在will restore state dict中,您将拥有还原BLE会话所需的所有信息。

Here you will find all apple's recommendations to work with the BLE in background. 在这里,您将找到所有有关在后台使用BLE的Apple建议。

Remember that the connection with your peripheral will always be severed if the user performs any actions that voluntarily causes the connection to go off, such as app killed, Blueetooth disabled. 请记住,如果用户执行任何自愿导致连接断开的操作(例如,应用被杀死,禁用了蓝牙),与外围设备的连接将始终被切断。 iOS will continue to awake/resurrect your app in case of crash, app in background and then killed by the system. 如果发生崩溃,iOS将继续唤醒/恢复您的应用程序,该应用程序在后台运行,然后被系统杀死。

The events that will cause the app to be awaken are notifications from registered notifying characteristics, new peripherals discovery, read of a characteristic. 导致应用程序唤醒的事件是来自注册的通知特征的通知,新的外围设备发现,特征的读取。 (basically any callback of the CoreBluetooth delegate) (基本上是CoreBluetooth委托的任何回调)

Keep in mind that a scan operation while in background has limitations. 请记住,在后台进行扫描操作有局限性。

From apple's documentation linked before: 从苹果之前链接的文档中:

Although you can perform many Bluetooth-related tasks while your app is in the background, keep in mind that scanning for peripherals while your app is in the background operates differently than when your app is in the foreground. 尽管您可以在后台运行应用程序时执行许多与蓝牙相关的任务,但请记住,在后台运行时扫描外围设备的操作与在前台运行时不同。 In particular, when your app is scanning for device while in the background: 特别是当您的应用在后台扫描设备时:

  • The CBCentralManagerScanOptionAllowDuplicatesKey scan option key is ignored, and multiple discoveries of an advertising peripheral are coalesced into a single discovery event. 将忽略CBCentralManagerScanOptionAllowDuplicatesKey扫描选项键,并将广告外围设备的多个发现合并为一个发现事件。
  • If all apps that are scanning for peripherals are in the background, the interval at which your central device scans for advertising packets increases. 如果所有正在扫描外围设备的应用程序都在后台运行,则中央设备扫描广告包的时间间隔会增加。 As a result, it may take longer to discover an advertising peripheral. 结果,发现广告外围设备可能需要更长的时间。

Happy coding. 快乐的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM