Reputation: 1637
I faced a problem. I need to unpair, or remove bluetooth device from windows. Here, I have my phone Redmi paired
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
Upvotes: 1
Views: 3796
Reputation: 1024
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.
Upvotes: 4