iOS前台再次找不到iBeacon
iOS Can not find the iBeacon again in fore ground
你好~我正在学习使用CLLocationManager检测iBeacon。我读了这篇文章:
http://developer.radiusnetworks.com/2013/11/13/ibeacon-monitoring-in-the-background-and-foreground.html
它说 startRangingBeaconsInRegion 将使系统每秒扫描一次信标。我测试是正确的。
但是如果程序只执行startMonitoringForRegion而不执行startRangingBeaconsInRegion,就会出现问题。
我的程序可以在我第一次启动信标硬件时找到信标,并且在我停止信标后调用函数 didExitRegion。但是在我第二次启动信标后,程序根本找不到它(执行didEnterRegion)。我等了1个小时
我用于测试的硬件是 iPhone 5s iOS 8.1.2 和 radBeacon USB。
这是我的代码。
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
@property (retain, nonatomic) IBOutlet UITextView *textView;
@property (strong, nonatomic) NSMutableString *myLog;
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLBeaconRegion *beaconRegion;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self startBeaconMonitoring];
}
- (void)startBeaconMonitoring {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
_locationManager.delegate = self;
[_locationManager performSelector:@selector(requestAlwaysAuthorization)];
[self userGeofencingPreferencesWereUpdatedWithNotification:nil];
[self updateLogWithString:@"start the app"];
}
- (void) userGeofencingPreferencesWereUpdatedWithNotification: (NSNotification *) notification
{
if (1) {
NSUUID *proximityUUID = [[NSUUID UUID] initWithUUIDString:@"EEF45689-BBE5-4FB6-9E80-41B78F6578E2"];
_beaconRegion = [[CLBeaconRegion alloc]
initWithProximityUUID:proximityUUID
identifier:@"1"];
_beaconRegion.notifyEntryStateOnDisplay = YES;
[_locationManager startMonitoringForRegion:_beaconRegion];
//[_locationManager startRangingBeaconsInRegion:beaconRegion];
//[_locationManager stopUpdatingLocation];
}
}
- (void) locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
[self updateLogWithString:@"enter"];
NSLog(@"enter");
}
- (void) locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
[self updateLogWithString:@"exit"];
NSLog(@"exit");
}
- (void)locationManager:(CLLocationManager *)manager
didRangeBeacons:(NSArray *)beacons
inRegion:(CLBeaconRegion *)region {
//NSLog(@"range");
}
- (void)dealloc {
[_textView release];
[_myLog release];
[_locationManager release];
[_beaconRegion release];
[super dealloc];
}
- (NSMutableString *)myLog {
if (!_myLog) {
_myLog = [[NSMutableString alloc] init];
}
return _myLog;
}
- (void) updateLogWithString:(NSString*)newLog {
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"hh:mm:ss";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString * logWithTime = [NSString stringWithFormat:@"%@---%@\n",[dateFormatter stringFromDate:now], newLog];
[self.myLog appendString:logWithTime];
self.textView.text = self.myLog;
[dateFormatter release];
}
@end
我偶然发现了一些东西,这解决了我的问题。当我使用名为 Broadcaster 的 iOS APP 模拟 iBeacon 时,它会非常快速地触发 iBeacon 检测 APP 运行 在另一个 iOS 设备上,无论是前台,后台还是屏幕关闭。
当我使用 Rad Beacon USB 时,仍然失败。我测试了我所有的 4 个 Rad Beacon USB,同样的事情发生了。
好像Rad Beacon USB发送的BLE消息和iOS模拟的有点不一样(我刚试了APP Broadcaster)。 iOS8 对待他们
在某些情况下会有所不同。
我的 Rad Beacon USB 是 2.0 版。
你好~我正在学习使用CLLocationManager检测iBeacon。我读了这篇文章: http://developer.radiusnetworks.com/2013/11/13/ibeacon-monitoring-in-the-background-and-foreground.html 它说 startRangingBeaconsInRegion 将使系统每秒扫描一次信标。我测试是正确的。
但是如果程序只执行startMonitoringForRegion而不执行startRangingBeaconsInRegion,就会出现问题。
我的程序可以在我第一次启动信标硬件时找到信标,并且在我停止信标后调用函数 didExitRegion。但是在我第二次启动信标后,程序根本找不到它(执行didEnterRegion)。我等了1个小时
我用于测试的硬件是 iPhone 5s iOS 8.1.2 和 radBeacon USB。 这是我的代码。
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
@property (retain, nonatomic) IBOutlet UITextView *textView;
@property (strong, nonatomic) NSMutableString *myLog;
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLBeaconRegion *beaconRegion;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self startBeaconMonitoring];
}
- (void)startBeaconMonitoring {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
_locationManager.delegate = self;
[_locationManager performSelector:@selector(requestAlwaysAuthorization)];
[self userGeofencingPreferencesWereUpdatedWithNotification:nil];
[self updateLogWithString:@"start the app"];
}
- (void) userGeofencingPreferencesWereUpdatedWithNotification: (NSNotification *) notification
{
if (1) {
NSUUID *proximityUUID = [[NSUUID UUID] initWithUUIDString:@"EEF45689-BBE5-4FB6-9E80-41B78F6578E2"];
_beaconRegion = [[CLBeaconRegion alloc]
initWithProximityUUID:proximityUUID
identifier:@"1"];
_beaconRegion.notifyEntryStateOnDisplay = YES;
[_locationManager startMonitoringForRegion:_beaconRegion];
//[_locationManager startRangingBeaconsInRegion:beaconRegion];
//[_locationManager stopUpdatingLocation];
}
}
- (void) locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
[self updateLogWithString:@"enter"];
NSLog(@"enter");
}
- (void) locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
[self updateLogWithString:@"exit"];
NSLog(@"exit");
}
- (void)locationManager:(CLLocationManager *)manager
didRangeBeacons:(NSArray *)beacons
inRegion:(CLBeaconRegion *)region {
//NSLog(@"range");
}
- (void)dealloc {
[_textView release];
[_myLog release];
[_locationManager release];
[_beaconRegion release];
[super dealloc];
}
- (NSMutableString *)myLog {
if (!_myLog) {
_myLog = [[NSMutableString alloc] init];
}
return _myLog;
}
- (void) updateLogWithString:(NSString*)newLog {
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"hh:mm:ss";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString * logWithTime = [NSString stringWithFormat:@"%@---%@\n",[dateFormatter stringFromDate:now], newLog];
[self.myLog appendString:logWithTime];
self.textView.text = self.myLog;
[dateFormatter release];
}
@end
我偶然发现了一些东西,这解决了我的问题。当我使用名为 Broadcaster 的 iOS APP 模拟 iBeacon 时,它会非常快速地触发 iBeacon 检测 APP 运行 在另一个 iOS 设备上,无论是前台,后台还是屏幕关闭。 当我使用 Rad Beacon USB 时,仍然失败。我测试了我所有的 4 个 Rad Beacon USB,同样的事情发生了。 好像Rad Beacon USB发送的BLE消息和iOS模拟的有点不一样(我刚试了APP Broadcaster)。 iOS8 对待他们 在某些情况下会有所不同。
我的 Rad Beacon USB 是 2.0 版。