Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retrieve an SD card's serial number in Android 7.0+?

Tags:

N.B.: This question about the serial number of the physical SD card, not the UUID of the mounted volume. These are two independent pieces of data.

In some versions of Android, and other variants of Linux, it's possible to get the serial number of a mounted SD card, e.g. by reading the contents of /sys/class/mmc_host/mmc0/mmc0:0001/serial or /sys/block/mmcblk0/device/serial (specific numbers may vary). In my testing this has worked pretty reliably, as long as the SD card is inserted in a built-in SD card slot (not mounted via USB adapter).

But as of Android 7.0 Nougat, the OS is said to be blocking access to this information, at least on some devices. I tested this by running a test app on a new Alcatel A30 GSM (Android 7.0), and in fact the above approach fails with a permission error:

java.io.FileNotFoundException: /sys/block/mmcblk0/device/serial (Permission denied) at java.io.FileInputStream.open(Native Method) 

For future reference, we (testing from an adb shell) have permissions to ls -ld the following:

  • /sys/class/mmc_host but not /sys/class/mmc_host/mmc0
  • /sys/block but not /sys/block/mmcblk0

Since the above approach no longer works,

  • Is there another way to obtain the serial number of a mounted SD card in Android 7.0 or later?

  • Failing that, is there any documentation or other statement from Google on plans for providing or not providing this function? I haven't found anything in the Android issue tracker, but maybe I'm not searching right.

To make sure the question is clear, I'm talking about what an ordinary (non-system) app running on a non-rooted device can do, with any permissions that an app can normally request and receive.

FYI, the /sbin directory doesn't seem to be readable, so commands like /sbin/udevadm aren't an option.

like image 699
LarsH Avatar asked Apr 19 '17 01:04

LarsH


People also ask

How do I find my SD card serial number?

The serial number is located at the bottom of the memory card. The first ten digits at the bottom of the memory card represent the serial number of the product. We add new series of codes on the bottom next to the original set of codes in order to have users easily recognize the product model.

How do I change the serial number on my micro SD card?

Right click the SD card partition, and click “Advanced” and then “Change Serial Number”. Step 2. In the pop-out window, you can edit a new serial number such as 123456.


1 Answers

In Android N access to /sys nad /proc was significantly restricted, this was done to provide stricter sandboxes where applications run. This is explained in https://issuetracker.google.com/issues/37091475 as intentional. Actually its not said that all the data in /sys is not accessible, and Google is open to allow access to other files from this location:

If there are specific files in /sys you believe should be available to applications, but are not, please file a new bug where the request can be evaluated. For instance, /sys/devices/system/cpu is available to all processes, so it's inaccurate to say all of /sys is restricted.

I have a bad feeling that google is making changes similar to Apple where it is not allowed to gain hardware id-s. If that is not resolved then the solution is to use google account IDs instead. But I am aware it is not always possible, and will require major changes in business logic (licensing etc.).

Hopefully your bug report will be considered positively.

another related SO I found : File system changes in Android Nougat

like image 74
marcinj Avatar answered Oct 05 '22 06:10

marcinj