If you would like, you can use my example. I managed to make it work for the most part, except disconnecting.
I am sorry for not having proper documentation.
The workaround for the disconnect function is to set a flag. For example:
var isConnected = False
When headband connects or disconnects you need to change value of a flag. It is not ideal solution, but it worked surprisingly fine.