ios – 为什么信标会导致蓝牙不断切换?

ios – 为什么信标会导致蓝牙不断切换?,第1张

概述我在使用iOS和iBeacon时遇到了一个非常奇怪的错误.我有一个非常简单的BeaconManager,它使用特定的UUID,主要和次要值来定义信标,并在找到它们后执行一些 *** 作.我的应用似乎正常工作,直到它不断切换蓝牙状态并停止工作.唯一可见的结果是状态栏中的蓝牙图标由于蓝牙停止和重新启动而开始闪烁. 在哪里集中注意力? 这是我的班级定义: #import "BeaconManager.h"@ 我在使用iOS和iBeacon时遇到了一个非常奇怪的错误.我有一个非常简单的BeaconManager,它使用特定的UUID,主要和次要值来定义信标,并在找到它们后执行一些 *** 作.我的应用似乎正常工作,直到它不断切换蓝牙状态并停止工作.唯一可见的结果是状态栏中的蓝牙图标由于蓝牙停止和重新启动而开始闪烁.

在哪里集中注意力?

这是我的班级定义:

#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 – 为什么信标会导致蓝牙不断切换?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1103026.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存