How can I programatically read the SD Card\'s CID register, which contains a Serial Number and other information? Can I do it through Android Java, or Native code?
As BMB wrote, you cannot know the real path of the SD card in the /sys filesystem. However, /sys implements aliases as well, and you may be able to retrieve the CID through /sys/block/mmcblk0/device/cid.
For example, on a Tattoo running 2.2, /sys/block/mmcblk0 is a soft link (established automatically by the system) to /sys/devices/platform/msm_sdcc.2/mmc_host/mmc1/mmc1:aaaa/block/mmcblk0.
You can get it with the StorageManager.
Here is a sample-code:
final StorageManager storageManager = (StorageManager) activity.getSystemService(Context.STORAGE_SERVICE);
final List<StorageVolume> storageVolumes = storageManager.getStorageVolumes();
final UserHandle user = android.os.Process.myUserHandle();
for (StorageVolume storageVolume : storageVolumes) {
// "SDCARD" must be your sd-card name
if (storageVolume.getDescription(activity).equals("SDCARD")){
// the SD Card ID number
return storageVolume.getUuid();
}
}
The only code I've found thus far to provide the id is C++ or C#
http://jo0ls-dotnet-stuff.blogspot.com/2008/12/read-secure-digital-sd-card-serial.html
http://jo0ls-dotnet-stuff.blogspot.com/2008/12/read-cid-and-csd-c-implementation.html
If you are a C++ developer you may be able to take this and make it work on Android.
I managed to find my SD card CID by plugging my phone to my computer via usb and using the adb tool (Android SDK)
$ adb shell
# cat /sys/block/mmcblk0/../../cid
My phone is rooted so I'm not sure if this is accessible on non-rooted phones.
Also try
$ adb shell
# cd /sys/block/mmcblk0/../../
# ls
block fwrev preferred_erase_size
cid hwrev scr
csd manfid serial
date name subsystem
driver oemid type
erase_size power uevent
These are explained in the kernel documentation http://www.mjmwired.net/kernel/Documentation/mmc/mmc-dev-attrs.txt
I was looking for this function too but could never get a home brewed solution. I ended up finding a company that makes an SID reader. Go to nexcopy.com and look in their SD or microSD duplicator section. It can read up to 20 cards at a time.
I made this one... it worked for me... hope it makes u clear!
String getSDCARDiD()
{
try {
File file = new File("/sys/block/mmcblk1");
if (file.exists() && file.isDirectory()) {
memBlk = "mmcblk1";
} else {
//System.out.println("not a directory");
memBlk = "mmcblk0";
}
Process cmd = Runtime.getRuntime().exec("cat /sys/block/"+memBlk+"/device/cid");
BufferedReader br = new BufferedReader(new InputStreamReader(cmd.getInputStream()));
sd_cid = br.readLine();
//System.out.println(sd_cid);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sd_cid;
}