C# 在 Windows Phone 8 下获取唯一设备 ID (UDID)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13975315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Unique Device ID (UDID) under Windows Phone 8
提问by flexo
Is there any unique device ID (UDID) or any similar ID I can read out on Windows Phone 8 (WP8) that doesn't change with hardware changes, app-reinstallation etc.?
是否有任何唯一的设备 ID (UDID) 或我可以在 Windows Phone 8 (WP8) 上读出的任何类似 ID 不会随着硬件更改、应用程序重新安装等而改变?
In older Windows Phone versions there were such IDs: WP7: Device Status for Windows Phone
在较旧的 Windows Phone 版本中有这样的 ID:WP7:Windows Phone 的设备状态
WP7.1: DeviceStatus Class
WP7.1:设备状态类
But they doesn't work anymore with SDK 8.0.
但它们不再适用于 SDK 8.0。
Why I ask:The idea is that a user gets some free credits with the first start of the the app and I want to avoid that the user just re-installs the app for getting new free credits. A registration with email or phone number could solve this, but if I can, I don't want do bother users at the first start with a registration.
为什么我问:这个想法是用户在应用程序的第一次启动时获得一些免费积分,我想避免用户只是重新安装应用程序以获得新的免费积分。使用电子邮件或电话号码注册可以解决这个问题,但如果可以的话,我不想在第一次注册时打扰用户。
---///---SOLUTION----------
- -/// - -解决方案 - - - - -
I can confirm that DeviceExtendedProperties.GetValue("DeviceUniqueId") still works in WP 8.0. Got a little bit confused when I read the following text:
我可以确认 DeviceExtendedProperties.GetValue("DeviceUniqueId") 在 WP 8.0 中仍然有效。当我阅读以下文字时有点困惑:
In Windows Phone OS 7.0, this class was used to query device-specific properties. In Windows Phone OS 7.1, most of the properties in DeviceExtendedProperties were deprecated, and the new DeviceStatus class should be used instead. However, where appropriate, you can still use any of the below properties that are not deprecated.
在 Windows Phone OS 7.0 中,此类用于查询特定于设备的属性。在 Windows Phone OS 7.1 中,不推荐使用 DeviceExtendedProperties 中的大多数属性,应改用新的 DeviceStatus 类。但是,在适当的情况下,您仍然可以使用以下任何未弃用的属性。
MSDN:DeviceExtendedProperties Class
MSDN:DeviceExtendedProperties 类
I can run the following code, delete the app and re-install it and get the same ID:
我可以运行以下代码,删除应用程序并重新安装它并获得相同的 ID:
byte[] myDeviceID = (byte[])Microsoft.Phone.Info.DeviceExtendedProperties.GetValue("DeviceUniqueId");
string DeviceIDAsString = Convert.ToBase64String(myDeviceID);
MessageBox.Show(DeviceIDAsString);
采纳答案by Frazell Thomas
I haven't yet started to develop for Windows Phone 8, still on 7, but you still should be able to use the original DeviceExtendedPropertiesclass to pull back the Device Unique ID.
我还没有开始为 Windows Phone 8 开发,还在 7 上,但是您仍然应该能够使用原始DeviceExtendedProperties类来拉回设备唯一 ID。
DeviceExtendedProperties.GetValue("DeviceUniqueId")
回答by giacoder
I've had this issue with returning the null value. Then remembered that it needs to be switched on.
我在返回空值时遇到了这个问题。然后想起需要开机。
In WMAppManifest.xml
-> Capabilities
tab -> switch on ID_CAP_IDENTITY_DEVICE
在WMAppManifest.xml
->Capabilities
标签 -> 打开ID_CAP_IDENTITY_DEVICE
回答by Jani Nevalainen
There's a twist to this DeviceUniqueId - it is unique only for one publisher. So it is not really device-wide unique identifier but unique device id for one publisher. We have noticed when we worked on some customer project where we tried to identify the same phone from different accounts (customer publishes under two different accounts).
此 DeviceUniqueId 有一个转折点 - 它仅对一个发布者是唯一的。所以它并不是真正的设备范围的唯一标识符,而是一个发布者的唯一设备 ID。我们注意到,当我们在某个客户项目中工作时,我们试图从不同的帐户(客户在两个不同的帐户下发布)识别同一部手机。
回答by prabh.arora
By not providing DeviceUniqueId in Windows Phone 8 and Windows 8, Microsoft tried to avoid User Tracking but with increased pressure from Dev community, they are bringing it back again.
通过在 Windows Phone 8 和 Windows 8 中不提供 DeviceUniqueId,微软试图避免用户跟踪,但随着来自开发社区的压力越来越大,他们又把它带回来了。
In Windows 8.1, Microsoft has introduced a new AdvertisingId API and may also bring similar Id to identify a unique user across apps in subsequent Windows Phone 8.1/9 versions.
在 Windows 8.1 中,微软引入了一个新的 AdvertisingId API,并且在后续的 Windows Phone 8.1/9 版本中也可能带来类似的 Id 来跨应用程序识别唯一用户。
回答by Jaihind
You can get your own wp8 device Id by DeviceExtendedProperties.GetValue("DeviceUniqueId") Here is the simple way to get deviceId as a string
您可以通过 DeviceExtendedProperties.GetValue("DeviceUniqueId") 获取自己的 wp8 设备 ID 这里是获取 deviceId 作为字符串的简单方法
byte[] id = (byte[])Microsoft.Phone.Info.DeviceExtendedProperties.GetValue("DeviceUniqueId");
string deviceID = Convert.ToBase64String(id);
回答by CarmenA
I used this:
我用过这个:
private static String getDeviceId()
{
byte[] id = (byte[])Microsoft.Phone.Info.DeviceExtendedProperties.GetValue("DeviceUniqueId");
return BitConverter.ToString(id).Replace("-", string.Empty);
}
But the key is to Check the ID_CAP_IDENTITY_DEVICE in WMAppManifest, or else it throws error.
但关键是检查WMAppManifest中的ID_CAP_IDENTITY_DEVICE,否则会报错。
回答by raevilman
I found this a new HostInformation.PublisherHostId property More info at http://msdn.microsoft.com/en-us/library/windowsphone/develop/windows.phone.system.analytics.hostinformation.publisherhostid.aspx..
我发现这是一个新的 HostInformation.PublisherHostId 属性 更多信息,请访问 http://msdn.microsoft.com/en-us/library/windowsphone/develop/windows.phone.system.analytics.hostinformation.publisherhostid.aspx..
回答by sunny_iexceed
string myDeviceID = (byte[])DeviceExtendedProperties.GetValue("DeviceUniqueId");
string DeviceIDAsString = Convert.ToBase64String(myDeviceID);
I have used this for windows phone unique device Id.
我已将此用于 Windows 手机的唯一设备 ID。
回答by Has AlTaiar
The answers above work for Windows Phone 7 and 8 Silverlight. However, they will not work for Windows Phone RT (Universal) or Store Apps since the SDK does not have this dll library (Microsoft.Phone).
以上答案适用于 Windows Phone 7 和 8 Silverlight。但是,它们不适用于 Windows Phone RT (Universal) 或 Store Apps,因为 SDK 没有这个 dll 库 (Microsoft.Phone)。
This is how you get the device ID and Name (and possibly other info) on Windows Phone 8.1 RT (Universal/Store Apps).
这是您在 Windows Phone 8.1 RT(通用/商店应用)上获取设备 ID 和名称(可能还有其他信息)的方式。
private string GetHardwareId()
{
var token = HardwareIdentification.GetPackageSpecificToken(null);
var hardwareId = token.Id;
var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(hardwareId);
byte[] bytes = new byte[hardwareId.Length];
dataReader.ReadBytes(bytes);
return BitConverter.ToString(bytes);
}
More details about reading the device info on Windows RT can be found here
可以在此处找到有关在 Windows RT 上读取设备信息的更多详细信息