iOS Question Getting Continuous RSSI Reading from BLE

walterf25

Expert
Licensed User
Longtime User
Hi All, i'm working on a small app which connects to a Nordic Thingy Device via BLE, the sensor mainly just measures Temperature and notifies the app every 30 seconds the temperature Value, this is all working good, but I also need to get the RSSI value periodically, i found the following code in the forum to get the RSSI value and it seems to work, except that when the RSSI value the first time for some reason the temperature value stops being sent to the app.
ReadRSSI Function:
#IF OBJC
#import <CoreBluetooth/CoreBluetooth.h>
#import <CoreBluetooth/CBPeripheral.h>

- (void)GetRSSIVal:(CBPeripheral*)peripheral {
    peripheral.delegate = self; //IMPORTANT or the event wont fire!
    [peripheral readRSSI];
}

- (void)peripheral:(CBPeripheral *)peripheral
       didReadRSSI:(NSNumber *)RSSI
             error:(NSError *)error {
            [self.bi raiseEvent:nil event:@"peripheral_rssiavailable:" params:@[RSSI]];
}
#End If

And then the following function is used to call the objective C code:
B4X:
Sub GetRSSI
    ''If connected = False Then Return 'No RSSI Available.
    Dim per As NativeObject = manager.GetPeripheralObject
    Dim NativeMe As NativeObject = Me
    NativeMe.RunMethod("GetRSSIVal:", Array(per))
End Sub

Sub Peripheral_RssiAvailable(RSSI As Double)
    Log("RSSI: " & RSSI)
End Sub

As I mentioned, the code seems to work, except that the RSSI value is only returned once and everything else stops working as well, the app continues to run but the the Temperature Value from the Nordic sensor stops being transmitted.

Does anyone have any input on this, why would the temperature value stop being transmitted when the GetRSSIVal function is called, if I comment out the GetRSSIVal line then everything goes back to normal, the temperature value is sent every 30 seconds.

Thanks all in advance for your help.

Walter
 

JordiCP

Expert
Licensed User
Longtime User
I guess that, as you are changing the delegate, you are preventig the previous delegate to receive the temperature reads.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Please post the link.
This is the link to the thread

Any help on how to fix the issue on the first post will be greatly appreciated.

Thanks,
Walter
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
I guess that, as you are changing the delegate, you are preventig the previous delegate to receive the temperature reads.
That's what i'm thinking but if that is the case, how can I keep both things working together?

Walter
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
That code returns the RSSI, but needs to be combined with a previous call to the readRSSI method, otherwise it always returns null.
RSSI field will only be initialized after calling readRSSI, and updated each time it is called. The 'complete' working solution is
B4X:
Sub GetSomeRSSIValues
    For k=0 to 10 'Just for test
        Dim per As NativeObject = manager.GetPeripheralObject
        per.RunMethod("readRSSI", Null)  '<- needs to be called
        Dim rro As NativeObject = per.GetField("RSSI")
        If rro.IsInitialized Then
            manager_rssiavailable(rro.AsNumber)
        Else
            Log("Not yet initted")  'Usually it will be if you call readRSSI before, but this prevents crashes if this is not the case.
        End If
        Sleep(2000)
    Next
End Sub

Sub manager_rssiavailable(RSSI As Int)
    Log( "New RSSI value: "&RSSI)
End Sub
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
That code returns the RSSI, but needs to be combined with a previous call to the readRSSI method, otherwise it always returns null.
RSSI field will only be initialized after calling readRSSI, and updated each time it is called. The 'complete' working solution is
B4X:
Sub GetSomeRSSIValues
    For k=0 to 10 'Just for test
        Dim per As NativeObject = manager.GetPeripheralObject
        per.RunMethod("readRSSI", Null)  '<- needs to be called
        Dim rro As NativeObject = per.GetField("RSSI")
        If rro.IsInitialized Then
            manager_rssiavailable(rro.AsNumber)
        Else
            Log("Not yet initted")  'Usually it will be if you call readRSSI before, but this prevents crashes if this is not the case.
        End If
        Sleep(2000)
    Next
End Sub

Sub manager_rssiavailable(RSSI As Int)
    Log( "New RSSI value: "&RSSI)
End Sub
Thanks a lot @JordiCP that works like a charm, appreciate your help.

Walter
 
Upvote 0
Top