首页 技术 正文
技术 2022年11月11日
0 收藏 404 点赞 2,709 浏览 1569 个字

1.首先是LoadLibrary

cocos2d中的C++代码会编译成一个.so文件。放在安卓文件夹下的libs/armeabi 下,然后java会load进来,这步我们不用做了,由于cocos2d已经帮我们做好了。

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();}

先看下java的是怎样些的。java中仅仅是定义了几个native的方法,然后java中调用这些方法就可以。主要看下C++是怎样实现的。

这里就举例上面的3个样例好了。

extern "C"
{
//test
void Java_cb_CbCCBLE_CbCCBLECentralManager_bleCenterManagerNotificationChangeState(JNIEnv* env, jobject thiz, jint oldState, jint newState)
{
CCLOG("Java_cb_CbCCBLE_CbCCBLECentralManager_bleCenterManagerNotificationChangeState"); CCLOG("oldState:%d, newState:%d", (int)oldState, (int)newState);
} void Java_cb_CbCCBLE_CbCCBLECentralManager_bleCenterManagerNotificationDidScanOnePeripheral(JNIEnv* env, jobject thiz, jstring peripheralId)
{
CCLOG("Java_cb_CbCCBLE_CbCCBLECentralManager_bleCenterManagerNotificationDidScanOnePeripheral");
std::string peripheralId = JniHelper::jstring2string(peripheralId);
CCLOG("%s", peripheralId.c_str());
} void Java_cb_CbCCBLE_CbCCBLECentralManager_bleCenterManagerNotificationDidFinishScanning(JNIEnv* env, jobject thiz)
{
CCLOG("Java_cb_CbCCBLE_CbCCBLECentralManager_bleCenterManagerNotificationDidFinishScanning");
}}

注意到我们c++的代码都是写在extern "C"中,方法名字特别长,可是是有格式的。Java开头,然后是包名字+类名字+方法名字,都是用’_’隔开。传过来的參数就是跟在后面就可以。里面jni数据类型到C++数据类型转换就不多讲了,參考前面一篇文章的写法。cocos2d 中使用jni Java 调用 C++ 方法

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297