【发布时间】:2017-07-17 13:32:01
【问题描述】:
我需要什么:
当应用程序死机且设备已插入或在附近时,启动 iBeacon 委托方法(例如 didDetermineState、didRangeBeacons、didEnterRegion 或 didExitRegion)的可预测、可靠且可靠的方式。
现状
我正在为父母制作一个应用程序,供他们的孩子使用,以帮助他们在重要时刻关闭手机。该应用采用 Objective-C 语言,即使在应用生命周期之后,它也需要保持与蓝牙设备的持久连接。
我已经尝试了很长时间才能让它发挥作用,并且我得到了很多 S.O. 的帮助。海报,目前我知道我必须在我的设备中使用 iBeacon 才能从终止状态启动(这是我使用它的唯一原因,如果有另一种方法可以从终止状态启动应用程序,我会很乐意转储它)。为了澄清,我需要在同一设备(我已经构建)iBeacon 和可靠的 BT 连接中使用 2 件东西。我需要此设备连接配对,因为这是从 BT 设备发送/接收命令的唯一方法。我发现在后台触发的didRange 或didEnter 委托方法充其量是不可靠的。他们并不总是立即开火,他们只开火几次,整个事情就死了(我现在知道这 10 秒的窗口是终止应用程序的预期行为)。我什至整天都在不停地插拔它,寻找应用程序恢复生机的任何迹象,但什么也没发生......
当应用程序打开时,一切正常,但是当应用程序靠近我的信标/蓝牙时,我希望它在应用程序内启动一种临时锁定屏幕。当应用程序在前台时,我已经很好地完成了这部分。如果一个孩子试图关闭应用程序或后台,我想通过让我的 BT 设备在它终止后启动到后台来响应(我知道 UI 不会出现,这很好,我只需要启动一系列功能) .然后它将连接到蓝牙并从设备接收一些命令。听起来很简单吧?事情变得一团糟。
一些背景:我在 info.plist 中为蓝牙和信标添加了所有背景模式,当应用程序在前台时一切正常...
如果在范围内检测到 iBeacon,然后我想使用 10 秒的窗口通过 BT 配对连接到我的盒子并通过命令发送。到目前为止,这很不稳定...... iBeacon 测距功能不会在应用程序终止时触发,它们只会在最奇怪的用例上触发。我似乎无法预测他们什么时候开火。
我的代码
ibeaconManager.h
@interface IbeaconManager : NSObject
@property (nonatomic) BOOL waitingForDeviceCommand;
@property (nonatomic, strong) NSTimer *deviceCommandTimer;
+ (IbeaconManager *) sharedInstance;
- (void)startMonitoring;
- (void)stopMonitoring;
- (void)timedLock:(NSTimer *)timer;
@end
ibeaconManager.m
@interface IbeaconManager () <CLLocationManagerDelegate>
@property (nonatomic, strong) BluetoothMgr *btManager;
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLBeaconRegion *region;
@property (nonatomic) BOOL connectedToDevice;
@end
NSString *const PROXMITY_UUID = @"00000000-1111-2222-3333-AAAAAAAAAAAA";
NSString *const BEACON_REGION = @"MY_CUSTOM_REGION";
const int REGION_MINOR = 0;
const int REGION_MAJOR = 0;
@implementation IbeaconManager
+ (IbeaconManager *) sharedInstance {
static IbeaconManager *_sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedInstance = [[IbeaconManager alloc] init];
});
return _sharedInstance;
}
- (id)init {
self = [super init];
if(self) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager requestAlwaysAuthorization];
self.connectedToDevice = NO;
self.waitingForDeviceCommand = NO;
self.region = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:PROXMITY_UUID]
major:REGION_MAJOR
minor:REGION_MINOR
identifier:BEACON_REGION];
self.region.notifyEntryStateOnDisplay = YES;
self.region.notifyOnEntry = YES;
self.region.notifyOnExit = YES;
}
return self;
}
- (void)startMonitoring {
if(self.region != nil) {
NSLog(@"**** started monitoring with beacon region **** : %@", self.region);
[self.locationManager startMonitoringForRegion:self.region];
[self.locationManager startRangingBeaconsInRegion:self.region];
}
}
- (void)stopMonitoring {
NSLog(@"*** stopMonitoring");
if(self.region != nil) {
[self.locationManager stopMonitoringForRegion:self.region];
[self.locationManager stopRangingBeaconsInRegion:self.region];
}
}
- (void)triggerCustomLocalNotification:(NSString *)alertBody {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = alertBody;
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
#pragma mark - CLLocationManager delegate methods
- (void)locationManager:(CLLocationManager *)manager
didDetermineState:(CLRegionState)state
forRegion:(CLRegion *)region {
NSLog(@"did determine state STATE: %ld", (long)state);
NSLog(@"did determine state region: %@", region);
[self triggerCustomLocalNotification:@"made it into the did determine state method"];
NSUInteger appState = [[UIApplication sharedApplication] applicationState];
NSLog(@"application's current state: %ld", (long)appState);
if(appState == UIApplicationStateBackground || appState == UIApplicationStateInactive) {
NSString *notificationText = @"Did range beacons... The app is";
NSString *notificationStateText = (appState == UIApplicationStateInactive) ? @"inactive" : @"backgrounded";
NSString *notificationString = [NSString stringWithFormat:@"%@ %@", notificationText, notificationStateText];
NSUserDefaults *userDefaults = [[NSUserDefaults alloc] init];
bool isAppLockScreenShowing = [userDefaults boolForKey:@"isAppLockScreenShowing"];
if(!isAppLockScreenShowing && !self.waitingForDeviceCommand) {
self.waitingForDeviceCommand = YES;
self.deviceCommandTimer = [NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(timedLock:)
userInfo:notificationString
repeats:NO];
}
} else if(appState == UIApplicationStateActive) {
if(region != nil) {
if(state == CLRegionStateInside) {
NSLog(@"locationManager didDetermineState INSIDE for %@", region.identifier);
[self triggerCustomLocalNotification:@"locationManager didDetermineState INSIDE"];
} else if(state == CLRegionStateOutside) {
NSLog(@"locationManager didDetermineState OUTSIDE for %@", region.identifier);
[self triggerCustomLocalNotification:@"locationManager didDetermineState OUTSIDE"];
} else {
NSLog(@"locationManager didDetermineState OTHER for %@", region.identifier);
}
}
//Upon re-entry, remove timer
if(self.deviceCommandTimer != nil) {
[self.deviceCommandTimer invalidate];
self.deviceCommandTimer = nil;
}
}
}
- (void)locationManager:(CLLocationManager *)manager
didRangeBeacons:(NSArray *)beacons
inRegion:(CLBeaconRegion *)region {
NSLog(@"Did range some beacons");
NSUInteger state = [[UIApplication sharedApplication] applicationState];
NSString *notificationStateText = (state == UIApplicationStateInactive) ? @"inactive" : @"backgrounded";
NSLog(@"application's current state: %ld", (long)state);
[self triggerCustomLocalNotification:[NSString stringWithFormat:@"ranged beacons, application's current state: %@", notificationStateText]];
if(state == UIApplicationStateBackground || state == UIApplicationStateInactive) {
NSString *notificationText = @"Did range beacons... The app is";
NSString *notificationString = [NSString stringWithFormat:@"%@ %@", notificationText, notificationStateText];
NSUserDefaults *userDefaults = [[NSUserDefaults alloc] init];
bool isAppLockScreenShowing = [userDefaults boolForKey:@"isAppLockScreenShowing"];
if(!isAppLockScreenShowing && !self.waitingForDeviceCommand) {
self.waitingForDeviceCommand = YES;
self.deviceCommandTimer = [NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(timedLock:)
userInfo:notificationString
repeats:NO];
}
} else if(state == UIApplicationStateActive) {
if(self.deviceCommandTimer != nil) {
[self.deviceCommandTimer invalidate];
self.deviceCommandTimer = nil;
}
}
}
- (void)timedLock:(NSTimer *)timer {
self.btManager = [BluetoothMgr sharedInstance];
[self.btManager sendCodeToBTDevice:@"magiccommand"
characteristic:self.btManager.lockCharacteristic];
[self triggerCustomLocalNotification:[timer userInfo]];
self.waitingForDeviceCommand = NO;
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSLog(@"Did Enter Region: %@", region);
[self triggerCustomLocalNotification:[NSString stringWithFormat:@"Did enter region: %@", region.identifier]];
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
NSLog(@"Did Exit Region: %@", region);
[self triggerCustomLocalNotification:[NSString stringWithFormat:@"Did exit region: %@", region.identifier]];
//Upon exit, remove timer
if(self.deviceCommandTimer != nil) {
[self.deviceCommandTimer invalidate];
self.deviceCommandTimer = nil;
}
}
- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error {
NSLog(@"monitoringDidFailForRegion EPIC FAIL for region %@ withError %@", region.identifier, error.localizedDescription);
}
@end
【问题讨论】:
标签: ios objective-c bluetooth ibeacon background-service