Using JNI Java to invoke C + + methods in cocos2d

Source: Internet
Author: User

1. The first is LoadLibrary


The C + + code in COCOS2D is compiled into a. So file, placed under the Libs/armeabi in the Android directory, and then Java will load in, which we don't have to do because cocos2d has done it for us.



Package CB. Cbccble;public class Cbccblecentralmanager {public    static final String TAG = "Cbccblecentralmanager Android";        Public native static void blecentermanagernotificationchangestate (int oldstate, int newstate);    Public native static void Blecentermanagernotificationdidscanoneperipheral (String peripheralid);    Public native static void Blecentermanagernotificationdidfinishscanning ();   }

look at what Java is about, just a few native methods are defined in Java, and these methods can be called in Java. The main view is how C + + is implemented. Here are just a few examples of the above 3.


extern "C" {//test void Java_cb_cbccble_cbccblecentralmanager_blecentermanagernotificationchangestate (JNIEnv* env, J Object Thiz, Jint oldstate, Jint newstate) {cclog ("Java_cb_cbccble_cbccblecentralmanager_blecentermanagernotifi             Cationchangestate ");    Cclog ("oldstate:%d, newstate:%d", (int) oldstate, (int) newstate); } void Java_cb_cbccble_cbccblecentralmanager_blecentermanagernotificationdidscanoneperipheral (JNIEnv* env, jobj ECT Thiz, jstring peripheralid) {cclog ("JAVA_CB_CBCCBLE_CBCCBLECENTRALMANAGER_BLECENTERMANAGERNOTIFICATIONDIDSC        Anoneperipheral ");        std::string peripheralid = jnihelper::jstring2string (peripheralid);    Cclog ("%s", Peripheralid.c_str ()); } void Java_cb_cbccble_cbccblecentralmanager_blecentermanagernotificationdidfinishscanning (JNIEnv* env, Jobject thi    Z) {cclog ("java_cb_cbccble_cbccblecentralmanager_blecentermanagernotificationdidfinishscanning"); }}

Notice that our C + + code is written in the extern "C", the method name is very long, but it is formatted, the beginning of Java, and then the package name + class name + method name, are separated by ' _ '. The pass-through parameter is followed by the following. The JNI data type inside the C + + data type conversion is not much to say, refer to the previous article's wording. using JNI Java to invoke C + + methods in cocos2d

http://www.waitingfy.com/archives/1651


Using JNI Java to invoke C + + methods in cocos2d

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.