当前位置:首页 > 操作系统 > MacOs

c#-获取设备的MAC地址

我正在写一个Windows Phone 8.1应用程序,它可以发现附近的Bluetooth Low Energy设备.

<code>foreach (DeviceInformation device in devices)
{
    BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromIdAsync(device.Id);
}
</code>

一切正常,但是bleDevice.BluetoothAddress属性包含ulong类型,而我需要一个字符串类型,其格式类似于Mac Address.

例:

bleDevice.BluetoothAddress: 254682828386071 (ulong)

Desired Mac Address: D1:B4:EC:14:29:A8 (string) (that’s an example of how I need it, not the actual Mac Address of the device)

有没有办法将long转换为Mac地址?还是有另一种方法可以直接发现Mac地址而不进行转换?我知道有个名为In The HAnd-32feet的工具可以为我提供帮助,但到目前为止,不支持Windows Phone 8.1.

解决方法:

您可以通过Google以及StackOverflow上找到许多主题.无论如何,这是一种方法:

<code>ulong input = 254682828386071;
var tempMac = input.ToString("X");
//tempMac is now 'E7A1F7842F17'

var regex = "(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})";
var replace = "$1:$2:$3:$4:$5:$6";
var macAddress = Regex.Replace(tempMac, regex, replace);
//macAddress is now 'E7:A1:F7:84:2F:17'
</code>

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