核心蓝牙广告,并在后台扫描 [英] Core Bluetooth advertise and scan in the background

查看:242
本文介绍了核心蓝牙广告,并在后台扫描的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图建立一个应用程序来使该设备既扫描外设和宣传作为外设。我们的目标是为两个设备在后台被唤醒时,他们通过蓝牙发现变得彼此靠近。从Apple文档,看来你应该能够在后台运行(带蓝牙功能的中央和蓝牙外设背景模式)BLE,当一个设备是在前景中我的应用程序工作。
首先,我做广告数据,像这样:

I have been trying to setup an app to make the device both scan for peripherals and advertise as a peripheral. The goal is for two devices to be woken up in the background when they become near each other via bluetooth discovery. From the Apple Documentation, it seems that you should be able to run BLE in the background (with bluetooth-central and bluetooth-peripheral background modes enabled), and my application works when one device is in the foreground. First, I advertise data like so:

NSDictionary *advertisingData = @{CBAdvertisementDataLocalNameKey:@"my-peripheral",
                              CBAdvertisementDataServiceUUIDsKey:@[[CBUUID UUIDWithString:identifier]]};

// Start advertising over BLE
[peripheralManager startAdvertising:advertisingData]; 

然后我将设备设置为扫描数据:

I then set the device to scan for data:

NSArray *services = @[[CBUUID UUIDWithString:identifier]];

[centralManager scanForPeripheralsWithServices:services options:nil];

然而,当进入的背景(设备已被锁定),蓝牙不能发现和

However, when both go into the background (device has to be locked), the bluetooth cannot discover and

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI

不会被调用任一设备上。我该如何解决这个问题?
谢谢

never gets called on either device. How can I fix this? Thanks

推荐答案

我怕你正在尝试做是行不通的。
我试图达到同样的事情。

I'm afraid what you are trying to do will not work. I have tried to achieve the same thing.

的问题是在前景和背景中扫描的差异。
当你在前台扫描的设备可以扫描任何东西。在后台,你必须指定要扫描的实际服务UUID。好吧,你知道你正在寻找的UUID这实际上不是一个问题。

The problem is the difference in scanning in foreground and background. When you are scanning for devices in the foreground you can scan for anything. In the background you must specify the actual service UUID you are scanning for. Ok, this isn't actually a problem as you know the UUID you are looking for.

外围设备:
广播作为外设再次工作在不同的前景和背景。
在前台它就像任何普通的BT外设。在其背后拥有的空间非常有限数量的工作,让你的外设UUID被藏起来,而不是广播。只有当中央设备(iPhone手机在前台)请求从它的信息它会唤醒你的应用程序,并显示它的UUID。

Peripheral: Broadcasting as a peripheral again works differently in foreground and background. In foreground it works like any normal BT peripheral. In the background it has a very limited amount of space to work with, so your peripherals UUID is hidden away and not broadcast. Only when a central device (an iPhone in foreground) requests the information from it will it wake your app and show it's UUID.

因此​​,2相互抵消。当你的后台扫描只能与特定的UUID扫描设备和你的背景外设不能公布其UUID,他们不能看到对方。

So the 2 cancel each other out. As your background scan can only scan for devices with a specific UUID and your background peripheral cannot advertise its UUID, they cannot see each other.

您的设备(无论是外围或中心)的1必须在前台工作。

1 of your devices (either peripheral or central) must be in the foreground to work.

这已经被苹果蓝牙邮件列表上多次讨论。

This has been discussed several times on the Apple Bluetooth mail list.

这篇关于核心蓝牙广告,并在后台扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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