fix(bluetooth): auto-reconnect to paired devices after reboot
- Auto-set enableOnBoot=true when BT is enabled or device is paired,
so board restarts with BT on and can reconnect
- Make RADIO_STATE_ON handler non-exclusive: scan for HID Host,
start HID Device, SPP and MIDI all independently (was else-if)
- Improve HID Host auto-connect for RPA: direct addr match, name-match
fallback, and direct connect to stored identity using resolving list
- Persist addrType in PairedDevice file for more reliable reconnection
- Add verbose logging for loadAll/save to debug missing files
Fixes issue where board restart did not reconnect to previously
connected BT devices. Also preserves f94cc160 fullscreen flag feature.
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <Tactility/bluetooth/Bluetooth.h>
|
||||
#include <Tactility/bluetooth/BluetoothPairedDevice.h>
|
||||
#include <Tactility/bluetooth/BluetoothPrivate.h>
|
||||
#include <Tactility/bluetooth/BluetoothSettings.h>
|
||||
|
||||
#include <Tactility/Assets.h>
|
||||
#include <Tactility/Tactility.h>
|
||||
@@ -480,6 +481,8 @@ static void hidHostSubscribeNext(HidHostCtx& ctx) {
|
||||
|
||||
auto peer_addr = ctx.peerAddr;
|
||||
getMainDispatcher().dispatch([peer_addr] {
|
||||
// Ensure BT stays on after reboot so this keyboard can be re-found.
|
||||
settings::setEnableOnBoot(true);
|
||||
// Find name from cached scan results
|
||||
std::string name;
|
||||
{
|
||||
@@ -488,17 +491,34 @@ static void hidHostSubscribeNext(HidHostCtx& ctx) {
|
||||
if (r.addr == peer_addr) { name = r.name; break; }
|
||||
}
|
||||
}
|
||||
uint8_t cached_type = 0;
|
||||
bool has_cached_type = getCachedScanAddrType(peer_addr.data(), &cached_type);
|
||||
settings::PairedDevice device;
|
||||
device.addr = peer_addr;
|
||||
device.profileId = BT_PROFILE_HID_HOST;
|
||||
device.autoConnect = true;
|
||||
device.addrType = has_cached_type ? cached_type : 0;
|
||||
const auto addr_hex = settings::addrToHex(peer_addr);
|
||||
LOG_I(TAG, "HID host ready: saving device %s name='%s' cached_type=%d has_cached=%d",
|
||||
addr_hex.c_str(), name.c_str(), (int)cached_type, (int)has_cached_type);
|
||||
settings::PairedDevice existing;
|
||||
if (settings::load(addr_hex, existing)) {
|
||||
LOG_I(TAG, "Existing file found for %s, preserving autoConnect=%d", addr_hex.c_str(), (int)existing.autoConnect);
|
||||
device.autoConnect = existing.autoConnect;
|
||||
// Preserve existing addrType if we don't have a cached one
|
||||
if (!has_cached_type) {
|
||||
device.addrType = existing.addrType;
|
||||
}
|
||||
// Preserve stored name if scan didn't provide one
|
||||
if (name.empty() && !existing.name.empty()) {
|
||||
name = existing.name;
|
||||
}
|
||||
} else {
|
||||
LOG_I(TAG, "No existing file for %s, creating new", addr_hex.c_str());
|
||||
}
|
||||
device.name = name;
|
||||
settings::save(device);
|
||||
bool saved = settings::save(device);
|
||||
LOG_I(TAG, "Save result for %s: %d", addr_hex.c_str(), (int)saved);
|
||||
if (Device* dev = device_find_first_active_by_type(&BLUETOOTH_TYPE)) {
|
||||
BtEvent e = {};
|
||||
e.type = BT_EVENT_PROFILE_STATE_CHANGED;
|
||||
@@ -794,13 +814,22 @@ void hidHostConnect(const std::array<uint8_t, 6>& addr) {
|
||||
// Notify driver that a HID host central connection is starting.
|
||||
if (Device* dev = device_find_first_active_by_type(&BLUETOOTH_TYPE)) bluetooth_set_hid_host_active(dev, true);
|
||||
|
||||
// Look up the addr_type from the cached scan results.
|
||||
// Look up the addr_type from the cached scan results, or from persisted storage.
|
||||
// For RPA devices we may have cached RPA type, but we want to connect using identity.
|
||||
// Prefer cached scan type for direct scan-match, fallback to stored file's addrType.
|
||||
ble_addr_t ble_addr = {};
|
||||
ble_addr.type = BLE_ADDR_PUBLIC;
|
||||
std::memcpy(ble_addr.val, addr.data(), 6);
|
||||
uint8_t addr_type = 0;
|
||||
if (getCachedScanAddrType(addr.data(), &addr_type)) {
|
||||
ble_addr.type = addr_type;
|
||||
} else {
|
||||
// Try persisted addrType if available
|
||||
const auto hex = settings::addrToHex(addr);
|
||||
settings::PairedDevice stored;
|
||||
if (settings::load(hex, stored)) {
|
||||
ble_addr.type = stored.addrType;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t own_addr_type;
|
||||
@@ -847,34 +876,58 @@ bool hidHostGetConnectedPeer(std::array<uint8_t, 6>& addr_out) {
|
||||
void autoConnectHidHost() {
|
||||
if (hidHostIsConnected()) return;
|
||||
|
||||
// Connect to the first saved HID host peer that appeared in the last scan.
|
||||
// cacheScanAddr() is populated during scanning so addr_type is available for ble_gap_connect.
|
||||
// Gather all stored peers that want auto-connect as HID host (central).
|
||||
auto all_peers = settings::loadAll();
|
||||
std::vector<settings::PairedDevice> auto_peers;
|
||||
for (const auto& p : all_peers) {
|
||||
if (p.autoConnect && p.profileId == BT_PROFILE_HID_HOST) {
|
||||
auto_peers.push_back(p);
|
||||
}
|
||||
}
|
||||
if (auto_peers.empty()) return;
|
||||
|
||||
auto scan = getScanResults();
|
||||
|
||||
// 1. Direct address match (public address devices, most keyboards).
|
||||
// cacheScanAddr() is populated during scanning so addr_type is available for ble_gap_connect.
|
||||
for (const auto& r : scan) {
|
||||
settings::PairedDevice stored;
|
||||
if (settings::load(settings::addrToHex(r.addr), stored) &&
|
||||
stored.autoConnect &&
|
||||
stored.profileId == BT_PROFILE_HID_HOST) {
|
||||
LOG_I(TAG, "Auto-connecting HID host to %s", settings::addrToHex(r.addr).c_str());
|
||||
LOG_I(TAG, "Auto-connecting HID host to %s (direct match)", settings::addrToHex(r.addr).c_str());
|
||||
hidHostConnect(r.addr);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Device not in the last scan. If we have an autoConnect HID host peer, restart
|
||||
// scanning so we keep checking until the device powers back on.
|
||||
auto peers = settings::loadAll();
|
||||
for (const auto& peer : peers) {
|
||||
if (peer.autoConnect && peer.profileId == BT_PROFILE_HID_HOST) {
|
||||
if (Device* dev = device_find_first_active_by_type(&BLUETOOTH_TYPE)) {
|
||||
if (!bluetooth_is_scanning(dev)) {
|
||||
LOG_I(TAG, "Auto-connect HID host: device not in scan, retrying scan");
|
||||
bluetooth_scan_start(dev);
|
||||
}
|
||||
// 2. RPA / name fallback: some peripherals use Resolvable Private Addresses.
|
||||
// Their advertised address (RPA) does not equal the stored identity address,
|
||||
// so hex-lookup fails. If we see a scan result whose name matches a stored
|
||||
// auto-connect peer, attempt a direct connection to the stored identity address.
|
||||
// The controller's resolving list (populated from NVS IRK) will resolve the RPA.
|
||||
for (const auto& r : scan) {
|
||||
if (r.name.empty()) continue;
|
||||
for (const auto& stored : auto_peers) {
|
||||
if (!stored.name.empty() && stored.name == r.name) {
|
||||
LOG_I(TAG, "Auto-connecting HID host to %s via name match '%s' (RPA handling: scan=%s stored=%s)",
|
||||
settings::addrToHex(stored.addr).c_str(),
|
||||
r.name.c_str(),
|
||||
settings::addrToHex(r.addr).c_str(),
|
||||
settings::addrToHex(stored.addr).c_str());
|
||||
hidHostConnect(stored.addr);
|
||||
return;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Direct connect fallback: if device not in scan (or uses RPA without name in adv),
|
||||
// try to connect directly to the first stored auto peer. ble_gap_connect() will
|
||||
// internally scan and use the resolving list to match RPA to identity.
|
||||
// This also covers the case where the keyboard is powered off and later on.
|
||||
LOG_I(TAG, "Auto-connect HID host: %d auto peer(s) not in scan, trying direct connect to %s",
|
||||
(int)auto_peers.size(), settings::addrToHex(auto_peers[0].addr).c_str());
|
||||
hidHostConnect(auto_peers[0].addr);
|
||||
}
|
||||
|
||||
} // namespace tt::bluetooth
|
||||
|
||||
Reference in New Issue
Block a user