0

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;
   }
}
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • Are you looking for the RSSI value of the connected device or the devices near by? - centralManager:didDiscoverPeripheral:advertisementData:RSSI: method returns the RSSI value of the scanned devices not the connected one. – Krrish Nov 23 '16 at 15:16
  • See http://stackoverflow.com/a/30174335/294949 – danh Nov 23 '16 at 15:20
  • @Krrish Thank you for the kind reply. how to get the connected bluetooth device's distance? – Paras Joshi Nov 24 '16 at 06:32

1 Answers1

1

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];
       });
    }
 }
Krrish
  • 2,256
  • 18
  • 21
  • Thank you so much for the solution. Actually I've tried this method for read the RSSI but the method readRSSI didn't call second time and I not got idea that I can also call this method second time with the time duration. Thanks again for the code and your great idea. :) – Paras Joshi Nov 24 '16 at 09:57