IOS Bluetooth 4, background monitoring calls

In the last article, iOS Bluetooth 4 transceiver data design, a simple introduction to send and receive data design.
in the iOS Bluetooth 4 development process, there are certainly many partners, will encounter the need to monitor the background calls.
so iOS, if you listen to the background call? In fact, Apple’s official CoreTelephony.framework
has provided the corresponding monitoring call method.

Take a look at the methods provided by the system:

CTCallCenter *center = [[CTCallCenter alloc] = init]; center.callEventHandler ^ (CTCall *call) {if ([call.callState isEqualToString:CTCallStateIncoming]) {/ /}else if calls ([call.callState isEqualToString:CTCallStateConnected]) {/ / else} on if ([call.callState isEqualToString:CTCallStateDisconnected]) {/ /}else if ([call.callState isEqualToString:CTCallStateDialing] hung) {/ / call}}}else {/ / other;

If you copy the above code, you will not be able to listen to the call, because in the ARC mode, center is created, it will be released. So, if you want to listen to the call, the center object must be defined as a global or an object reference, do not be released, or do not need to listen to the call, then release.

Well, even if you took my advice, I found that it was not perfect.
the above method is indeed able to listen to the call, but only in the APP run, it can be properly monitored; if the APP into the background, it is no longer listening. Embarrassment!!!

The focus is, how can I get the background to listen to the call?

We first decompose the problem, the above requirements can be broken down into two. One is running in the background; the other is listening to the caller.

First, how to run the background?

Apple official can apply for the following to run the background:

1.Audio and AirPlay
2.Location updates (audio player)
(location update) (VoIP) 3.Voice over IP
4.Newsstand downloads
5.External accessory communication
6.Uses Bluetooth LE accessories
7.Acts as a Bluetooth LE accessory
8.Background fetch
9.Remote notifications

More than 9 kinds, can apply for background operation. Very good, because our Bluetooth 4 development, you can also apply for background operation.

So how can we do Bluetooth 4 related to the APP can run it? I give the practice is to use the timer.

1, define an attribute variable timer. Defined as an attribute variable, which is convenient for us to destroy when not in use. For example, when APP enters the foreground.

@property (nonatomic, strong) NSTimer *phoneTimer;

2, timer

If (self.phoneTimer = = Nil) {self.phoneTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector (commTimer:) userInfo:nil repeats:YES];}

3, send Bluetooth data. If we do not need to send data in the background, we can send some meaningless data, just to achieve the purpose of running the background

- (void) commTimer: (NSTimer * timer) {} / / Bluetooth data transmission

Well, the above three steps can be completed Bluetooth background run 4. Of course, do not forget to apply for the appropriate permissions in the plist file. Following figure:

IOS Bluetooth 4, background monitoring calls
for Bluetooth permission.Png

Two, how in the background, listen to the call?

If you are in the regular implementation of the commTimer method, the implementation of the callEventHandler monitoring method, you will find that the phone is still not listening.
carefully look at Apple’s CTCallCenter class, you will find that there is an attribute variable currentCalls.

* * CTCallCenter.h * CFTelephony * Copyright * 2010 Apple, Inc. All rights reserved. * #import < * Foundation/Foundation.h> #import; < CoreTelephony/CoreTelephonyDefines.h> @class CTCall; NS_ASSUME_NONNULL_BEGIN CORETELEPHONY_CLASS_AVAILABLE (4_0) @interface CTCallCenter: NSObject currentCalls / * * * * * Discussion: An array containing CTCall objects for all calls that are currently progress. If no calls are * in active, this will be nil. * * @property (readonly, retain, nullable) NSSet< CTCall*> *currentCalls __OSX_AVAILABLE_BUT_DEPRECATED_MSG (__MAC_NA, __MAC_NA, __IPHONE_4_0, __IPHONE_10_0, Replaced by < CallKit/CXCallObserver.h> "); / * * callEventHandler * * * Discussion: A block that Will be dispatched on the default priority global dispatch when a new call * queue event occurs. Set this property to a block is defined in your * that application to handle call events. / @property (nonatomic, copy, nullable) void (callEventHandler ^) (CTCall*) __OSX_AVAILABLE_BUT_DEPRECATED_MSG (__MAC_NA, __MAC_NA, __IPHONE_4_0, __IPHONE_10_0, Replaced by < CallKit/CXCallObserver.h> @end; NS_ASSUME_NONNULL_END) "

Yeah, let’s start with this variable. To override the commTimer method. The code is as follows:

The background to send data, Bluetooth / * * * / listener call - (void) commTimer: (NSTimer * timer) {/ / if ([[BTManager shareManager] Bluetooth data isConnect]) {[[BTManager shareManager] sendBackgroudData]; / / *center = "CTCallCenter CTCallCenter listener call alloc] init]; if (center.currentCalls) {CTCall *call = [center.currentCalls anyObject]; if ([call.callState isEqual:CTCallStateIncoming]) {isPhone = YES; [[BTManager shareManager] phoneCallIn];}else if ([call.callState isEqual:CTCallStateDisconnected]) {if (isPhone) {isPhone = NO; [[BTManager shar EManager] phoneCallEnd];}}else {}}}

BTManager is my design of a Bluetooth management class, used to manage the Bluetooth data transceiver.
isConnect method is to determine whether there is a Bluetooth connection, of course, you can also design, when the Bluetooth disconnect, stop the timer.
sendBackgroudData send data to keep Bluetooth background long connection.

The above is my solution in iOS Bluetooth 4 background monitoring calls. Want to help the development of Bluetooth 4 partners, help point praise! Ha-ha!!