【问题标题】:Can't detect when iBeacon bluetooth is turned off iOS无法检测到 iBeacon 蓝牙何时关闭 iOS
【发布时间】:2014-02-15 23:00:04
【问题描述】:

我正在编写两个简单的应用程序。一个是信标应用程序,您可以通过轻触按钮来启动或停止其信号。另一种是接收器应用程序,它在检测到信标信号时编辑标签的文本。

我尝试使用 didDetermineStateForRegion、didExitRegion 和 didEnterRegion 方法来检测应用程序何时运行。这些可以很好地确定接收器何时移入和移出信标附近,但确定我已关闭信标上的蓝牙大约需要 30 秒。我也尝试将我的 CLLocationManager 的 pausesLocationUpdatesAutomatically 字段设置为 NO,但同样的事情。理想情况下,它会立即在我的标签上加上“不”;我该怎么做?

MyView.h

@interface MyView : UIViewController

@property (weak, nonatomic)   IBOutlet UILabel *statusLabel;
@property (strong, nonatomic) CLBeaconRegion   *myBeaconRegion;
@property (strong, nonatomic) CLLocationManager *locationManager;

@end

MyView.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // Initialize location manager and set ourselves as the delegate
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.pausesLocationUpdatesAutomatically=NO;

    // Create a NSUUID with the same UUID as the broadcasting beacon
    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"ID"];

    // Setup a new region with that UUID and same identifier as the broadcasting beacon
    self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
                                        identifier:@"identifier"];

    // Tell location manager to start monitoring for the beacon region
    [self.locationManager startMonitoringForRegion:self.myBeaconRegion];
}

- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion*)region
{
[self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
self.statusLabel.text = @"Yes";
}

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {
if (state == CLRegionStateInside) {
    self.statusLabel.text = @"Yes";
} else {
    self.statusLabel.text = @"No";
}

}

-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion*)region
{
[self.locationManager stopRangingBeaconsInRegion:self.myBeaconRegion];
self.statusLabel.text = @"No";
}

【问题讨论】:

    标签: ios bluetooth ibeacon


    【解决方案1】:

    区域监控非常慢,您可以将其用于更一般的通知,以便在您靠近 iBeacon 时通知您。我认为对于这种情况,您可能希望使用didRangeBeacons,您的检测应用程序将每秒收到信标信号强度通知。您可以使用此信号强度来决定您是否不再看到信标(CoreLocation 在信标消失几秒钟后往往仍会“看到”信标)。

    只需将以下方法添加到您的委托:

    -(void)locationManager:(CLLocationManager*)manager
           didRangeBeacons:(NSArray*)beacons
                  inRegion:(CLBeaconRegion*)region
    

    然后开始为您所在的地区进行测距:

    [locationManager startRangingBeaconsInRegion:(CLBeaconRegion*)region];
    

    【讨论】:

    • 是的,但 didRangeBeacons 仅在应用程序不在后台时有效。如果你的应用被最小化了,你就只能依赖 enterRegion 和 region 监控了。
    • 没错。请记住,在后台进入和退出区域并不是超级响应 - iOS 可能需要几分钟才会通知您更改。
    • 另外,当蓝牙关闭时,didRangeBeacons 不会被调用 - 只是想解决这个问题,可能需要跟踪自上次 didRangeBeacons 以来的时间,看看它是否超过 2-3秒左右 - 否则每 1 秒调用一次。 ps:didRangeBeacons 确实会被调用,如果蓝牙打开,但周围没有信标 - 有一个空的信标数组。
    猜你喜欢
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 2016-03-29
    • 2021-12-26
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多