iOS Question Help with inline objective c code

henrywood

Active Member
Licensed User
Hey !

I hope this is not taken as a stupid question - I am new to Objective C and trying to get my head around it.

I am trying to translate this JAVA method into Objective C:

B4X:
protected static double calculateAccuracy(int txPower, double rssi) {

   if (rssi == 0) {
         return -1.0; // if we cannot determine accuracy, return -1.
     }

     double ratio = rssi*1.0/txPower;

   if (ratio < 1.0) {
       return Math.pow(ratio,10);
   
   } else {
       double accuracy =  (0.89976)*Math.pow(ratio,7.7095) + 0.111; 
       return accuracy;
     }
}
My translation thus far to Objective C and a B4i wrapper:

B4X:
#If OBJC
- (double) calculateAccuracy:(int)txPower :(double)rssi
{
   if (rssi == 0) {
       return -1.0; // if we cannot determine accuracy, return -1.
     }

   double ratio = rssi*1.0/txPower;

   if (ratio < 1.0) {
       return powf(ratio,10);
     } else {
       double accuracy =  (0.89976) * powf(ratio,7.7095) + 0.111; 
       return accuracy;
     }
}
#End if

' Returns the estimated distance to a beacon in meters
Sub calculateDistanceFromPowerAndSignalStrength( txPower As Int, rssi As Double) As Double

   Dim n As NativeObject = Me
   Return n.RunMethod("calculateAccuracy::", Array As Object(txPower, rssi)).AsDouble

End Sub
My questions:


1. Is the powf function always available or do I need to import some header file ?
2. I've sometimes seen + instead of - as the first character of the method signature ? What is the difference ?
3. Is this line:

B4X:
Return n.RunMethod("calculateAccuracy::", Array As Object(txPower, rssi)).AsDouble
correct ? Or shouldn't it be:

B4X:
Return n.RunMethod("calculateAccuracy:txPower:rssi:", Array As Object(txPower, rssi)).AsDouble
because the full method signature/selector is calculateAccuracy:txPower:rssi: ?
 
Last edited:

henrywood

Active Member
Licensed User
@narek adonts
Of course you are right about that, but I found that JAVA code on the net and thought I'd learn something about ObjC converting it to ObjC.

What I plan to do with it is to find the distance in meters to an iBeacon (in the iOS version of my app, I am using BLEManager to scan for/react to iBeacons and there is no distance method in the BLEManager - as opposed to in the EasiBeacon library that I am using in the Android version).

So I read online that I could estimate the distance to an iBeacon using that JAVA code and then I thought I would give it a try since the BLEManager reports the rssi (signal strength) and the battery power in the Advertising data of the iBeacon.

So think of it as a challenge to myself...

By the way, do you know the names of the keys in the advertising map reported by the BLEManager ? (Just asking in case you have experience working with iBeacons/BLE components). I am a total newbie with regards to iBeacons.

Also, I am a bit handicapped as the iPhone I use for developing is currently at the repair shop.... :(
 
Last edited:
Top