Kindly you may need to reset your S Pen as below:
-From the notifications panel select S Pen remote.
-From the top right menu select Reset S Pen
Please be advised that the connection can be failed because of defects in the S pen, if it is not inserted properly or a magnet is nearby.
If a magnet is nearby (inside a cover case or a finger ring), please remove it and test again.
Additionally, please check the S pen is inserted properly.