简体   繁体   中英

Find 32Feet BluetoothAddress of local Bluetooth adapter in C#

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM