iOS 10 上的后台 CoreBluetooth 广告
CoreBluetooth advertising in background on iOS 10
首先:我有一个 iPad Air 2 和一个 iPhone 7。为了进一步阅读,我们估计该应用程序处于活动状态并在 iPad 打开并处于后台模式iPhone。该应用程序完全相同,即使具有相同的蓝牙服务 UUID 和相同的 DataLocalNameKey。
我想编写一个可以在后台宣传蓝牙服务并能够发现此服务的应用程序(在后台也是最佳的)。 As I already read 我不能使用 apples beacon 技术,因为它只能在后台附近通过 beacons 获得 scanning/notified(我测试过这个,工作正常)但不能做广告。因此,我开始按照上述 SO 答案中的描述使用 CoreBluetooth,因为可以在后台做广告。
我的应用程序在 iPhone(应用程序在后台)上调用 CBCentralManagerDelegate 中的 didDiscoverPeripheral
方法,因此它检测到 iPad。 iPhone 上的 CBPeripheralManager isAdvertising
属性 是 true
。但是 didDiscoverPeripheral
没有在 iPad 上被调用。我有点困惑。一种选择是我的 iPad 由于某些原因无法再检测到 iPhone 或者我的 iPhone 在撒谎,它不是广告。
所以我觉得自己有点笨,于是在谷歌上搜索 "CoreBluetooth debug apps"。我找到了Vicinity and AltBeacon。这两个应用程序的行为是一样的!如果您后台(按 home/sleep 按钮)广告应用程序,浏览应用程序将无法再发现它。如果您打开后台应用程序,它会立即发现它。这两个应用程序都提到可以在后台播放。
我做错了什么吗?这种行为是预期的吗?我误解了框架吗?你能确认这种行为吗?
要确认这一点,最快的方法是在两台设备上安装Vicinity(您必须将NSBluetoothPeripheralUsageDescription
键添加到Info.plist)设置一台设备广播并按主页按钮。
这不是 iOS 10 特有的内容——在 iOS 上一直如此。 在 iOS,应用程序在后台时根本无法发送标准蓝牙 LE 广告。他们必须在前台才能执行此操作。
也就是说,iOS 对在后台宣传 GATT 服务的应用的支持有限。这使用专有技术,仅适用于正在寻找这些服务的其他 iOS 设备。 (因为它使用非标准专有方案,例如,相同的技术不适用于信标广告。)
Apple 是这样描述它的:
...you should be aware that advertising while your app is in the background operates differently than when your app is in the foreground. In particular, when your app is advertising while in the background:
- The CBAdvertisementDataLocalNameKey advertisement key is ignored, and the local name of peripheral is not advertised.
- All service UUIDs contained in the value of the CBAdvertisementDataServiceUUIDsKey advertisement key are placed in a special “overflow” area; they can be discovered only by an iOS device that is explicitly scanning for them.
- If all apps that are advertising are in the background, the frequency at which your peripheral device sends advertising packets may decrease.
这是 ios 10.0.(1) 中的错误,部分 已在 ios 10.1
中修复
首先:我有一个 iPad Air 2 和一个 iPhone 7。为了进一步阅读,我们估计该应用程序处于活动状态并在 iPad 打开并处于后台模式iPhone。该应用程序完全相同,即使具有相同的蓝牙服务 UUID 和相同的 DataLocalNameKey。
我想编写一个可以在后台宣传蓝牙服务并能够发现此服务的应用程序(在后台也是最佳的)。 As I already read 我不能使用 apples beacon 技术,因为它只能在后台附近通过 beacons 获得 scanning/notified(我测试过这个,工作正常)但不能做广告。因此,我开始按照上述 SO 答案中的描述使用 CoreBluetooth,因为可以在后台做广告。
我的应用程序在 iPhone(应用程序在后台)上调用 CBCentralManagerDelegate 中的 didDiscoverPeripheral
方法,因此它检测到 iPad。 iPhone 上的 CBPeripheralManager isAdvertising
属性 是 true
。但是 didDiscoverPeripheral
没有在 iPad 上被调用。我有点困惑。一种选择是我的 iPad 由于某些原因无法再检测到 iPhone 或者我的 iPhone 在撒谎,它不是广告。
所以我觉得自己有点笨,于是在谷歌上搜索 "CoreBluetooth debug apps"。我找到了Vicinity and AltBeacon。这两个应用程序的行为是一样的!如果您后台(按 home/sleep 按钮)广告应用程序,浏览应用程序将无法再发现它。如果您打开后台应用程序,它会立即发现它。这两个应用程序都提到可以在后台播放。
我做错了什么吗?这种行为是预期的吗?我误解了框架吗?你能确认这种行为吗?
要确认这一点,最快的方法是在两台设备上安装Vicinity(您必须将NSBluetoothPeripheralUsageDescription
键添加到Info.plist)设置一台设备广播并按主页按钮。
这不是 iOS 10 特有的内容——在 iOS 上一直如此。 在 iOS,应用程序在后台时根本无法发送标准蓝牙 LE 广告。他们必须在前台才能执行此操作。
也就是说,iOS 对在后台宣传 GATT 服务的应用的支持有限。这使用专有技术,仅适用于正在寻找这些服务的其他 iOS 设备。 (因为它使用非标准专有方案,例如,相同的技术不适用于信标广告。)
Apple 是这样描述它的:
...you should be aware that advertising while your app is in the background operates differently than when your app is in the foreground. In particular, when your app is advertising while in the background:
- The CBAdvertisementDataLocalNameKey advertisement key is ignored, and the local name of peripheral is not advertised.
- All service UUIDs contained in the value of the CBAdvertisementDataServiceUUIDsKey advertisement key are placed in a special “overflow” area; they can be discovered only by an iOS device that is explicitly scanning for them.
- If all apps that are advertising are in the background, the frequency at which your peripheral device sends advertising packets may decrease.
这是 ios 10.0.(1) 中的错误,部分 已在 ios 10.1
中修复