I am trying to write an app for Android P using the Bluetooth HID device profile service to be used as a Bluetooth keyboard. According to the documentation I am supposed to use
There is a semi-official sample code available here: https://github.com/ginkage/wearmouse — actually by Google itself (see https://opensource.google.com/projects/wearmouse).
It is intended for using on Wear OS watches, but has everything you need to use it on a phone as well: proxy class for the profile, a callback class, HID Descriptor example, QoS, callbacks for cases like battery level inquiry from the host, etc.
The app itself is available at Google Play here: https://play.google.com/store/apps/details?id=com.ginkage.wearmouse (the app supports Android 8 as well, by the way, but that only works on watches, not phones).