如何使用 C# .NET "unpair"、"remove" Windows 中的蓝牙设备

How to "unpair", "remove" Bluetooth device in Windows with C# .NET

我遇到了问题。我需要从 windows 取消配对或移除蓝牙设备。 在这里,我有我的 phone Redmi 配对

而且我需要取消配对,所以基本上我想达到与按下 "Remove device" 按钮相同的效果

我试过了,但它对我不起作用,因为这个解决方案断开了蓝牙设备,但它仍然保持配对:How to disconnect a bluetooth device from C# .Net in Win7

我正在使用 C# WPF 和 InTheHand 库进行配对,但它没有取消配对功能

我如何实现我的目标?谢谢

要取消对经典蓝牙设备的验证,您必须调用 BluetoothRemoveDevice 函数。

对于 .NET 可以按如下方式导入

[StructLayout(LayoutKind.Explicit)]
struct BLUETOOTH_ADDRESS
{
  [FieldOffset(0)]
  [MarshalAs(UnmanagedType.I8)]
  public Int64 ullLong;
  [FieldOffset(0)]
  [MarshalAs(UnmanagedType.U1)]
  public Byte rgBytes_0;
  [FieldOffset(1)]
  [MarshalAs(UnmanagedType.U1)]
  public Byte rgBytes_1;
  [FieldOffset(2)]
  [MarshalAs(UnmanagedType.U1)]
  public Byte rgBytes_2;
  [FieldOffset(3)]
  [MarshalAs(UnmanagedType.U1)]
  public Byte rgBytes_3;
  [FieldOffset(4)]
  [MarshalAs(UnmanagedType.U1)]
  public Byte rgBytes_4;
  [FieldOffset(5)]
  [MarshalAs(UnmanagedType.U1)]
  public Byte rgBytes_5;
};

[DllImport("BluetoothAPIs.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.U4)]
static extern UInt32 BluetoothRemoveDevice(
  [param: In, Out] ref BLUETOOTH_ADDRESS pAddress);

调用方法如下:

UInt32 Unpair(Int64 Address)
{
  BLUETOOTH_ADDRESS Addr = new BLUETOOTH_ADDRESS();
  Addr.ullLong = Address;
  return BluetoothRemoveDevice(ref Addr);
}

请注意,此功能仅允许取消配对经典蓝牙设备。要取消配对蓝牙 LE 设备,您必须使用基于 WinRT 的其他方式。