get Unique ID from wifi mac address - Android Wifi

Android examples for Wifi:Wifi Address

Description

get Unique ID from wifi mac address

Demo Code


//package com.java2s;

import java.math.BigInteger;

import java.security.MessageDigest;

import android.content.Context;

import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import android.telephony.TelephonyManager;

public class Main {

    public static String getUniqueID(Context ctx) {
        TelephonyManager tm = (TelephonyManager) ctx
                .getSystemService(Context.TELEPHONY_SERVICE);
        String device_id = "";
        try {//from   w  w w. j  ava 2s. co m
            device_id = tm.getDeviceId();
        } catch (Exception e) {
            e.printStackTrace();
            // TODO: handle exception
        }
        if (device_id == null) {
            try {
                WifiManager wifi = (WifiManager) ctx
                        .getSystemService(Context.WIFI_SERVICE);
                WifiInfo info = wifi.getConnectionInfo();
                MessageDigest md = MessageDigest.getInstance("MD5");
                device_id = new BigInteger(1, md.digest(info
                        .getMacAddress().getBytes())).toString(16)
                        .toUpperCase();
            } catch (Exception e) {
                device_id = "";
            }
        }

        return device_id;
    }
}

Related Tutorials