I have this problem as well. My iPad Pro is about 20 months old as is the keyboard. No problems until the last month or so, then failing to connect, not being recognised etc. I contacted Apple support and was eventually told to go to my dealer - which I did and to be fair they gave me a new keyboard with no quibbles.
Only problem is that although the new keyboard is recognised, it is very unstable in that it keeps disconnecting then reconnecting. Detaching and re-attaching the keyboard seems to work, but nothing completely solves the problem. in addition when the iPad is in my bag it will go into a cycle of disconnectign and reconnecting (I realised this when I could here the “chime” noise you get when you plug in the power source going off over and over again - and this is draining the iPad battery.
It’s by far the most inconvenient problem I have ever had with an Apple product and I will going back to the dealer this week.
Unless and until there is a proper solution for this I would really urge people considering buying a smart keyboard to think again - the product is not fit for purpose. Unless you want a really expensive iPad stand that drains your battery of course....