2

我想编写一个 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];
}
4

3 回答 3

2

您可能会查看 iOS 7 的CoreLocation框架的增强功能。如果不破坏 NDA,我不能说更多,但我认为您可能会找到一些有用的信标来指导您的方式。:)

于 2013-06-27T02:04:25.320 回答
1

首先在 iOS 设置中显示所有蓝牙设备(BLE 和非 BLE)。根据您的代码,它只能检测到在外围模式下工作的低功耗蓝牙设备。请确保您列出了一个在外围模式下工作的 BLE 设备(例如 iBeacon)。通常用于在 Central 或 Peripheral 模式下检测 BLE 设备使用经典的 BT 框架。对于 iBeacon 设备,最好应用 CoreLocation 框架。

于 2015-01-13T10:48:44.017 回答
1

BLE 返回可用于查找设备接近度的信号强度参数。如果您可以从核心蓝牙获得它,您将能够在路径损耗方程中使用并找到离您最近的设备

于 2020-03-07T20:29:28.383 回答