在哪里集中注意力?
这是我的班级定义:
#import "BeaconManager.h"@implementation BeaconManager- (instancetype)init { self = [super init]; if (self) { NSURL *beep = [[NSBundle mainBundle] URLForResource:@"beep" withExtension:@"aiff"]; soundfileURLRef = (CFURLRef) CFBrIDgingRetain(beep); AudioServicesCreateSystemSoundID(soundfileURLRef,&soundfileObject); // Initializes propertIEs beacon = [CLBeacon new]; foundBeacons = [NSMutableArray new]; _lastBeaconActionTimes = [[NSMutableDictionary alloc] init]; } return self;}- (voID)initRegion { // Initializes the beacon region by giving it an UUID and an IDentifIEr NSUUID *uuID = [[NSUUID alloc] initWithUUIDString:BEACON]; beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuID IDentifIEr:@"beacon.region"]; // Starts looking for beacon within the region [self.locationManager startMonitoringForRegion:beaconRegion];}- (voID)checkBeacon { if (!self.locationManager) { self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) [self.locationManager requestWhenInUseAuthorization]; } [self initRegion]; [self locationManager:self.locationManager dIDStartMonitoringForRegion:beaconRegion];}#pragma mark - CLLocationManagerDelegate- (voID)locationManager:(CLLocationManager *)manager dIDStartMonitoringForRegion:(CLRegion *)region { [self.locationManager startMonitoringForRegion:beaconRegion]; [self.locationManager startRangingBeaconsInRegion:beaconRegion];}- (voID)locationManager:(CLLocationManager *)manager dIDEnterRegion:(CLRegion *)region { [self.locationManager startRangingBeaconsInRegion:beaconRegion];}- (voID)locationManager:(CLLocationManager *)manager dIDExitRegion:(CLRegion *)region { [self.locationManager stopRangingBeaconsInRegion:beaconRegion];}- (voID)locationManager:(CLLocationManager *)manager monitoringDIDFailForRegion:(CLRegion *)region withError:(NSError *)error { NSLog(@"Failed monitoring region: %@",error);}- (voID)locationManager:(CLLocationManager *)manager dIDFailWithError:(NSError *)error { NSLog(@"Location manager Failed: %@",error);}- (voID)locationManager:(CLLocationManager *)manager dIDRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region { if (foundBeacons.count == 0) { for (CLBeacon *filterBeacon in beacons) { // If a beacon is located near the device and its major value is equal to 1000 (MAJOR constant) if (((filterBeacon.proximity == CLProximityImmediate) || (filterBeacon.proximity == CLProximityNear))) // Registers the beacon to the List of found beacons [foundBeacons addobject:filterBeacon]; } } // DID some beacon get found? if (foundBeacons.count > 0) { // Takes first beacon of the List beacon = [foundBeacons firstObject]; if (([beacon.major isEqualToNumber:[NSNumber numberWithInt:MAJOR]]) && ([beacon.minor isEqualToNumber:[NSNumber numberWithInt:MInor]])) { // Takes the actual date and time NSDate *Now = [[NSDate alloc] init]; Nsstring *key = [Nsstring stringWithFormat:@"%@ %@ %@",[beacon.proximityUUID UUIDString],beacon.major,beacon.minor]; NSDate *lastBeaconActionTime = [_lastBeaconActionTimes objectForKey:key]; if ((lastBeaconActionTime == nil) || ([Now timeIntervalSinceDate:lastBeaconActionTime] > MINIMUM_ACTION_INTERVAL_SECONDS)) { [_lastBeaconActionTimes setobject:Now forKey:key]; // Plays beep sound AudioServicesPlaySystemSound(soundfileObject); if (self.delegate) { // Performs actions related to the beacon (i.e. delivers a coupon) [self.delegate dIDFoundBeacon:self]; } self.locationManager = nil; } // else [self.locationManager stopMonitoringForRegion:region]; } [foundBeacons removeObjectAtIndex:0]; beacon = nil; }}@end解决方法 不能肯定这是蓝牙不断切换的原因,但这部分肯定是可疑的:
- (voID)locationManager:(CLLocationManager *)manager dIDStartMonitoringForRegion:(CLRegion *)region { [self.locationManager startMonitoringForRegion:beaconRegion]; [self.locationManager startRangingBeaconsInRegion:beaconRegion];}
这基本上是一个无限循环.一旦监控开始,iOS就会调用dIDStartMonitoring方法……它开始监控同一个区域,这使得iOS再次调用dIDStartMonitoring方法,其中……
我首先从代码的这一部分中删除startMonitoringForRegion行.
总结以上是内存溢出为你收集整理的ios – 为什么信标会导致蓝牙不断切换?全部内容,希望文章能够帮你解决ios – 为什么信标会导致蓝牙不断切换?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)