How to read the SD Card ID number?

47,539

Solution 1

Here is sample code for getting SID and CID

if (isExteranlStorageAvailable()) {
    try {
        File input = new File("/sys/class/mmc_host/mmc1");
        String cid_directory = null;
        int i = 0;
        File[] sid = input.listFiles();

        for (i = 0; i < sid.length; i++) {
            if (sid[i].toString().contains("mmc1:")) {
                cid_directory = sid[i].toString();
                String SID = (String) sid[i].toString().subSequence(
                        cid_directory.length() - 4,
                        cid_directory.length());
                Log.d(TAG, " SID of MMC = " + SID);
                break;
            }
        }
        BufferedReader CID = new BufferedReader(new FileReader(
                cid_directory + "/cid"));
        String sd_cid = CID.readLine();
        Log.d(TAG, "CID of the MMC = " + sd_cid);

    } catch (Exception e) {
        Log.e("CID_APP", "Can not read SD-card cid");
    }

} else {
    Toast.makeText(this, "External Storage Not available!!",
            Toast.LENGTH_SHORT).show();
}

Solution 2

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

Solution 3

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.

Solution 4

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;
    }

Solution 5

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.

Share:
47,539

Related videos on Youtube

Eric Hamilton
Author by

Eric Hamilton

Updated on July 09, 2022

Comments

  • Eric Hamilton
    Eric Hamilton almost 2 years

    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?

    Thanks in advance, Eric

  • Jan Hudec
    Jan Hudec over 12 years
    The path is different on different on each device.
  • Jan Hudec
    Jan Hudec over 12 years
    It does not necessarily have to be mmcblk0, since there may be more than mmcblk device in some devices. If you are looking for particular CID, you should try looking in mmcblk*n* and if you are looking for CID of card where some file is, you should stat the file, and access the device via /sys/dev/block/m:n/.. where m:n is the major and minor device number you got from stat. So e.g. /sys/dev/block/179:1/... The .. is to get from the partition to the card.
  • BMB
    BMB over 12 years
    Yes, that is why Samuel Tardieu's answer above is better, though not perfect.
  • babak
    babak over 12 years
    THanks for giving nice example for fetching data from any of the device without any other constraint.....
  • Jan Hudec
    Jan Hudec about 12 years
    Does not look either relevant or useful. It returns 32-bit integer, but CID is longer than that. Four times.
  • Jan Hudec
    Jan Hudec about 12 years
    They are perfectly capable of reading it. The driver exports in under /sys pseudo-filesystem. It is however somewhat tricky to find the right device there.
  • IT-Dan
    IT-Dan almost 11 years
    Although it doesn't give you the CID, it is useful for uniquely identifying the current "external storage".
  • LarsH
    LarsH over 8 years
    @JanHudec: Is it possible to stat the file from within an Android app to get the major/minor device numbers? I don't see stat() or anything similar in the Android APIs.
  • LarsH
    LarsH over 8 years
    Can you explain what this is doing? Is /sys/class/mmc_host/mmc1 something we can rely on across manufacturers? What will this code do if there is more than one microSD card, e.g. one embedded and one removable?
  • LarsH
    LarsH over 8 years
    This doesn't answer the whole question, but it does give you the card's serial number, which is relevant and useful. I came here thinking I needed the CID, and learned that what I was really looking for was the serial number, which is only a part of the CID, and is easier to obtain. So thanks for this answer. Actually Environment.getExternalStorageDirectory() is not a reliable way to find the path of the microSD card, but that's a side issue.
  • Jan Hudec
    Jan Hudec over 8 years
    @LarsH, probably not. I use native code anyway, so I just call it there.
  • LarsH
    LarsH over 8 years
    On the other hand, since FileUtils and getFatVolumeId() are undocumented, there's no assurance that they will be present on any particular Android version or device. And indeed it's not there on mine (see e.g. stackoverflow.com/a/20859031/423105).
  • Dinesh Prajapati
    Dinesh Prajapati over 8 years
    just read the code you can understand what code is doing. we can not reply on this completely, it was for Android 2.x versions not it might have changed. so according to that we need to modify the code.
  • LarsH
    LarsH over 8 years
    I can see what the code is actually doing, but what I meant was, what is its intent? E.g. what is "SID" supposed to signify? It's not a standard term for SD cards, as far is I can tell. It's not the serial number, nor the CID register, nor the OEM ID. I tried this approach via adb shell on a new device, and it works, except that I don't know whether mmc1, mmc2, or mmc0 reflect the removable SD card.
  • LarsH
    LarsH over 8 years
  • LarsH
    LarsH over 3 years
    This gets the volume UUID, not the SD card ID (serial number) nor the CID.
  • Waqar UlHaq
    Waqar UlHaq almost 3 years
    This is what I was actually looking for... But this method will work on on Android Version >= 24, since StorageVolume are introduced in Android 24

Related