كيفية الحصول على عنوان ماك بلوتوث من جهاز الكمبيوتر المحلي؟

StackOverflow https://stackoverflow.com/questions/1819971

  •  10-07-2019
  •  | 
  •  

سؤال

وأريد الحصول على عنوان MAC للجهاز بلوتوث على جهاز كمبيوتر يعمل بنظام التشغيل طلبي جرا.

ولقد حاولت ما يلي:

private void GetMacAddress()
{
     string macAddresses = "";
     foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
     {
          if (nic.OperationalStatus == OperationalStatus.Up)
          {
               macAddresses += nic.GetPhysicalAddress().ToString();
               Console.WriteLine(macAddresses);
          }
     }
}

ولكن الإخراج لا تتطابق مع "ipconfig / كافة" في commandprompt. فإنه لا يطبع عنواني blueotths ماك. أي حلول؟

وأنا على استعداد لتحليل الإخراج أحصل من "ipconfig / كافة" ولكن كيف يمكنني الحصول على الناتج كسلسلة؟

هل كانت مفيدة؟

المحلول

ويمكنك ربما استخدام WMI للحصول على النتائج. طيه تصل إلى حل WMI أن يتخلف عن طريق أجهزة الشبكة.

وأنا نشر الشفرة هنا في حالة الموقع باستمرار، ولكن كل الفضل في ذلك إلى المؤلف الأصلي، PsychoCoder. استخدام WMI للحصول على عنوان MAC في C #

ورمز:

//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();

ويمكنك أيضا إلقاء نظرة على MSDN بشأن 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;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top