Get MAC address of device - c#

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.

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'

Related

Always the same Bluetooth Mac-Address using BLE.Plugin in Xamarin

I'm using BLE.Plugin for scaning other device in Xamarin. There is no problem with scanning and the plugin finds some devices, but all the devices have the same mac-address. The found address doesn't match to the device addresses of the devices, that it should find. Below the code I'm using.
adapter.DeviceDiscovered += (s, a) =>
{
if (a.Device.NativeDevice is BluetoothDevice device)
{
if (addresses.Contains(device.Address))
{
...
}
}
};
await adapter.StartScanningForDevicesAsync();
Do you need to use the NativeDevice? The Id field on a.Device is the Unique Id for each bluetooth pheriperal that is found.
Seems you can't use the Mac address on iOS anyway and Id is compatible across platforms -> How to get Mac Address From CBPeripheral And CBCenter

Search for devices in range of bluetooth UWP

Is it possible to scan for devices in range of bluetooth? I tried FindAll method but it returns all paired devices. Also tried deviceWatcher with the same result.
Is it possible to scan for devices in range of bluetooth?
This is possible, we can use either DeviceInformation.FindAllAsync method or DeviceWatcher class. But to get all Bluetooth devices, we need to note that currently Bluetooth APIs don't provide a selector to get ALL devices that are both paired and non-paired. BluetoothDevice.GetDeviceSelector method actually returns the same value as BluetoothDevice.GetDeviceSelectorFromPairingState(true). So when you use this selector (Advanced Query Syntax (AQS) string), you will always get all paired devices.
To get all devices, we can use this selector get paired devices first and then use BluetoothDevice.GetDeviceSelectorFromPairingState(false) to get the rest unpaired devices.
Or we can just specify the AQS string like following:
//The AQS string for getting all Bluetooth devices
var BluetoothDeviceSelector = "System.Devices.DevObjectType:=5 AND System.Devices.Aep.ProtocolId:=\"{E0CBF06C-CD8B-4647-BB8A-263B43F0F974}\"";
//Using DeviceInformation.FindAllAsync method
var deviceInfoCollection = await DeviceInformation.FindAllAsync(BluetoothDeviceSelector);
//Using DeviceWatcher class
var deviceWatcher = DeviceInformation.CreateWatcher(BluetoothDeviceSelector);
PS: FindAllAsync method is more often used to look through the devices that are currently connected to or paired with the system. To get both paired and non-paired devices, it better to use DeviceWatcher class.
For more info about how to use FindAllAsync method or DeviceWatcher class, please see Enumerate devices and official Device enumeration and pairing sample.
Besides Bluetooth devices, there are also Bluetooth LE devices you might want to get. And for Bluetooth LE devices, the AQS string would be System.Devices.DevObjectType:=5 AND System.Devices.Aep.ProtocolId:="{BB7BB05E-5972-42B5-94FC-76EAA7084D49}".
GetDeviceSelector and friends return strings containing AQS queries. Have you ever looked at them?
Paired
System.Devices.DevObjectType:=5
AND System.Devices.Aep.ProtocolId:="{E0CBF06C-CD8B-4647-BB8A-263B43F0F974}"
AND (
System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#True
OR System.Devices.Aep.Bluetooth.IssueInquiry:=System.StructuredQueryType.Boolean#False
)
Unpaired
System.Devices.DevObjectType:=5
AND System.Devices.Aep.ProtocolId:="{E0CBF06C-CD8B-4647-BB8A-263B43F0F974}"
AND (
System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#False
OR System.Devices.Aep.Bluetooth.IssueInquiry:=System.StructuredQueryType.Boolean#True
)
What do you think is tested by the expression in parentheses?
What do you think might happen if you omit this clause from the string you supply to CreateWatcher?
Something less obvious that you may find helpful is the fact that there are two Bluetooth protocol identifiers. The one shown above is for classic Bluetooth and the query does not match BLE (Bluetooth Low Energy) devices.
The selector I suggest you use is
System.Devices.DevObjectType:=5
AND (
System.Devices.Aep.ProtocolId:="{BB7BB05E-5972-42B5-94FC-76EAA7084D49}"
OR System.Devices.Aep.ProtocolId:="{E0CBF06C-CD8B-4647-BB8A-263B43F0F974}#"
)
I have put line breaks in all these queries to help you read them. Don't include them in your code. For convenience here's some code from one of my projects.
string BleSelector = "System.Devices.DevObjectType:=5 AND (System.Devices.Aep.ProtocolId:=\"{BB7BB05E-5972-42B5-94FC-76EAA7084D49}\" OR System.Devices.Aep.ProtocolId:=\"{E0CBF06C-CD8B-4647-BB8A-263B43F0F974}\")";
string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected", "System.Devices.Aep.ProtocolId" };
deviceWatcher = DeviceInformation.CreateWatcher(BleSelector, requestedProperties, DeviceInformationKind.AssociationEndpoint);
Using a watcher has the advantage that the list becomes available progressively. FindAllAsync may be asynchronous but it takes ages to return.
I provide BluetoothLE Device Selector string in the following code. It works for me.
DeviceWatcher dWatcher = null;
var BluetoothDeviceSelector = "System.Devices.DevObjectType:=5 AND System.Devices.Aep.ProtocolId:=\"{BB7BB05E-5972-42B5-94FC-76EAA7084D49}\" AND ((System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#True OR System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#False) OR System.Devices.Aep.Bluetooth.IssueInquiry:=System.StructuredQueryType.Boolean#False)";
dWatcher = DeviceInformation.CreateWatcher(BluetoothDeviceSelector);
dWatcher.Added += DeviceAdded;
dWatcher.Updated += DeviceUpdated;
dWatcher.Start();

