我想寫一個iOS應用程序,列出所有附近的藍牙設備及其名稱,UUID,RSSI和距離我當前位置的大致距離。我探索了CoreBlueTooth API並創建了一箇中央管理器,但這並沒有幫助。以下是我迄今爲止所做的。掃描30秒後,我沒有在我附近找到任何設備,但是當我進入設備設置和藍牙部分時,它會顯示設備列表。任何想法在這裏可能是錯的?如何使用iOS應用程序識別BTLE設備的名稱,UUID,RSSI和大致距離
- (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];
}
您在Settings.apps中登錄的設備是BLE還是傳統藍牙? – Larme