NvServices
NvServices

Reputation: 227

CodenameOne - ObjectiveC Bridge - how to read an NSString from a native SDK

I am trying to use the ObjectiveC Bridge library from within CodenameOne as it sounds like a very useful tool. The native iOS SDK is am trying to use is the Honeywell Captuvo SDK. However when I try to call the getCaptuvoName function which is supposed to return the name of the device as an NSString all I get is "Not available"? The code I am using:

    if (Objc.isSupported())
    {
        Pointer captuvoShared =eval("Captuvo.sharedCaptuvoDevice",0).asPointer();
        if (captuvoShared!=null)
        {
            Dialog.show("ObjC", "Captuvo not null", "OK", null);
            String name=Objc.getProperty(captuvoShared,"getCaptuvoName").asString();
            Dialog.show("ObjC", name, "OK", null);
        }
        else
            Dialog.show("ObjC", "Captuvo NULL!", "OK", null);
    }
    else
        Dialog.show("ObjC", "Objective-C not supported on this platform", "OK", null);

Upvotes: 1

Views: 54

Answers (2)

steve hannah
steve hannah

Reputation: 4716

Objc.getProperty() takes the property name, which is slightly different than the message name. E.g. If you have messages getName and setName, then the property name would be "name". E.g. The following would be equivalent:

Objc.eval(myObj, "getName") and Objc.getProperty(myObj, "name")

In your case, you are trying to call the getCaptuvoName message. So you could do either Objc.eval(instance, "getCaptuvoName") or Objc.getProperty(instance, "captuvoName") but not Objc.getProperty(instance, "getCaptuvoName").

One other observation on your code.

Pointer captuvoShared =eval("Captuvo.sharedCaptuvoDevice",0).asPointer();

The sharedCaptuvoDevice message takes no parameters, so you shouldn't pass the "0" parameter. It should be:

Pointer captuvoShared =eval("Captuvo.sharedCaptuvoDevice").asPointer();

A rule of thumb: The number of parameters you supply to a message should be the same as the number of colons in the message name.

Upvotes: 1

Shai Almog
Shai Almog

Reputation: 52770

Not an expert on this so I may be wrong...

As reference for people reading it this is the API you are using: https://github.com/DataSplice/HoneywellScanner/blob/master/src/ios/Honeywell/Captuvo.h

-(NSString*)getCaptuvoName;

So getCaptuvoName is a "message" that returns a NSString. So the first thing you need to do is invoke the message:

Pointer p = Objc.eval(instance, "getCaptuvoName").asPointer();

Now you need to convert the pointer to a Java String:

String name = p.asString();

Notice you will need the instance of the CaptuvoEventsProtocol to perform the eval.

Upvotes: 1

Related Questions