In cocos2dx development, we want to obtain the Mac address of the mobile phone. Currently, only java-Layer Code on Android can directly obtain the Mac address, if someone asks me what if I want to use a Mac address in C ++ code, what should I do?
The idea is to first obtain the Mac address string at the java layer and then upload it to the C ++ layer.
The implementation is as follows:
I. java layer:
Public static String getMacID () {String str = "wei"; WifiManager wifi = (WifiManager) mActivity. getSystemService (Context. WIFI_SERVICE); WifiInfo = wifi. getConnectionInfo (); str = info. getMacAddress (); if (str = null) {Log. e ("failed to get android mac address", "0000000");} Log. e ("get android mac address" + str, "00000000"); return str ;}
The code is simple, that is, the string that returns the Mac address. I only need to specify mActivity, which is a declared static Activity object.
Private static Activity mActivity = null;
Assign a value to the create FUNCTION: mActivity = this;
Also, do not forget about permissions: Add wifi access permissions to the AndroidManifest. xml file.
Ii. Let's talk about what the C ++ layer should do.
Std: string FruitGameMenuScene: getMacID () {std: string strMac = ""; # if (CC_TARGET_PLATFORM = CC_PLATFORM_ANDROID) JniMethodInfo methodInfo; // obtain the function body bool isHave = JniHelper: getStaticMethodInfo (methodInfo, "com/ymook/FruitGame", "getMacID", "() Ljava/lang/String; "); if (isHave) {jstring jstr; CCLog (" java layer getMacID function exists; "); jstr = (jstring) methodInfo. env-> CallStaticObjectMethod (methodInfo. classID, methodInfo. methodID); strMac = JniHelper: jstring2string (jstr); CCMessageBox ("Mac", strMac. c_str ();} else {CCLog ("the java-layer getMacID function does not exist;") ;}# endif return strMac ;}
This code is also simple, that is, to call the getMacID function of the java layer and return the desired Mac string. Of course, you can call a static handle before calling a dynamic function using JNI. However, I don't think that method is too troublesome. Here, I only specify:
"com/ymook/FruitGame/FruitGame"
This is the path of the main Activity on the Android platform;
Okay, that's simple. If you have any programming problems, you can join Firefox's technology and Startup Group 98817001.