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

网上搜索了一下获取android mac地址的方法。这里使用了andriod API的方法 WifiManager

AndroidManifest.xml  添加:

"android.permission.ACCESS_WIFI_STATE" >

//根据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;
    	
    }




"android.permission.ACCESS_WIFI_STATE" >

你可能感兴趣的:(cocos2dx,cocos2dx,Android,JNI)