我想编写一个 iOS 应用程序,列出附近的所有蓝牙设备及其名称、UUID、RSSI 以及距我当前位置的大概距离。我探索了 CoreBlueTooth API 并创建了一个中央管理器,但这没有帮助。以下是我到目前为止所做的事情。扫描 30 秒后,我没有找到附近有任何设备,但当我进入设备设置和蓝牙部分时,它会显示设备列表。知道这里可能出了什么问题吗?
- (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];
}
最佳答案
您可以查看 CoreLocation 的增强功能iOS 7 的框架。在不违反 NDA 的情况下,我不能说太多,但我认为您可能会找到一些有用的信标来指导您的道路。 :)
关于ios - 如何使用 iOS 应用程序识别 BTLE 设备的名称、UUID、RSSI 和大致距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17332354/