繁体   English   中英

无法在iPhone 6上检测到ibeacon

[英]Unable to detect ibeacon on iphone 6

嗨,我有下面的代码可以在iPod touch 5上正常工作,但是相同的代码在iPhone 6上无法正常工作,我对相同的问题进行了研究,但是我没有发现任何有用的东西。 两种设备都具有最新的iOS。

这两台设备都装有iOS 8

//  MapViewController.m
//  SidebarDemo
//
//  Created by Simon on 30/6/13.
//  Copyright (c) 2013 Appcoda. All rights reserved.
//

#import "PetFinderViewController.h"
#import "SWRevealViewController.h"

@interface PetFinderViewController ()

@end

@implementation PetFinderViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithRed:(51/255.0) green:(51/255.0) blue:(51/255.0) alpha:1] ;

    self.title = @"Pet Finder";

    // Change button color
    //_sidebarButton.tintColor = [UIColor colorWithWhite:0.96f alpha:0.2f];

    // Set the side bar button action. When it's tapped, it'll show up the sidebar.
    _sidebarButton.target = self.revealViewController;
    _sidebarButton.action = @selector(revealToggle:);

    // Set the gesture
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

    // Check if beacon monitoring is available for this device
    if (![CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]]) {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Monitoring not available" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; return;
    }
    else
    {
        // Initialize location manager and set ourselves as the delegate
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;


        // Create a NSUUID
        NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"ebefd083-70a2-47c8-9837-e7b5634df524"];

        // Setup a new region AND start monitoring
        str_beaconIdentifier = @"in.appstute.marketing";
        self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid major:1 minor:1 identifier:str_beaconIdentifier];

        self.myBeaconRegion.notifyEntryStateOnDisplay = YES;
        self.myBeaconRegion.notifyOnEntry = YES;
        self.myBeaconRegion.notifyOnExit = YES;

        [self.locationManager startMonitoringForRegion:self.myBeaconRegion];

        self.lbl_rangeStatus.text = @"Finding Your Pet";
        self.lbl_regionStatus.text = @"";
        self.lbl_distance.text = @"";
    }
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"Couldn't turn on ranging: Location services are not enabled.");
    }

    if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorized) {
        NSLog(@"Couldn't turn on monitoring: Location services not authorised.");
        [self.locationManager requestAlwaysAuthorization];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Core Location Delegate methods

- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion *)region
{

    UILocalNotification *notify = [[UILocalNotification alloc] init];
    notify.alertBody = @"You are near your Pet's region.";
    notify.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] presentLocalNotificationNow:notify];

    // We entered a region, now start looking for our target beacons!
    //self.statusLabel.text = @"Finding beacons.";
    self.lbl_rangeStatus.text = @"Pet Found";
    self.lbl_regionStatus.text = @"Status : Entered Region";
    [self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];

    //Opening camera
    /*if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
        imagePicker.delegate = self;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.allowsEditing = YES;

        //[self presentModalViewController:imagePicker animated:YES];
        [self presentViewController:imagePicker animated:YES completion:nil];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Camera Unavailable"
                                                       message:@"Unable to find a camera on your device."
                                                      delegate:nil
                                             cancelButtonTitle:@"OK"
                                             otherButtonTitles:nil, nil];
        [alert show];
        alert = nil;
    }*/
}


-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion *)region
{
    UILocalNotification *notify = [[UILocalNotification alloc] init];
    notify.alertBody = @"You are far away from your Pet's region.";
    notify.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] presentLocalNotificationNow:notify];

    // Exited the region
    //self.statusLabel.text = @"None found.";
    self.lbl_rangeStatus.text = @"Pet Not Found";
    self.lbl_regionStatus.text = @"Status : Exited Region";
    [self.locationManager stopRangingBeaconsInRegion:self.myBeaconRegion];
}

-(void)locationManager:(CLLocationManager*)manager didRangeBeacons:(NSArray*)beacons inRegion:(CLBeaconRegion*)region
{
    CLBeacon *foundBeacon = [beacons firstObject];

    // Retrieve the beacon data from its properties
    NSString *uuid = foundBeacon.proximityUUID.UUIDString;
    NSString *major = [NSString stringWithFormat:@"%@", foundBeacon.major];
    NSString *minor = [NSString stringWithFormat:@"%@", foundBeacon.minor];
    NSLog(@"uuid=%@, major=%@, minor=%@",uuid, major, minor);

    self.lbl_regionStatus.text = @"Status : Entered Region";

    if(foundBeacon.proximity==CLProximityImmediate)
    {
        NSLog(@"Immediate");
        //self.Lb_proxomity.text = @"Immediate";
    }
    else if (foundBeacon.proximity==CLProximityNear)
    {
        NSLog(@"Near");
        //self.Lb_proxomity.text = @"Near";
    }
    else if(foundBeacon.proximity==CLProximityFar)
    {
        NSLog(@"Far");
        //self.Lb_proxomity.text = @"Far";
    }
    else if(foundBeacon.proximity==CLProximityUnknown)
    {
        NSLog(@"Unknown");
        //self.Lb_proxomity.text = @"Unknown";
    }

    float actualDistance = foundBeacon.accuracy/10;
    NSLog(@"Distance = %f",actualDistance);
    if(actualDistance >= 0.0)
    {
        self.lbl_distance.text = [NSString stringWithFormat:@"Distance : %.2f m",actualDistance];
    }
    //self.Lb_meter.text = [NSString stringWithFormat:@"%.2f",foundBeacon.accuracy];
    //self.Lb_centimeter.text = [NSString stringWithFormat:@"%.2f",(foundBeacon.accuracy*100)];
    //[self presentExhibitInfoWithMajorValue:foundBeacon.major.integerValue];

    //Calling this method to display strength for distance between user and the pet
    [self fn_showStrengthForDistanceBetweenUserAndPet:actualDistance];
}

#pragma mark - Check Background App Refresh status
-(BOOL)CanDeviceSupportAppBackgroundRefresh
{
    // Override point for customization after application launch.
    if ([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusAvailable) {
        NSLog(@"Background updates are available for the app.");
        return YES;
    }else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusDenied)
    {
        NSLog(@"The user explicitly disabled background behavior for this app or for the whole system.");
        return NO;
    }else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusRestricted)
    {
        NSLog(@"Background updates are unavailable and the user cannot enable them again. For example, this status can occur when parental controls are in effect for the current user.");
        return NO;
    }

    return NO;
}

#pragma mark - Check if monitoring region failed
- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
    NSLog(@"monitoringDidFailForRegion - error: %@", [error localizedDescription]);
}


- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLBeaconRegion *)region{

    if (state == CLRegionStateInside) {


        //Start Ranging
        [manager startRangingBeaconsInRegion:region];
    }

    else{

        //Stop Ranging
        [manager stopRangingBeaconsInRegion:region];
    }

}

@end

我怀疑您的iPhone上存在授权问题。 设置一个断点或添加NSLog语句以确保此行被调用:

[self.locationManager requestAlwaysAuthorization];

你得到提示吗? 如果不是,请卸载并重新安装。

另外,请检查设置以确保在电话上启用了蓝牙和位置服务,并检查应用程序上的设置以查看是否已为其实际启用位置服务。

您需要设置以下一项

请求位置更新(即使使用iBeacons)时, NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription

如果不这样做,则在iOS 8中,此操作将静默失败。

暂无
暂无

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

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