0

I have BLE source code that displays several warnings and I am new to BLE. Please see the code below. I have tried replacing with readRSSI but tells me I can’t compare an Int with Void. How do I get an Int value for readRSSI? Or how should I change the code?

- (void)peripheralDidUpdateRSSI:(CBPeripheral * _Nonnull)peripheral error:(NSError * _Nullable)error
{
    if (!isConnected)
        return;
    if (rssi != peripheral.RSSI.intValue)
    {
        rssi = peripheral.RSSI.intValue;
        [[self delegate] bleDidUpdateRSSI:activePeripheral.RSSI];
    }
}

*rssi is a static int.

*isConnected is a boolean.

Edit: The problem is that RSSI is deprecated since iOS 8.0.

Wenjun Wu
  • 21
  • 5

2 Answers2

5

There are two ways of obtaining the CBPeripheral's RSSI. The first is when the peripheral is discovered. You will get a call to the CBCentralManagerDelegate method

func centralManager(_ central: CBCentralManager, 
             didDiscover peripheral: CBPeripheral, 
       advertisementData: [String : Any], 
                    rssi RSSI: NSNumber) {
    let rssi = intValue
    ...
}

If you are running in the foreground then you can supply a value of true to for the key CBCentralManagerScanOptionAllowDuplicatesKey in the scanning options to get repeated calls to didDiscover. This doesn't work in the background.

If you are connected to a peripheral then you can periodically call peripheral.readRSSI(). This will result in a callback to the didReadRSSI CBPeripheralDelegate method:

optional func peripheral(_ peripheral: CBPeripheral, 
         didReadRSSI RSSI: NSNumber, 
               error: Error?) {
    let rssi = RSSI.intValue
    ...
}
Paulw11
  • 108,386
  • 14
  • 159
  • 186
0

swift 3

@IBAction func btnGetRSSI(_ sender: UIButton){

    self.selectedPeripehral.readRSSI() 
}

func peripheral(_ peripheral: CBPeripheral, didReadRSSI RSSI: NSNumber, error: Error?) {

    var rssiInt: Int!

    rssiInt = RSSI.intValue

}
Deepak Tagadiya
  • 2,187
  • 15
  • 28