Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create callbacks between android code and native code?

I have a requirement for creating call backs between native code ( c language code) and Android code . I wrote JNI functions for calling C code from the android like this

JNI code here

#include <android/log.h>

void initSocket(); 

#ifdef __cplusplus
extern "C" {
#endif

  JNIEXPORT void JNICALL Java_org_pjsip_pjsua_pjsua_1appJNI_initSocket(JNIEnv *jenv, jclass jcls) {

    __android_log_write(ANDROID_LOG_INFO, " JNI CODE ", " APP INIT SOCKET");        
    initSocket();        
  }
}

C code looks like this

void initSocket()
{
    /// some more stuff
    printf("  initSocket function ");           
}

static int worker_thread(void *unused)
{       
    /// some more stuff
    return 0;
}

pj_bool_t on_rx_data1(pj_stun_sock *stun_sock, void *pkt, unsigned pkt_len, const pj_sockaddr_t *src_addr, unsigned addr_len)
{      
    /// some more stuff    
    return PJ_TRUE;
}

pj_bool_t on_data_sent1 (pj_stun_sock *stun_sock, pj_ioqueue_op_key_t *send_key, pj_ssize_t sent)
{        
    /// some more stuff
    return PJ_TRUE;  
}
pj_bool_t on_status1(pj_stun_sock *stun_sock, pj_stun_sock_op op, pj_status_t status)
{        
    /// some more stuff
    returnsockaddress();            
    return PJ_TRUE;
}

char* returnsockaddress()
{     
    /// some more stuff   
    return ipinttostring(sock_address);
}

char* ipinttostring(unsigned int addr )
{       
    /// some more stuff
    return fullIP;
}

this is the code i am using in C language, calling initSocket() function from JNI. Now i want to create a callback from this C code when on_status1 function called in this code. this on_status1 will repeat in few seconds when ever it's called i want to call a function in android code.

EDIT

I tried like this, but not succeeded

JNIEXPORT void JNICALL Java_org_pjsip_pjsua_pjsua_1appJNI_initSocket(JNIEnv *jenv, jobject obj) {

      __android_log_write(ANDROID_LOG_INFO, " JNI CODE ", " APP INIT SOCKET");            
      initSocket();             
      jclass cls = jenv->GetObjectClass(obj);
      jmethodID methodid = env->GetMethodID(cls, "callback", "()V");            
      if(!methodid) {
          return;
      }            
      jenv->CallVoidMethod(obj , methodid);            
  }

I was declared function like this in android code.

public static void callback(String value) {
    Log.e(TAG, "value:" + value);
}
like image 234
RajaReddy PolamReddy Avatar asked Nov 14 '12 10:11

RajaReddy PolamReddy


People also ask

How does callback work on Android?

Callbacks are all over the place in Android Development. That's simply because they do a job, and they do it well! By definition: A callback is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.

How does an Android APK execute compiled native code using the Java native interface?

Using Android Studio 2.2 and higher, you can use the NDK to compile C and C++ code into a native library and package it into your APK using Gradle, the IDE's integrated build system. Your Java code can then call functions in your native library through the Java Native Interface (JNI) framework.

What is callback interface in Android?

What's a Callback? Callbacks is a mechanism in Object Oriented Programming that allows an application to handle subscribed events, arising at runtime, through a listener interface. The subscribers will need to provide a concrete implementation of the interface abstract methods.


1 Answers

Try this :

JNIEXPORT void JNICALL Java_org_pjsip_pjsua_pjsua_1appJNI_initSocket(JNIEnv *jenv, jobject obj) {
  __android_log_write(ANDROID_LOG_INFO, " JNI CODE ", " APP INIT SOCKET");            
  initSocket();             
  // jclass cls = (*jenv)->GetObjectClass(jenv, obj);
  // or something like this :
  jclass cls = (*jenv)->FindClass(jenv, "org/pjsip/pjsua/pjsua_appJNI"); 
  jmethodID methodid = (*jenv)->GetStaticMethodID(jenv, cls, "callback", "(Ljava/lang/String;)V");            
  if(!methodid) {
      return;
  }       
  jstring jstr = (*jenv)->NewStringUTF(jenv, "Hello from C"); 
  (*jenv)->CallStaticVoidMethod(jenv, cls, methodid, jstr); 
  }
like image 102
ra. Avatar answered Oct 07 '22 05:10

ra.