Я не получаю правильное значение сигнала для определения расстояния между iPhone и устройством Bluetooth
Я не получаю правильное значение сигнала для определения расстояния между iPhone и устройством Bluetooth. Я подключил 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;
}
}
1 ответ
Метод делегата CBCentralManager:
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
Это возвращает рекламные данные для отсканированных периферийных устройств, то есть периферийных устройств рядом с вами, но не подключенных к нему. В большинстве случаев, когда приложение подключается к периферийному устройству, оно прекращает рекламу (но зависит от реализации в оборудовании).
Получение 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];
});
}
}