android 工程 IMEI MAC地址的获取
1 创建PhoneInfo.java类
package org.cocos2dx.javascript; import android.app.Activity; import android.content.Context; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.provider.Settings; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Log; //import com.wifi.data.open.WKData; //import com.wifi.openapi.WKConfig; //import com.wifi.openapi.common.WKPermissionConfig; import org.cocos2dx.javascript.AppActivity; import java.lang.reflect.Method; import java.net.NetworkInterface; import java.net.SocketException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Enumeration; public class PhoneInfo { // private static String APP_ID = "wifi_ocpc_wzq"; private static String APP_ID = "TD0674"; private static String AES_KEY = "YHAZRF7q7LAxrSkd"; private static String AES_IV = "lAessrGnjnLKPplv"; private static String MD5_KEY = "vuBDjRkY63ZxeVa6RsJx3qfY8xrY4az7"; private static String CHANNEL = "wifi_ocpc_wzq"; // public static void InitSdk(Activity app) { // System.out.println("初始化"); // WKData.setDebugMode(false); // // WKConfig.build(app.getApplication(),APP_ID, AES_KEY, AES_IV, MD5_KEY, CHANNEL) // .setOverSea(false)//默认false 海外版设置为true // .useCrashSdk(true)//是否使用catch捕获功能默认为true // .init(); // WKPermissionConfig.allowGrantIMEI(true);//允许获取imei // WKPermissionConfig.allowGrantWifiState(true);//允许获取wifi信息 // WKPermissionConfig.allowGrantLocation(true);//允许获取位置信息 // } public static String getMAC(Context context) { System.out.println("mac:获取mac"); String mac = ""; mac = getMacAddress(); System.out.println("mac:" + mac); if (mac.equals("")) { mac = getLocalMac(context); System.out.println("mac:" + mac); } System.out.println("macmd5:" + mac); System.out.println("md5(mac):" + md5(mac)); return md5(mac); } public static String getIMEI(Context context) { System.out.println("imei:获取imei"); String androidID = getAndroidID(context); String imei = getPhoneIMEI(context,0); System.out.println("androidID:" + androidID); System.out.println("imei:" + imei); System.out.println("md5(imei):" + md5(imei)); return md5(imei); } public static String getAndroidID(Context context) { String androidID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); return androidID; } /** * 这个方法一定可以获取到 华为不能用 * * @param slotId slotId为卡槽Id,它的值为 0、1; * @return */ public static String getPhoneIMEI(Context context, int slotId) { try { TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); Method method = manager.getClass().getMethod("getImei", int.class); String imei = (String) method.invoke(manager, slotId); return imei; } catch (Exception e) { return ""; } } public static String getLocalMac(Context mContext) { WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); WifiInfo connectInfo = wifiManager.getConnectionInfo(); if (connectInfo != null) { String mac = connectInfo.getMacAddress(); Log.d("mac", "mac:" + mac); if (mac != null && TextUtils.equals(mac, "02:00:00:00:00:00")) { String temp = getMacAddress(); Log.d("mac", "temp:" + temp); if (!TextUtils.isEmpty(temp)) { mac = temp; } } return mac; } return ""; } public static String getMacAddress() { String macAddress = ""; try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iF = interfaces.nextElement(); byte[] addr = iF.getHardwareAddress(); if (addr == null || addr.length == 0) { continue; } StringBuilder buf = new StringBuilder(); for (byte b : addr) { buf.append(String.format("%02X:", b)); } if (buf.length() > 0) { buf.deleteCharAt(buf.length() - 1); } String mac = buf.toString(); Log.d("mac", "interfaceName=" + iF.getName() + ", mac=" + mac); if (TextUtils.equals(iF.getName(), "wlan0")) { return mac; } } } catch (SocketException e) { e.printStackTrace(); return macAddress; } return macAddress; } public static String md5(String string) { if (string == ""||string == null) { return ""; } MessageDigest md5 = null; try { md5 = MessageDigest.getInstance("MD5"); byte[] bytes = md5.digest(string.getBytes()); String result = ""; for (byte b : bytes) { String temp = Integer.toHexString(b & 0xff); if (temp.length() == 1) { temp = "0" + temp; } result += temp; } return result.toUpperCase(); } catch (NoSuchAlgorithmException e) { System.out.println(e); } return ""; } }
//IMEI AndroidManifest.xml中添加权限
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
//mac AndroidManifest.xml中添加权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission
android:name="android.permission.INTERNET"/> <uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"/>
2 AppActivity调用
public static String GstIMEI() { Log.e("phoneInfo getIMEI","getMAC"); //return PhoneInfo.getIMEI(Instance); return PhoneInfo.getIMEI(Instance); } public static String GstMAC() { Log.e("phoneInfo GstMAC","GstMAC 获取返回值mac"); return PhoneInfo.getMAC(Instance); }
3 cocos jsb调用
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", “GstIMEI”, "()V");