我没有得到正确的信号值来识别 iPhone 和蓝牙设备之间的距离
I am not getting the proper signal value for identify the distance between iPhone and the bluetooth device
我没有得到正确的信号值来识别 iPhone 和蓝牙设备之间的距离。我已使用以下代码连接 Peripheral
设备。
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
if (central.state != CBCentralManagerStatePoweredOn) {
return;
}
switch (central.state) {
case CBCentralManagerStatePoweredOn:{
centmanager = central;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[centmanager scanForPeripheralsWithServices:nil options:options];
NSArray *uuidArray = @[uuid];
NSArray *itemArray = [central retrieveConnectedPeripheralsWithServices:uuidArray];
if ([itemArray count]>0) {
self.myPeripheral = [itemArray objectAtIndex:0];
[centmanager connectPeripheral:self.myPeripheral options:nil];
}
return;
}
break;
default:
break;
}
}
并且在 CBCentralManagerDelegate
的委托方法中,我得到了 RSSI
值,但这不正确且不一致。
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
// Reject any where the value is above reasonable range
if (RSSI.integerValue > -15) {
NSLog(@"\n\n Above reasonable range and Range ==>>> %d",RSSI.integerValue);
return;
}
// Reject if the signal strength is too low to be close enough (Close is around -22dB)
if (RSSI.integerValue < -35) {
NSLog(@"\n\n Out Of Range and Range ==>>> %d",RSSI.integerValue);
return;
}
}
CBCentralManager 委托方法:
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
此returns扫描外围设备的广告数据,即您附近但未连接的外围设备。大多数情况下,一旦应用程序连接到外围设备,它就会停止广告(但取决于硬件中的实现)。
获取已连接外围设备的 RSSI
这就是我为我的项目所做的,以获得 RSSI 的持续更新并且它有效。
连接后
[self.connectedPeripheral setDelegate:self];
[self.connectedPeripheral readRSSI];
这将调用 CBPeripheralDelegate 方法
/*!
* @method peripheral:didReadRSSI:error:
*
* @param peripheral The peripheral providing this update.
* @param RSSI The current RSSI of the link.
* @param error If an error occurred, the cause of the failure.
*
* @discussion This method returns the result of a @link readRSSI: @/link call.
*/
- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(nullable NSError *)error {
if([self.connectedPeripheral isEqualTo:peripheral]) {
// <Use the RSSI value>
/*
* Call the readRSSI method again after a certain time, I am using
* RSSI_UPDATE_INTERVAL -> 5 secs
* This will get you an update of RSSI continuously
*/
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, RSSI_UPDATE_INTERVAL * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[peripheral readRSSI];
});
}
}
我没有得到正确的信号值来识别 iPhone 和蓝牙设备之间的距离。我已使用以下代码连接 Peripheral
设备。
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
if (central.state != CBCentralManagerStatePoweredOn) {
return;
}
switch (central.state) {
case CBCentralManagerStatePoweredOn:{
centmanager = central;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[centmanager scanForPeripheralsWithServices:nil options:options];
NSArray *uuidArray = @[uuid];
NSArray *itemArray = [central retrieveConnectedPeripheralsWithServices:uuidArray];
if ([itemArray count]>0) {
self.myPeripheral = [itemArray objectAtIndex:0];
[centmanager connectPeripheral:self.myPeripheral options:nil];
}
return;
}
break;
default:
break;
}
}
并且在 CBCentralManagerDelegate
的委托方法中,我得到了 RSSI
值,但这不正确且不一致。
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
// Reject any where the value is above reasonable range
if (RSSI.integerValue > -15) {
NSLog(@"\n\n Above reasonable range and Range ==>>> %d",RSSI.integerValue);
return;
}
// Reject if the signal strength is too low to be close enough (Close is around -22dB)
if (RSSI.integerValue < -35) {
NSLog(@"\n\n Out Of Range and Range ==>>> %d",RSSI.integerValue);
return;
}
}
CBCentralManager 委托方法:
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
此returns扫描外围设备的广告数据,即您附近但未连接的外围设备。大多数情况下,一旦应用程序连接到外围设备,它就会停止广告(但取决于硬件中的实现)。
获取已连接外围设备的 RSSI
这就是我为我的项目所做的,以获得 RSSI 的持续更新并且它有效。
连接后
[self.connectedPeripheral setDelegate:self];
[self.connectedPeripheral readRSSI];
这将调用 CBPeripheralDelegate 方法
/*!
* @method peripheral:didReadRSSI:error:
*
* @param peripheral The peripheral providing this update.
* @param RSSI The current RSSI of the link.
* @param error If an error occurred, the cause of the failure.
*
* @discussion This method returns the result of a @link readRSSI: @/link call.
*/
- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(nullable NSError *)error {
if([self.connectedPeripheral isEqualTo:peripheral]) {
// <Use the RSSI value>
/*
* Call the readRSSI method again after a certain time, I am using
* RSSI_UPDATE_INTERVAL -> 5 secs
* This will get you an update of RSSI continuously
*/
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, RSSI_UPDATE_INTERVAL * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[peripheral readRSSI];
});
}
}