Wednesday, March 2, 2011

Getting unique ids of Android device

Pretty simple and generally painless. If device is a phone you can get
  • IMEI number
  • Phone number
  • Subscriber id
  • SIM card's serial id
If its not a phone, the only other sensible thing to do is to get the MAC address of the WiFi. And here is the code:

  String infos = "";
  TelephonyManager tManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
  infos+= "Device ID"+tManager.getDeviceId()+"\n";
  infos+= "Phone Number"+tManager.getLine1Number()+"\n";
  infos+= "SIM Serial Number"+tManager.getSimSerialNumber()+"\n";
  infos+= "Subscriber ID"+tManager.getSubscriberId()+"\n";
  
  WifiManager wifiMan = (WifiManager) this.getSystemService(
                Context.WIFI_SERVICE);
  WifiInfo wifiInf = wifiMan.getConnectionInfo();
  infos+= "WiFi MAC: "+ wifiInf.getMacAddress()+"\n";
  
  
  Toast t=Toast.makeText(this, infos, Toast.LENGTH_LONG);
  t.show();

And of course the following permissions have to be added to the manifest: