cocos2dx 使用jni方法获取android mac地址
16lz
2021-12-04
网上搜索了一下获取android mac地址的方法。这里使用了andriod API的方法 WifiManager
AndroidManifest.xml 添加:
//根据Wifi信息获取本地Mac public static String getLocalMacAddressFromWifiInfo(Context context){ WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo info = wifi.getConnectionInfo(); return info.getMacAddress(); } |
Xcode 代码:
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID )#include #include <../libs/cocos2dx/platform/android/jni/JniHelper.h>#include #elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)#include // Per msqr#include #include #include #endif
CCString * HelloWorld:: getMacID(){ #if( CC_TARGET_PLATFORM ==CC_PLATFORM_ANDROID) JniMethodInfo methodInfo; //用于获取函数体 bool flag = JniHelper::getStaticMethodInfo( methodInfo, "com/cwa/jniTestAndroid/jniTestAndroid", "getAvtivity" , "()Ljava/lang/Object;" ); jobject jobj; int num=10; if(flag) { jobj = methodInfo.env->CallStaticObjectMethod(methodInfo.classID,methodInfo.methodID ); }else { CCLog("获取方法Activity失败"); } flag = JniHelper::getMethodInfo( methodInfo, "com/cwa/jniTestAndroid/jniTestAndroid", "getAndroidMacID", "()Ljava/lang/String;"); jstring jstr; if(flag) { jstr = (jstring) methodInfo.env->CallObjectMethod(jobj,methodInfo.methodID ); std::string str = JniHelper::jstring2string(jstr); CCLog(" jni 调用结束 str=%s",str.c_str()); return CCString::create(str.c_str()); }else { CCLog(" 获取方法getAndroidMacID失败"); } return NULL; #endif}
android代码:
public static Activity instance;protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);instance = this;}
public static Object getAvtivity() { return instance; } public String getAndroidMacID() { String str = null; WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo info = wifi.getConnectionInfo(); str = info.getMacAddress(); if(str==null) { Log.e("获取android mac地址失败", "0000000"); } Log.e("获取android mac地址 "+str, "00000000"); return str; }
|
更多相关文章
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用
- python list.sort()根据多个关键字排序的方法实现
- 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
- android 笔记 --- Bitmap与Drawable转换
- Canvas
- Android(安卓)如何让EditText不自动获取焦点
- 清理android桌面
- 介绍GLSurfaceView