Starting at the beginning, remove the Smart keyboard from your iPad - and carefully/gently clean the smart connector contacts on both the iPad and keyboard with a soft pencil eraser. Ensure that the contacts are cleaned of all dirt, dust and debris from the eraser before reconnecting the two.
If you can't connect your iPad to your Magic Keyboard, Smart Keyboard, or Smart Keyboard Folio - Apple Support
Also see:
If you see an ‘Accessory may not be supported’ alert - Apple Support
If you are using a Smart (folio) keyboard, the alert and or intermittent loss of keyboard connection is typical of a faulty folio keyboard. The electrical connections that pass though the flexible fabric hinges typically fracture over time, this usually resulting in errors and/or the on-screen notification that you may see.
The Smart folio keyboard itself is not repairable. The only remedy is replacement of the keyboard. If you are within your one-year limited warranty, you need to contact Apple Support or your retailer.
If you need assistance from Apple Support, you will find the Get Support link at top-right of this page. Alternatively, you can initiate a support case using the excellent Apple Support App. If not already installed on your device, the App can be downloaded from the App Store:
https://apps.apple.com/gb/app/apple-support/id1130498044
A further alternative is to visit the Genius Bar at your local Apple Store, where the technicians will be delighted to assist. When visiting the Genius Bar, a reservation is recommended:
Genius Bar Reservation and Apple Support Options - Apple