Paras Joshi Paras Joshi - 10 days ago 5
iOS Question

I am not getting the proper signal value for identify the distance between iPhone and the bluetooth device

I am not getting the proper signal value for identify the distance between iPhone and the bluetooth device. I've connect the

Peripheral
device with below code.

- (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;
}
}


And in the delegate method of the
CBCentralManagerDelegate
, I got the
RSSI
value but that is not right and consistent.

- (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;
}
}

Answer

The CBCentralManager delegate method:

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI

This returns the advertisement data for the scanned peripherals i.e. the peripherals near you but not connected to it. Most of the cases once an app connects to a peripheral it stops its advertisement(but depends on the implementation in the hardware).

Getting RSSI of connected peripheral

This is what I did for my project to get continuos update of RSSI and it works.

Once connected

[self.connectedPeripheral setDelegate:self];
[self.connectedPeripheral readRSSI];

This will call the CBPeripheralDelegate method

/*!
 *  @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];
       });
    }
 }
Comments