CoreBluetooth扫描

时间:2014-04-16 02:23:56

标签: nstimer bluetooth-lowenergy core-bluetooth

我希望实现的结果是每隔8秒使用一个定时器循环一次蓝牙扫描器,外围信号在startScan方法中存在字典,以Rssi打印,开始效果正常,但循环时间开始后一个问题,有时扫描设备,或者外围设备的数量少于错误,我想问一下可以增加哪些稳定性呢?

-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral*)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber*)RSSI{
  NSString *str = [[NSString alloc]initWithFormat:@"%@",peripheral.identifier];
  localName = [advertisementData objectForKey:@"kCBAdvDataLocalName"];
  NSArray * numberArray = [str componentsSeparatedByString:@"> "];
  NSString * numberStr = [numberArray objectAtIndex:1];
  **[mutableDictionary setValue:RSSI forKey:numberStr]; //Save rssi**
}

-(void)startScan:(NSTimer*)timer{
  [_manager stopScan];
  database = [self openDB];
  resultSet = [database executeQuery:@"select * from AboutDog "];
  while([resultSet next]){
      sqlite= [[Sqlite alloc]init];
      sqlite.rssiString =[mutableDictionary valueForKey:numberStr];
  }
  [database close];
  [self.collectionView reloadData];
  [_manager scanForPeripheralsWithServices:nil options:options];
  mutableDictionary = [NSMutableDictionary new]; //Each side of the empty
  [NSTimer scheduledTimerWithTimeInterval:8 target:self selector:@selector(startScan:)userInfo:nil repeats:NO];//Cycles every 8 seconds
}

0 个答案:

没有答案