简体   繁体   English

如何使用 C# 从 IOS 设备获取 IMEI

[英]How to get IMEI from IOS device using C#

I need to read out the IMEI of an IOS device using C#...我需要使用 C# 读取 IOS 设备的 IMEI ...

Is this even possible in C#/Xamarin?这在 C#/Xamarin 中是否可行? Or is there another value that i can use to identify a device?还是有另一个值可以用来识别设备?

Some device identifiers are now impossible to be obtained from public APIs of iOS:一些设备标识符现在无法从 iOS 的公共 API 中获取:

IMSI - International Mobile Subscriber Identity (SIM card number) IMSI - 国际移动用户识别码(SIM 卡号)

IMEI - International Mobile Equipment Identity (Device ID) IMEI - 国际移动设备识别码(设备 ID)

UDID - Unique Device Identifier for Apple iDevices UDID - Apple iDevices 的唯一设备标识符

MAC address - Media Access Control Address (Network address) MAC 地址 - 媒体访问控制地址(网络地址)

Take a look here: http://studyswift.blogspot.gr/2015/12/asidentifiermanager-get-idfv-vendor.html看看这里: http : //studyswift.blogspot.gr/2015/12/asidentifiermanager-get-idfv-vendor.html

If you could use any of the provided IDs the code is in Swift but if you use C# / Xamarin it won't be difficult to convert.如果您可以使用任何提供的 ID,代码在 Swift 中,但如果您使用 C#/Xamarin,则转换起来并不困难。

Hope this helps希望这有帮助

I've also tried to find a way to capture the IMEI, but I believe this is not possible.我也试图找到一种方法来捕获 IMEI,但我相信这是不可能的。 The only way I solved it was to use this code, it returns serial number我解决它的唯一方法是使用此代码,它返回序列号

 public class IosDevice 
 {
        [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
        private static extern uint IOServiceGetMatchingService(uint masterPort, IntPtr matching);

        [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
        private static extern IntPtr IOServiceMatching(string s);

        [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
        private static extern IntPtr IORegistryEntryCreateCFProperty(uint entry, IntPtr key, IntPtr allocator, uint options);

        [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
        private static extern int IOObjectRelease(uint o);

        public string GetIdentifier()
        {
            string serial = string.Empty;
            uint platformExpert = IOServiceGetMatchingService(0, IOServiceMatching("IOPlatformExpertDevice"));

            if (platformExpert != 0)
            {
                NSString key = (NSString)"IOPlatformSerialNumber";
                IntPtr serialNumber = IORegistryEntryCreateCFProperty(platformExpert, key.Handle, IntPtr.Zero, 0);

                if (serialNumber != IntPtr.Zero)
                {
                    serial = NSString.FromHandle(serialNumber);
                }

                IOObjectRelease(platformExpert);
            }

            return serial;
        }
    }

In case someone wants to get vid, pid of an USB Device in MacOS如果有人想在 MacOS 中获得 USB 设备的 vid、pid

public class OsxDeviceDiscovery
{
    [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
    private static extern int IOServiceGetMatchingService(int masterPort, IntPtr matching);

    [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
    private static extern int IOServiceGetMatchingServices(int masterPort, IntPtr matching, out IntPtr iterator);

    [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
    private static extern IntPtr IOServiceMatching(string name);

    [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
    private static extern IntPtr IORegistryEntryCreateCFProperty(int entry, IntPtr key, IntPtr allocator, uint options);

    [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
    private static extern int IOObjectRelease(int o);

    [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
    private static extern int IOIteratorNext(IntPtr iterator);

    [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
    private static extern bool CFNumberGetValue(IntPtr number,long type, ref long value);

    [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
    private static extern int CFNumberGetType(IntPtr number);

    [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
    private static extern bool CFStringGetCString(IntPtr stringRef, byte[] str, int size, int encoding);

    [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
    private static extern int IORegisterEntryCreateIterator(IntPtr entry, IntPtr plane, int options, out IntPtr iterator);

    public static string GetIdentifier()
    {
        string deviceName = string.Empty;
        IntPtr matchingNodes;
        int platformExpert = IOServiceGetMatchingServices(0, IOServiceMatching("IOUSBDevice"), out matchingNodes);

        int node = -1;
        while ((node = IOIteratorNext(matchingNodes)) != 0)
        {
            long vendorID = 0;
            long productID = 0;
            long locationId = 0;

            NSString key = (NSString)"idVendor";
            IntPtr proRef = IORegistryEntryCreateCFProperty(node, key.Handle, IntPtr.Zero, 0);
            if (proRef != IntPtr.Zero)
            {
                long type = CFNumberGetType(proRef);
                CFNumberGetValue(proRef, type, ref vendorID);
            }

            key = (NSString)"idProduct";
            proRef = IORegistryEntryCreateCFProperty(node, key.Handle, IntPtr.Zero, 0);
            if (proRef != IntPtr.Zero)
            {
                long type = CFNumberGetType(proRef);
                CFNumberGetValue(proRef, type, ref productID);
            }

            if (vendorID != 0x1234 || productID != 0x5678)
            {
                IOObjectRelease(node);
                continue;
            }

            key = (NSString)"locationID";
            proRef = IORegistryEntryCreateCFProperty(node, key.Handle, IntPtr.Zero, 0);
            if (proRef != IntPtr.Zero)
            {
                long type = CFNumberGetType(proRef);
                CFNumberGetValue(proRef, type, ref locationId);
            }

            key = (NSString)"kUSBSerialNumberString";
            proRef = IORegistryEntryCreateCFProperty(node, key.Handle, IntPtr.Zero, 0);
            if (proRef != IntPtr.Zero)
            {
                byte[] byteArray = new byte[20];
                CFStringGetCString(proRef, byteArray, 20, 0x0600);
                string serialNumber = System.Text.Encoding.UTF8.GetString(byteArray);
                deviceName = "/dev/cu.usbmodem" + serialNumber;
            }


            IOObjectRelease(node);
        }

        return deviceName;
    }
}

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

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