从未在CoreBluetooth Framework中看到任何关于在后台保持连接活动的信息 [英] Never seen anything in CoreBluetooth Framework about keeping the connection alive in the background

查看:170
本文介绍了从未在CoreBluetooth Framework中看到任何关于在后台保持连接活动的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xcode 5和CoreBluetooth Framework。

在plist文件中

I am using the Xcode 5 and CoreBluetooth Framework.
In plist file

所需的背景模式 - > App使用CoreBluetooth进行通信

在Xcode 5中是应用程序功能中背景模式的部分。将背景模式设置为开启并选中使用蓝牙LE配件

In Xcode 5 is a section for Background Modes in the your app's capabilities. Set Background Modes to ON and check "Uses Bluetooth LE accessories"

我们的应用和BLE连接在应用程序运行时工作正常。

问题是当手机进入睡眠状态或背部进入后台时,BLE连接停止工作。

我还没有在CoreBluetooth框架中看到有关在后台保持连接活动的任何内容。

Our app and BLE connection works fine while the app is running.
The problem is when the phone goes to sleep or if the back goes to the background, the BLE connection stops working.
I haven't seen anything in the CoreBluetooth Framework about keeping the connection alive in the background.

当应用获得背景时将方法调用 startBackgroundNotification 。如下方法

When app get background than will call method to startBackgroundNotification. like below method

-(void)startBackgroundNotification{
    self.counterTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
            [[UIApplication sharedApplication] endBackgroundTask:counterTask];
    }];
    BackgroundManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
    nDevices = [[NSMutableArray alloc]init];
    sensorTags = [[NSMutableArray alloc]init];
}

在startBackgroundNotification调用之后调用(委托)到 centralManagerDidUpdateState

After startBackgroundNotification call than will call (delegate) to centralManagerDidUpdateState

-(void)centralManagerDidUpdateState:(CBCentralManager *)central {
    if (central.state != CBCentralManagerStatePoweredOn) {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"BLE not supported !"
                                                               message:[NSString stringWithFormat:@"CoreBluetooth return state: %d",central.state]
                                                              delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];
        }
    }
    else
        [central scanForPeripheralsWithServices:nil options:nil];
}

但永远不要调用以下功能

But never call below function

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



当应用程序在后台时,我们按下一个按钮在BLE设备上,手机会发出通知,告知设备需要访问应用程序,因此我必须再次手动打开应用程序。


When the app is in the background and we press a button on the BLE device, the phone gives a notification saying that the device needs access to the app so I have to open the app manually again.

推荐答案

这实际上记录在Apple的API文档中;在没有扫描任何服务的情况下进行扫描不会在后台返回任何内容,如这里

This is actually documented in Apple's API documentation; doing a scan without scanning for any services will not return anything while in the background, as explained here:


允许指定蓝牙中央背景模式的应用程序在后台扫描。也就是说,他们必须通过在serviceUUIDs参数中指定它们来显式扫描一个或多个服务。在后台扫描时忽略CBCentralManagerOptionShowPowerAlertKey扫描选项。

Apps that have specified the bluetooth-central background mode are allowed to scan while in the background. That said, they must explicitly scan for one or more services by specifying them in the serviceUUIDs parameter. The CBCentralManagerOptionShowPowerAlertKey scan option is ignored while scanning in the background.

因此,要获得任何结果,您必须扫描具有特定服务的外围设备,通过在serviceUUIDs参数中传递UUID。或者,如果您之前已经连接到您正在寻找的设备,则可以拨打 connectPeripheral 功能直接

Therefore, to get any results, you must scan for peripherals with specific services, by passing a UUID in the serviceUUIDs parameter. Alternatively, if you have connected to the device you're looking for before, you can just call the connectPeripheral function directly

这篇关于从未在CoreBluetooth Framework中看到任何关于在后台保持连接活动的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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