Android: 用jni 获取MAC地址

java中实现获取IMEI的DeviceID和MacID的代码:

/*TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);	//获取设备id
        String deviceId = tm.getDeviceId();
        final CharSequence cs = deviceId;
        Log.e(TAG, "Key==deviceId==" + cs);

        WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);	//获取MacID
		WifiInfo info = wifi.getConnectionInfo();
        String macID = info.getMacAddress();		
        final CharSequence ms = macID;
        Log.e(TAG, "Key==macID==" + ms);
        */

另外注意AndroidManifest.xml中需要打开权限:




在jni中实现获取IMEI的DeviceID和MacID的代码:

java的activity类中有

String deviceIdRutrun=ct.nativeGetDeviceId(FdActivity.this);
        Log.e(TAG, "Key==deviceIdRutrun==" + deviceIdRutrun);
调用jni的java类中有

public static native String nativeGetDeviceId(Object mContext);
cpp文件中有


JNIEXPORT jstring JNICALL Java_org_samples_CT_nativeGetDeviceId
  (JNIEnv *env, jclass, jobject mContext){
		//t = (double)getTickCount();

	    if(mContext == 0){
	        return env->NewStringUTF("[+] Error: Context is 0");
	    }
	    jclass cls_context = env->FindClass("android/content/Context");
	    if(cls_context == 0){
	        return env->NewStringUTF("[+] Error: FindClass  Error");
	    }

	    jmethodID getSystemService = (env)->GetMethodID(cls_context, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
	    if(getSystemService == 0){
	        return env->NewStringUTF("[+] Error: GetMethodID failed");
	    }

	    jfieldID TELEPHONY_SERVICE = (env)->GetStaticFieldID(cls_context, "TELEPHONY_SERVICE", "Ljava/lang/String;");
	    if(TELEPHONY_SERVICE == 0){
	        return env->NewStringUTF("[+] Error: GetStaticFieldID Failed");
	    }

	    jstring telephonystr = (jstring)(env)->GetStaticObjectField(cls_context, TELEPHONY_SERVICE);
	    jobject telephonymanager = ((env)->CallObjectMethod(mContext, getSystemService, telephonystr));
	    if(telephonymanager == 0){
	        return env->NewStringUTF("[+] Error: CallObjectMethod failed");
	    }

	    jclass cls_TelephoneManager = (env)->FindClass( "android/telephony/TelephonyManager");
	    if(cls_TelephoneManager == 0){
	        return env->NewStringUTF("[+] Error: FindClass TelephoneManager failed");
	    }

	    jmethodID getDeviceId = ((env)->GetMethodID(cls_TelephoneManager, "getDeviceId", "()Ljava/lang/String;"));
	    if(getDeviceId == 0){
	        return env->NewStringUTF("[+] Error: GetMethodID getDeviceID failed");
	    }

	    jobject DeviceID = (env)->CallObjectMethod(telephonymanager,getDeviceId);

	    //macID

	   	    jfieldID WIFI_SERVICE = (env)->GetStaticFieldID(cls_context, "WIFI_SERVICE", "Ljava/lang/String;");
	   	    if(WIFI_SERVICE == 0){
	   	        return env->NewStringUTF("[+] Error: GetStaticFieldID Failed");
	   	    }

	   	    jstring wifistr = (jstring)(env)->GetStaticObjectField(cls_context, WIFI_SERVICE);
	   	    jobject wifimanager = ((env)->CallObjectMethod(mContext, getSystemService, wifistr));
	   	    if(wifimanager == 0){
	   	        return env->NewStringUTF("[+] Error: CallObjectMethod failed");
	   	    }

	   	    jclass cls_WifiManager = (env)->FindClass( "android/net/wifi/WifiManager");
	   	    if(cls_WifiManager == 0){
	   	        return env->NewStringUTF("[+] Error: FindClass cls_WifiManager failed");
	   	    }

	   	    jmethodID getWifiId = ((env)->GetMethodID(cls_WifiManager, "getConnectionInfo", "()Landroid/net/wifi/WifiInfo;"));
	   	    if(getDeviceId == 0){
	   	        return env->NewStringUTF("[+] Error: GetMethodID getWifiId failed");
	   	    }

	   	    jobject wifiInfo = env->CallObjectMethod(wifimanager,getWifiId);

	   	    jclass wifiInfoclass = env->GetObjectClass(wifiInfo);

	   	    jmethodID getMacId = env->GetMethodID(wifiInfoclass,"getMacAddress","()Ljava/lang/String;");

	   	 jobject MacID = env->CallObjectMethod(wifiInfo,getMacId);



	   	 /*char DP_IMIE[128]={0};
	   	 DP_IMIE = (env)->GetStringUTFChars( DeviceID, 0);

*/
	   	/*char*  DP_IMIE=  jstringTostring(env,(jstring)DeviceID);
	   	LOGI("nativeGetDeviceId_DeviceID==%s",DP_IMIE);
	   	char*  DP_IMIE2=  jstringTostring(env,(jstring)MacID);
	   		   	LOGI("nativeGetDeviceId_MacID==%s",DP_IMIE2);
*/

		//t = ((double)getTickCount() - t)/getTickFrequency();
		//LOGI("nativeGetDeviceId_time==%f",t);

	    return (jstring)DeviceID;

}
测试nativeGetDeviceId的运行时间为0.03s,由于只需要执行一次,所以问题不算大。代码比较乱,可以按需更改。另外,cpp中添加内部方法相当好用,可以更好重用这种jni的调用。


参考文献:

Android: 用jni 获取MAC地址http://www.cnblogs.com/hibraincol/archive/2012/07/04/2575992.html
NativeCode中通过JNI反射调用Java层的代码,以获取IMEI为例http://www.cnblogs.com/rainduck/p/3644124.html


你可能感兴趣的:(android,VC)