编程语言
首页 > 编程语言> > c#-获取设备的MAC地址

c#-获取设备的MAC地址

作者:互联网

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

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

一切正常,但是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上找到许多主题.无论如何,这是一种方法:

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'

标签:windows-phone-8-1,bluetooth-lowenergy,c
来源: https://codeday.me/bug/20191029/1956534.html