0

我正在尝试将代码示例用于我的案例,从 配对蓝牙设备到具有 32feet .NET 蓝牙库的计算机

在这里,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地址的正确方法?

4

1 回答 1

1

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

基本上要创建蓝牙端点,您需要适配器的有效蓝牙 mac 地址。要获取本地机器的本地蓝牙 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;
    }
于 2016-12-29T10:37:58.193 回答