如何在IOS中扫描Beacons?

时间:2015-12-25 06:33:41

标签: ios objective-c ibeacon

我正在开发一个应用程序来搜索最近的信标设备,但无法调用任何代表,我已经有信标可用但没有找到任何信标。

另外我不明白NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"3863C90B-1BA6-4A4E-ADD2-D64FF1286898"];是否传递静态?

 NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"3863C90B-1BA6-4A4E-ADD2-D64FF1286898"];
    self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
                                                             identifier:@"com.jss.myidentifire"];

    self.myBeaconRegion.notifyOnEntry = YES;
    [self.locationManager startMonitoringForRegion:self.myBeaconRegion];
    [self.locationManager startUpdatingHeading];
    [self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
-(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{
    [locationManager requestStateForRegion:region];
}
-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
    if (state == CLRegionStateInside)
    {
        [self locationManager:locationManager didEnterRegion:region];
    }

}
- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion *)region
{
    // We entered a region, now start looking for our target beacons!
    self.statusLabel.text = @"Finding beacons.";
    [self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"BeaconDetail" message:@"DidEnterRegion Called" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
    [alert show];
}
-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion *)region
{
    // Exited the region
    self.statusLabel.text = @"None found.";
    [self.locationManager stopRangingBeaconsInRegion:self.myBeaconRegion];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"BeaconDetail" message:@"didExitRegion Called" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
    [alert show];

}
-(void)locationManager:(CLLocationManager*)manager
       didRangeBeacons:(NSArray*)beacons
              inRegion:(CLBeaconRegion*)region
{
    self.statusLabel.text = @"Beacon found!";
    CLBeacon *foundBeacon = [beacons firstObject];
    NSString *uuid = foundBeacon.proximityUUID.UUIDString;
    NSString *major = [NSString stringWithFormat:@"%@", foundBeacon.major];
    NSString *minor = [NSString stringWithFormat:@"%@", foundBeacon.minor];
    NSString *accuracy = [NSString stringWithFormat:@"%f", foundBeacon.accuracy];
    accuracy = [NSString stringWithFormat:@"%f", foundBeacon.accuracy];

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

    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [dict setObject:uuid forKey:@"uuid"];
    [dict setObject:major forKey:@"major"];
    [dict setObject:minor forKey:@"minor"];
    [dict setObject:accuracy forKey:@"accuracy"];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"BeaconDetail" message:[NSString stringWithFormat:@"%@",dict] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
    [alert show];
}

2 个答案:

答案 0 :(得分:2)

通过添加以下内容确保您已获得扫描信标的权限:

if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
    [self.locationManager requestAlwaysAuthorization];
}

要使上述工作正常,您还需要在Info.plist文件中添加一个新密钥:NSLocationAlwaysUsageDescription,其值设置为"此应用程序要扫描信标"

答案 1 :(得分:-1)

如果你已经在信标范围内启动接收器,它就不会发射。只是检查你是否远离灯塔走路然后走回范围才能开火?