我正在研究Estimote信标,并制作了演示应用程序以找出最接近的信标的次要和主要价值。但是,当我检查estimote示例应用程序时,我会获得适当的主要和次要价值,但是当我在项目中使用该代码时,它给了我错误的次要和主要价值。

这是我的代码:-

typedef enum : int
{
    ESTScanTypeBluetooth,
    ESTScanTypeBeacon

} ESTScanType;


- (id)initWithScanType:(ESTScanType)scanType completion:(void (^)(ESTBeacon *))completion
{
    self = [super init];
    if (self)
    {
        self.scanType = scanType;
        self.completion = [completion copy];
    }
    return self;

}
- (void)viewDidLoad
{
    [super viewDidLoad];

  self.beaconManager = [[ESTBeaconManager alloc] init];
    self.beaconManager.delegate = self;

   self.region = [[ESTBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:BEACONUUID1]
        identifier:@“AllBeacons”];
    if (self.scanType == ESTScanTypeBeacon)
    {
        [self.beaconManager startRangingBeaconsInRegion:self.region];
    }
    else
    {
        [self.beaconManager startEstimoteBeaconsDiscoveryForRegion:self.region];
    }

    // Do any additional setup after loading the view from its nib.
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

    /*
     *Stops ranging after exiting the view.
     */
    [self.beaconManager stopRangingBeaconsInRegion:self.region];
    [self.beaconManager stopEstimoteBeaconDiscovery];
}

#pragma mark - ESTBeaconManager delegate

- (void)beaconManager:(ESTBeaconManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(ESTBeaconRegion *)region
{
    self.beaconsArray = beacons;

    [tblView reloadData];
}

- (void)beaconManager:(ESTBeaconManager *)manager didDiscoverBeacons:(NSArray *)beacons inRegion:(ESTBeaconRegion *)region
{
    self.beaconsArray = beacons;

    [tblView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    __weak  BeconsDetailCell *cell=(BeconsDetailCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"BeconsDetailCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

     ESTBeacon *beacon = [self.beaconsArray objectAtIndex:indexPath.row];

    //beacon name

    cell.lblBeaconname.text = @"Estimote Beacon";


    //Beacon major

    NSString *strMajor = [NSString stringWithFormat:@"%@",beacon.major];
    strMajor = [strMajor stringByReplacingOccurrencesOfString:@"-" withString:@""];
    cell.lblBeaconMajor.text = strMajor;

    //Beacon minor

    NSString *strMinor = [NSString stringWithFormat:@"%@",beacon.minor];
    strMinor = [strMinor stringByReplacingOccurrencesOfString:@"-" withString:@""];
    cell.lblBeaconMinor.text = strMinor;

    //Beacon distance

    switch (beacon.proximity)
    {
        case CLProximityUnknown:
            cell.lblBeaconDistance.text = @"Unknown";
            break;

        case CLProximityFar:
            cell.lblBeaconDistance.text = @"Far";
            break;

        case CLProximityNear:

            cell.lblBeaconDistance.text = @"Near";
            break;

        case CLProximityImmediate:
            cell.lblBeaconDistance.text = @"Immediate";
            break;

        default:
            break;
    }
    return cell;
}


提前致谢。

最佳答案

如果您参考ESTBeacon class的文档,则会看到major和minor是NSNumber,尽管可以在字符串上下文中评估NSNumber以通过description方法获取值,但这是不正确的数值访问方式。

你应该用

if (beacon.major != nil) {
   cell.lblBeaconMajor.text= [NSString stringWithFormat:@"%ld",[beacon.major intValue]];
}
else {
   cell.lblBeaconMajor.text=@"";
}

if (beacon.minor != nil) {
   cell.lblBeaconMinor.text= [NSString stringWithFormat:@"%ld",[beacon.minor intValue]];
}
else {
   cell.lblBeaconMinor.text=@"";
}

关于ios - 错误估计估计信标的次要和主要值(value),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24954531/

10-10 21:07