Is it possible to get PC's # of physical USB ports?

I'm trying to get number of physical USB ports of PC for different Windows operating systems. To start with it seemed like an easy task but now I'm getting desperate.
Maybe this question is not even valid since I don't know how USB ports are treated on hardware level.
I thought using WMI(C) and ManagementObjectSearcher would be the right path to take and it returned right values... on certain operating systems. Or that's what I thought.
For example, I tried the following:
// >wmic path win32_usbhub get name
private const string UsbProperty = "Name";
private const string UsbPath = "Win32_USBHub";
private const string UsbPortName = "USB ROOT HUB";
...
// Get USB Ports
public IEnumerable<string> GetUsbPorts()
{
// All from Win32_USBHub where name contains USB ROOT HUB
return GetManagementObjectValues(UsbProperty, UsbPath).Where(name =>
CultureInfo.CurrentCulture.CompareInfo.IndexOf(
name, UsbPortName, CompareOptions.IgnoreCase) >= 0);
}
.
// Query
private static IEnumerable<string> GetManagementObjectValues(
string properties, string path, Func<object, string> formatter = null)
{
var values = new List<string>();
string query = string.Format("SELECT {0} FROM {1}", properties, path);
var search = new ManagementObjectSearcher(query);
try
{
foreach (ManagementObject item in search.Get())
{
string value = string.Empty;
foreach (string property in properties.Split(',')
.Select(prop => prop.Trim()))
{
if (item[property] == null)
continue;
if (value.Length > 0)
value += " ";
if (formatter != null)
value += formatter(item[properties]);
value += item[property].ToString();
}
values.Add(value.TrimEnd());
}
}
catch (Exception e)
{
if (e is ManagementException)
Logger.Warn(string.Format(
"Can't extract {0} properties of {1}", properties, path), e);
else
Logger.Error(e);
}
return values.Count >= 1 ? values : new List<string> { DefaultValue };
}
This seemed to get me the right amount on Windows8 but on WindowsXP it was totally off.
Next, I tried (for example) the following. I noticed that on Win8 I have USB<number> as ExternalReferenceDesignator but on WinXP, there's plain USB as InternalReferenceDesignator and external is empty.
For XP this seemed to work just fine but then again on Win8 amount of ports was six (6). Actual port count is 3 and with docking station station seven (7).
// >wmic path Win32_PortConnector get ExternalReferenceDesignator,InternalReferenceDesignator
private const string UsbPortName = "USB";
private const string PortProperties =
"ExternalReferenceDesignator, InternalReferenceDesignator";
private const string PortPath = #"Win32_PortConnector";
...
public IEnumerable<string> GetEthernetPorts()
{
// All where external includes USB or internal equals USB
return GetManagementObjectValues(PortProperties, PortPath).Where(name =>
CultureInfo.CurrentCulture.CompareInfo.IndexOf(
name, UsbPortName, CompareOptions.IgnoreCase) >= 0 ||
string.Compare(name, UsbPortName, StringComparison.OrdinalIgnoreCase) == 0);
}
So is it even possible or am I just looking from the wrong place?
And to answer my own question: No, it's not possible.
WMIC query results for Win32_USBController (or some related path) might seem right but you can't draw any conclusions from them. Information about connectors aren't stored on the baseboard or any other location either.
For example an old Dell Latitude D830 with Windows XP SP3 has three (3) physical connectors but WMIC and USBView.exe shows results below:
C:\>wmic path win32_usbcontroller get caption
Caption
Intel(R) ICH8 Family USB Universal Host Controller - 2834
Intel(R) ICH8 Family USB Universal Host Controller - 2835
Intel(R) ICH8 Family USB2 Enhanced Host Controller - 283A
Intel(R) ICH8 Family USB Universal Host Controller - 2830
Intel(R) ICH8 Family USB Universal Host Controller - 2831
Intel(R) ICH8 Family USB Universal Host Controller - 2832
Intel(R) ICH8 Family USB2 Enhanced Host Controller - 2836
I know it has been a while since the question was asked, but I just happened to be working on a USB port tree for a support app of some sort.
Initially, I tried to use Win32..., as much as it is not intended to be used as a source of information for the rebuilding of the device tree, the answer of this post explains some possibilities (Get List of connected USB Devices).
I played with it but I did not like it. UsbView by itself was also not an option (lack of skill in C ). Luckily I found NativeUsbLib. It provides you with a USB device tree, you just need to understand how to read it. Not a physical USB port This image shows a port that clearly states it is not possible to use it. In addition to that parameter, there is "Companion Hub Symbolic Link Name", in my experience, it has to be present on a valid USB port.
As for multiple controllers, and even multiple ports in one controller that satisfy my previous statement, there is an explanation. Multiple ports for same physical port, in my case, ports 1 and 13 are the same. If a device is 1.1 or 2.0 it will show under port 1, and if it supports 3.0 it will show up under port 13. And same goes for two controllers, they don't have 100% the same structure, but once you strip unnecessary data, and merge data (not necessarily all of it) that is left, the result will be a USB port tree. I can't guarantee all of the statements are true, but that is my current experience, and I might update this post. If not, feel free to message me if you have some questions. Btw. NativeUsbLib by default does not provide data for ports that don't have a device present (plugged in). To fix that, comment out lines in DeviceFactory that check if the device is present (line 35).
Hope this helps someone and sorry for my spelling, I am sure I messed up somewhere...

