繁体   English   中英

如何在didExitRegion中获取信标主要和次要ID

[英]How to get beacon major and minor id in didExitRegion

我已经为信标创建了一个iPhone应用程序。 在那我想从所有信标区域退出时显示消息。

  1. 我不想为每个信标出口区域显示消息。 例如,如果有3个信标,我只想在我退出所有3个信标时才显示消息。 有可能这样做吗?

  2. 我也想在didExitRegion获取现有的信标主要和次要值

我使用以下代码:

-(void)locationManager:(CLLocationManager*)manager
   didRangeBeacons:(NSArray*)beacons
          inRegion:(CLBeaconRegion*)region
{
// Beacon found!
NSLog(@"iBeacons found");
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Successfully found" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]; [alert show];

CLBeacon *foundBeacon = [beacons firstObject];

// You can retrieve the beacon data from its properties
NSString *uuid = foundBeacon.proximityUUID.UUIDString;
NSString *majorId = [NSString stringWithFormat:@"%@", foundBeacon.major];
NSString *minorId = [NSString stringWithFormat:@"%@", foundBeacon.minor];
NSLog(@"UUID: %@", uuid);
}

在上面的代码中,我可以获得信标的uuid,major,minor。 但是我想在didExitRegion获取现有信标的值。 可能吗?

提前致谢。

使用可变数组来跟踪检测到的信标的区域详细信息。 并通过检测新的信标区域来更新该阵列。 像下面的代码

在委托方法didDetermineState添加

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
    if(state == CLRegionStateInside)
    {
        [regions addObject:region]; // regions is the mutable array 
    }
}

并用didExitRegion方法添加

// Tells the delegate that the user left the specified region. 
    - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
    {
        [regions removeObject:region];
        CLBeaconRegion *beaconRegion = (CLBeaconRegion *)region;
        NSLog(@"\nExited region id: %@",beaconRegion.identifier);
        NSLog(@"\nExited region major: %@",beaconRegion.major);
        NSLog(@"\nExited region minor: %@",beaconRegion.minor);

       // Add a check with regions array here to show the custom alert message

    }

我不想为每个信标出口区域显示消息。 例如,如果有3个信标,我只想在我退出所有3个信标时才显示消息。 有可能这样做吗?

您的要求似乎并不十分清楚。 如果这三个信标由您要监视的单个区域描述,那么这很简单:当您离开该区域中最后一个信标的范围时,只会收到一条didExitRegion:消息。 否则,Alex的上述建议似乎是合理的:跟踪您正在进入和退出的区域,然后在didExitRegion:有条件地执行一些代码didExitRegion:当您当前所在的区域计数降至零时。

我也想在didExitRegion获取现有的信标主要和次要值

我不确定“出口信标”是否在这里定义明确(您是说,是您离开范围的区域中的最后一个信标,因此触发了didExitRegion:消息?),但是无论如何您都无法获得您在这里要什么。 Alex在记录majorminor ,它们是要监视的区域 (在这种情况下是退出)的属性,而不是任何特定的信标

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM