我有一个代码可以知道哪个信标最近,但是当信标的精度为 -1.00000 时我遇到了问题,应用程序采用了第二个。
所以有一个 bucle 寻找最近的信标寻找其他信标并将最近的信标与周围发现的其他信标进行比较。然后,当我知道哪个是最近的我向用户显示它的 View 时,但是当其他信标更靠近 ipad 时, View 接近并显示 View 。
我在两台 iPad 上使用 iOS 7.1 和 7.0.4,第一台是 iPad3,第二台是 iPad Mini Retina。
这里有代码:
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}
- (void)initRegion {
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"00000000-0000-0000-0000-000000000002"];
self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"com.devfright.myRegion"];
[self.locationManager startMonitoringForRegion:self.beaconRegion];
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSLog(@"Beacon Found");
[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
NSLog(@"Left Region");
[self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];
}
-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
CLBeacon *beacon = [[CLBeacon alloc] init];
CLBeacon *closestBeacon = [[CLBeacon alloc] init];
closestBeacon = [beacons lastObject];
//Search for the closest and show the view
for (int i=0; i<beacons.count; i++) {
beacon = beacons[i];
if (beacon.accuracy < closestBeacon.accuracy && beacon.accuracy != -1.000000 && beacon.minor != closestBeacon.minor && beacon.proximity != CLProximityFar) {
closestBeacon = beacons[i];
//NSString *closeAccuracy = closestBeacon.minor.stringValue;
}
NSLog(@"\nActualBeacon:%@\nAccuracy:%f", beacon.minor.stringValue,beacon.accuracy);
NSLog(@"\nCloserBeacon: %@\nAccuracy:%f", closestBeacon.minor.stringValue, closestBeacon.accuracy);
}
NSLog(@"\nClosestBeacon: %@", closestBeacon.minor.stringValue);
if (viewShown == NO) {
if ([closestBeacon.minor.stringValue isEqual: @"1"]) {
actualSection = 1;
[self immediateDetection];
}else if ([closestBeacon.minor.stringValue isEqual: @"2"]){
actualSection = 2;
[self immediateDetection];
}else if ([closestBeacon.minor.stringValue isEqual: @"3"]){
actualSection = 3;
[self immediateDetection];
}else if ([closestBeacon.minor.stringValue isEqual: @"4"]){
actualSection = 4;
[self immediateDetection];
}else if ([closestBeacon.minor.stringValue isEqual: @"5"]){
actualSection = 5;
[self immediateDetection];
}else if ([closestBeacon.minor.stringValue isEqual: @"6"]){
actualSection = 6;
[self immediateDetection];
NSLog(@"6");
}else if ([closestBeacon.minor.stringValue isEqual: @"7"]){
actualSection = 7;
[self immediateDetection];
NSLog(@"7");
}else if ([closestBeacon.minor.stringValue isEqual: @"8"]){
actualSection = 8;
[self immediateDetection];
NSLog(@"8");
}else if ([closestBeacon.minor.stringValue isEqual: @"9"]){
actualSection = 9;
[self immediateDetection];
NSLog(@"9");
}else if ([closestBeacon.minor.stringValue isEqual: @"10"]){
actualSection = 10;
[self immediateDetection];
NSLog(@"10");
}else if ([closestBeacon.minor.stringValue isEqual: @"11"]){
actualSection = 11;
[self immediateDetection];
NSLog(@"11");
}else if ([closestBeacon.minor.stringValue isEqual: @"30"]){
actualSection = 30;
[self immediateDetection];
}
} else if (viewShown == YES) {
if (closestBeacon.minor.integerValue != actualSection) {
//Alarm: Beacon 11
// if (closestBeacon.minor.integerValue != 11 || closestBeacon.minor.integerValue != 10) {
[self dismissViewControllerAnimated:NO completion:nil];
//actualSection = 0;
viewShown = NO;
// }
}
}
}
- (void)immediateDetection
{
viewShown = YES;
if (self.presentedViewController)
{
return;
}
if (actualSection == 1) {
SectionViewController *sectionView = [[SectionViewController alloc] init];
sectionView.section = 1;
[self presentViewController:sectionView animated:NO completion:nil];
sectionView.lbl_name.text = self.lbl_name.text;
}else if (actualSection == 2){
SectionViewController *sectionView = [[SectionViewController alloc] init];
sectionView.section = 2;
[self presentViewController:sectionView animated:NO completion:nil];
sectionView.lbl_name.text = self.lbl_name.text;
}else if (actualSection == 3){
SectionViewController *sectionView = [[SectionViewController alloc] init];
sectionView.section = 3;
[self presentViewController:sectionView animated:NO completion:nil];
sectionView.lbl_name.text = self.lbl_name.text;
}else if (actualSection == 4){
SectionViewController *sectionView = [[SectionViewController alloc] init];
sectionView.section = 4;
[self presentViewController:sectionView animated:NO completion:nil];
sectionView.lbl_name.text = self.lbl_name.text;
}else if (actualSection == 5){
SectionViewController *sectionView = [[SectionViewController alloc] init];
sectionView.section = 5;
[self presentViewController:sectionView animated:NO completion:nil];
sectionView.lbl_name.text = self.lbl_name.text;
}else if (actualSection == 6){
SectionViewController *sectionView = [[SectionViewController alloc] init];
sectionView.section = 6;
[self presentViewController:sectionView animated:NO completion:nil];
sectionView.lbl_name.text = self.lbl_name.text;
}else if (actualSection == 7){
SectionViewController *sectionView = [[SectionViewController alloc] init];
sectionView.section = 7;
[self presentViewController:sectionView animated:NO completion:nil];
sectionView.lbl_name.text = self.lbl_name.text;
}else if (actualSection == 8){
SectionViewController *sectionView = [[SectionViewController alloc] init];
sectionView.section = 8;
[self presentViewController:sectionView animated:NO completion:nil];
sectionView.lbl_name.text = self.lbl_name.text;
}else if (actualSection == 9){
SectionViewController *sectionView = [[SectionViewController alloc] init];
sectionView.section = 9;
[self presentViewController:sectionView animated:NO completion:nil];
sectionView.lbl_name.text = self.lbl_name.text;
}else if (actualSection == 10){
ThiefAlarmViewController *thiefAlarmView = [[ThiefAlarmViewController alloc] init];
[self presentViewController:thiefAlarmView animated:NO completion:nil];
}else if (actualSection == 11){
ThiefAlarmViewController *thiefAlarmView = [[ThiefAlarmViewController alloc] init];
[self presentViewController:thiefAlarmView animated:NO completion:nil];
}else if (actualSection == 30){
SectionViewController *sectionView = [[SectionViewController alloc] init];
sectionView.section = 30;
[self presentViewController:sectionView animated:NO completion:nil];
sectionView.lbl_name.text = self.lbl_name.text;
}
}
当我知道最接近的 iBeacon 时,我会显示它的 View ,但当它的准确度为 -1.00000 并与其他应用程序进行比较时,应用程序采用第二个,因为第一个的准确度 = -1.00000。
希望有人能帮助我,因为我不知道该怎么做,也许这是一个 iBeacon 问题,但我认为这个问题有解决方案。
谢谢。
最佳答案
locationManager:didRangeBeacons:inRegion:
将按接近顺序返回给您一个信标列表。也就是说,列表中的第一个信标将离您最近。
但是,此规则有一个异常(exception):如果任何信标的接近度为“未知”,则它们将位于列表的顶部(因为它们的准确度为 -1)。为了解决这个问题,只需过滤掉任何未知的信标,然后选择列表中的第一个:
beacons = [beacons filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"proximity != %d", CLProximityUnknown]];
CLBeacon *nearestBeacon = [beacons firstObject];
关于ios - 如何知道最近的 iBeacon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23290544/