我想写一个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];
}
答案 0 :(得分:2)
您可能会看一下iOS 7 CoreLocation框架的增强功能。在不破坏NDA的情况下,我不能说更多,但我认为您可能会找到一些有用的信标来指导您的方式。 :)
答案 1 :(得分:1)
首先在iOS设置中显示所有蓝牙设备(BLE而不是BLE)。根据您的代码,它只能检测在外设模式下工作的蓝牙低功耗设备。请确保您列出了一个在外围模式下工作的BLE设备(例如iBeacon)。通常用于检测中央或外围模式的BLE设备使用经典的BT框架。对于iBeacon设备更好地应用CoreLocation框架。
答案 2 :(得分:1)
BLE返回一个信号强度参数,可用于查找设备接近度。如果您可以从核心蓝牙获得它,则可以在路径损耗方程中使用并找到距离您最近的设备