From 02658b0d2ffd5fc3050fd24ba302e0f1165b54ec Mon Sep 17 00:00:00 2001 From: Ahmed Hussein Date: Thu, 12 Nov 2026 13:09:44 +0200 Subject: [PATCH] fix(windows): fall back to an RFCOMM channel when UUID connect fails Some paired SPP devices make Winsock's own service-class resolution fail with WSAEADDRNOTAVAIL (10049) although their serial service works. On that error only, retry on the channel an SDP query reports and, for SPP, on RFCOMM channel 1. Other failures end as before. The connectionFailed message now carries the WSA error codes, and its details add wsaError. Tested on Windows 11 with a serial thermal printer that failed with 10049 before the fix and connects and prints after it. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 13 ++ windows/flutter_classic_bluetooth_plugin.cpp | 135 +++++++++++++++---- 2 files changed, 119 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ffef9dc..a676502 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,16 @@ +## 1.7.4 + +### Fixed + +- **Some paired SPP printers never connected on Windows.** `connect()` asked + Winsock to resolve the service UUID itself, and with some devices that + lookup fails with `WSAEADDRNOTAVAIL` (10049) even though the device is + paired and its serial service works: Windows had created a COM port for it, + and a socket opened on RFCOMM channel 1 printed. On that error only, + `connect()` now retries on the channel an SDP query reports and, for SPP, + on channel 1. Other failures end as before. The `connectionFailed` message + now carries the WSA error codes, and its details add `wsaError`. + ## 1.7.3 ### Fixed diff --git a/windows/flutter_classic_bluetooth_plugin.cpp b/windows/flutter_classic_bluetooth_plugin.cpp index 865c00f..0b5eb82 100644 --- a/windows/flutter_classic_bluetooth_plugin.cpp +++ b/windows/flutter_classic_bluetooth_plugin.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -31,6 +32,76 @@ using flutter::EncodableMap; using flutter::EncodableValue; namespace { +// Serial Port Profile service class, the one serial printers expose. +constexpr GUID kSppServiceClass = { + 0x00001101, 0x0000, 0x1000, {0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}}; + +// Asks SDP which RFCOMM channel [service] runs on. Returns 0 when the lookup +// fails or the record carries no channel. +ULONG LookupRfcommChannel(BTH_ADDR address, const GUID& service) { + wchar_t context[32]; + swprintf_s(context, L"(%02X:%02X:%02X:%02X:%02X:%02X)", + static_cast((address >> 40) & 0xFF), + static_cast((address >> 32) & 0xFF), + static_cast((address >> 24) & 0xFF), + static_cast((address >> 16) & 0xFF), + static_cast((address >> 8) & 0xFF), + static_cast(address & 0xFF)); + WSAQUERYSETW query = {}; + query.dwSize = sizeof(query); + query.lpServiceClassId = const_cast(&service); + query.dwNameSpace = NS_BTH; + query.lpszContext = context; + HANDLE lookup = nullptr; + if (WSALookupServiceBeginW(&query, LUP_FLUSHCACHE | LUP_RETURN_ADDR, + &lookup) != 0) { + return 0; + } + ULONG channel = 0; + alignas(WSAQUERYSETW) BYTE buffer[4096]; + DWORD size = sizeof(buffer); + auto* result = reinterpret_cast(buffer); + if (WSALookupServiceNextW(lookup, LUP_RETURN_ADDR, &size, result) == 0 && + result->lpcsaBuffer != nullptr && + result->lpcsaBuffer->RemoteAddr.lpSockaddr != nullptr) { + channel = reinterpret_cast( + result->lpcsaBuffer->RemoteAddr.lpSockaddr)->port; + } + WSALookupServiceEnd(lookup); + return channel; +} + +// One RFCOMM connect attempt. Port 0 lets Windows resolve [service] through +// SDP; any other port connects to that channel directly. On failure, returns +// INVALID_SOCKET and stores the WSA error in [error]. +SOCKET TryRfcommConnect(BTH_ADDR address, const GUID& service, ULONG port, + bool secure, int* error) { + SOCKET sock = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM); + if (sock == INVALID_SOCKET) { + *error = WSAGetLastError(); + return INVALID_SOCKET; + } + // Honor the secure flag: request an authenticated (and thus encrypted) + // RFCOMM link before connecting. Best-effort, ignored if unsupported. + if (secure) { + ULONG auth = TRUE; + setsockopt(sock, SOL_RFCOMM, SO_BTH_AUTHENTICATE, + reinterpret_cast(&auth), sizeof(auth)); + } + SOCKADDR_BTH addr = {}; + addr.addressFamily = AF_BTH; + addr.btAddr = address; + addr.serviceClassId = port == 0 ? service : GUID{}; + addr.port = port; + if (connect(sock, reinterpret_cast(&addr), sizeof(addr)) == + SOCKET_ERROR) { + *error = WSAGetLastError(); + closesocket(sock); + return INVALID_SOCKET; + } + return sock; +} + // Safely extract an int id from an EncodableValue, tolerating the codec // delivering it as either int32_t or int64_t. Avoids throwing std::get. bool ExtractInt(const EncodableValue& value, int* out) { @@ -521,38 +592,44 @@ void FlutterClassicBluetoothPlugin::HandleConnect( auto result_ptr = std::shared_ptr>(std::move(result)); std::weak_ptr weak_dispatcher = dispatcher_; std::thread([this, bth_addr, uuid, address, secure, result_ptr, weak_dispatcher]() { - SOCKET sock = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM); - if (sock == INVALID_SOCKET) { - auto d = weak_dispatcher.lock(); - if (d) d->Post([result_ptr, address]() { - result_ptr->Error("connectionFailed", "Failed to create socket", - EncodableValue(EncodableMap{ - {EncodableValue("address"), EncodableValue(address)}})); - }); - return; - } - - // Honor the secure flag: request an authenticated (and thus encrypted) - // RFCOMM link before connecting. Best-effort, ignored if unsupported. - if (secure) { - ULONG auth = TRUE; - setsockopt(sock, SOL_RFCOMM, SO_BTH_AUTHENTICATE, - reinterpret_cast(&auth), sizeof(auth)); + // Some devices (seen on a cheap serial thermal printer) make Windows' + // own service-class resolution fail with WSAEADDRNOTAVAIL although the + // service works. Only for that error, retry on the channel SDP reports, + // and for SPP fall back to channel 1, where serial printers listen. Other + // errors (out of range, refused) fail as before, without the extra wait. + const GUID service = StringToGuid(uuid); + int uuid_error = 0; + int channel_error = 0; + SOCKET sock = TryRfcommConnect(bth_addr, service, 0, secure, &uuid_error); + if (sock == INVALID_SOCKET && uuid_error == WSAEADDRNOTAVAIL) { + std::vector channels; + const ULONG sdp_channel = LookupRfcommChannel(bth_addr, service); + if (sdp_channel != 0) channels.push_back(sdp_channel); + if (sdp_channel != 1 && IsEqualGUID(service, kSppServiceClass)) { + channels.push_back(1); + } + for (ULONG channel : channels) { + sock = TryRfcommConnect(bth_addr, service, channel, secure, + &channel_error); + if (sock != INVALID_SOCKET) break; + } } - SOCKADDR_BTH addr = {}; - addr.addressFamily = AF_BTH; - addr.btAddr = bth_addr; - addr.serviceClassId = StringToGuid(uuid); - addr.port = 0; - - if (connect(sock, (SOCKADDR*)&addr, sizeof(addr)) == SOCKET_ERROR) { - closesocket(sock); + if (sock == INVALID_SOCKET) { auto d = weak_dispatcher.lock(); - if (d) d->Post([result_ptr, address]() { - result_ptr->Error("connectionFailed", "Failed to connect", - EncodableValue(EncodableMap{ - {EncodableValue("address"), EncodableValue(address)}})); + if (d) d->Post([result_ptr, address, uuid_error, channel_error]() { + std::string message = + "Failed to connect (WSA error " + std::to_string(uuid_error); + if (channel_error != 0) { + message += ", WSA error " + std::to_string(channel_error) + + " on the channel fallback"; + } + message += ")"; + result_ptr->Error( + "connectionFailed", message, + EncodableValue(EncodableMap{ + {EncodableValue("address"), EncodableValue(address)}, + {EncodableValue("wsaError"), EncodableValue(uuid_error)}})); }); return; }