简体   繁体   English

如何在 Android 7.0+ 中检索 SD 卡的序列号?

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

NB: This question about the serial number of the physical SD card, not the UUID of the mounted volume .注意:这个问题是关于物理 SD 卡的序列号,而不是挂载的 UUID。 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, eg by reading the contents of /sys/class/mmc_host/mmc0/mmc0:0001/serial or /sys/block/mmcblk0/device/serial (specific numbers may vary).在某些版本的 Android 和其他 Linux 变体中,可以获取已安装 SD 卡的序列号,例如通过读取/sys/class/mmc_host/mmc0/mmc0:0001/serial/sys/block/mmcblk0/device/serial (具体数字可能有所不同)。 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).在我的测试中,只要将 SD 卡插入内置 SD 卡插槽(未通过 USB 适配器安装),此方法就非常可靠。

But as of Android 7.0 Nougat, the OS is said to be blocking access to this information, at least on some devices.但从 Android 7.0 Nougat 开始,据说操作系统会阻止访问这些信息,至少在某些设备上是这样。 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:我通过在新的 Alcatel A30 GSM (Android 7.0) 上运行测试应用程序对此进行了测试,实际上上述方法失败并出现权限错误:

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:为了将来参考,我们(从 adb shell 进行测试)有权ls -ld执行以下操作:

  • /sys/class/mmc_host but not /sys/class/mmc_host/mmc0 /sys/class/mmc_host但不是/sys/class/mmc_host/mmc0
  • /sys/block but not /sys/block/mmcblk0 /sys/block但不是/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?在Android 7.0或更高版本中,还有其他方法可以获取已挂载的SD卡的序列号吗?

  • 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.我没有在 Android 问题跟踪器中找到任何东西,但也许我没有找到正确的方法。

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.为了确保问题清楚,我说的是在非 root 设备上运行的普通(非系统)应用程序可以做什么,具有应用程序通常可以请求和接收的任何权限。

FYI, the /sbin directory doesn't seem to be readable, so commands like /sbin/udevadm aren't an option.仅供参考, /sbin目录似乎不可读,因此/sbin/udevadm类的命令不是一个选项。

In Android N access to /sys nad /proc was significantly restricted, this was done to provide stricter sandboxes where applications run.在 Android N 中,对 /sys nad /proc 的访问受到显着限制,这样做是为了提供更严格的应用程序运行沙箱。 This is explained in https://issuetracker.google.com/issues/37091475 as intentional.这是有意在https://issuetracker.google.com/issues/37091475 中解释的。 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:实际上并不是说 /sys 中的所有数据都不可访问,并且 Google 已开放以允许从该位置访问其他文件:

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.如果 /sys 中有您认为应可用于应用程序的特定文件,但不是,请提交一个新的错误,以便可以评估请求。 For instance, /sys/devices/system/cpu is available to all processes, so it's inaccurate to say all of /sys is restricted.例如,/sys/devices/system/cpu 可用于所有进程,因此说所有/sys 都受到限制是不准确的。

I have a bad feeling that google is making changes similar to Apple where it is not allowed to gain hardware id-s.我有一种不好的感觉,谷歌正在做出类似于苹果的改变,不允许获得硬件 id-s。 If that is not resolved then the solution is to use google account IDs instead.如果未解决,则解决方案是改用谷歌帐户 ID。 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我发现另一个相关的 SO: Android Nougat 中的文件系统更改

Use StorageVolume.getUuid() on StorageVolume which you get from StorageManager.在您从 StorageManager 获得的StorageVolume上使用StorageVolume.getUuid()

The value is volume ID assigned during formatting of the card, and its length/format differs depending on file system type.该值是格式化卡时分配的卷 ID,其长度/格式因文件系统类型而异。 For FAT32 it is XXXX-XXXX, for NTFS it's longer hex string, for Internal mass storage it returns null.对于 FAT32,它是 XXXX-XXXX,对于 NTFS,它是更长的十六进制字符串,对于内部大容量存储,它返回 null。

public String getSDCARDiD()
{
    String sd_cid = null;
    try {

        File file = new File("/sys/block/mmcblk1");
        String memBlk;
        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;
}

try this: reference original link : Android get id of SD Card programmatically试试这个:参考原始链接: Android以编程方式获取SD卡的ID

adb shell cat /sys/class/mmc_host/mmc1/mmc1:*/cid

You can also try你也可以试试

sudo hwinfo --disk

to get information on your disks, including SD Cards.获取有关磁盘(包括 SD 卡)的信息。

Also try也试试

sudo hdparm -I /dev/sdb | more

As an FYI to those looking into UUID or volume serial numbers of FAT type volumes under Android: Some Fujifilm cameras, including the X-T30 (Firmware 1.10) do not write a volume serial number into the FAT volume when formatting.作为在 Android 下查看 UUID 或 FAT 类型卷的卷序列号的人的参考:某些 Fujifilm 相机,包括 X-T30(固件 1.10)在格式化时不会将卷序列号写入 FAT 卷。

Under Windows, CHKDSK displays no volume serial number at all.在 Windows 下,CHKDSK 根本不显示卷序列号。 On Android, calling StorageVolume.getUuid() returns "0000-0000".在 Android 上,调用 StorageVolume.getUuid() 返回“0000-0000”。

This is all fine and dandy, until you on Android mount two Fujifilm-formatted cards via a hub.这一切都很好,很花哨,直到您在 Android 上通过集线器安装两张 Fujifilm 格式的卡。 Then there seems to be identity collision, where the Android OS prompts the user to format one of the cards.然后似乎是身份冲突,Android 操作系统会提示用户格式化其中一张卡。 Separately they are accessible.它们分别是可访问的。

I'm guessing there are two combined problems here: 1) Fujifilm is not writing a volume serial number when formatting, and 2) Android uses the volume serial number as part of the mount point path, leading to collision.我猜这里有两个组合问题:1)Fujifilm 在格式化时没有写入卷序列号,2)Android 使用卷序列号作为挂载点路径的一部分,导致冲突。

Fujifilm and Google might both want to pay attention to this issue.富士胶片和谷歌可能都想关注这个问题。

EDIT: Card formatted in a Nikon D810 also has the same problem, no Volume Serial Number.编辑:用尼康 D810 格式化的卡也有同样的问题,没有卷序列号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM