T-vK / ESP32-BLE-Keyboard

Bluetooth LE Keyboard library for the ESP32 (Arduino IDE compatible)
2.41k stars 404 forks source link

Work with ESP32-BLE-Mouse? #14

Open blackketter opened 4 years ago

blackketter commented 4 years ago

I can't seem to get the ESP32-BLE-Keyboard and ESP32-BLE-Mouse to work together in the same sketch.

I've made some modifications to BleConnectionStatus to have unique BleMouseConnectionStatus and BleKeyboardConnectionStatus files and classes, but it seems that only one of the endpoints is detected on the host. Any tips on how to make them work together? Happy to submit a patch once I've got it working...

T-vK commented 4 years ago

Good question. I haven't tried that yet, but it would indeed be nice if those libraries worked together. I'm not sure if the underlying Bluetooth library even allows for multiple devices at the same time, but what you could do is merge the two _hidReportDescriptors. Then you'd have one Bluetooth device with keyboard and mouse capabilities. But you'd also have to merge the mouse and keyboard methods and adjust things for the new hid descriptor...

blackketter commented 4 years ago

It seems that's the case. I'm working on trying to merge them now. What would you think of a combo library with classes for different combinations to avoid code duplication?

ankitpatle commented 4 years ago

@blackketter Any luck at testing after merge??

blackketter commented 4 years ago

Sorry, I’m out of town for a couple of days. When I left it was crashing, I’ll continue to poke at it when I get back

On Feb 15, 2020, at 9:33 AM, Ankit Patle notifications@github.com wrote:

 @blackketter Any luck at testing after merge??

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

T-vK commented 4 years ago

Well, the two libraries have been written with the intention of creating drop-in replacements for the original usb "keyboard" and "mouse" libraries. I also feel like they should be maintained in separate repositories. It might however be a good idea to create a common submodule for all that redundant BLE stuff.

In case it is only possible to get these to work together using a common hid descriptor, we should, in my opinion, create a separate repository for that because there may be edge cases where having one device show up as multiple devices causes a problem. Ideally we'd get the underlying BLE library patched to support multiple devices at the same time, if it doesn't already.

ankitpatle commented 4 years ago

So, I was looking in the bluetooth descriptor IDs and from this document, got these,

Descriptor type: 0000 = Keyboard 0001 = Game Pad 0010 = Mouse 0011 = COMBO 0100 = JOYSTICK 0101 = DIGITIZER 0110 = SENSOR 0111 = USE CFG 1XXX = Reserved

Some keyboard+mouse devices use the Combo Descriptor. So i think maybe we can use that. I will check that once i have some free time from my exams.

I should add that, the document is for RN-41/42 modules. But my point is that we can have a Combo Descriptor.

blackketter commented 4 years ago

So, as an experiment, I've forked this library and made a combo mouse and keyboard library here: https://github.com/blackketter/ESP32-BLE-Combo

Not sure if this is the right approach long-term but it seems to be working well on my Mac and iPhone. Would love feedback. This could be extended to add joystick and other descriptor types.

I didn't use the COMBO descriptor listed above, just concatenated the two descriptors and created a separate mouse class that uses the keyboard class' bluetooth interface.

I did notice that the Arduino USB mouse and keyboard libraries have globally defined Keyboard and Mouse objects, I'm not sure that's the right approach here, but I thought I'd note it.

lemmingDev commented 4 years ago

Hey @blackketter and @T-vK

Am hacking around both the mouse library and the combo library to turn the mouse into an absolute mouse for making a Bluetooth lightgun (such as the wired ones here https://github.com/samuelballantyne/IR-Light-Gun/tree/master/)

Converted the ESP32-BLE-Mouse library no problem (SAMCO lightgun 1.1), but would now like to convert the ESP32-BLE-Combo library too to have extra functionality (SAMCO lightgun 2.0), but am getting stuck soooo close to getting it working perfectly.

The issue I'm having is that everything works perfectly when it's paired the first time after programming, however when it reconnects after being off, or after reprogramming, only the keyboard functionality works.

If I remove the BT device in Windows BT settings and the re-add it, it works fine again, until I turn BT off and off again and it reconnects, but then only the keyboard part of it works. Remove the device, re-add, and then it works fine again.

My question - would one of you be willing to look at the combo code for me to potentially fix?

I've also attached the fully working mouse-only version if you're interested.

ESP32-BLE-Combo.zip

ESP32-BLE-MouseAbs.zip

lemmingDev commented 4 years ago

Oh - just realised that the @blackketter ESP32-BLE-Combo library has the same issue

Can anyone confirm the issue?

If I turn the system Bluetooth off, and then on again, it works, but if I turn the ESP32 off and on again, only the keyboard part of it works, until I remove the BT combo device pairing from Windows, and re-add it...

chibisuke commented 4 years ago

@lemmingDev I can confirm this issue, but this is out of scope on this repository. However there is an easy fix to it. (https://github.com/blackketter/ESP32-BLE-Combo/pull/2)

lemmingDev commented 4 years ago

@lemmingDev I can confirm this issue, but this is out of scope on this repository. However there is an easy fix to it. (blackketter#2)

Forgot to say --> thanks heaps