简体   繁体   English

iOS BLE后台扫描

[英]iOS BLE background scanning

I'm having some trouble understanding how scanning is performed when an iOS application is in bacground. 我在理解当iOS应用程序处于bacground时如何执行扫描时遇到一些麻烦。 I have a very simple test application which just scans for devices and outputs the results to the console. 我有一个非常简单的测试应用程序,它只扫描设备并将结果输出到控制台。 I've added bluetooth-central to required background modes in Info.plist so I should be fine and I'm scanning for device with one specified service, that is 我已经在Info.plist中添加了bluetooth-central到所需的后台模式,所以我应该没问题,我正在使用一个指定的服务扫描设备,即

NSArray *cbuuidArray = [NSArray arrayWithObjects:[CBUUID UUIDWithString:@"UUIDFromUUIDGEN"],nil];
[self.centralManager scanForPeripheralsWithServices:cbuuidArray options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];

When the application is in foreground I'm having only one (or none) BLE device connected to the power source so iOS detects it and shows the results quite frequently. 当应用程序处于前台时,我只有一个(或没有)BLE设备连接到电源,因此iOS检测到它并经常显示结果。 When I go to background there are no more results of the first device which is expected since CBCentralManagerScanOptionAllowDuplicatesKey is set to NO. 当我转到后台时,由于CBCentralManagerScanOptionAllowDuplicatesKey设置为NO,因此不再有第一个设备的结果。

At this point I'm powering the second BLE device and eagerly wait for it to show in the results. 在这一点上,我正在为第二个BLE设备供电,并急切地等待它显示在结果中。 After 10 minutes of waiting nothing shows up. 等了10分钟后什么也没出现。 The application is not terminated since my last notification comes from applicationDidEnterBackground and applicationWillTerminate was never called while I was working on the task. 应用程序未终止,因为我的上一个通知来自applicationDidEnterBackground并且在我处理任务时从未调用applicationWillTerminate

In a quite accidental way I discovered that if my app is running and is still scanning in the background and another BLE scanning application (I'm using the excelent BLExplr ) is in the foreground and starts scanning, my application is finaly receiving results in the same time as the foreground appliaction. 以一种非常偶然的方式,我发现如果我的应用程序正在运行并且仍然在后台扫描而另一个BLE扫描应用程序(我正在使用优秀的BLExplr )位于前台并开始扫描,那么我的应用程序最终会在同时作为前景应用。 This makes some sense since the advertising packets are handled by the system and dispatched to applications but why doesn't my application receive anything on it's own? 这是有道理的,因为广告数据包由系统处理并发送到应用程序,但为什么我的应用程序不能自己接收任何内容?

Did anyone have similar experience or knows what can this be caused by? 有没有人有类似的经历或知道这可能是由什么引起的? I've read probably all Apple resources regarding backgrounding and bluetooth with no hints regarding this issue. 我已经阅读了有关背景和蓝牙的所有Apple资源,但没有关于此问题的提示。 I'm working on iOS 4s with iOS 5.1.1. 我正在使用iOS 5.1.1开发iOS 4s。 My main ViewController which is a CBCentralManagerDelegate delegate looks like this. 我的主要ViewControllerCBCentralManagerDelegate委托,如下所示。

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
    }
    return self;
}

#pragma mark -
#pragma mark CBCentralManagerDelegate methods

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    if( central.state == CBCentralManagerStatePoweredOn ){
        NSArray *cbuuidArray = [NSArray arrayWithObjects:
                                [CBUUID UUIDWithString:@"UUID"],
                                nil];
        [self.centralManager scanForPeripheralsWithServices:cbuuidArray options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
    }
}


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

    NSLog(@"%s / peripheral: %@, adData: %@, RSSI: %@" , __PRETTY_FUNCTION__ , peripheral, advertisementData, RSSI);
    NSLog(@"Periphal name: %@", peripheral.name);

}


#pragma mark -
#pragma mark CBPeripheralDelegate methods



@end

There's nothing more going on in the application besides a NavigationController initialization in application delegate. 除了应用程序委托中的NavigationController初始化之外,应用程序中没有其他任何内容。

In ios side if you trying to connect or scanning ble device in Background mode it's never working. 在ios方面,如果你试图在后台模式下连接或扫描ble设备,它永远不会工作。 but once your device connected with ble in foreground you can continue fetching data from Ble device in Background mode. 但是,一旦您的设备与前景中的ble连接,您可以继续在后台模式下从Ble设备获取数据。

CoreBluetooth apps in the background cannot scan for peripherals like they do in the foreground. 后台的CoreBluetooth应用程序无法像在前台一样扫描外围设备。

Please check Background scan ble device 请检查后台扫描设备

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

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