hid_keyboard_demo: Reconnect to last bonded device on startup

This commit is contained in:
Matthias Ringwald 2025-03-17 13:23:59 +01:00
parent c8ea651435
commit 32b985fcd4
2 changed files with 36 additions and 3 deletions

View File

@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- POSIX: support error condition for file descriptors in btstack_run_loop
- Linux: HCI Transport for Linux HCI Kernel Socket
- Linux: Audio sink implementation for Linux ALSA
- HID Keyboard Demo: Reconnect to last bonded device on startup
### Fixed
- HCI: fix incorrect assert/regression in num completed packets handling introduced in 6ec1ca0

View File

@ -47,6 +47,7 @@
// *****************************************************************************
#include <btstack_tlv.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@ -70,6 +71,9 @@
static uint16_t host_max_latency = 1600;
static uint16_t host_min_timeout = 3200;
// TAG to store remote device address in TLV
#define TLV_TAG_HIDR ((((uint32_t) 'H') << 24 ) | (((uint32_t) 'I') << 16) | (((uint32_t) 'D') << 8) | 'R')
#define REPORT_ID 0x01
// close to USB HID Specification 1.1, Appendix B.1
@ -199,6 +203,10 @@ static bd_addr_t device_addr;
static const char * device_addr_string = "BC:EC:5D:E6:15:03";
#endif
// used to store remote device in TLV
static const btstack_tlv_t * btstack_tlv_singleton_impl;
static void * btstack_tlv_singleton_context;
static enum {
APP_BOOTING,
APP_NOT_CONNECTED,
@ -275,6 +283,20 @@ static void queue_character(char character){
// Demo Application
static void hid_start_reconnect(void){
// check if we have a bonded device
// used to store remote device in TLV
if (btstack_tlv_singleton_impl){
int len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, TLV_TAG_HIDR, (uint8_t *) &device_addr, sizeof(bd_addr_t));
if (len == sizeof(device_addr)){
printf("Bonded, reconnect to device with address %s ...\n", bd_addr_to_str(device_addr));
hid_device_connect(device_addr, &hid_cid);
return;
}
}
printf("Please connect to HID Keyboard Demo from smartphone or computer.\n");
}
#ifdef HAVE_BTSTACK_STDIN
// On systems with STDIN, we can directly type on the console
@ -336,6 +358,8 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * pack
case BTSTACK_EVENT_STATE:
if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
app_state = APP_NOT_CONNECTED;
btstack_tlv_get_instance(&btstack_tlv_singleton_impl, &btstack_tlv_singleton_context);
hid_start_reconnect();
break;
case HCI_EVENT_USER_CONFIRMATION_REQUEST:
@ -357,10 +381,18 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * pack
}
app_state = APP_CONNECTED;
hid_cid = hid_subevent_connection_opened_get_hid_cid(packet);
#ifdef HAVE_BTSTACK_STDIN
printf("HID Connected, please start typing...\n");
hid_subevent_connection_opened_get_bd_addr(packet, device_addr);
// store device as bonded
if (btstack_tlv_singleton_impl){
btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, TLV_TAG_HIDR, (const uint8_t *) &device_addr, sizeof(bd_addr_t));
}
#ifdef HAVE_BTSTACK_STDIN
printf("HID Connected to %s, please start typing...\n", bd_addr_to_str(device_addr));
#else
printf("HID Connected, sending demo text...\n");
printf("HID Connected to %s, sending demo text...\n", bd_addr_to_str(device_addr));
demo_text_timer_handler(NULL);
#endif
break;