I had problems pairing my Apple bluetooth keyboard with my MacBook at home, but I got a clue from a forum discussion.
The trick was to hold the power button on the keyboard and to keep holding it until the MacBook presented a pairing code. When I just held the power button long enough for the green light to start blinking, I had trouble completing the pair.
This keyboard paired effortlessly when it was outside my home, so I presume there was another device (perhaps my son’s laptop) that somehow would interfere with the pairing of the MacBook with the keyboard.