1

I faced a problem. I need to unpair, or remove bluetooth device from windows. Here, I have my phone Redmi paired

enter image description here

And I need to unpair it, so basically I want to achieve the same effect as pressing "Remove device" button

I tried this, but it didn't work for me, since this solution disconnects bluetooth device, but it still stays paired: How to disconnect a bluetooth device from C# .Net in Win7

I am using C# WPF and InTheHand library for pairing, but it does not have unpair functionality

How do I achieve my goal? Thank

IntegerOverlord
  • 1,477
  • 1
  • 14
  • 33
  • I do not know how it is implemented in 32feet but you have to call [BluetoothRemoveDevice](https://learn.microsoft.com/en-us/windows/desktop/api/bluetoothapis/nf-bluetoothapis-bluetoothremovedevice) function. – Mike Petrichenko Dec 12 '18 at 16:30
  • @MikePetrichenko thanks, I will try that. In InTheHand lib (as I understand the same thing as 32feet) there is no remove device function Thanks a lot – IntegerOverlord Dec 12 '18 at 19:24
  • @MikePetrichenko how exactly should I call it? Have not much of experience with these things – IntegerOverlord Dec 13 '18 at 09:53

1 Answers1

4

To unapir classic Bluetooth device you have to call BluetoothRemoveDevice function.

For .NET it can be imported as below

[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);

Here is how to call it:

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

Please not that this function allows to unpair Classic Bluetooth devices only. To unpair Bluetooth LE devices you have to use other way based on WinRT.

  • This actually solved a huge problem for me... thanks a lot! edit: I put this to use in a very simple program, full source's on [GitHub](https://github.com/lflfm/btRemover), for those who may need it. – LFLFM Feb 21 '19 at 11:56