Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

classic bluetooth hid device doesn't reconnect (IDFGH-8662) #10107

Closed
3 tasks done
lishuai199502 opened this issue Nov 4, 2022 · 7 comments
Closed
3 tasks done

classic bluetooth hid device doesn't reconnect (IDFGH-8662) #10107

lishuai199502 opened this issue Nov 4, 2022 · 7 comments
Assignees
Labels
Resolution: Done Issue is done internally Status: Done Issue is done internally

Comments

@lishuai199502
Copy link

Answers checklist.

  • I have read the documentation ESP-IDF Programming Guide and the issue is not addressed there.
  • I have updated my IDF branch (master or release) to the latest version and checked that the issue is present there.
  • I have searched the issue tracker for a similar issue and not found a similar issue.

General issue report

Hi,i just use classic bluetooth with the example,esp-idf-v4.4.2\examples\bluetooth\bluedroid\classic_bt\bt_hid_mouse_device,
when I connect my android phone or my pc, it works well, but when I off my phone, the bluetooth disconnect,
then I on my phone, it doesn't reconnect to esp32.
The same problem occurs in my PC,
I want to know how to deal with this.

@espressif-bot espressif-bot added the Status: Opened Issue is new label Nov 4, 2022
@github-actions github-actions bot changed the title classic bluetooth hid device doesn't reconnect classic bluetooth hid device doesn't reconnect (IDFGH-8662) Nov 4, 2022
@boblane1
Copy link
Collaborator

boblane1 commented Nov 7, 2022

Hi @lishuai199502

The ESP32 HID device set the SDP attribute HIDReconnectInitiate to True, it indicates that the HID device will be primarily responsible for connection re-establishment. So if you want to reconnect to your phone or PC, you can call the function esp_bt_hid_device_connect at the user specific frequency.

@lishuai199502
Copy link
Author

@boblane1 Thanks very much for your reply, I've tested the method your show me, and it works well. Now I wonder know, how to distinguish the disconnect mode. For example, I turned my phone bluetooth off, then I turn on, esp32 should re-connect, but when I disconnect bluetooth by click disconnect button, esp32 shouldn't re-connect to.

@boblane1
Copy link
Collaborator

@lishuai199502 ,

It seems that the Bluetooth stack can't distinguish the difference between the two procedures that you mentioned.

@lishuai199502
Copy link
Author

@boblane1 Hi,it's me again,Now I set ESP32 try to connect the last device after boot. But if the device is off, I want another device can connect ESP32.
I test like this, first I use my PC connect ESP32, then I turn off my PC BT, Now I use my phone to connect ESP32, it report incoming connections from different device, rejecting.
Could you tell me what should I do to solve this problem, hope my question didn't bother you.
Thanks.

@boblane1
Copy link
Collaborator

@lishuai199502 ,

According to the Bluetooth HID Profile version 1.0, when a cabled HID is plugged into a host by a user, the user creates both a “bond” between the devices and a 1:1 relationship. So, if you want the HID Device can be connected by other HID Host, you shall remove the virtually cabled device first. You can call esp_bt_hid_device_virtual_cable_unplug, but this will also remove the previous bonded HID Host information.

Please try the following patch.
test.zip

@lishuai199502
Copy link
Author

lishuai199502 commented Nov 18, 2022

@boblane1
Hi, my bro. I've tested the patch and it works well, but another problem occured.
I have a HID keyboard moudle, it works as this,
First, I pair my pc to the moudle and pair my phone to the module. After this, I turn off the three.
Second, I turn on my phone BT, when I turn on the moudle, it can connect to my phone automatically. Then I turn off them two.
Third, I turn on my PC BT, then turn on the moudle, also, it can connect automatically.
Forth, it confused me, I connect my phone to moudle, then I turn on my PC BT, and then I turn off my phone BT, the moudle also can connect to my PC automatically. By contrast, I connect my PC to the moudle, then turn on my phone BT, it cann't connect automatically.
It seems that, the PC will try to connect my moudle every time my PC BT turn on.
meanwhile, I found that, I turn off my moudle, when I turn on my PC BT, it shows connected firstly, after a moment, it shows paired. But the PC BT list show my ESP32 alwayes connected no matter I turn on or off my ESP32.

BT_state_change.mp4

I don't know what happened between my PC and my moudle.
The video below shows that I turn on my moudle first then I turn on my PC BT, it connected automatically.
In the video, I click the device means it connect successfully.

bt2.mp4

I want my ESP32 BT also can work like this , could give me a little more help.
Thanks my bro.

@boblane1
Copy link
Collaborator

@lishuai199502 ,

meanwhile, I found that, I turn off my moudle, when I turn on my PC BT, it shows connected firstly, after a moment, it shows paired. But the PC BT list show my ESP32 alwayes connected no matter I turn on or off my ESP32.

Like what I said before,

The ESP32 HID device set the SDP attribute HIDReconnectInitiate to True, it indicates that the HID device will be primarily responsible for connection re-establishment. So if you want to reconnect to your phone or PC, you can call the function esp_bt_hid_device_connect at the user specific frequency.

so if you want to the HID Host to initiate the reconnect procedure, you can change the attribute HIDReconnectInitiate to False.
You can change

result &= SDP_AddAttribute(handle, ATTR_ID_HID_RECONNECT_INITIATE, BOOLEAN_DESC_TYPE, 1, (uint8_t *)&bool_true);

to

result &= SDP_AddAttribute(handle, ATTR_ID_HID_RECONNECT_INITIATE, BOOLEAN_DESC_TYPE, 1, (uint8_t *)&bool_false);

in the function of HID_DevAddRecord in /esp-idf/components/bt/host/bluedroid/stack/hid/hidd_api.c.

The following attach is Bluetooth HID Profile version 1.0, you can learn some details about the implementation.
HID_SPEC_V10.pdf

@espressif-bot espressif-bot added Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally Resolution: Done Issue is done internally and removed Status: Opened Issue is new Resolution: NA Issue resolution is unavailable labels Dec 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Done Issue is done internally Status: Done Issue is done internally
Projects
None yet
Development

No branches or pull requests

3 participants