Rightpoint/RZBluetooth

issue with heartrate example

mph070770 opened this issue · 1 comments

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

Interesting, I haven't ran the example in a while. If you have the time to make a PR with your fixes that would help.