private bool Callback(IntPtr pvParam, ref BLUETOOTH_AUTHENTICATION_CALLBACK_PARAMS pAuthCallbackParams)
        {
            switch (pAuthCallbackParams.authenticationMethod)
            {
            case BluetoothAuthenticationMethod.Passkey:
            case BluetoothAuthenticationMethod.NumericComparison:
                BLUETOOTH_AUTHENTICATE_RESPONSE__NUMERIC_COMPARISON_PASSKEY_INFO nresponse = new BLUETOOTH_AUTHENTICATE_RESPONSE__NUMERIC_COMPARISON_PASSKEY_INFO
                {
                    authMethod          = pAuthCallbackParams.authenticationMethod,
                    bthAddressRemote    = pAuthCallbackParams.deviceInfo.Address,
                    numericComp_passkey = pAuthCallbackParams.Numeric_Value_Passkey
                };
                return(Win32Native.BluetoothSendAuthenticationResponseEx(IntPtr.Zero, ref nresponse) == 0);

            case BluetoothAuthenticationMethod.Legacy:
                BLUETOOTH_AUTHENTICATE_RESPONSE__PIN_INFO response = new BLUETOOTH_AUTHENTICATE_RESPONSE__PIN_INFO();
                response.authMethod       = pAuthCallbackParams.authenticationMethod;
                response.bthAddressRemote = pAuthCallbackParams.deviceInfo.Address;
                response.pinInfo.pin      = new byte[16];
                System.Text.Encoding.ASCII.GetBytes(_pin).CopyTo(response.pinInfo.pin, 0);
                response.pinInfo.pinLength = _pin.Length;

                return(Win32Native.BluetoothSendAuthenticationResponseEx(IntPtr.Zero, ref response) == 0);
            }

            return(false);
        }
        public Win32BluetoothAuthentication(BluetoothAddress address, string pin)
        {
            Address = address;
            _pin    = pin;
            BLUETOOTH_DEVICE_INFO device = BLUETOOTH_DEVICE_INFO.Create();

            device.Address = address;
            Win32Native.BluetoothGetDeviceInfo(IntPtr.Zero, ref device);
            _callback = new BluetoothAuthenticationCallbackEx(Callback);

            int result = Win32Native.BluetoothRegisterForAuthenticationEx(ref device, out _handle, _callback, IntPtr.Zero);
        }
        private bool disposedValue = false; // To detect redundant calls

        void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects).
                }

                Win32Native.BluetoothUnregisterAuthentication(_handle);

                disposedValue = true;
            }
        }