C#中查找本地蓝牙适配器的32Feet BluetoothAddress

Find 32Feet BluetoothAddress of local Bluetooth adapter in C#

我正在尝试为我的案例使用来自 Pair bluetooth devices to a computer with 32feet .NET Bluetooth library

在此,xmashallax 提到了本地 mac 地址。为了获得本地地址,我正在尝试这个-

public static BluetoothAddress GetBTMacAddress()
    {
        var nics = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface nic in nics)
        {
            // Only consider Bluetooth network interfaces
            if (nic.NetworkInterfaceType != NetworkInterfaceType.FastEthernetFx &&
                nic.NetworkInterfaceType != NetworkInterfaceType.Wireless80211 && nic.Description.Contains("Bluetooth"))
            {

                return  new BluetoothEndPoint(nic.GetPhysicalAddress().GetAddressBytes(), BluetoothService.SerialPort); 
            }
        }
        return null;
    }

我在这里遇到错误“The requested address is not valid in its contextErrorCode: AddressNotAvailable

能否请您建议获取当前本地 PC mac 地址的正确方法?

为可能面临类似情况的其他人发布此答案。

基本上,要创建蓝牙端点,您需要适配器的有效蓝牙 mac 地址。要获取本地 machine 的本地蓝牙 mac 地址,只需使用

BluetoothRadio.PrimaryRadio.LocalAddress

所以上面的代码需要改成

  public static BluetoothAddress GetBTMacAddress()
    {

        BluetoothRadio myRadio = BluetoothRadio.PrimaryRadio;
        if (myRadio == null)
        {
            //     Console.WriteLine("No radio hardware or unsupported software stack");
            return null;
        }

        return myRadio.LocalAddress;
    }