List all USB audio headsets connected to a Windows PC

I want to use C# to retrieve the USB headset devices connected to PC. I tried the below solutions but didn't work:
Solution 1:
How to enumerate audio out devices in c#
I tried this but the device name appears as "(Generic USB Audio)" and not the actual name.
Solution 2:
How to get the default audio device?
Solution 3:
Detecting Audio Input & output devices connected to system
Solution 2 and Solution 3 gave me the below result:
The device name is truncated to 31 characters.
Eg: "Microphone (Sennheiser VOICE 689"
****Question: Is there any way I can get the complete name of the device?****
If you know it's an USB audio device, and assuming the driver is correctly written for the device, you could do:
foreach (ManagementObject drive in
new ManagementObjectSearcher(
"select Name from Win32_USBDevice where Service='usbaudio'").Get())
{
{
string s = drive["Name"].ToString();
// Continue
}
}
Addition
You're only getting 31 characters (technically 32) because the PInvoke to the native .DLLs use a char[32], so it can't return more than that; you won't get what you need from solution 1 & 2.
Also, I don't know why you can't use Win32_USBDevice, as I'm also using Win7 x64 and I'm having no problems. This link might help you.
Possible Alternate
You might be able to use the Win32_PnPEntity class:
foreach (ManagementObject drive in
new ManagementObjectSearcher(
"select Name from Win32_PnPEntity where Service='usbaudio'").Get())
{
{
string s = drive["Name"].ToString();
// Continue. Can look at Description, Caption, etc. too
}
}

