【问题标题】:UWP GET my computer local bluetooth mac addressUWP GET我的电脑本地蓝牙mac地址
【发布时间】:2021-04-06 17:50:26
【问题描述】:

作为主题。我需要获取电脑的本地mac地址,找不到api。

【问题讨论】:

标签: uwp bluetooth


【解决方案1】:

UWP GET我的电脑本地蓝牙mac地址

恐怕你不能用UWP api获取蓝牙电台的mac地址,根据你的要求,我们建议你使用win32 api来获取它,也就是在UWP桌面扩展中调用win32 api。而32feet.NET nuget 可以用来获取蓝牙mac地址。

代码

public static BluetoothAddress GetBTMacAddress()
{

    BluetoothRadio myRadio = BluetoothRadio.PrimaryRadio;
    if (myRadio == null)
    {
       
        return null;
    }

    return myRadio.LocalAddress;
}

我明白了,从 UWP api 获取的 BluetoothAddress 是十进制数。以上是十六进制数,它们是相同的值。我使用以下内容转换为十六进制。感谢@Mike Petrichenko 在下面的评论。

 var adapter = await BluetoothAdapter.GetDefaultAsync();
 var bleAddress = Convert.ToString(Convert.ToInt64(adapter.BluetoothAddress.ToString()), 16).ToUpper();

【讨论】:

  • 可以使用我上面描述的类(蓝牙适配器)。当然,它也可以使用 Win API 和蓝牙框架来完成 (btframework.com/bluetoothframework.htm)
  • 我已更新案例回复,感谢 cmets。
  • BluetoothAdapter.BluetoothAddress 已经是 Int64 值(ulong),所以它可以很简单:adapter.BluetoothAddress.ToString("X12")(MAC 长度为 6 个字节,12 个十六进制数字)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-27
  • 1970-01-01
  • 2015-12-30
  • 2015-04-24
  • 2020-01-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多