CoreBluetooth应用程序在后台可以做什么? [英] What exactly can CoreBluetooth applications do whilst in the background?

查看:115
本文介绍了CoreBluetooth应用程序在后台可以做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主题说的全部,真的。文档,就其本身而言,表明针对在iOS设备上运行的CoreBluetooth框架编写的应用程序可以在其后台权限列表中添加bluetooth-central,因此在处于非活动状态时处理某种蓝牙事件,但完全正确事件有没有交付?

The subject says it all, really. Documentation, insofar as it exists at all, suggests that apps written against the CoreBluetooth framework running on iOS devices can add "bluetooth-central" to their background privilege list and so process some kind of Bluetooth events whilst inactive, but which exact events do and do not get delivered?

例如:


  1. 我可以与已经建立配对的设备进行通信吗?

  2. 我可以发出定期发现请求来查找超出范围/我以前从未见过的设备吗? (例如,如果我想在遇到新的有趣设备时能够发送通知)

  3. 如果设备超出范围然后又回来怎么办?我是否会在没有用户干预的情况下获得断开连接和连接的事件,或者我是否需要预先设定并让用户明确请求重新连接?


推荐答案

似乎没人知道,所以我买了一个iOS开发者帐户并进行了一些实验。这是我发现的:

Nobody seemed to know, so I bought an iOS developer account and ran some experiments. Here's what I found:

在前台运行时,您可以使用CBCentralManager :: scanForPeripheralsWithServices开始扫描。您的扫描可以限制到广告特定服务的设备,或不受限制(对该调用的参数传递nil)。它还可以允许禁止重复;在前一种情况下,每次iPhone收到广告包时,你都会得到一个didDiscoverPeripheral回调;在后者中,每个设备只能获得一次回调。

When running in the foreground, you can start a scan using CBCentralManager::scanForPeripheralsWithServices. Your scan can be restricted to devices advertising a particular service, or unrestricted (pass nil for that call's parameter). It can also allow or disallow duplicates; in the former case you'll get a didDiscoverPeripheral callback every time the iPhone receives an advertisment packets; in the latter you'll only get one callback per device found.

当您输入背景时,规则如下:

When you enter the background, the rules appear to be as follows:


  • 如果您正在运行不受限制的扫描,则会以静默方式取消扫描。您将无法获得任何didDiscover回调。

  • 如果您的扫描受限制(即您指定了一个或多个您正在寻找的服务UUID),您的扫描将继续运行,但允许重复标记将被忽略。这意味着您现在只能获得设备的didDiscoverPeripheral回调。如果在前台看到所有设备,则根本不会收到任何回调。

  • 启动和停止扫描不会重置哪些设备被视为新设备。如果存在一个设备,即使在多次扫描中也只会进行一次回调,除非......

  • 如果您连接到设备,那么断开连接,然后再次扫描,设备将再次枚举(即您将再次调用didDiscoverPeripheral)。我想iOS认为它在设备中显示出兴趣。

  • If you were running an unrestricted scan, it will be silently cancelled. You will not get any didDiscover callbacks.
  • If your scan was restricted (i.e. you specified one or more service UUIDs you were looking for), your scan will continue to run, but the allow duplicates flag will be ignored. This means that you will now only get didDiscoverPeripheral callbacks for new devices. If all devices were seen whilst in the foreground you will get no callbacks at all.
  • Starting and stopping the scan does not reset which devices are considered new. If there is one device present, you will only get a single callback, even across multiple scans, unless...
  • If you connect to a device, then disconnect, then scan again, the device will be enumerated again (i.e. you will get one more call to didDiscoverPeripheral). I guess iOS regards that as having "shown interest" in the device.

我不知道连接是否尝试连接到非连接设备(例如BLE广告商,就像那些实现邻近配置文件的广告商一样好,因为我的示例设备是可连接的。但是至少对于可连接的设备,此扫描/连接/断开/扫描程序足以轮询设备在后台的存在。

I don't know whether connect attempts to nonconnectable devices (e.g. BLE Advertisers, like those implementing the proximity profile) is good enough as my example devices are connectable. However at least for connectable devices, this scan/connect/disconnect/scan procedure suffices to poll for a device's presence in the background.

上述结果是使用iPhone收集的4S运行iOS 5.0.1

The above results were gathered using an iPhone 4S running iOS 5.0.1

这篇关于CoreBluetooth应用程序在后台可以做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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