超出范围时,从UITableView中删除BluetoothLE外设

时间:2013-11-21 02:50:09

标签: ios uitableview core-bluetooth

我有一个可以扫描BluetoothLE外围设备的ios应用程序。当它发现它们时,它将它们作为自定义单元格(PeripheralCell)添加到我的UITableView中。当iPhone再次发现它时,它会自动更新UITableView中的RSSI值。

我遇到的问题是当BluetoothLE外设超出范围时,它会保留在我的UITableView上。如何在一段时间后找不到BLE外设时,如何自动更新并从我的表中删除外设...比如1.5秒?

 /**
 Called when scanner finds device
 First checks if it exists, if not then insert new device
 */
- (void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{

    BOOL (^test)(id obj, NSUInteger idx, BOOL *stop);
    test = ^ (id obj, NSUInteger idx, BOOL *stop) {
        if([[[obj peripheral] name] compare:peripheral.name] == NSOrderedSame)
            return YES;
        return NO;
    };

    PeripheralCell* pcell; //custom UITableViewCell
    NSUInteger t=[peripherals indexOfObjectPassingTest:test];
    if(t!= NSNotFound)
    {
        pcell=[peripherals objectAtIndex:t];
        pcell.peripheral=peripheral;
        pcell.rssi=RSSI;
        [scanResult reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:t inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
    }else{
        pcell=[[PeripheralCell alloc] init];
        [peripherals addObject: pcell];
        pcell.peripheral=peripheral;
        pcell.rssi=RSSI;
        [scanResult insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[peripherals count]-1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
    }

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(peripherals==nil)
        return 0;
    return [peripherals count];
}

/**
 Configures the list of found peripherals in the table view
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"foundDevice";

    PeripheralCell *cell = [scanResult dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell.
    PeripheralCell *pcell=[peripherals objectAtIndex: [indexPath row]];
    cell.peripheralTitleLabel.text = [pcell.peripheral name];
    cell.rssiLabel.text = [NSString stringWithFormat:@"RSSI %d",[pcell.rssi intValue]];
    //self.colorNames objectAtIndex: [indexPath row]];

    return cell;
}

0 个答案:

没有答案