0

我正在为 iPad 开发一个应用程序,它使用多个信标,当我进入它的区域时我需要显示一个视图,然后,当我靠近信标而远离另一个信标时,我需要第一个关闭并打开第二个。

我的代码是这样的:

    -(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
    CLBeacon *beacon = [[CLBeacon alloc] init];
    beacon = [beacons lastObject];


    SectionViewController *sectionView = [[SectionViewController alloc] init];
    Sections2ViewController *sectionView2 = [[Sections2ViewController alloc] init];

    if ((beacon.proximity == CLProximityImmediate) && viewShown == NO && beacon.accuracy != -1.000000) {

        if ((beacon.minor.integerValue != actualSection) && actualSection != 0) {
            [self dismissViewControllerAnimated:NO completion:nil];
            actualSection = 0;
            NSLog(@"View Out");
        }

        if (beacon.minor.integerValue  == 1) {
            [self presentViewController:sectionView animated:NO completion:nil];
            sectionView.section = 1;
            actualSection = 1;
            sectionView.lbl_name.text = self.lbl_name.text;
            NSLog(@"View In: 1");
            viewShown = YES;
        }else if (beacon.minor.integerValue == 2){
            [self presentViewController:sectionView2 animated:NO completion:nil];
            sectionView.section = 2;
            actualSection = 2;
            NSLog(@"View In: 2");
            viewShown = YES;
        }
    }else if ((beacon.proximity == CLProximityNear || beacon.proximity == CLProximityFar) && viewShown == YES && beacon.accuracy != -1.000000){
        [self dismissViewControllerAnimated:NO completion:nil];
        viewShown = NO;
    }else{
        NSLog(@"Unknown Distance");
    }

    NSLog(@"\nUUID: %@\nProximity:%ld\nMajor:%@\nMinor:%@\nAccuracy:%f\nRSSI:%ld\nSection: %ld", beacon.proximityUUID.UUIDString, beacon.proximity, beacon.major, beacon.minor, beacon.accuracy, (long)beacon.rssi, (long)self.section);
}
  • sectionView 和 SectionView2 是我要显示的视图。

  • 我使用 viewShown 来了解是否显示了视图或者我在 mainView 中。

  • 实际部分是要知道我在哪里。

问题是,如果我只使用 1 个信标,它可以完美运行,但是当我使用 2 个信标时,应用程序不会同时检测到它们,并且很难显示正确的视图。

我现在正在尝试,但该应用程序仅检测到 1 个信标,并且我在它们两个上都使用相同的 UUID,并且仅更改次要值。

我现在在我的 iPad Mini 视网膜上使用 ios 7.0.4,但我很快就会更新到 iOS 7.1 - 有问题吗?


解决了:

    - (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];
    beacon = [beacons lastObject];

    //Search for the closest and show the view
    for (int i=0; i<beacons.count; i++) {
        beacon = beacons[i];

        if (beacon.proximity == CLProximityUnknown) {
            NSLog(@"Unknown Proximity");
        } else if ((beacon.proximity == CLProximityImmediate) && viewShown == NO && beacon.accuracy != -1.000000) {
            if ([beacon.minor.stringValue  isEqual: @"1"]) {
                actualSection = 1;
                [self immediateDetection];
            }else if ([beacon.minor.stringValue  isEqual: @"2"]){
                actualSection = 2;
                [self immediateDetection];
            }else if ([beacon.minor.stringValue  isEqual: @"3"]){
                actualSection = 3;
                [self immediateDetection];
            }else if ([beacon.minor.stringValue  isEqual: @"4"]){
                actualSection = 4;
                [self immediateDetection];
            }
        } else if ((beacon.proximity == CLProximityNear || beacon.proximity == CLProximityFar) && viewShown == YES && beacon.accuracy != -1.000000) {
            if (beacon.minor.integerValue == actualSection) {
                [self dismissViewControllerAnimated:NO completion:nil];
                actualSection = 0;
                viewShown = NO;
            }
        } else if (beacon.proximity == CLProximityFar) {
            NSLog(@"Far");
        }
    }
}
- (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;
    }
}

现在这是正确的并检测到接近的。谢谢

4

0 回答 0