Get device MAC address
I am writing a Windows Phone 8.1 application that detects nearby Bluetooth Low Energy devices.
foreach (DeviceInformation device in devices)
{
BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromIdAsync(device.Id);
}
Everything works fine, but the property bleDevice.BluetoothAddress
contains a type ulong
, whereas I need a string type formatted as a Mac address.
Example:
bleDevice.BluetoothAddress: 254682828386071 (ulong)
Desired Mac address: D1: B4: EC: 14: 29: A8 (string) (this is an example of how I need it, not the actual Mac address of the device)
Is there a way to convert long URL on Mac? Or is there another way to directly open the Mac address without conversions? I know a tool named In The HAnd - 32feet
can help me, but Windows Phone 8.1 is not supported at the moment.
source to share
There are many topics that you can find via Google and here on StackOverflow. Anyway, here's one way to do it:
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'
source to share