1

我知道如何scanForPeripheralsWithServices使用它的回调来接收新的可用外围设备通知didDiscoverPeripheral来填充UITableView列表。

但是,当未连接的蓝牙设备不再可用时,如何接收通知?

/****************************************************************************/
/*                              Discovery                                   */
/****************************************************************************/
- (void) startScanningForUUIDString:(NSString *)uuidString
{
    NSArray         *uuidArray  = [NSArray arrayWithObjects:[CBUUID UUIDWithString:uuidString], nil];
    NSDictionary    *options    = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];

    [centralManager scanForPeripheralsWithServices:uuidArray options:options];
}

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    if (![foundPeripherals containsObject:peripheral]) {
        [foundPeripherals addObject:peripheral];
        [discoveryDelegate discoveryDidRefresh];
    }
}
4

1 回答 1

3

我认为您想知道的原因是因为您想刷新foundPeripherals数组并正确更新 UI?好吧,由于您当前与设备没有联系,因此无法知道设备何时移开或靠近。您的解决方案是这样的:启动一个 NSTimer 来清除您foundPeripherals并在它触发时再次开始发现。如果您在新发现中看到它,它仍然存在,您应该重新添加到foundPeripherals. 否则就没了。

于 2013-06-10T21:44:49.683 回答