Reputation: 167
I'm writing a Windows Phone 8.1 Application that discovers nearby Bluetooth Low Energy devices.
foreach (DeviceInformation device in devices)
{
BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromIdAsync(device.Id);
}
Everything works fine, but the bleDevice.BluetoothAddress
property contains a ulong
type, while I need a string type, formatted like a Mac Address.
Example:
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)
Is there a way to convert the long to a Mac Address? Or is there another way to directly discover the Mac Address without conversions? I know there's a tool named In The HAnd - 32feet
that could help me, but as of now Windows Phone 8.1 is not supported.
Upvotes: 3
Views: 3936
Reputation: 138776
Here is a way of doing it:
var address = string.Join(":", BitConverter.GetBytes(addressAsUInt64)
.Take(6)
.Reverse()
.Select(b => b.ToString("X2"))
);
Upvotes: 0
Reputation: 14432
There are numerous topics you can find through 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'
Upvotes: 6