The Magic Keyboard for iPad makes connection with iPad using the Smart Connector electrical contacts on the rear of the iPad; this keyboard does not utilise Bluetooth.
Unless the iPad connects correctly via the Smart Connector, the keyboard and trackpad will not work; similarly, you will not be able to charge the iPad from the Magic Keyboard USB-C connector.
Check the connector pins on the Magic Keyboard. Are they damaged - or perhaps covered? Likewise, check the corresponding connector pads on the back of the iPad.
Use your Smart Keyboard Folio or Smart Keyboard with your iPad – Apple Support