Retrieve host VM MAC address via guest VM

This has been an issue I have been looking to for two days. I will share my findings.
I am currently working on an in-house license management system for our software. It's nothing too fancy - as long as it can uniquely identify a user, it's good enough. Our mechanism currently relies on user sign-in + password + MAC address.
99% of the users so far have had no issues, but there is a small subset, the 1%, that has been returning an issue. This 1% is so important to us, because one failure means one hole in our system, something we would like to weed out. Okay - onto the main topic.
Method 1:
public static string returnMAC1()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select MACAddress, PNPDeviceID FROM Win32_NetworkAdapter WHERE MACAddress IS NOT NULL AND PNPDEVICEID IS NOT NULL");
ManagementObjectCollection mObject = searcher.Get();
foreach (ManagementObject obj in mObject)
{
string pnp = obj["PNPDeviceID"].ToString();
if (pnp.Contains("PCI\\"))
{
string mac = obj["MACAddress"].ToString();
mac = mac.Replace(":", string.Empty);
return mac;
}
}
return "Nothing happened...";
}
Method 1 retrieves the MAC address based on the fact that the physical card is connected to the PCI interface.
Method 2:
public static string returnMAC2()
{
string mac = string.Empty;
foreach (System.Net.NetworkInformation.NetworkInterface nic in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up)
{
macAddresses += nic.GetPhysicalAddress().ToString();
break;
}
}
return mac;
}
The second method is a standard method retrieved from MSDN documentation in-regards to MAC addresses.
Based on some tests, it seems the second method is a tad unreliable to retrieve MAC addresses, since it retrieves the wireless card's address. We've had some users returning null addresses as a result of using that method, and while I don't know why that would happen, it could be because there's a lack of a wireless card in their computers. With that said, that's only conjecture. Method #1 relies on using SQL queries to retrieve the PCI MAC. This one has been reliable.
Tests:
Using a Windows 8.1 Enterprise Evaluation edition (free 90-day trial, yay!) installed onto the VirtualBox VM, the tests confirmed that there are major differences in the MAC addresses returned via the guest VM and the host VM.
According to my research, in most cases, the virtual machine is assigned the same MAC address every time it is powered on, so long as the virtual machine is not moved and no changes are made to the certain settings in the configuration file. With that said, and here's the bad news... The guest VM MAC could be anything. So it seems, this is one of the reasons the MAC addresses are inconsistent when used as unique identifiers, which is an issue I found out when some users were on their company VMs. I never knew that's the way people worked, but here we are, so no gloating about it at this point.
My question is - is there any way, without making the user change any settings on their end, to retrieve the host VM's MAC as opposed to the guest VM?
At this point I don't see any reason why someone won't assign the same MAC to every single guest machine to get around our floating license mechanism. Retrieving the host VM MAC would get around this issue, as it would show as one MAC.
We decided this is both impossible and unnecessary. We also decided to use the motherboard UUID as the primary unique identifier, and falling back to the MAC address using the MAC address method below, in case the UUID returns a FFFF-FFFF....... on the rare occasion the vendor does not supply a UUID to that motherboard.
public static string returnMAC1()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select MACAddress, PNPDeviceID FROM Win32_NetworkAdapter WHERE MACAddress IS NOT NULL AND PNPDEVICEID IS NOT NULL");
ManagementObjectCollection mObject = searcher.Get();
foreach (ManagementObject obj in mObject)
{
string pnp = obj["PNPDeviceID"].ToString();
if (pnp.Contains("PCI\\"))
{
string mac = obj["MACAddress"].ToString();
mac = mac.Replace(":", string.Empty);
return mac;
}
}
return "Nothing happened...";
}

Categories

Resources