近くにあるすべての Bluetooth デバイスを、その名前、UUID、RSSI、現在地からのおおよその距離とともに一覧表示する iOS アプリを作成したいと考えています。CoreBlueTooth API を調査し、中央マネージャーを作成しましたが、役に立ちません。以下は私がこれまでに行ったことです。30 秒間スキャンした後、近くにデバイスが見つかりませんが、デバイス設定と Bluetooth セクションに移動すると、デバイスのリストが表示されます。ここで何が間違っているのでしょうか?
- (void)viewDidAppear:(BOOL)animated {
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
if (central.state != CBCentralManagerStatePoweredOn) {
return;
}
[self scan];
}
- (void)scan
{
self.anAcivityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.anAcivityIndicator.center = self.view.center;
[self.anAcivityIndicator startAnimating];
[self.view addSubview:self.anAcivityIndicator];
[NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(stopScanning) userInfo:nil repeats:NO];
[self.centralManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
NSLog(@"Scanning started");
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSString *aBeaconName = peripheral.name;
NSUUID *anIdentifier = peripheral.identifier;
if (aBeaconName && anIdentifier) {
[self.beaconUUID setObject:anIdentifier forKey:aBeaconName];
}
}
- (void)stopScanning {
[self.centralManager stopScan];
if ([self.beaconUUID count] > 0) {
[self.tableView reloadData];
} else {
UIAlertView *anAlertView = [[UIAlertView alloc] initWithTitle:@"No Data" message:@"No beacon found!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[anAlertView show];
}
}
- (void)alertView:(UIAlertView *)iAlertView clickedButtonAtIndex:(NSInteger)iButtonIndex {
[self.navigationController popToRootViewControllerAnimated:YES];
}