为什么找不到我的信标? [英] Why doesn't it find my beacons?

查看:88
本文介绍了为什么找不到我的信标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写需要在设备周围查找BLE信标的Android和iOS应用.

I am writing both Android and iOS apps which need to find BLE beacons around the device.

当我从Android运行代码时,它会在我所在的房间中找到多个信标.

When I run my code from Android, it finds several beacons in the room I am in.

我有8个信标.

当我从iPhone运行信标代码时,它将返回正好为0个信标的列表.

When I run the beacon code from iPhone, it returns a list of exactly 0 beacons.

这是我的代码:

    - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    [self initRegion];
    [self locationManager:self.locationManager didStartMonitoringForRegion:self.beaconRegion];
}

- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}

- (void)initRegion {
    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:BEACONUUID];
    self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:BEACONIDENTIFIER];
    [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];
    self.beaconFoundLabel.text = @"No";
}

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
    CLBeacon *beacon = [beacons lastObject];//<== is always 0

    self.beaconFoundLabel.text = @"Yes";
    self.proximityUUIDLabel.text = beacon.proximityUUID.UUIDString;
    self.majorLabel.text = [NSString stringWithFormat:@"%@", beacon.major];
    self.minorLabel.text = [NSString stringWithFormat:@"%@", beacon.minor];
    self.accuracyLabel.text = [NSString stringWithFormat:@"%f", beacon.accuracy];
    if (beacon.proximity == CLProximityUnknown) {
        self.distanceLabel.text = @"Unknown Proximity";
    } else if (beacon.proximity == CLProximityImmediate) {
        self.distanceLabel.text = @"Immediate";
    } else if (beacon.proximity == CLProximityNear) {
        self.distanceLabel.text = @"Near";
    } else if (beacon.proximity == CLProximityFar) {
        self.distanceLabel.text = @"Far";
    }
    self.rssiLabel.text = [NSString stringWithFormat:@"%ld", (long)beacon.rssi];
}

在我的 didRangeBeaconsInRegion 中,信标 NSArray 总是带有0个对象.虽然我有8个对象.而且我下载了一些不是我的应用程序,它们都看到了我的几个信标.

In my didRangeBeaconsInRegion, the beacons NSArray always comes up with 0 objects. Though I have 8 objects. And i've downloaded several apps that are not mine, and they all see several of my beacons.

为什么我的代码看不到我的任何信标?

Why doesn't my code see any of my beacons?

推荐答案

每当我设置iBeacon应用程序时,这就是我要做的事情.

Here is what I do whenever I'm setting up an iBeacon app.

并非所有这些事情都是必需的,但是它将

Not all these things are necessary, but it will

  1. 工作
  2. 让用户满意
  3. (也许最重要的是)让苹果开心


仅iOS 8 +

第一件事:如果您使用的是iOS 8,则在使用CLLocationManager之前需要确保您确实具有访问权限.


iOS 8+ Only

First things first: if you're using iOS 8, you need to make sure you actually have access before using CLLocationManager.

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;

// You can either use requestAlwaysAuthorization, or requestWhenInUseAuthorization
[self.locationManager requestAlwaysAuthorization];

您还将在plist中再次输入 NSLocationAlwaysUsageDescription 的条目(同样,仅适用于iOS 8)

You'll also need an entry for NSLocationAlwaysUsageDescription in your plist (again, iOS 8 only )

无论您使用的是iOS 8还是7,都应在plist文件中添加以下内容(您需要确定是否使用背景).

Regardless you're using iOS 8 or 7, you should add the following to your plist file (you need to decide if you'll use background or not).

注意::以下形式为KEY:KEY TYPE:用于字符串的VALUE,KEY:KEY TYPE:用于数组的[Value1,Value2 ...] :

Note: The below is in the form of KEY : KEY TYPE : VALUE for string, and KEY : KEY TYPE : [ Value1, Value2... ] for Arrays:

NSLocationUsageDescription : String : "Gimmie access to your location or else..."
NSBluetoothPeripheralUsageDescription : String : "Gimmie access to your blue tooth or else"

// Optionally supply if you need background modes.  I don't believe you need this either if you plan to turn these options on using the Capabilities section of your App's Settings (see below section)
UIBackgroundModes : Array : [ location, bluetooth-central ]
UIApplicationExitsOnSuspend : Boolean : NO





此部分已删除,因为这可能导致您的应用被拒绝(如@heypiotr在评论中指出的那样)

this section has been removed as this can cause your app to be rejected (as noted by @heypiotr in the comments)


最终思想

最后的建议是尝试将 [self locationManager:self.locationManager didStartMonitoringForRegion:self.beaconRegion] 移到您的 viewDidAppear 中.

这是我通常所做的一个示例(对我来说效果很好).

Here is an example of what I typically do ( which works quite well for me ).

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [self initLocationManager];
    [self initBeaconRegion];
    [self startMonitoring];
}

-(void)initLocationManager {

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

    // Not necessary, but I like to do it.
    if( ![CLLocationManager locationServicesEnabled] ) {
        [self.locationManager startUpdatingLocation];
    }

    // Only necessary if you're in iOS 8.  Checking for existence though to support iOS 7
    if( ![CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorized ) {
        if ([CLLocationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [self.locationManager performSelector:@selector( requestAlwaysAuthorization )];
        }
    }
}

-(void)initBeaconRegion {

    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:kYOUR_UUID_HERE];

    self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:kYOUR_IDENTIFIER_HERE];

    // Change this to whatever you want....
    self.beaconRegion.notifyEntryStateOnDisplay = YES;
    self.beaconRegion.notifyOnEntry = NO;
    self.beaconRegion.notifyOnExit = YES;
}


# pragma mark -
# pragma mark Monitoring Beacons
# pragma mark -

-(void)startMonitoring {
    // Monitor Beacon signals around me and report them back to me
    [self.locationManager startMonitoringForRegion:self.beaconRegion];
}

最后一部分(放置在 viewDidAppear 中)可能有帮助,也可能没有帮助-这全都取决于我认为有太多因素需要在stackoverflow响应中考虑.

The last part ( placement in viewDidAppear ) may or may not help - it all depends I guess on too many factors to consider in a stackoverflow response.

希望有帮助,祝你好运!

Hope that helps and good luck!

忘记了另一件事可能会有所帮助.您可以实施一些有用的方法,这些方法可以帮助您进一步调试问题.这是其中一些示例:

Forgot one more thing that may help. There are some helpful methods that you can implement that can help you debug the issue further. Here is an example of a few of them:

#pragma mark Authorization Status Changed
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {

    if( ![CLLocationManager locationServicesEnabled] ) {
        NSLog(@"Couldn't turn on ranging: Location services are not enabled.");
    } else {
        NSLog(@"Location services ARE enabled.");
    }

    if( [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorized ) {
        NSLog(@"Couldn't turn on monitoring: Location services not authorized.");
    } else {
        NSLog(@"Location services ARE authorized.");
    }
}


#pragma mark -
#pragma mark CLLocationManager Errors
#pragma mark -

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog( @"FAIL ERROR: %@", [error description] );
}

-(void)locationManager:(CLLocationManager *)manager rangingBeaconsDidFailForRegion (CLBeaconRegion *)region withError:(NSError *)error {
    NSLog( @"RANGE BEACONS ERROR: %@", [error description] );
}

这篇关于为什么找不到我的信标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