Get Mac address for cocos2dx Android

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.