23
votes

I am using core-bluetooth framework for my app in Iphone-4s.
This is typically has to be a background app which can run as longer as possible.

Now it is only running for 40 min - 1 hour max.

I am hoping for at least 1 day or so.

For this "bluetooth-central" value is added in "Required background modes" key in .plist file.

It seems like my app is going to "suspend" mode, at the end. since when I open the app again (background to foreground state) it is sending the notification again, it means the bluetooth connection is still connected and BLE-device is still sending notification. If i press home button and app comes to background, it does NOT get notification again.

Can anybody tell me why my app live in background mode only for max 1 hour. It should continue run like normal music app in background for like forever.

Is Apple say anything specific about on which condition an background app (which is one of those continuous running background app falling with in the 5 categories) failing which it will go to suspend mode?

Referring "iPhoneAppProgrammingGuide", on "Communicating with a Bluetooth Accessory" section, I come to know that, for long running background task for Bluetooth LE application 2 implementations are necessory:

1) UIBackgroundModes key should be "bluetooth-central" in Info.plist file. 2) Any app that supports the background processing of Bluetooth data must be session-based.

So for my app, the FIRST implementation was incorporated, and with that application is able to run in background and do all the tasks for max 1 hour duration.

Now I need to implement 2nd implementation. i.e. session-based. Which will allow to get the events even if the app is in "suspend" state according to the documentation. I tried to find to create a suitable session specific to Bluetooth LE (Core Bluetooth framework) like the "EASession" present for Classic Bluetooth (External Accessory framework). But I did not find it.

Basically I am not sure which session class i need to use for BLE purpose. For audio/video, networking and internet, external accessory, there are individual session class available. There is none for Core Bluetooth framework.

Could anybody help me with, which session class is suitable for BLE.

1
You should be able to run in the background for as long as you want, did you ever get this working?Greg Price
I'm seeing the same thing. My peripheral may lose connectivity to the ios device for several minutes, and when it comes back in range, the backgrounded app will find it, and continue. But if it loses connectivity for hours, the backgrounded app ceases to try to reconnect again.Jonathan
If you need to hack around it, you could enable background sound playback too and loop a short silent sound continuously when the app enters the background, and stop it when it becomes active.Will Jenkins
Wasn't that CBCentralManager will trigger the delegate when the device is power on with CBCentralManagerStatePoweredOn ?Ken Cheung

1 Answers

5
votes

I think the problem is simple try to implement you info.plist like my screen:

enter image description here
And if you want you can add the function on AppDelegate.m under the didFinishLaunchingWithOptions to enable Bluetooth, AirPlay WiFi and more, this is only a example code, but i think can help you.

NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error:nil];

UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

Hope this can help you.