Registering a java method as a callback function

334 views
Skip to first unread message

Elvis Dowson

unread,
Jul 2, 2009, 8:15:29 AM7/2/09
to android-ndk
Hi,
Does anyone have a code snippet for registering a java method as
a callback function? I have a c library that, takes as input to one of
its methods, a parameter, which is callback function. This callback
function gets called whenever some data arrives at the serial port.

Now, if I use NDK to wrap this c serial library, what should I do to
register a java method as a call back function for this c library
method, so that it calls the java method, everytime data is available
on the serial port?

Elvis

David Turner

unread,
Jul 2, 2009, 8:30:35 AM7/2/09
to andro...@googlegroups.com

You probably can't do that very easily. It's probably a *lot* easier to send
data to a Unix pipe that a Java thread could read directly instead.

Use a native method to create  the pipe and return the read end to the
Java side, and that should be it.
 

Elvis


fadden

unread,
Jul 2, 2009, 1:27:32 PM7/2/09
to android-ndk
On Jul 2, 5:15 am, Elvis Dowson <elvis.dow...@gmail.com> wrote:
> Now, if I use NDK to wrap this c serial library, what should I do to
> register a java method as a call back function for this c library
> method, so that it calls the java method, everytime data is available
> on the serial port?

Why not just pass the library a C function that calls a Java method?

Brian Cloutier

unread,
Jul 2, 2009, 1:56:59 PM7/2/09
to andro...@googlegroups.com

Lewis Z.

unread,
Jul 2, 2009, 2:44:03 PM7/2/09
to android-ndk
A simple callback is easy to do. Sheng Liang's book The Java Native
Interface shows how to do it. But if you need a sophisticated callback
to pass data to your UI, you may want to add an Android Handler to
your callback.
Reply all
Reply to author
Forward
0 new messages