您的位置:首页 > 移动开发 > Cocos引擎

cocos2dx 使用jni方法获取android mac地址

2013-08-07 14:05 232 查看
网上搜索了一下获取android mac地址的方法。这里使用了andriod API的方法 WifiManager

AndroidManifest.xml 添加:

Xcode 代码:

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID )
#include <jni.h>
#include <../libs/cocos2dx/platform/android/jni/JniHelper.h>
#include <android/log.h>

#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

#include <sys/socket.h> // Per msqr
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>

#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;

}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: