Accessing native methods via the harness interface

Does anyone have any code samples which shows how to access the native IOS or Android methods from JME?

Such as the ‘reshape’ method in IOS harness. Or to access native GUI elements.

I know there is some information in the documentation but it doesn’t go into enough detail for my feeble brain and any example would be perfect.

Any help would be much appreciated.

For android it’s easy :
In JME project create some interface

public interface MobileNativeInterface{
       public void doNativeStuff();
}

then still on JME side, in your main application (the one extending SimpleApplication)

public class MyApp extends simpleApplication{
...

  MobileNativeInterface mobile;
 
  public void setMobileNativeInterface(MobileNativeInterface mobile){
       this.mobile = mobile;
  }

....

  public void doNativeStuff(){
       mobile.doNativeStuff();
  }

....
}

then still on JME side at some point call your native stuff, from where ever you want as long as you have a reference to the app (pass it, or use an AppState)

app.doNativeStuff();

then on the android side, in the MainActivity in the onCreate() method

   public void onCreate(Bundle savedInstanceState){
          super.onCreate(savedInstanceState); //don't forget this
          //give the mobile interface to the app (app is a MainActivity attribute holding the reference to the JME app)
          app.setMobileNativeInterface(new MobileNativeInterface(){
                    
                  public void doNativeStuff(){
                           //here you have to be careful, remember the android activity and the JME app 
                           //run on 2 different threads so you have to somehow synchronize them 
                           //else you may get into some threading issues, especially if you want to update 
                           //something on the android ui thread
                           runOnUiThread(new Runnable() {
                              public void run() {
                                     actuallyDoYourNativeStuff();
                              }
                           });
                           
                  }
          });
   }

Note that I inlined the MobileNativeInterface for the sake of the example, you can make a separate class or even hove your MainActivity implements the interface and provide the doNativeStuff method.
That’s all for android.

However for Ios it might be trickier. I never tried Ios deployment so I can’t really answer. The idea is probably the same as it’s simple delegate pattern, but since all java code is compiled into native code, I’m not sure how you would give the MobileNativeInteface implementation to the JME app.

Maybe @normen knows better about this.

1 Like

The iOS app stub has examples on how to use native code (see ios/src/ folder)

1 Like

Hi

How do I call methods in the XCODE project? I cant find any examples anywhere??

Its right there in the code, if you don’t see it idk how to help you…

Thanks Normen. I’m new to objective C so this may not be the correct way to do it but it works.

I added a native method based on the ShowDialog example and I then create an instance of JmeAppDelegate and then I can call methods within that. That was so I can interact with native GUI controls and the root view controller.

JNIEXPORT void JNICALL
Java_JmeAppHarness_showIOSLoginScreen(JNIEnv* e, jobject c) {
jmeAppDelegate *myInstance = [[jmeAppDelegate alloc]init];
[myInstance showIOSLoginScreen];
}

This calls the native method inside the XCODE project and in turn has access to all fields and methods in there for native GUI interaction.

-(void)showIOSLoginScreen {

MainMenu.frame = CGRectMake(_glview.frame.size.width / 2 - 300,_glview.frame.size.height / 4-100,600,500);
MainMenu.backgroundColor = [UIColor grayColor];

…etc
…etc
…etc
…etc