Find 32Feet BluetoothAddress of local Bluetooth adapter in C#

2.3k views Asked by At

I am trying to use code sample for my case from Pair bluetooth devices to a computer with 32feet .NET Bluetooth library

In this, xmashallax have mentioned local mac address. To get local address I am trying this-

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;
    }

I am getting error here "The requested address is not valid in its context" ErrorCode: AddressNotAvailable

Can you please suggest what should be the right way to get the mac address of current local PC?

1

There are 1 answers

1
Bit_Pulse On BEST ANSWER

Posting this answer for others who may face similar scenario.

Basically to create a Bluetooth endpoint you need a valid Bluetooth mac address of the adapter. To get the local Bluetooth mac address of local machine just use

BluetoothRadio.PrimaryRadio.LocalAddress

so the above code will need to be changed to

  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;
    }