Skip to content

issue with heartrate example #57

@mph070770

Description

@mph070770

Hello. I may be misunderstanding something but I couldn't get your heartrate example to work without some modifications.

In ScanListViewController.m, when I was looking for my heartrate sensor, I would get the following error in viewWillAppear:

2017-04-07 17:01:28.751428 RZBluetoothExample[286:10774] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 0 from section 0 which only contains 0 rows before the update'

The first issue (i think) was that scannedDevices wasn't getting populated with anything from scanInfo, so I added the line:

[self.scannedDevices addObject:scanInfo];

just after

[self.centralManager scanForPeripheralsWithServices:@[self.scanUUID] options:@{} onDiscoveredPeripheral:^(RZBScanInfo *scanInfo, NSError *error) {
        if (error) {
            NSLog(@"Error scanning: %@", error);
            return;
        }

This was then correctly populating devices into scannedDevices but they weren't being shown (still the same error above) until I rewrote the populating of the list like this:

NSIndexPath *durPath = [NSIndexPath indexPathForRow:0 inSection:0];
            NSArray *paths = [NSArray arrayWithObject:durPath];
            [self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationNone];

instead of:

[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:existingIndex inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];

I know that this isn't as elegant, but it worked and I now get heartrate data. The full method is this:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.centralManager scanForPeripheralsWithServices:@[self.scanUUID] options:@{} onDiscoveredPeripheral:^(RZBScanInfo *scanInfo, NSError *error) {
        if (error) {
            NSLog(@"Error scanning: %@", error);
            return;
        }
        
        [self.scannedDevices addObject:scanInfo];
        
        __block NSUInteger existingIndex = 0;
        [self.scannedDevices enumerateObjectsUsingBlock:^(RZBScanInfo * info, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([info.peripheral.identifier isEqual:scanInfo.peripheral.identifier]) {
                info.advInfo = scanInfo.advInfo;
                info.RSSI = scanInfo.RSSI;
                existingIndex = idx;
            }
        }];
        
        NSLog(@"%@ - %@", [scanInfo.peripheral.identifier UUIDString], scanInfo.advInfo);

        if (existingIndex == NSNotFound) {
            [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.scannedDevices.count - 1 inSection:0]]
                                  withRowAnimation:UITableViewRowAnimationAutomatic];
        }
        else {
            NSLog(@"numberOfRowsInSection: %ld", (long)[self tableView:self.tableView numberOfRowsInSection:0]);
            
            NSIndexPath *durPath = [NSIndexPath indexPathForRow:0 inSection:0];
            NSArray *paths = [NSArray arrayWithObject:durPath];
            [self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationNone];
            
            //[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:existingIndex inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
            

        }
    }];
}

However, did I do something wrong with your code, or is there a more elegant way of achieving what I "fixed"?

I have a follow-on question which I will post in a second thread.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions