iOS Question Help with inline objective c code

Discussion in 'iOS Questions' started by henrywood, Apr 16, 2015.

  1. henrywood

    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:

    Code:
    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:

    Code:
    #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:

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

    Code:
    Return n.RunMethod("calculateAccuracy:txPower:rssi:"Array As Object(txPower, rssi)).AsDouble
    because the full method signature/selector is calculateAccuracy:txPower:rssi: ?
     
    Last edited: Apr 17, 2015
  2. narek adonts

    narek adonts Well-Known Member Licensed User

    Why you need objective c for this method. You can use b4i syntax only.

    Narek
     
  3. henrywood

    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: Apr 16, 2015
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice