로컬 PC에서 Bluetooth Mac 주소를 얻는 방법은 무엇입니까?
-
10-07-2019 - |
문제
PC에서 Bluetooth 장치의 MAC 주소를 얻고 싶습니다. 응용 프로그램이 실행 중입니다.
다음을 시도했습니다.
private void GetMacAddress()
{
string macAddresses = "";
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.OperationalStatus == OperationalStatus.Up)
{
macAddresses += nic.GetPhysicalAddress().ToString();
Console.WriteLine(macAddresses);
}
}
}
그러나 출력은 CommandPrompt의 'IpConfig /All'과 일치하지 않습니다. 내 Blueotths Mac 주소를 인쇄하지 않습니다. 솔루션이 있습니까?
'ipconfig /all'에서 얻은 출력을 구문 분석 할 준비가되었지만 출력을 문자열로 어떻게 얻습니까?
해결책
WMI를 사용하여 결과를 얻을 수 있습니다. 여기에서는 네트워크 장치를 통과하는 WMI 솔루션에 대한 링크가 있습니다.
웹 사이트가 다운 된 경우 여기에 코드를 게시하고 있지만 모든 크레딧은 원래 저자 인 Psychocoder에게 전달됩니다.WMI를 사용하여 C#에서 Mac 주소를 얻습니다.
그리고 코드 :
//Namespace reference
using System.Management;
/// <summary>
/// Returns MAC Address from first Network Card in Computer
/// </summary>
/// <returns>MAC Address in string format</returns>
public string FindMACAddress()
{
//create out management class object using the
//Win32_NetworkAdapterConfiguration class to get the attributes
//af the network adapter
ManagementClass mgmt = new ManagementClass("Win32_NetworkAdapterConfiguration");
//create our ManagementObjectCollection to get the attributes with
ManagementObjectCollection objCol = mgmt.GetInstances();
string address = String.Empty;
//My modification to the code
var description = String.Empty;
//loop through all the objects we find
foreach (ManagementObject obj in objCol)
{
if (address == String.Empty) // only return MAC Address from first card
{
//grab the value from the first network adapter we find
//you can change the string to an array and get all
//network adapters found as well
if ((bool)obj["IPEnabled"] == true)
{
address = obj["MacAddress"].ToString();
description = obj["Description"].ToString();
}
}
//dispose of our object
obj.Dispose();
}
//replace the ":" with an empty space, this could also
//be removed if you wish
address = address.Replace(":", "");
//return the mac address
return address;
}
System.Management에 대한 참조를 포함하십시오. 네트워크 장치 이름을 얻으려면 obj["Description"].ToString();
WMI, 특히 win32_networkadapterConfiguration 클래스
도움이 되었기를 바랍니다.
다른 팁
public static PhysicalAddress GetBTMacAddress() {
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) {
// Only consider Bluetooth network interfaces
if (nic.NetworkInterfaceType != NetworkInterfaceType.FastEthernetFx &&
nic.NetworkInterfaceType != NetworkInterfaceType.Wireless80211){
return nic.GetPhysicalAddress();
}
}
return null;
}
제휴하지 않습니다 StackOverflow