Using the native interface

186 views
Skip to first unread message

jam...@fastlaneinnovations.com

unread,
Nov 1, 2014, 6:15:34 PM11/1/14
to codenameone...@googlegroups.com
I'm having trouble understanding how to fully use the native interface. I have the interface defined with some methods, and am implementing code in the native stubs to carry out the methods, but I need information to pass both ways. I'm working on the Bluetooth LE functionality, and it uses some callbacks to respond to data that gets received from a peripheral device.

It's unknown when this will occur, so the callback is called by the Bluetooth adapter when this happens and the data is then stored in a class field of the native class. How do I get the main CN1 program to know when the callback inside the native code has been triggered? I can't pass a reference to the native class from other CN1 non-native objects (can I?), do I somehow assign some listener to some invisible text field or something like that?

In general, I can see that it's easy for the CN1 code to call the native interfaces that I set up, but how do I go the other way and have the native code cause something to happen in the CN1 code?

David Wafula

unread,
Nov 1, 2014, 6:25:46 PM11/1/14
to CodenameOne Discussions
If your native source is Android, declare a static object  in a  CN1 class and use it as hook to call CN1 code directly from your native side.

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discu...@googlegroups.com.
Visit this group at http://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/1ffdbbaa-5944-4ff2-8f9d-04d69ef3f711%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
David Wafula

jam...@fastlaneinnovations.com

unread,
Nov 1, 2014, 7:17:57 PM11/1/14
to codenameone...@googlegroups.com
Ok, that should be easy for the Android case. What should I do for iOS though?

Shai Almog

unread,
Nov 1, 2014, 8:38:16 PM11/1/14
to

Tim Gaul

unread,
Nov 2, 2014, 4:33:57 AM11/2/14
to codenameone...@googlegroups.com
David, do you have a "best way" to watch these static objects for changes, in Java?


On Sunday, November 2, 2014 12:25:46 AM UTC+2, davidwaf wrote:
If your native source is Android, declare a static object  in a  CN1 class and use it as hook to call CN1 code directly from your native side.
On Sun, Nov 2, 2014 at 12:15 AM, <jam...@fastlaneinnovations.com> wrote:
I'm having trouble understanding how to fully use the native interface. I have the interface defined with some methods, and am implementing code in the native stubs to carry out the methods, but I need information to pass both ways. I'm working on the Bluetooth LE functionality, and it uses some callbacks to respond to data that gets received from a peripheral device.

It's unknown when this will occur, so the callback is called by the Bluetooth adapter when this happens and the data is then stored in a class field of the native class. How do I get the main CN1 program to know when the callback inside the native code has been triggered? I can't pass a reference to the native class from other CN1 non-native objects (can I?), do I somehow assign some listener to some invisible text field or something like that?

In general, I can see that it's easy for the CN1 code to call the native interfaces that I set up, but how do I go the other way and have the native code cause something to happen in the CN1 code?

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.



--
David Wafula

David Wafula

unread,
Nov 2, 2014, 9:53:46 AM11/2/14
to CodenameOne Discussions
Nothing very special as far as i know. Usually i have a dedicated class in CN1 that exposes the functionally i need to access from native. Then say if my main class in CN1 is MyApp,  i would  instantiate the dedicated class in the init() of MyApp, and make it public static.

So from native, I would simply do:

MyApp.theobjectExposingCN1Functionality.theMethod()

So, in effect, CN1 is not really watching/listening for changes in native, rather native is simply calling CN1 as per need - sort of pushing info into CN1.
 
There could  other ways of doing this of course.


To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discu...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
David Wafula

Steve Hannah

unread,
Nov 2, 2014, 11:18:10 AM11/2/14
to codenameone...@googlegroups.com
If you haven't already, you should check out the CN1Sockets library as a reference.  I needed to support callbacks to handle reading from sockets.  I used the strategy mentioned here:  Use a static method, and call it from native code.

Steve


For more options, visit https://groups.google.com/d/optout.



--
Steve Hannah
Web Lite Solutions Corp.

jam...@fastlaneinnovations.com

unread,
Nov 2, 2014, 12:52:11 PM11/2/14
to codenameone...@googlegroups.com
OK, I'm starting to see how it works in your Sockets lib. You can call the static methods from each native language, I wasn't sure about being able to do that with non-Java, but I see how you've done it in the iOS code.

So I think for handling the BLE callback, when data is received and the callback is invoked in native code, I'll have it invoke a static CN1 method, passing the data as an argument, and then I can have the CN1 method also revalidate the GUI. I just have to see if the iOS BLE implementation uses a similar approach.

Since we're talking about native code and CN1LIBs, to create one it looks like I just make a class that has methods that match up to the Native Interface I define, then I just implement the native code by adding to the stubs. I do this all in NetBeans, and I recall reading in the blog or something that I need to do a build with special targets and other things. Is there anywhere I can find a list of the things I need to do to build the CN1LIB?
Reply all
Reply to author
Forward
0 new messages