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