ios - 如何使用 iOS 应用程序识别 BTLE 设备的名称、UUID、RSSI 和大致距离

标签 ios objective-c cocoa-touch core-bluetooth

我想编写一个 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/

相关文章:

ios - 如果在 block 中引用 self,为什么这不是一个保留循环?

iphone - 适用于大型应用程序的模块化架构,具有最佳实践以及 Storyboard 和 XIB 的使用

ios - 可选值上的 Swift KeyPath

ios 无法获取 BatchedBridge,请确保您的包已正确打包

带有改变大小的 UIButtons 的 iOS 回流布局

ios - 未找到 RCTBridgeModule - RNGoogleSignin

iOS 9 - 网络可达性变化导致 View 卡住 - 不是崩溃

objective-c - 为什么我们使用 [super dealloc] 而不是 [self dealloc] 或 [object dealloc]

ios - 我们可以在 swift playgrounds 中使用 AVAudiorecorder 吗?

ios - 在 UIWebView 中禁用下载