diff --git a/packages/flutter_blue_plus_winrt/.gitignore b/packages/flutter_blue_plus_winrt/.gitignore new file mode 100644 index 00000000..dd5eb989 --- /dev/null +++ b/packages/flutter_blue_plus_winrt/.gitignore @@ -0,0 +1,31 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ +migrate_working_dir/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +#.vscode/ + +# Flutter/Dart/Pub related +# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock. +/pubspec.lock +**/doc/api/ +.dart_tool/ +.flutter-plugins-dependencies +/build/ +/coverage/ diff --git a/packages/flutter_blue_plus_winrt/.metadata b/packages/flutter_blue_plus_winrt/.metadata new file mode 100644 index 00000000..b8dd3d34 --- /dev/null +++ b/packages/flutter_blue_plus_winrt/.metadata @@ -0,0 +1,10 @@ +# This file tracks properties of this Flutter project. +# Used by Flutter tool to assess capabilities and perform upgrades etc. +# +# This file should be version controlled and should not be manually edited. + +version: + revision: "19074d12f7eaf6a8180cd4036a430c1d76de904e" + channel: "stable" + +project_type: package diff --git a/packages/flutter_blue_plus_winrt/CHANGELOG.md b/packages/flutter_blue_plus_winrt/CHANGELOG.md new file mode 100644 index 00000000..34a7a6f5 --- /dev/null +++ b/packages/flutter_blue_plus_winrt/CHANGELOG.md @@ -0,0 +1,61 @@ +## 0.0.18 +* Fix multithreading & connection stability (#5) + +## 0.0.17 +* Fix: Initialize `manufacturer_data` map correctly (#10) + +## 0.0.16 +* Fix: MTU was always 23 on Windows. Now reports negotiated MTU correctly. + +## 0.0.15 +* Fix: missing runtimeobject.lib (#7) + +## 0.0.14 +* Improve: `systemDevices` now discovers both paired and connected devices. +* Fix: Build error (unused variable warning) in `flutterRestart`. + +## 0.0.13 +* Feat: Add bonding support and improve connection logic. + +## 0.0.12 +* Fix: Resolve extensive compilation errors (C2220, C2065, C2653, C4456) caused by scope resolution and variable shadowing in GATT discovery logic. +* Fix: `readRssi` now handles cache misses gracefully by returning 0 instead of throwing an exception when RSSI is not in the cache. + +## 0.0.11 +* Refactor characteristic lookup and fix memory management. + +## 0.0.10 +* Fix: ensure memory resources (like RSSI cache) are properly cleared upon disconnection. + +## 0.0.9 +* Fix: "vector erase iterator outside range" crash by safely handling device closure to avoid synchronous vector modifications. + +## 0.0.8 +* Fix: ensure `connect` method waits for actual physical connection completion before returning a result. + +## 0.0.7 +* Fix: ensure `connect` returns `false` on failure instead of throwing an exception. +* Improvement: refined connection state waiting logic for better reliability. + +## 0.0.6 +* Fix: ensure `connect` method waits for actual physical connection completion before returning a result. +* Fix: enhanced thread safety during rapid connection and disconnection to prevent `vector erase` crashes. +* Improvement: more robust resource cleanup when connection attempts fail. + +## 0.0.5 +* Fix: compilation errors (C2220) and typos in method signatures. + +## 0.0.4 +* Fix: crash during connection/disconnection caused by invalid vector iterators. +* Feature: add `setOptions` implementation. + +## 0.0.3 +* Fix: ensure physical disconnection by explicitly closing all GATT services. +* Fix: potential race condition where a device might stay connected if disconnected during the connection process. +* Fix: properly clear characteristic and descriptor caches upon disconnection. + +## 0.0.2 +* Fix: a version issue with `flutter_blue_plus_platform_interface` #1 + +## 0.0.1 +* Initial release. diff --git a/packages/flutter_blue_plus_winrt/LICENSE b/packages/flutter_blue_plus_winrt/LICENSE new file mode 100644 index 00000000..d2dd1d7a --- /dev/null +++ b/packages/flutter_blue_plus_winrt/LICENSE @@ -0,0 +1,7 @@ +Copyright 2025 Himchan Park + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/packages/flutter_blue_plus_winrt/README.md b/packages/flutter_blue_plus_winrt/README.md new file mode 100644 index 00000000..5c894281 --- /dev/null +++ b/packages/flutter_blue_plus_winrt/README.md @@ -0,0 +1,15 @@ +# flutter_blue_plus_winrt + +The Windows implementation of [`flutter_blue_plus`][1]. + +## Usage + +This package is [endorsed][2], which means you can simply use `flutter_blue_plus` +normally. This package will be automatically included in your app when you do, +so you do not need to add it to your `pubspec.yaml`. + +However, if you `import` this package to use any of its APIs directly, you +should add it to your `pubspec.yaml` as usual. + +[1]: https://pub.dev/packages/flutter_blue_plus +[2]: https://flutter.dev/to/endorsed-federated-plugin diff --git a/packages/flutter_blue_plus_winrt/analysis_options.yaml b/packages/flutter_blue_plus_winrt/analysis_options.yaml new file mode 100644 index 00000000..1e432d55 --- /dev/null +++ b/packages/flutter_blue_plus_winrt/analysis_options.yaml @@ -0,0 +1,28 @@ +# This file configures the analyzer, which statically analyzes Dart code to +# check for errors, warnings, and lints. +# +# The issues identified by the analyzer are surfaced in the UI of Dart-enabled +# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be +# invoked from the command line by running `flutter analyze`. + +# The following line activates a set of recommended lints for Flutter apps, +# packages, and plugins designed to encourage good coding practices. +include: package:flutter_lints/flutter.yaml + +linter: + # The lint rules applied to this project can be customized in the + # section below to disable rules from the `package:flutter_lints/flutter.yaml` + # included above or to enable additional rules. A list of all available lints + # and their documentation is published at https://dart.dev/tools/linter-rules. + # + # Instead of disabling a lint rule for the entire project in the + # section below, it can also be suppressed for a single line of code + # or a specific dart file by using the `// ignore: name_of_lint` and + # `// ignore_for_file: name_of_lint` syntax on the line or in the file + # producing the lint. + rules: + avoid_print: false # Uncomment to disable the `avoid_print` rule + # prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule + +# Additional information about this file can be found at +# https://dart.dev/tools/analysis diff --git a/packages/flutter_blue_plus_winrt/lib/flutter_blue_plus_winrt.dart b/packages/flutter_blue_plus_winrt/lib/flutter_blue_plus_winrt.dart new file mode 100644 index 00000000..9c6dc515 --- /dev/null +++ b/packages/flutter_blue_plus_winrt/lib/flutter_blue_plus_winrt.dart @@ -0,0 +1,555 @@ +import 'dart:async'; +import 'dart:convert'; + +import 'package:flutter/foundation.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_blue_plus_platform_interface/flutter_blue_plus_platform_interface.dart'; + +final class FlutterBluePlusWinrt extends FlutterBluePlusPlatform { + @visibleForTesting + final methodChannel = const MethodChannel('flutter_blue_plus/methods'); + + var _initialized = false; + var _logLevel = LogLevel.none; + var _logColor = true; + + final _onAdapterStateChangedController = StreamController.broadcast(); + final _onBondStateChangedController = StreamController.broadcast(); + final _onCharacteristicReceivedController = StreamController.broadcast(); + final _onCharacteristicWrittenController = StreamController.broadcast(); + final _onConnectionStateChangedController = StreamController.broadcast(); + final _onDescriptorReadController = StreamController.broadcast(); + final _onDescriptorWrittenController = StreamController.broadcast(); + final _onDiscoveredServicesController = StreamController.broadcast(); + final _onMtuChangedController = StreamController.broadcast(); + final _onNameChangedController = StreamController.broadcast(); + final _onReadRssiController = StreamController.broadcast(); + final _onScanResponseController = StreamController.broadcast(); + final _onServicesResetController = StreamController.broadcast(); + final _onDetachedFromEngineController = StreamController.broadcast(); + + @override + Stream get onAdapterStateChanged { + return _onAdapterStateChangedController.stream; + } + + @override + Stream get onBondStateChanged { + return _onBondStateChangedController.stream; + } + + @override + Stream get onCharacteristicReceived { + return _onCharacteristicReceivedController.stream; + } + + @override + Stream get onCharacteristicWritten { + return _onCharacteristicWrittenController.stream; + } + + @override + Stream get onConnectionStateChanged { + return _onConnectionStateChangedController.stream; + } + + @override + Stream get onDescriptorRead { + return _onDescriptorReadController.stream; + } + + @override + Stream get onDescriptorWritten { + return _onDescriptorWrittenController.stream; + } + + @override + Stream get onDiscoveredServices { + return _onDiscoveredServicesController.stream; + } + + @override + Stream get onMtuChanged { + return _onMtuChangedController.stream; + } + + @override + Stream get onNameChanged { + return _onNameChangedController.stream; + } + + @override + Stream get onReadRssi { + return _onReadRssiController.stream; + } + + @override + Stream get onScanResponse { + return _onScanResponseController.stream; + } + + @override + Stream get onServicesReset { + return _onServicesResetController.stream; + } + + @override + Stream get onDetachedFromEngine { + return _onDetachedFromEngineController.stream; + } + + static void registerWith() { + FlutterBluePlusPlatform.instance = FlutterBluePlusWinrt(); + } + + @override + Future clearGattCache( + BmClearGattCacheRequest request, + ) async { + return await _invokeMethod( + 'clearGattCache', + request.remoteId.str, + ) == true; + } + + @override + Future connect( + BmConnectRequest request, + ) async { + return await _invokeMethod( + 'connect', + request.toMap(), + ) == true; + } + + @override + Future createBond( + BmCreateBondRequest request, + ) async { + return await _invokeMethod( + 'createBond', + request.toMap(), + ) == true; + } + + @override + Future disconnect( + BmDisconnectRequest request, + ) async { + return await _invokeMethod( + 'disconnect', + request.remoteId.str, + ) == true; + } + + @override + Future discoverServices( + BmDiscoverServicesRequest request, + ) async { + return await _invokeMethod( + 'discoverServices', + request.remoteId.str, + ) == true; + } + + @override + Future getAdapterName( + BmBluetoothAdapterNameRequest request, + ) async { + return BmBluetoothAdapterName( + adapterName: await _invokeMethod( + 'getAdapterName', + ), + ); + } + + @override + Future getAdapterState( + BmBluetoothAdapterStateRequest request, + ) async { + return BmBluetoothAdapterState.fromMap( + await _invokeMethod( + 'getAdapterState', + ), + ); + } + + @override + Future getBondState( + BmBondStateRequest request, + ) async { + return BmBondStateResponse.fromMap( + await _invokeMethod( + 'getBondState', + request.remoteId.str, + ), + ); + } + + @override + Future getBondedDevices( + BmBondedDevicesRequest request, + ) async { + return BmDevicesList.fromMap( + await _invokeMethod( + 'getBondedDevices', + ), + ); + } + + @override + Future getPhySupport( + PhySupportRequest request, + ) async { + return PhySupport.fromMap( + await _invokeMethod( + 'getPhySupport', + ), + ); + } + + @override + Future getSystemDevices( + BmSystemDevicesRequest request, + ) async { + // Windows implementation currently takes request.toMap(), platform interface default does not. + // Keeping request.toMap() for consistency with existing Windows code. + return BmDevicesList.fromMap( + await _invokeMethod( + 'getSystemDevices', + request.toMap(), + ), + ); + } + + @override + Future isSupported( + BmIsSupportedRequest request, + ) async { + return await _invokeMethod( + 'isSupported', + ) == true; + } + + @override + Future readCharacteristic( + BmReadCharacteristicRequest request, + ) async { + return await _invokeMethod( + 'readCharacteristic', + request.toMap(), + ) == true; + } + + @override + Future readDescriptor( + BmReadDescriptorRequest request, + ) async { + return await _invokeMethod( + 'readDescriptor', + request.toMap(), + ) == true; + } + + @override + Future readRssi( + BmReadRssiRequest request, + ) async { + return await _invokeMethod( + 'readRssi', + request.remoteId.str, + ) == true; + } + + @override + Future removeBond( + BmRemoveBondRequest request, + ) async { + return await _invokeMethod( + 'removeBond', + request.remoteId.str, + ) == true; + } + + @override + Future requestConnectionPriority( + BmConnectionPriorityRequest request, + ) async { + return await _invokeMethod( + 'requestConnectionPriority', + request.toMap(), + ) == true; + } + + @override + Future requestMtu( + BmMtuChangeRequest request, + ) async { + return await _invokeMethod( + 'requestMtu', + request.toMap(), + ) == true; + } + + @override + Future setLogLevel( + BmSetLogLevelRequest request, + ) async { + _logLevel = request.logLevel; + _logColor = request.logColor; + + return await _invokeMethod( + 'setLogLevel', + request.logLevel.index, + ) == true; + } + + @override + Future setNotifyValue( + BmSetNotifyValueRequest request, + ) async { + return await _invokeMethod( + 'setNotifyValue', + request.toMap(), + ) == true; + } + + @override + Future setOptions( + BmSetOptionsRequest request, + ) async { + return await _invokeMethod( + 'setOptions', + request.toMap(), + ) == true; + } + + @override + Future setPreferredPhy( + BmPreferredPhy request, + ) async { + return await _invokeMethod( + 'setPreferredPhy', + request.toMap(), + ) == true; + } + + @override + Future startScan( + BmScanSettings request, + ) async { + return await _invokeMethod( + 'startScan', + request.toMap(), + ) == true; + } + + @override + Future stopScan( + BmStopScanRequest request, + ) async { + return await _invokeMethod( + 'stopScan', + ) == true; + } + + @override + Future turnOff( + BmTurnOffRequest request, + ) async { + return await _invokeMethod( + 'turnOff', + ) == true; + } + + @override + Future turnOn( + BmTurnOnRequest request, + ) async { + return await _invokeMethod( + 'turnOn', + ) == true; + } + + @override + Future writeCharacteristic( + BmWriteCharacteristicRequest request, + ) async { + return await _invokeMethod( + 'writeCharacteristic', + request.toMap(), + ) == true; + } + + @override + Future writeDescriptor( + BmWriteDescriptorRequest request, + ) async { + return await _invokeMethod( + 'writeDescriptor', + request.toMap(), + ) == true; + } + + Future _invokeMethod( + String method, [ + dynamic arguments, + ]) async { + // initialize + await _initFlutterBluePlus(); + + // log args + if (_logLevel == LogLevel.verbose) { + var func = '<$method>'; + var args = arguments.toString(); + func = _logColor ? '\x1B[1;30m$func\x1B[0m' : func; + args = _logColor ? '\x1B[1;35m$args\x1B[0m' : args; + print('[FBP] $func args: $args'); + } + + // invoke + final out = await methodChannel.invokeMethod(method, arguments); + + // log result + if (_logLevel == LogLevel.verbose) { + var func = '($method)'; + var result = out.toString(); + func = _logColor ? '\x1B[1;30m$func\x1B[0m' : func; + result = _logColor ? '\x1B[1;33m$result\x1B[0m' : result; + print('[FBP] $func result: $result'); + } + + return out; + } + + Future _initFlutterBluePlus() async { + if (_initialized) { + return; + } + + _initialized = true; + + // set platform method handler + methodChannel.setMethodCallHandler(_methodCallHandler); + + // flutter restart - wait for all devices to disconnect + if ((await methodChannel.invokeMethod('flutterRestart')) != 0) { + await Future.delayed(const Duration(milliseconds: 50)); + while ((await methodChannel.invokeMethod('connectedCount')) != 0) { + await Future.delayed(const Duration(milliseconds: 50)); + } + } + } + + Future _methodCallHandler( + MethodCall call, + ) async { + // log result + if (_logLevel == LogLevel.verbose) { + var func = '[[ ${call.method} ]]'; + var result = switch (call.method) { + 'OnDiscoveredServices' => _prettyPrint(call.arguments), + _ => call.arguments.toString(), + }; + func = _logColor ? '\x1B[1;30m$func\x1B[0m' : func; + result = _logColor ? '\x1B[1;33m$result\x1B[0m' : result; + print('[FBP] $func result: $result'); + } + + // handle method call + switch (call.method) { + case 'OnAdapterStateChanged': + return _onAdapterStateChangedController.add( + BmBluetoothAdapterState.fromMap( + call.arguments, + ), + ); + case 'OnBondStateChanged': + return _onBondStateChangedController.add( + BmBondStateResponse.fromMap( + call.arguments, + ), + ); + case 'OnCharacteristicReceived': + return _onCharacteristicReceivedController.add( + BmCharacteristicData.fromMap( + call.arguments, + ), + ); + case 'OnCharacteristicWritten': + return _onCharacteristicWrittenController.add( + BmCharacteristicData.fromMap( + call.arguments, + ), + ); + case 'OnConnectionStateChanged': + return _onConnectionStateChangedController.add( + BmConnectionStateResponse.fromMap( + call.arguments, + ), + ); + case 'OnDescriptorRead': + return _onDescriptorReadController.add( + BmDescriptorData.fromMap( + call.arguments, + ), + ); + case 'OnDescriptorWritten': + return _onDescriptorWrittenController.add( + BmDescriptorData.fromMap( + call.arguments, + ), + ); + case 'OnDetachedFromEngine': + return _onDetachedFromEngineController.add( + BmDetachedFromEngineResponse(), + ); + case 'OnDiscoveredServices': + return _onDiscoveredServicesController.add( + BmDiscoverServicesResult.fromMap( + call.arguments, + ), + ); + case 'OnMtuChanged': + return _onMtuChangedController.add( + BmMtuChangedResponse.fromMap( + call.arguments, + ), + ); + case 'OnNameChanged': + return _onNameChangedController.add( + BmNameChanged.fromMap( + call.arguments, + ), + ); + case 'OnReadRssi': + return _onReadRssiController.add( + BmReadRssiResult.fromMap( + call.arguments, + ), + ); + case 'OnScanResponse': + return _onScanResponseController.add( + BmScanResponse.fromMap( + call.arguments, + ), + ); + case 'OnServicesReset': + return _onServicesResetController.add( + BmBluetoothDevice.fromMap( + call.arguments, + ), + ); + } + } + + String _prettyPrint( + dynamic data, + ) { + if (data is Map || data is List) { + return const JsonEncoder.withIndent(' ').convert(data); + } else { + return data.toString(); + } + } +} diff --git a/packages/flutter_blue_plus_winrt/pubspec.yaml b/packages/flutter_blue_plus_winrt/pubspec.yaml new file mode 100644 index 00000000..b0a87a47 --- /dev/null +++ b/packages/flutter_blue_plus_winrt/pubspec.yaml @@ -0,0 +1,26 @@ +name: flutter_blue_plus_winrt +description: Windows implementation of the flutter_blue_plus plugin. +version: 0.0.20 +homepage: https://github.com/chan150/flutter_blue_plus_winrt + +environment: + sdk: ^3.0.0 + flutter: '>=3.7.0' + +dependencies: + flutter: + sdk: flutter + flutter_blue_plus_platform_interface: ">=9.0.0" + +dev_dependencies: + flutter_test: + sdk: flutter + flutter_lints: + +flutter: + plugin: + implements: flutter_blue_plus + platforms: + windows: + pluginClass: FlutterBluePlusPlugin + dartPluginClass: FlutterBluePlusWinrt diff --git a/packages/flutter_blue_plus_winrt/windows/.gitignore b/packages/flutter_blue_plus_winrt/windows/.gitignore new file mode 100644 index 00000000..b3eb2be1 --- /dev/null +++ b/packages/flutter_blue_plus_winrt/windows/.gitignore @@ -0,0 +1,17 @@ +flutter/ + +# Visual Studio user-specific files. +*.suo +*.user +*.userosscache +*.sln.docstates + +# Visual Studio build-related files. +x64/ +x86/ + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ diff --git a/packages/flutter_blue_plus_winrt/windows/CMakeLists.txt b/packages/flutter_blue_plus_winrt/windows/CMakeLists.txt new file mode 100644 index 00000000..2dcbb3b4 --- /dev/null +++ b/packages/flutter_blue_plus_winrt/windows/CMakeLists.txt @@ -0,0 +1,73 @@ +# The Flutter tooling requires that developers have a version of Visual Studio +# installed that includes CMake 3.14 or later. You should not increase this +# version, as doing so will cause the plugin to fail to compile for some +# customers of the plugin. +cmake_minimum_required(VERSION 3.14) + +# Project-level configuration. +set(PROJECT_NAME "flutter_blue_plus_winrt") +project(${PROJECT_NAME} LANGUAGES CXX) + +# Enable C++20 and coroutines +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +# Explicitly opt in to modern CMake behaviors to avoid warnings with recent +# versions of CMake. +cmake_policy(VERSION 3.14...3.25) + +# This value is used when generating builds using this plugin, so it must +# not be changed +set(PLUGIN_NAME "flutter_blue_plus_winrt_plugin") + +# Any new source files that you add to the plugin should be added here. +list(APPEND PLUGIN_SOURCES + "flutter_blue_plus_winrt_plugin.cpp" + "flutter_blue_plus_winrt_plugin.h" +) + +# Define the plugin library target. Its name must not be changed (see comment +# on PLUGIN_NAME above). +add_library(${PLUGIN_NAME} SHARED + "include/flutter_blue_plus_winrt/flutter_blue_plus_plugin.h" + "flutter_blue_plus_winrt_plugin_c_api.cpp" + ${PLUGIN_SOURCES} +) + +# Apply a standard set of build settings that are configured in the +# application-level CMakeLists.txt. This can be removed for plugins that want +# full control over build settings. +apply_standard_settings(${PLUGIN_NAME}) + +# Symbols are hidden by default to reduce the chance of accidental conflicts +# between plugins. This should not be removed; any symbols that should be +# exported should be explicitly exported with the FLUTTER_PLUGIN_EXPORT macro. +set_target_properties(${PLUGIN_NAME} PROPERTIES + CXX_VISIBILITY_PRESET hidden) +target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL) + +# Source include directories and library dependencies. Add any plugin-specific +# dependencies here. +target_include_directories(${PLUGIN_NAME} INTERFACE + "${CMAKE_CURRENT_SOURCE_DIR}/include") +target_link_libraries(${PLUGIN_NAME} PRIVATE flutter flutter_wrapper_plugin runtimeobject ole32) + +# List of absolute paths to libraries that should be bundled with the plugin. +# This list could contain prebuilt libraries, or libraries created by an +# external build triggered from this build file. +set(flutter_blue_plus_winrt_bundled_libraries + "" + PARENT_SCOPE +) + +# === Tests === +# These unit tests can be run from a terminal after building the example, or +# from Visual Studio after opening the generated solution file. + +# Only enable test builds when building the example (which sets this variable) +# so that plugin clients aren't building the tests. +# Test section disabled because test directory is missing from the package. +# if (${include_${PROJECT_NAME}_tests}) +# ... +# endif() diff --git a/packages/flutter_blue_plus_winrt/windows/flutter_blue_plus_winrt_plugin.cpp b/packages/flutter_blue_plus_winrt/windows/flutter_blue_plus_winrt_plugin.cpp new file mode 100644 index 00000000..38f83d7f --- /dev/null +++ b/packages/flutter_blue_plus_winrt/windows/flutter_blue_plus_winrt_plugin.cpp @@ -0,0 +1,1964 @@ +#pragma warning(disable : 4819) + +#include "flutter_blue_plus_winrt_plugin.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace winrt; +using namespace Windows::Foundation; +using namespace Windows::Foundation::Collections; +using namespace Windows::Devices::Bluetooth; +using namespace Windows::Devices::Bluetooth::Advertisement; +using namespace Windows::Devices::Bluetooth::GenericAttributeProfile; +using namespace Windows::Devices::Enumeration; +using namespace Windows::Devices::Radios; + +namespace flutter_blue_plus_winrt { + +// Debug Logging Helper +void Log(const char* format, ...) { + char buffer[1024]; + va_list args; + va_start(args, format); + vsnprintf(buffer, sizeof(buffer), format, args); + va_end(args); + std::string msg = "[FBP-Win] "; + msg += buffer; + msg += "\n"; + OutputDebugStringA(msg.c_str()); +} + +namespace utils { +std::string to_string(const winrt::hstring& hstr) { + return winrt::to_string(hstr); +} + +template +T from_value(const flutter::EncodableValue* value) { + if (auto* ptr = std::get_if(value)) { + return *ptr; + } + if constexpr (std::is_integral_v && !std::is_same_v) { + if (auto* ptr64 = std::get_if(value)) { + return static_cast(*ptr64); + } + if (auto* ptr32 = std::get_if(value)) { + return static_cast(*ptr32); + } + } + return T{}; +} + +uint64_t mac_to_uint64(const std::string& mac_address) { + std::stringstream ss(mac_address); + uint64_t result = 0; + for (int i = 0; i < 6; ++i) { + long long byte; + ss >> std::hex >> byte; + result = (result << 8) | byte; + if (i < 5) { + ss.ignore(); + } + } + return result; +} + +std::vector to_vector(const winrt::Windows::Storage::Streams::IBuffer& buffer) { + auto reader = winrt::Windows::Storage::Streams::DataReader::FromBuffer(buffer); + std::vector data(reader.UnconsumedBufferLength()); + if (!data.empty()) { + reader.ReadBytes(data); + } + return data; +} + +std::string to_uuid_string(const winrt::guid& uuid) { + std::string uuid_str_with_braces = winrt::to_string(winrt::to_hstring(uuid)); + std::string full_uuid; + + if (uuid_str_with_braces.length() >= 2 && uuid_str_with_braces.front() == '{' && uuid_str_with_braces.back() == '}') { + full_uuid = uuid_str_with_braces.substr(1, uuid_str_with_braces.length() - 2); + } else { + full_uuid = uuid_str_with_braces; + } + + std::transform(full_uuid.begin(), full_uuid.end(), full_uuid.begin(), + [](unsigned char c){ return static_cast(std::tolower(c)); }); + + if (full_uuid.length() == 36 && + full_uuid.substr(0, 4) == "0000" && + full_uuid.substr(8) == "-0000-1000-8000-00805f9b34fb") { + return full_uuid.substr(4, 4); + } + return full_uuid; +} + +winrt::guid parse_uuid(const std::string& uuid_str) { + std::string full_uuid = uuid_str; + if (full_uuid.length() == 4) { + full_uuid = "0000" + full_uuid + "-0000-1000-8000-00805f9b34fb"; + } + else if (full_uuid.length() == 8) { + full_uuid = full_uuid + "-0000-1000-8000-00805f9b34fb"; + } + + if (full_uuid.length() > 0 && full_uuid.front() != '{') { + full_uuid = "{" + full_uuid + "}"; + } + + std::wstring wstr(full_uuid.begin(), full_uuid.end()); + GUID guid; + if (SUCCEEDED(IIDFromString(wstr.c_str(), &guid))) { + return winrt::guid(guid); + } + return winrt::guid(); +} + +} // namespace utils + +// --- GATT Helper Members --- + +winrt::Windows::Foundation::IAsyncOperation +FlutterBluePlusWinrtPlugin::FindCharacteristicInServiceAsync( + GattDeviceService service, + int instance_id, + BluetoothCacheMode cacheMode) +{ + try { + auto charsResult = co_await service.GetCharacteristicsAsync(cacheMode); + if (charsResult.Status() == GattCommunicationStatus::Success) { + for (auto c : charsResult.Characteristics()) { + if (instance_id != 0 && static_cast(c.AttributeHandle()) == instance_id) { + co_return c; + } + } + } + + auto includedResult = co_await service.GetIncludedServicesAsync(cacheMode); + if (includedResult.Status() == GattCommunicationStatus::Success) { + for (auto includedService : includedResult.Services()) { + auto c = co_await FindCharacteristicInServiceAsync(includedService, instance_id, cacheMode); + if (c) co_return c; + } + } + } catch (...) {} + co_return nullptr; +} + +winrt::Windows::Foundation::IAsyncOperation +FlutterBluePlusWinrtPlugin::FindCharacteristicByDescriptorHandleInServiceAsync( + GattDeviceService service, + int descriptor_handle, + BluetoothCacheMode cacheMode) +{ + if (descriptor_handle == 0) co_return nullptr; + + try { + auto charsResult = co_await service.GetCharacteristicsAsync(cacheMode); + if (charsResult.Status() == GattCommunicationStatus::Success) { + for (auto c : charsResult.Characteristics()) { + auto descResult = co_await c.GetDescriptorsAsync(cacheMode); + if (descResult.Status() == GattCommunicationStatus::Success) { + for (auto d : descResult.Descriptors()) { + if (static_cast(d.AttributeHandle()) == descriptor_handle) { + co_return c; + } + } + } + } + } + + auto includedResult = co_await service.GetIncludedServicesAsync(cacheMode); + if (includedResult.Status() == GattCommunicationStatus::Success) { + for (auto includedService : includedResult.Services()) { + auto c = co_await FindCharacteristicByDescriptorHandleInServiceAsync(includedService, descriptor_handle, cacheMode); + if (c) co_return c; + } + } + } catch (...) {} + co_return nullptr; +} + +winrt::Windows::Foundation::IAsyncOperation +FlutterBluePlusWinrtPlugin::GetCharacteristicByHandleAsync( + BluetoothLEDevice device, + int instance_id) +{ + auto servicesResult = co_await device.GetGattServicesAsync(BluetoothCacheMode::Cached); + if (servicesResult.Status() == GattCommunicationStatus::Success) { + for (auto service : servicesResult.Services()) { + auto c = co_await FindCharacteristicInServiceAsync(service, instance_id, BluetoothCacheMode::Cached); + if (c) co_return c; + } + } + + servicesResult = co_await device.GetGattServicesAsync(BluetoothCacheMode::Uncached); + if (servicesResult.Status() == GattCommunicationStatus::Success) { + for (auto service : servicesResult.Services()) { + auto c = co_await FindCharacteristicInServiceAsync(service, instance_id, BluetoothCacheMode::Uncached); + if (c) co_return c; + } + } + co_return nullptr; +} + +winrt::Windows::Foundation::IAsyncOperation +FlutterBluePlusWinrtPlugin::GetCharacteristicByDescriptorHandleAsync( + BluetoothLEDevice device, + int descriptor_handle) +{ + auto servicesResult = co_await device.GetGattServicesAsync(BluetoothCacheMode::Cached); + if (servicesResult.Status() == GattCommunicationStatus::Success) { + for (auto service : servicesResult.Services()) { + auto c = co_await FindCharacteristicByDescriptorHandleInServiceAsync(service, descriptor_handle, BluetoothCacheMode::Cached); + if (c) co_return c; + } + } + servicesResult = co_await device.GetGattServicesAsync(BluetoothCacheMode::Uncached); + if (servicesResult.Status() == GattCommunicationStatus::Success) { + for (auto service : servicesResult.Services()) { + auto c = co_await FindCharacteristicByDescriptorHandleInServiceAsync(service, descriptor_handle, BluetoothCacheMode::Uncached); + if (c) co_return c; + } + } + co_return nullptr; +} + +winrt::Windows::Foundation::IAsyncOperation +FlutterBluePlusWinrtPlugin::GetCharacteristicAsync( + BluetoothLEDevice device, + std::string service_uuid_str, + std::string characteristic_uuid_str, + std::string primary_service_uuid_str, + int instance_id) +{ + GattCharacteristic targetChar = nullptr; + winrt::guid targetServiceUuid = utils::parse_uuid(service_uuid_str); + winrt::guid targetCharUuid = utils::parse_uuid(characteristic_uuid_str); + + if (!primary_service_uuid_str.empty()) { + winrt::guid primaryUuid = utils::parse_uuid(primary_service_uuid_str); + GattDeviceServicesResult primaryResult = co_await device.GetGattServicesForUuidAsync(primaryUuid, BluetoothCacheMode::Cached); + if (primaryResult.Status() != GattCommunicationStatus::Success || primaryResult.Services().Size() == 0) { + primaryResult = co_await device.GetGattServicesForUuidAsync(primaryUuid, BluetoothCacheMode::Uncached); + } + + if (primaryResult.Status() == GattCommunicationStatus::Success) { + for (auto primaryService : primaryResult.Services()) { + GattDeviceServicesResult includedResult = co_await primaryService.GetIncludedServicesForUuidAsync(targetServiceUuid, BluetoothCacheMode::Cached); + if (includedResult.Status() != GattCommunicationStatus::Success || includedResult.Services().Size() == 0) { + includedResult = co_await primaryService.GetIncludedServicesForUuidAsync(targetServiceUuid, BluetoothCacheMode::Uncached); + } + + if (includedResult.Status() == GattCommunicationStatus::Success) { + for (auto service : includedResult.Services()) { + GattCharacteristicsResult charsResult = co_await service.GetCharacteristicsForUuidAsync(targetCharUuid, BluetoothCacheMode::Cached); + if (charsResult.Status() != GattCommunicationStatus::Success || charsResult.Characteristics().Size() == 0) { + charsResult = co_await service.GetCharacteristicsForUuidAsync(targetCharUuid, BluetoothCacheMode::Uncached); + } + + if (charsResult.Status() == GattCommunicationStatus::Success) { + for (auto characteristic : charsResult.Characteristics()) { + if (instance_id == 0 || static_cast(characteristic.AttributeHandle()) == instance_id) { + targetChar = characteristic; + break; + } + } + } + if (targetChar) break; + } + } + if (targetChar) break; + } + } + } else { + GattDeviceServicesResult servicesResult = co_await device.GetGattServicesForUuidAsync(targetServiceUuid, BluetoothCacheMode::Cached); + if (servicesResult.Status() != GattCommunicationStatus::Success || servicesResult.Services().Size() == 0) { + servicesResult = co_await device.GetGattServicesForUuidAsync(targetServiceUuid, BluetoothCacheMode::Uncached); + } + + if (servicesResult.Status() == GattCommunicationStatus::Success) { + for (auto service : servicesResult.Services()) { + if (service.Uuid() == targetServiceUuid) { + GattCharacteristicsResult charsResult = co_await service.GetCharacteristicsForUuidAsync(targetCharUuid, BluetoothCacheMode::Cached); + if (charsResult.Status() != GattCommunicationStatus::Success || charsResult.Characteristics().Size() == 0) { + charsResult = co_await service.GetCharacteristicsForUuidAsync(targetCharUuid, BluetoothCacheMode::Uncached); + } + if (charsResult.Status() == GattCommunicationStatus::Success && charsResult.Characteristics().Size() > 0) { + for (auto c : charsResult.Characteristics()) { + if(instance_id == 0 || static_cast(c.AttributeHandle()) == instance_id) { + targetChar = c; + break; + } + } + } + } + if (targetChar) break; + } + } + } + co_return targetChar; +} + +winrt::Windows::Foundation::IAsyncOperation +FlutterBluePlusWinrtPlugin::GetDescriptorAsync( + GattCharacteristic characteristic, + std::string descriptor_uuid_str) +{ + if (!characteristic) co_return nullptr; + + winrt::guid descUuid = utils::parse_uuid(descriptor_uuid_str); + GattDescriptor targetDesc = nullptr; + + auto descResult = co_await characteristic.GetDescriptorsForUuidAsync(descUuid, BluetoothCacheMode::Cached); + if (descResult.Status() == GattCommunicationStatus::Success) { + for (auto descriptor : descResult.Descriptors()) { + if (utils::to_uuid_string(descriptor.Uuid()) == descriptor_uuid_str) { + targetDesc = descriptor; break; + } + } + } + + if (!targetDesc) { + descResult = co_await characteristic.GetDescriptorsForUuidAsync(descUuid, BluetoothCacheMode::Uncached); + if (descResult.Status() == GattCommunicationStatus::Success) { + for (auto descriptor : descResult.Descriptors()) { + if (utils::to_uuid_string(descriptor.Uuid()) == descriptor_uuid_str) { + targetDesc = descriptor; break; + } + } + } + } + co_return targetDesc; +} + +winrt::Windows::Foundation::IAsyncAction +FlutterBluePlusWinrtPlugin::PopulateCharacteristicsAsync( + GattDeviceService service, + std::string remote_id, + std::string primaryServiceUuid, + std::shared_ptr outList) +{ + auto charsResult = co_await service.GetCharacteristicsAsync(BluetoothCacheMode::Cached); + if (charsResult.Status() != GattCommunicationStatus::Success || charsResult.Characteristics().Size() == 0) { + charsResult = co_await service.GetCharacteristicsAsync(BluetoothCacheMode::Uncached); + } + + if (charsResult.Status() == GattCommunicationStatus::Success) { + for (auto characteristic : charsResult.Characteristics()) { + try { + flutter::EncodableMap charMap; + std::string serviceUuid = utils::to_uuid_string(service.Uuid()); + std::string charUuid = utils::to_uuid_string(characteristic.Uuid()); + int32_t handle = static_cast(characteristic.AttributeHandle()); + + { + std::lock_guard cache_lock(gatt_cache_mutex_); + characteristic_cache_[remote_id + ":" + std::to_string(handle)] = characteristic; + } + + charMap[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + if (!primaryServiceUuid.empty()) { + charMap[flutter::EncodableValue("primary_service_uuid")] = flutter::EncodableValue(primaryServiceUuid); + } + charMap[flutter::EncodableValue("service_uuid")] = flutter::EncodableValue(serviceUuid); + charMap[flutter::EncodableValue("characteristic_uuid")] = flutter::EncodableValue(charUuid); + charMap[flutter::EncodableValue("instance_id")] = flutter::EncodableValue(handle); + + auto props = characteristic.CharacteristicProperties(); + flutter::EncodableMap propsMap; + propsMap[flutter::EncodableValue("broadcast")] = flutter::EncodableValue((static_cast(props) & static_cast(GattCharacteristicProperties::Broadcast)) ? 1 : 0); + propsMap[flutter::EncodableValue("read")] = flutter::EncodableValue((static_cast(props) & static_cast(GattCharacteristicProperties::Read)) ? 1 : 0); + propsMap[flutter::EncodableValue("write_without_response")] = flutter::EncodableValue((static_cast(props) & static_cast(GattCharacteristicProperties::WriteWithoutResponse)) ? 1 : 0); + propsMap[flutter::EncodableValue("write")] = flutter::EncodableValue((static_cast(props) & static_cast(GattCharacteristicProperties::Write)) ? 1 : 0); + propsMap[flutter::EncodableValue("notify")] = flutter::EncodableValue((static_cast(props) & static_cast(GattCharacteristicProperties::Notify)) ? 1 : 0); + propsMap[flutter::EncodableValue("indicate")] = flutter::EncodableValue((static_cast(props) & static_cast(GattCharacteristicProperties::Indicate)) ? 1 : 0); + propsMap[flutter::EncodableValue("authenticated_signed_writes")] = flutter::EncodableValue((static_cast(props) & static_cast(GattCharacteristicProperties::AuthenticatedSignedWrites)) ? 1 : 0); + propsMap[flutter::EncodableValue("extended_properties")] = flutter::EncodableValue((static_cast(props) & static_cast(GattCharacteristicProperties::ExtendedProperties)) ? 1 : 0); + propsMap[flutter::EncodableValue("notify_encryption_required")] = flutter::EncodableValue(0); + propsMap[flutter::EncodableValue("indicate_encryption_required")] = flutter::EncodableValue(0); + + charMap[flutter::EncodableValue("properties")] = propsMap; + + auto descResult = co_await characteristic.GetDescriptorsAsync(BluetoothCacheMode::Cached); + if (descResult.Status() != GattCommunicationStatus::Success) { + descResult = co_await characteristic.GetDescriptorsAsync(BluetoothCacheMode::Uncached); + } + + flutter::EncodableList descList; + if (descResult.Status() == GattCommunicationStatus::Success) { + for (auto descriptor : descResult.Descriptors()) { + try { + flutter::EncodableMap descMap; + int32_t d_handle = static_cast(descriptor.AttributeHandle()); + + { + std::lock_guard cache_lock(gatt_cache_mutex_); + descriptor_cache_[remote_id + ":" + std::to_string(d_handle)] = descriptor; + } + + descMap[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + if (!primaryServiceUuid.empty()) { + descMap[flutter::EncodableValue("primary_service_uuid")] = flutter::EncodableValue(primaryServiceUuid); + } + descMap[flutter::EncodableValue("service_uuid")] = flutter::EncodableValue(serviceUuid); + descMap[flutter::EncodableValue("characteristic_uuid")] = flutter::EncodableValue(charUuid); + descMap[flutter::EncodableValue("descriptor_uuid")] = flutter::EncodableValue(utils::to_uuid_string(descriptor.Uuid())); + descMap[flutter::EncodableValue("instance_id")] = flutter::EncodableValue(handle); + + descList.push_back(descMap); + } catch(...) {} + } + } + charMap[flutter::EncodableValue("descriptors")] = descList; + outList->push_back(charMap); + } catch(...) {} + } + } + co_return; +} + +winrt::Windows::Foundation::IAsyncOperation +FlutterBluePlusWinrtPlugin::GetCharacteristicInternalAsync( + BluetoothLEDevice device, + std::string remote_id, + std::string service_uuid_str, + std::string characteristic_uuid_str, + std::string primary_service_uuid_str, + int instance_id) +{ + if (instance_id != 0) { + std::string cache_key = remote_id + ":" + std::to_string(instance_id); + winrt::Windows::Foundation::IInspectable cached = nullptr; + { + std::lock_guard cache_lock(gatt_cache_mutex_); + auto it = characteristic_cache_.find(cache_key); + if (it != characteristic_cache_.end()) cached = it->second; + } + if (cached) co_return cached.as(); + } + + GattCharacteristic targetChar = nullptr; + if (instance_id != 0) { + targetChar = co_await GetCharacteristicByHandleAsync(device, instance_id); + } + if (!targetChar) { + targetChar = co_await GetCharacteristicAsync(device, service_uuid_str, characteristic_uuid_str, primary_service_uuid_str, instance_id); + } + + if (targetChar && instance_id != 0) { + std::lock_guard cache_lock(gatt_cache_mutex_); + characteristic_cache_[remote_id + ":" + std::to_string(instance_id)] = targetChar; + } + co_return targetChar; +} + +// --- Plugin Implementation --- + +void FlutterBluePlusWinrtPlugin::RegisterWithRegistrar( + flutter::PluginRegistrarWindows* registrar) { + auto plugin = std::make_unique(registrar); + auto* plugin_pointer = plugin.get(); + + plugin_pointer->channel_ = std::make_unique>( + registrar->messenger(), "flutter_blue_plus/methods", + &flutter::StandardMethodCodec::GetInstance()); + + plugin_pointer->channel_->SetMethodCallHandler( + [plugin_pointer](const auto& call, auto result) { + plugin_pointer->HandleMethodCall(call, std::move(result)); + }); + + registrar->AddPlugin(std::move(plugin)); +} + +FlutterBluePlusWinrtPlugin::FlutterBluePlusWinrtPlugin(flutter::PluginRegistrarWindows* registrar) + : registrar_(registrar) { + watcher_.ScanningMode(BluetoothLEScanningMode::Active); + received_token_ = watcher_.Received( + { this, &FlutterBluePlusWinrtPlugin::OnAdvertisementReceived }); + stopped_token_ = watcher_.Stopped( + { this, &FlutterBluePlusWinrtPlugin::OnAdvertisementStopped }); + + PeriodicConnectionCheck(); +} + +FlutterBluePlusWinrtPlugin::~FlutterBluePlusWinrtPlugin() { + // Signal all in-flight coroutines / event handlers that the plugin is gone. + // They capture a copy of this shared flag and bail out before touching + // `this`, avoiding use-after-free during teardown. + *is_alive_ = false; + try { watcher_.Stop(); } catch (...) {} + watcher_.Stopped(stopped_token_); + watcher_.Received(received_token_); + + // Revoke any still-registered per-device ConnectionStatusChanged handlers + // so a late event can't invoke OnConnectionStatusChanged on a destroyed + // plugin. Lock order is devices_mutex_ then gatt_cache_mutex_ (never the + // reverse anywhere), so this cannot deadlock. + std::lock_guard devices_lock(devices_mutex_); + std::lock_guard cache_lock(gatt_cache_mutex_); + auto revoke_all = [this](const std::vector>& devices) { + for (const auto& pair : devices) { + auto it = connection_tokens_.find(pair.first); + if (it == connection_tokens_.end()) continue; + try { pair.second.as().ConnectionStatusChanged(it->second); } catch (...) {} + } + }; + revoke_all(connected_devices_); + revoke_all(currently_connecting_devices_); + connection_tokens_.clear(); +} + +void FlutterBluePlusWinrtPlugin::OnAdvertisementReceived( + const BluetoothLEAdvertisementWatcher&, + const BluetoothLEAdvertisementReceivedEventArgs& args) { + + [this, args, alive = is_alive_]() -> winrt::fire_and_forget { + try { + co_await ui_thread_; + if (!*alive) co_return; + + if (channel_) { + std::string remote_id = uint64_to_mac_string(args.BluetoothAddress()); + + // Bound memory usage: a hostile environment can rotate BLE + // addresses rapidly (address randomization / spoofing). Cap the + // number of distinct devices tracked during a scan session so + // the caches cannot grow without limit. Already-known devices + // keep updating normally. + constexpr size_t kMaxScanDevices = 4096; + bool is_new_device = scan_results_cache_.find(remote_id) == scan_results_cache_.end(); + if (is_new_device && scan_results_cache_.size() >= kMaxScanDevices) { + co_return; + } + + rssi_cache_[remote_id] = static_cast(args.RawSignalStrengthInDBm()); + + if (is_new_device) { + scan_results_cache_[remote_id] = {}; + } + auto& map = scan_results_cache_[remote_id]; + + map[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + map[flutter::EncodableValue("rssi")] = flutter::EncodableValue(static_cast(args.RawSignalStrengthInDBm())); + + int connectable = args.IsConnectable() ? 1 : 0; + if (connectable == 0) { + auto it = map.find(flutter::EncodableValue("connectable")); + if (it != map.end()) { + if (auto* val = std::get_if(&it->second)) { + if (*val == 1) connectable = 1; + } + } + } + map[flutter::EncodableValue("connectable")] = flutter::EncodableValue(connectable); + + auto advertisement = args.Advertisement(); + std::string localNameStr = utils::to_string(advertisement.LocalName()); + if (!localNameStr.empty()) { + map[flutter::EncodableValue("adv_name")] = flutter::EncodableValue(localNameStr); + map[flutter::EncodableValue("platform_name")] = flutter::EncodableValue(localNameStr); + } + + if (args.TransmitPowerLevelInDBm() != nullptr) { + map[flutter::EncodableValue("tx_power_level")] = + flutter::EncodableValue(static_cast(args.TransmitPowerLevelInDBm().Value())); + } + + for (const auto& section : advertisement.DataSections()) { + if (section.DataType() == 0x19) { + // Guard against a malformed/hostile peripheral sending an + // appearance section shorter than 2 bytes; ReadUInt16 would + // otherwise throw and abort processing of the whole packet. + if (section.Data().Length() < 2) break; + auto reader = winrt::Windows::Storage::Streams::DataReader::FromBuffer(section.Data()); + reader.ByteOrder(winrt::Windows::Storage::Streams::ByteOrder::LittleEndian); + uint16_t appearance_value = reader.ReadUInt16(); + map[flutter::EncodableValue("appearance")] = flutter::EncodableValue(static_cast(appearance_value)); + break; + } + } + + if (advertisement.ManufacturerData().Size() > 0) { + flutter::EncodableMap msd_map = flutter::EncodableMap(); + for (const auto& msd : advertisement.ManufacturerData()) { + msd_map[flutter::EncodableValue(static_cast(msd.CompanyId()))] = + flutter::EncodableValue(utils::to_vector(msd.Data())); + } + map[flutter::EncodableValue("manufacturer_data")] = msd_map; + } + + flutter::EncodableMap service_data_map; + auto it_sd = map.find(flutter::EncodableValue("service_data")); + if (it_sd != map.end()) { + if (auto* existing = std::get_if(&it_sd->second)) { + service_data_map = *existing; + } + } + + bool has_new_service_data = false; + for (const auto& section : advertisement.GetSectionsByType(0x16)) { + auto buffer = section.Data(); + if (buffer.Length() >= 2) { + has_new_service_data = true; + auto all_data = utils::to_vector(buffer); + uint16_t uuid16 = (all_data[1] << 8) | all_data[0]; + std::stringstream ss; + ss << std::hex << std::setfill('0') << std::setw(4) << uuid16; + std::string uuid_str = "0000" + ss.str() + "-0000-1000-8000-00805f9b34fb"; + std::vector data_vec(all_data.begin() + 2, all_data.end()); + service_data_map[flutter::EncodableValue(uuid_str)] = flutter::EncodableValue(data_vec); + } + } + if (has_new_service_data || !service_data_map.empty()) { + map[flutter::EncodableValue("service_data")] = service_data_map; + } + + if (advertisement.ServiceUuids().Size() > 0) { + flutter::EncodableList service_uuids_list; + auto it_u = map.find(flutter::EncodableValue("service_uuids")); + if (it_u != map.end()) { + if (auto* existing = std::get_if(&it_u->second)) { + service_uuids_list = *existing; + } + } + for (const auto& uuid : advertisement.ServiceUuids()) { + std::string uuid_str = utils::to_uuid_string(uuid); + bool found = false; + for(const auto& existing_val : service_uuids_list) { + if (auto* s = std::get_if(&existing_val)) { + if (*s == uuid_str) { found = true; break; } + } + } + if (!found) service_uuids_list.push_back(flutter::EncodableValue(uuid_str)); + } + map[flutter::EncodableValue("service_uuids")] = service_uuids_list; + } + + flutter::EncodableMap response; + response[flutter::EncodableValue("advertisements")] = flutter::EncodableList{ flutter::EncodableValue(map) }; + channel_->InvokeMethod("OnScanResponse", std::make_unique(response)); + } + } catch (...) {} + }(); +} + +void FlutterBluePlusWinrtPlugin::OnAdvertisementStopped( + const BluetoothLEAdvertisementWatcher&, + const BluetoothLEAdvertisementWatcherStoppedEventArgs&) { +} + +winrt::fire_and_forget FlutterBluePlusWinrtPlugin::GetSystemDevicesAsync(std::unique_ptr> result) { + std::string error_msg; + try { + // Broad selector for all Bluetooth LE devices using Association Endpoint protocol + auto selector = L"System.Devices.Aep.ProtocolId:=\"{bb7bb05e-5972-42b5-9513-ade58156ee23}\""; + + Log("GetSystemDevices: Searching for connected BLE AEPs..."); + // Explicitly search for AssociationEndpoints + auto deviceInfoCollection = co_await DeviceInformation::FindAllAsync(selector, {}, DeviceInformationKind::AssociationEndpoint); + + Log("GetSystemDevices: Found %d biological AEPs", deviceInfoCollection.Size()); + + flutter::EncodableMap response = {}; + flutter::EncodableList deviceList; + + for (auto&& deviceInfo : deviceInfoCollection) { + try { + // Skip devices without a name as they are usually not useful + if (deviceInfo.Name().empty()) continue; + + auto bleDevice = co_await BluetoothLEDevice::FromIdAsync(deviceInfo.Id()); + if (!bleDevice) continue; + + // User requested ONLY connected devices + if (bleDevice.ConnectionStatus() == BluetoothConnectionStatus::Connected) { + std::string remote_id = uint64_to_mac_string(bleDevice.BluetoothAddress()); + + flutter::EncodableMap deviceMap = {}; + deviceMap[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + deviceMap[flutter::EncodableValue("platform_name")] = flutter::EncodableValue(utils::to_string(deviceInfo.Name())); + deviceMap[flutter::EncodableValue("connection_state")] = flutter::EncodableValue(1); + deviceList.push_back(flutter::EncodableValue(deviceMap)); + + Log(" Added connected device: %ls (%s)", deviceInfo.Name().c_str(), remote_id.c_str()); + } + } catch (...) { + // Ignore failures for individual devices + } + } + + // Also ensure any devices connected directly by this app instance are included + // if they weren't already found by the system query (sometimes system cache lags) + { + std::lock_guard lock(devices_mutex_); + for (size_t i = 0; i < connected_devices_.size(); ++i) { + const auto& pair = connected_devices_[i]; + bool already_added = false; + for (const auto& val : deviceList) { + if (auto* m = std::get_if(&val)) { + auto it = m->find(flutter::EncodableValue("remote_id")); + if (it != m->end()) { + if (auto* s = std::get_if(&it->second)) { + if (*s == pair.first) { already_added = true; break; } + } + } + } + } + if (!already_added) { + try { + auto d = pair.second.as(); + flutter::EncodableMap deviceMap = {}; + deviceMap[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(pair.first); + deviceMap[flutter::EncodableValue("platform_name")] = flutter::EncodableValue(utils::to_string(d.Name())); + deviceMap[flutter::EncodableValue("connection_state")] = flutter::EncodableValue(1); + deviceList.push_back(flutter::EncodableValue(deviceMap)); + } catch (...) {} + } + } + } + + + response[flutter::EncodableValue("devices")] = deviceList; + co_await ui_thread_; + result->Success(flutter::EncodableValue(response)); + co_return; + } catch (const std::exception& e) { error_msg = e.what(); Log("GetSystemDevices Error: %s", error_msg.c_str()); } + catch (...) { error_msg = "Unknown error occurred"; Log("GetSystemDevices Unknown Error"); } + + if (!error_msg.empty()) { + co_await ui_thread_; + result->Error("getSystemDevices", error_msg); + } +} + +winrt::fire_and_forget FlutterBluePlusWinrtPlugin::GetAdapterStateAsync(std::unique_ptr> result) { + std::string error_msg; + try { + auto radios = co_await Radio::GetRadiosAsync(); + std::string adapter_name = ""; + int adapter_state = 0; + for (auto radio : radios) { + if (radio.Kind() != RadioKind::Bluetooth) continue; + adapter_name = utils::to_string(radio.Name()); + switch (radio.State()) { + case RadioState::On: adapter_state = 4; break; + case RadioState::Off: adapter_state = 6; break; + default: adapter_state = 0; break; + } + break; + } + flutter::EncodableMap response = {}; + response[flutter::EncodableValue("adapter_state")] = flutter::EncodableValue(adapter_state); + response[flutter::EncodableValue("adapter_name")] = flutter::EncodableValue(adapter_name); + + co_await ui_thread_; + result->Success(flutter::EncodableValue(response)); + co_return; + } catch (const std::exception& e) { error_msg = e.what(); } + catch (...) { error_msg = "Unknown error occurred"; } + + if (!error_msg.empty()) { + co_await ui_thread_; + result->Error("getAdapterState", error_msg); + } +} + +winrt::fire_and_forget FlutterBluePlusWinrtPlugin::GetBondedDevicesAsync(std::unique_ptr> result) { + std::string error_msg; + try { + auto selector = BluetoothDevice::GetDeviceSelectorFromPairingState(true); + DeviceInformationCollection deviceInfoCollection = co_await DeviceInformation::FindAllAsync(selector); + flutter::EncodableMap response = {}; + flutter::EncodableList deviceList; + + for (auto&& deviceInfo : deviceInfoCollection) { + try { + auto bleDevice = co_await BluetoothLEDevice::FromIdAsync(deviceInfo.Id()); + if (!bleDevice) continue; + std::string remote_id = uint64_to_mac_string(bleDevice.BluetoothAddress()); + flutter::EncodableMap deviceMap = {}; + deviceMap[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + deviceMap[flutter::EncodableValue("platform_name")] = flutter::EncodableValue(utils::to_string(deviceInfo.Name())); + deviceMap[flutter::EncodableValue("connection_state")] = flutter::EncodableValue(bleDevice.ConnectionStatus() == BluetoothConnectionStatus::Connected ? 1 : 0); + deviceList.push_back(flutter::EncodableValue(deviceMap)); + } catch (...) {} + } + response[flutter::EncodableValue("devices")] = deviceList; + co_await ui_thread_; + result->Success(flutter::EncodableValue(response)); + co_return; + } catch (const std::exception& e) { error_msg = e.what(); } + catch (...) { error_msg = "Unknown error"; } + + if (!error_msg.empty()) { + co_await ui_thread_; + result->Error("getBondedDevices", error_msg); + } +} + +winrt::fire_and_forget FlutterBluePlusWinrtPlugin::CreateBondAsync(std::string remote_id, std::unique_ptr> result) { + std::string error_msg; + try { + auto bleDevice = co_await BluetoothLEDevice::FromBluetoothAddressAsync(utils::mac_to_uint64(remote_id)); + if (bleDevice) { + auto pairing = bleDevice.DeviceInformation().Pairing(); + auto result_val = co_await pairing.PairAsync(); + co_await ui_thread_; + result->Success(flutter::EncodableValue(result_val.Status() == DevicePairingResultStatus::Paired || result_val.Status() == DevicePairingResultStatus::AlreadyPaired)); + co_return; + } else { + error_msg = "Device not found"; + } + } catch (const std::exception& e) { error_msg = e.what(); } + catch (...) { error_msg = "Unknown error"; } + + if (!error_msg.empty()) { + co_await ui_thread_; + result->Error("createBond", error_msg); + } +} + +winrt::fire_and_forget FlutterBluePlusWinrtPlugin::RemoveBondAsync(std::string remote_id, std::unique_ptr> result) { + std::string error_msg; + try { + auto bleDevice = co_await BluetoothLEDevice::FromBluetoothAddressAsync(utils::mac_to_uint64(remote_id)); + if (bleDevice) { + auto pairing = bleDevice.DeviceInformation().Pairing(); + auto result_val = co_await pairing.UnpairAsync(); + co_await ui_thread_; + result->Success(flutter::EncodableValue(result_val.Status() == DeviceUnpairingResultStatus::Unpaired || result_val.Status() == DeviceUnpairingResultStatus::AlreadyUnpaired)); + co_return; + } else { + error_msg = "Device not found"; + } + } catch (const std::exception& e) { error_msg = e.what(); } + catch (...) { error_msg = "Unknown error"; } + + if (!error_msg.empty()) { + co_await ui_thread_; + result->Error("removeBond", error_msg); + } +} + +winrt::fire_and_forget FlutterBluePlusWinrtPlugin::GetBondStateAsync(std::string remote_id, std::unique_ptr> result) { + std::string error_msg; + try { + auto bleDevice = co_await BluetoothLEDevice::FromBluetoothAddressAsync(utils::mac_to_uint64(remote_id)); + if (bleDevice) { + bool isPaired = bleDevice.DeviceInformation().Pairing().IsPaired(); + flutter::EncodableMap response; + response[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + response[flutter::EncodableValue("bond_state")] = flutter::EncodableValue(isPaired ? 2 : 0); + co_await ui_thread_; + result->Success(flutter::EncodableValue(response)); + co_return; + } else { + error_msg = "Device not found"; + } + } catch (const std::exception& e) { error_msg = e.what(); } + catch (...) { error_msg = "Unknown error"; } + + if (!error_msg.empty()) { + co_await ui_thread_; + result->Error("getBondState", error_msg); + } +} + +winrt::fire_and_forget FlutterBluePlusWinrtPlugin::ConnectAsync( + std::string remote_id, + std::unique_ptr> result) { + + std::string error_msg; + try { + uint64_t bluetoothAddress = utils::mac_to_uint64(remote_id); + + co_await ui_thread_; + + { + std::lock_guard lock(devices_mutex_); + auto it_existing = std::find_if(connected_devices_.begin(), connected_devices_.end(), + [&remote_id](const auto& pair) { return pair.first == remote_id; }); + if (it_existing != connected_devices_.end()) { + flutter::EncodableMap connection_state; + connection_state[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + connection_state[flutter::EncodableValue("connection_state")] = flutter::EncodableValue(1); + channel_->InvokeMethod("OnConnectionStateChanged", std::make_unique(connection_state)); + result->Success(flutter::EncodableValue(true)); + co_return; + } + } + + co_await winrt::resume_background(); + auto device = co_await BluetoothLEDevice::FromBluetoothAddressAsync(bluetoothAddress); + if (device) { + co_await ui_thread_; + { + std::lock_guard lock(devices_mutex_); + auto it = std::find_if(currently_connecting_devices_.begin(), currently_connecting_devices_.end(), + [&remote_id](const auto& pair) { return pair.first == remote_id; }); + if (it == currently_connecting_devices_.end()) { + currently_connecting_devices_.emplace_back(remote_id, device); + } + } + auto conn_token = device.ConnectionStatusChanged({ this, &FlutterBluePlusWinrtPlugin::OnConnectionStatusChanged }); + { + std::lock_guard cache_lock(gatt_cache_mutex_); + connection_tokens_[remote_id] = conn_token; + } + + co_await winrt::resume_background(); + // Force connection by discovery + auto gatt_result = co_await device.GetGattServicesAsync(BluetoothCacheMode::Uncached); + + // Strictly wait for ConnectionStatus to be Connected (up to 5s) + int wait_retry = 0; + while (device.ConnectionStatus() != BluetoothConnectionStatus::Connected && wait_retry < 10) { + co_await winrt::resume_after(std::chrono::milliseconds(500)); + wait_retry++; + } + + co_await ui_thread_; + + bool should_close = false; + bool already_in_connected = false; + { + std::lock_guard lock(devices_mutex_); + + // Check if device was already moved to connected_devices_ by OnConnectionStatusChanged + auto it_connected = std::find_if(connected_devices_.begin(), connected_devices_.end(), + [&remote_id](const auto& pair) { return pair.first == remote_id; }); + + auto it_connecting = std::find_if(currently_connecting_devices_.begin(), currently_connecting_devices_.end(), + [&remote_id](const auto& pair) { return pair.first == remote_id; }); + + // If neither in connected nor connecting list, connection was cancelled/failed + if (it_connecting == currently_connecting_devices_.end() && it_connected == connected_devices_.end()) { + should_close = true; + } else if (gatt_result.Status() == GattCommunicationStatus::Success && device.ConnectionStatus() == BluetoothConnectionStatus::Connected) { + // Ensure device is in connected_devices_ (might already be added by OnConnectionStatusChanged) + if (it_connected == connected_devices_.end()) { + connected_devices_.emplace_back(remote_id, device); + } else { + already_in_connected = true; + } + + // Remove from connecting list if still there + currently_connecting_devices_.erase(std::remove_if(currently_connecting_devices_.begin(), currently_connecting_devices_.end(), + [&remote_id](const auto& pair) { return pair.first == remote_id; }), currently_connecting_devices_.end()); + } + } + + if (should_close) { + if (device) { + { std::lock_guard cache_lock(gatt_cache_mutex_); connection_tokens_.erase(remote_id); } + device.Close(); + } + result->Success(flutter::EncodableValue(false)); + co_return; + } + + if (gatt_result.Status() == GattCommunicationStatus::Success && device.ConnectionStatus() == BluetoothConnectionStatus::Connected) { + // Only send connection state if not already sent by OnConnectionStatusChanged + if (!already_in_connected) { + flutter::EncodableMap connection_state; + connection_state[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + connection_state[flutter::EncodableValue("connection_state")] = flutter::EncodableValue(1); + channel_->InvokeMethod("OnConnectionStateChanged", std::make_unique(connection_state)); + } + result->Success(flutter::EncodableValue(true)); + co_return; + } else { + std::vector devices_to_close; + { + std::lock_guard lock(devices_mutex_); + + // Check connected devices + for (size_t i = 0; i < connected_devices_.size(); ++i) { + if (connected_devices_[i].first == remote_id) { + devices_to_close.push_back(connected_devices_[i].second.as()); + } + } + // Check connecting devices + for (size_t i = 0; i < currently_connecting_devices_.size(); ++i) { + if (currently_connecting_devices_[i].first == remote_id) { + devices_to_close.push_back(currently_connecting_devices_[i].second.as()); + } + } + + { + auto it = connected_devices_.begin(); + while (it != connected_devices_.end()) { + if (it->first == remote_id) { + it = connected_devices_.erase(it); + } else { + ++it; + } + } + } + + { + auto it = currently_connecting_devices_.begin(); + while (it != currently_connecting_devices_.end()) { + if (it->first == remote_id) { + it = currently_connecting_devices_.erase(it); + } else { + ++it; + } + } + } + } + + { std::lock_guard cache_lock(gatt_cache_mutex_); connection_tokens_.erase(remote_id); } + + for (auto& d : devices_to_close) { + try { d.Close(); } catch(...) {} + } + + flutter::EncodableMap connection_state; + connection_state[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + connection_state[flutter::EncodableValue("connection_state")] = flutter::EncodableValue(0); + channel_->InvokeMethod("OnConnectionStateChanged", std::make_unique(connection_state)); + + result->Success(flutter::EncodableValue(false)); + co_return; + } + } else error_msg = "Device not found."; + } catch (const std::exception& e) { error_msg = e.what(); } + catch (...) { error_msg = "Unknown error occurred"; } + + if (!error_msg.empty()) { + co_await ui_thread_; + result->Success(flutter::EncodableValue(false)); + } +} + +std::string FlutterBluePlusWinrtPlugin::uint64_to_mac_string(uint64_t addr) { + std::stringstream stream; + stream << std::hex << std::uppercase << std::setfill('0') << std::setw(2) << ((addr >> 40) & 0xFF) << ":" << std::setw(2) << ((addr >> 32) & 0xFF) << ":" << std::setw(2) << ((addr >> 24) & 0xFF) << ":" << std::setw(2) << ((addr >> 16) & 0xFF) << ":" << std::setw(2) << ((addr >> 8) & 0xFF) << ":" << std::setw(2) << (addr & 0xFF); + return stream.str(); +} + +void FlutterBluePlusWinrtPlugin::OnConnectionStatusChanged(const BluetoothLEDevice& device, const IInspectable&) { + // Capture device by value to avoid dangling references + [this, alive = is_alive_](BluetoothLEDevice d) -> winrt::fire_and_forget { + co_await ui_thread_; + if (!*alive) co_return; + + std::string remote_id = uint64_to_mac_string(d.BluetoothAddress()); + flutter::EncodableMap connection_state; + connection_state[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + + if (d.ConnectionStatus() == BluetoothConnectionStatus::Connected) { + bool solicited = false; + { + std::lock_guard lock(devices_mutex_); + + auto it_connected = std::find_if(connected_devices_.begin(), connected_devices_.end(), + [&remote_id](const auto& pair) { return pair.first == remote_id; }); + + if (it_connected != connected_devices_.end()) { + solicited = true; + } else { + auto it_connecting = std::find_if(currently_connecting_devices_.begin(), currently_connecting_devices_.end(), + [&remote_id](const auto& pair) { return pair.first == remote_id; }); + + if(it_connecting != currently_connecting_devices_.end()) { + auto dev_obj = it_connecting->second; + connected_devices_.emplace_back(remote_id, dev_obj); + + // Safe removal: use temporary variable to store index to delete + currently_connecting_devices_.erase( + std::remove_if(currently_connecting_devices_.begin(), currently_connecting_devices_.end(), + [&remote_id](const auto& pair) { return pair.first == remote_id; } + ), + currently_connecting_devices_.end() + ); + solicited = true; + } + } + } + + if (solicited) { + connection_state[flutter::EncodableValue("connection_state")] = flutter::EncodableValue(1); + channel_->InvokeMethod("OnConnectionStateChanged", std::make_unique(connection_state)); + } else { + Log("Unsolicited connection for %s, closing to prevent unintended auto-reconnect.", remote_id.c_str()); + try { d.Close(); } catch(...) {} + } + } else { + // Disconnection handling - immediately remove from connected list + Log("OnConnectionStatusChanged: device %s disconnected", remote_id.c_str()); + + ClearDeviceResources(remote_id); + + { + std::lock_guard lock(devices_mutex_); + + // Remove device from vectors first + currently_connecting_devices_.erase( + std::remove_if(currently_connecting_devices_.begin(), currently_connecting_devices_.end(), + [&remote_id](const auto& pair) { return pair.first == remote_id; } + ), + currently_connecting_devices_.end() + ); + + connected_devices_.erase( + std::remove_if(connected_devices_.begin(), connected_devices_.end(), + [&remote_id](const auto& pair) { return pair.first == remote_id; } + ), + connected_devices_.end() + ); + } + + // Close the device - similar to win_ble approach + try { + if (d) { + d.Close(); + } + } catch(...) { + Log("OnConnectionStatusChanged: error closing device %s", remote_id.c_str()); + } + + connection_state[flutter::EncodableValue("connection_state")] = flutter::EncodableValue(0); + channel_->InvokeMethod("OnConnectionStateChanged", std::make_unique(connection_state)); + } + }(device); +} + +winrt::fire_and_forget FlutterBluePlusWinrtPlugin::DiscoverServicesAsync( + std::string remote_id, + std::unique_ptr> result) { + + auto result_ptr = std::shared_ptr>(std::move(result)); + std::string error_msg; + try { + BluetoothLEDevice device = nullptr; + + { + std::lock_guard lock(devices_mutex_); + auto it = std::find_if(connected_devices_.begin(), connected_devices_.end(), [&](const auto& pair) { return pair.first == remote_id; }); + if (it != connected_devices_.end()) device = it->second.as(); + } + if (!device) { error_msg = "device is disconnected"; } + else { + co_await winrt::resume_background(); + auto servicesResult = co_await device.GetGattServicesAsync(BluetoothCacheMode::Uncached); + if (servicesResult.Status() != GattCommunicationStatus::Success) { + error_msg = "GetGattServicesAsync failed"; + } else { + flutter::EncodableList servicesList; + for (auto service : servicesResult.Services()) { + { + std::lock_guard cache_lock(gatt_cache_mutex_); + service_cache_[remote_id].push_back(service); + } + flutter::EncodableMap serviceMap; + std::string serviceUuid = utils::to_uuid_string(service.Uuid()); + serviceMap[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + serviceMap[flutter::EncodableValue("service_uuid")] = flutter::EncodableValue(serviceUuid); + auto charsList = std::make_shared(); + co_await PopulateCharacteristicsAsync(service, remote_id, "", charsList); + serviceMap[flutter::EncodableValue("characteristics")] = *charsList; + servicesList.push_back(serviceMap); + + auto includedResult = co_await service.GetIncludedServicesAsync(BluetoothCacheMode::Cached); + if (includedResult.Status() == GattCommunicationStatus::Success) { + for (auto includedService : includedResult.Services()) { + { + std::lock_guard cache_lock(gatt_cache_mutex_); + service_cache_[remote_id].push_back(includedService); + } + flutter::EncodableMap includedMap; + std::string includedUuid = utils::to_uuid_string(includedService.Uuid()); + includedMap[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + includedMap[flutter::EncodableValue("service_uuid")] = flutter::EncodableValue(includedUuid); + includedMap[flutter::EncodableValue("primary_service_uuid")] = flutter::EncodableValue(serviceUuid); + auto includedCharsList = std::make_shared(); + co_await PopulateCharacteristicsAsync(includedService, remote_id, serviceUuid, includedCharsList); + includedMap[flutter::EncodableValue("characteristics")] = *includedCharsList; + servicesList.push_back(includedMap); + } + } + } + flutter::EncodableMap response; + response[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + response[flutter::EncodableValue("services")] = servicesList; + response[flutter::EncodableValue("success")] = flutter::EncodableValue(1); + response[flutter::EncodableValue("error_code")] = flutter::EncodableValue(0); + response[flutter::EncodableValue("error_string")] = flutter::EncodableValue("GATT_SUCCESS"); + co_await ui_thread_; + channel_->InvokeMethod("OnDiscoveredServices", std::make_unique(response)); + result_ptr->Success(flutter::EncodableValue(true)); + co_return; + } + } + } catch (const std::exception& e) { error_msg = e.what(); } + catch (...) { error_msg = "Unknown error"; } + + if (error_msg.empty()) error_msg = "Unknown error"; + co_await ui_thread_; + result_ptr->Error("discoverServices", error_msg); +} + +void FlutterBluePlusWinrtPlugin::OnCharacteristicValueChanged(std::string remote_id, const GattCharacteristic& sender, const GattValueChangedEventArgs& args) { + [this, remote_id, sender, args, alive = is_alive_]() -> winrt::fire_and_forget { + try { + co_await ui_thread_; + if (!*alive) co_return; + flutter::EncodableMap response; + response[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + response[flutter::EncodableValue("service_uuid")] = flutter::EncodableValue(utils::to_uuid_string(sender.Service().Uuid())); + response[flutter::EncodableValue("characteristic_uuid")] = flutter::EncodableValue(utils::to_uuid_string(sender.Uuid())); + response[flutter::EncodableValue("instance_id")] = flutter::EncodableValue(static_cast(sender.AttributeHandle())); + response[flutter::EncodableValue("value")] = flutter::EncodableValue(utils::to_vector(args.CharacteristicValue())); + response[flutter::EncodableValue("success")] = flutter::EncodableValue(1); + response[flutter::EncodableValue("error_code")] = flutter::EncodableValue(0); + response[flutter::EncodableValue("error_string")] = flutter::EncodableValue("GATT_SUCCESS"); + channel_->InvokeMethod("OnCharacteristicReceived", std::make_unique(response)); + } catch(...) {} + }(); +} + +void FlutterBluePlusWinrtPlugin::OnMaxPduSizeChanged(std::string remote_id, const GattSession& sender, const IInspectable&) { + [this, remote_id, sender, alive = is_alive_]() -> winrt::fire_and_forget { + try { + co_await ui_thread_; + if (!*alive) co_return; + flutter::EncodableMap response; + response[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + response[flutter::EncodableValue("mtu")] = flutter::EncodableValue(static_cast(sender.MaxPduSize())); + response[flutter::EncodableValue("success")] = flutter::EncodableValue(1); + response[flutter::EncodableValue("error_code")] = flutter::EncodableValue(0); + response[flutter::EncodableValue("error_string")] = flutter::EncodableValue(""); + channel_->InvokeMethod("OnMtuChanged", std::make_unique(response)); + } catch(...) {} + }(); +} + +winrt::fire_and_forget FlutterBluePlusWinrtPlugin::SetNotifyValueAsync(std::shared_ptr args_ptr) { + co_await winrt::resume_background(); + flutter::EncodableMap& args = *args_ptr; + std::string remote_id = utils::from_value(&args[flutter::EncodableValue("remote_id")]); + std::string service_uuid_hint = utils::from_value(&args[flutter::EncodableValue("service_uuid")]); + std::string characteristic_uuid_hint = utils::from_value(&args[flutter::EncodableValue("characteristic_uuid")]); + std::string primary_service_uuid_str = ""; + auto it_primary = args.find(flutter::EncodableValue("primary_service_uuid")); + if (it_primary != args.end()) primary_service_uuid_str = utils::from_value(&it_primary->second); + int instance_id = utils::from_value(&args[flutter::EncodableValue("instance_id")]); + bool enable = utils::from_value(&args[flutter::EncodableValue("enable")]); + bool force_indications = false; + auto it_force = args.find(flutter::EncodableValue("force_indications")); + if (it_force != args.end()) force_indications = utils::from_value(&it_force->second); + + std::string error_string = "Unknown Error"; + bool success_event = false; + std::vector return_value = {0x00, 0x00}; + GattCharacteristic targetChar = nullptr; + std::string actual_service_uuid = service_uuid_hint; + std::string actual_char_uuid = characteristic_uuid_hint; + + try { + BluetoothLEDevice device = nullptr; + co_await ui_thread_; + { + std::lock_guard lock(devices_mutex_); + auto it = std::find_if(connected_devices_.begin(), connected_devices_.end(), [&](const auto& pair) { return pair.first == remote_id; }); + if (it != connected_devices_.end()) device = it->second.as(); + } + co_await winrt::resume_background(); + if (!device) { error_string = "Device not connected"; } + else { + targetChar = co_await this->GetCharacteristicInternalAsync(device, remote_id, service_uuid_hint, characteristic_uuid_hint, primary_service_uuid_str, instance_id); + if (!targetChar) { error_string = "Characteristic not found"; } + else { + actual_service_uuid = utils::to_uuid_string(targetChar.Service().Uuid()); + actual_char_uuid = utils::to_uuid_string(targetChar.Uuid()); + std::string token_key = remote_id + ":" + actual_service_uuid + ":" + actual_char_uuid + ":" + std::to_string(instance_id); + + if (enable) { + GattClientCharacteristicConfigurationDescriptorValue cccdValue = GattClientCharacteristicConfigurationDescriptorValue::None; + auto props = targetChar.CharacteristicProperties(); + bool canNotify = (static_cast(props) & static_cast(GattCharacteristicProperties::Notify)) != 0; + bool canIndicate = (static_cast(props) & static_cast(GattCharacteristicProperties::Indicate)) != 0; + if (force_indications && canIndicate) { cccdValue = GattClientCharacteristicConfigurationDescriptorValue::Indicate; return_value = {0x02, 0x00}; } + else if (canNotify) { cccdValue = GattClientCharacteristicConfigurationDescriptorValue::Notify; return_value = {0x01, 0x00}; } + else if (canIndicate) { cccdValue = GattClientCharacteristicConfigurationDescriptorValue::Indicate; return_value = {0x02, 0x00}; } + else { error_string = "Notify/Indicate not supported"; goto send_event; } + + GattCommunicationStatus status = GattCommunicationStatus::ProtocolError; + try { status = co_await targetChar.WriteClientCharacteristicConfigurationDescriptorAsync(cccdValue); } catch (...) {} + if (status != GattCommunicationStatus::Success) { + GattDescriptor cccdDescriptor = co_await GetDescriptorAsync(targetChar, "2902"); + if (cccdDescriptor) { + auto writer = winrt::Windows::Storage::Streams::DataWriter(); + writer.ByteOrder(winrt::Windows::Storage::Streams::ByteOrder::LittleEndian); + writer.WriteUInt16((cccdValue == GattClientCharacteristicConfigurationDescriptorValue::Indicate) ? 2 : 1); + auto writeResult = co_await cccdDescriptor.WriteValueWithResultAsync(writer.DetachBuffer()); + status = writeResult.Status(); + } + } + + if (status != GattCommunicationStatus::Success && (actual_char_uuid == "2a05" || actual_char_uuid == "2A05")) { + status = GattCommunicationStatus::Success; + } + + if (status == GattCommunicationStatus::Success) { + co_await ui_thread_; + std::lock_guard cache_lock(gatt_cache_mutex_); + auto it = subscribed_characteristics_.find(token_key); + if (it != subscribed_characteristics_.end()) { + try { it->second.characteristic.as().ValueChanged(it->second.token); } catch(...) {} + subscribed_characteristics_.erase(it); + } + auto token = targetChar.ValueChanged([this, remote_id](GattCharacteristic const& sender, GattValueChangedEventArgs const& args) { + this->OnCharacteristicValueChanged(remote_id, sender, args); + }); + subscribed_characteristics_[token_key] = { targetChar, token }; + success_event = true; error_string = "GATT_SUCCESS"; + } else error_string = "Write CCCD failed: " + std::to_string((int)status); + } else { + return_value = {0x00, 0x00}; + GattCommunicationStatus status = co_await targetChar.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue::None); + if (status == GattCommunicationStatus::Success || actual_char_uuid == "2a05") { + co_await ui_thread_; + std::lock_guard cache_lock(gatt_cache_mutex_); + auto it = subscribed_characteristics_.find(token_key); + if (it != subscribed_characteristics_.end()) { + try { it->second.characteristic.as().ValueChanged(it->second.token); } catch(...) {} + subscribed_characteristics_.erase(it); + } + success_event = true; error_string = "GATT_SUCCESS"; + } else error_string = "Disable CCCD failed"; + } + } + } + } catch (const std::exception& ex) { error_string = ex.what(); } + catch (...) { error_string = "Unknown Exception"; } + +send_event: + co_await ui_thread_; + flutter::EncodableMap response; + response[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + response[flutter::EncodableValue("primary_service_uuid")] = primary_service_uuid_str.empty() ? flutter::EncodableValue() : flutter::EncodableValue(primary_service_uuid_str); + response[flutter::EncodableValue("service_uuid")] = flutter::EncodableValue(actual_service_uuid); + response[flutter::EncodableValue("characteristic_uuid")] = flutter::EncodableValue(actual_char_uuid); + response[flutter::EncodableValue("descriptor_uuid")] = flutter::EncodableValue("2902"); + response[flutter::EncodableValue("instance_id")] = flutter::EncodableValue(instance_id); + response[flutter::EncodableValue("value")] = flutter::EncodableValue(return_value); + response[flutter::EncodableValue("success")] = flutter::EncodableValue(success_event ? 1 : 0); + response[flutter::EncodableValue("error_code")] = flutter::EncodableValue(success_event ? 0 : 1); + response[flutter::EncodableValue("error_string")] = flutter::EncodableValue(error_string); + if (channel_) channel_->InvokeMethod("OnDescriptorWritten", std::make_unique(response)); +} + +winrt::fire_and_forget FlutterBluePlusWinrtPlugin::ReadCharacteristicAsync(flutter::EncodableMap args, std::unique_ptr> result) { + auto result_ptr = std::shared_ptr>(std::move(result)); + std::string error_msg; + try { + std::string remote_id = utils::from_value(&args[flutter::EncodableValue("remote_id")]); + std::string service_uuid_str = utils::from_value(&args[flutter::EncodableValue("service_uuid")]); + std::string characteristic_uuid_str = utils::from_value(&args[flutter::EncodableValue("characteristic_uuid")]); + int instance_id = utils::from_value(&args[flutter::EncodableValue("instance_id")]); + std::string primary_service_uuid_str = ""; + auto it_primary = args.find(flutter::EncodableValue("primary_service_uuid")); + if (it_primary != args.end()) primary_service_uuid_str = utils::from_value(&it_primary->second); + + BluetoothLEDevice device = nullptr; + { + std::lock_guard lock(devices_mutex_); + auto it = std::find_if(connected_devices_.begin(), connected_devices_.end(), [&](const auto& pair) { return pair.first == remote_id; }); + if (it != connected_devices_.end()) device = it->second.as(); + } + if (!device) { error_msg = "device is disconnected"; } + else { + co_await winrt::resume_background(); + GattCharacteristic targetChar = co_await this->GetCharacteristicInternalAsync(device, remote_id, service_uuid_str, characteristic_uuid_str, primary_service_uuid_str, instance_id); + if (!targetChar) { error_msg = "Characteristic not found"; } + else { + auto readResult = co_await targetChar.ReadValueAsync(BluetoothCacheMode::Uncached); + if (readResult.Status() == GattCommunicationStatus::Success) { + flutter::EncodableMap response; + response[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + response[flutter::EncodableValue("service_uuid")] = flutter::EncodableValue(service_uuid_str); + response[flutter::EncodableValue("characteristic_uuid")] = flutter::EncodableValue(characteristic_uuid_str); + response[flutter::EncodableValue("instance_id")] = flutter::EncodableValue(instance_id); + response[flutter::EncodableValue("value")] = flutter::EncodableValue(utils::to_vector(readResult.Value())); + response[flutter::EncodableValue("success")] = flutter::EncodableValue(1); + response[flutter::EncodableValue("error_code")] = flutter::EncodableValue(0); + response[flutter::EncodableValue("error_string")] = flutter::EncodableValue("GATT_SUCCESS"); + co_await ui_thread_; + channel_->InvokeMethod("OnCharacteristicReceived", std::make_unique(response)); + result_ptr->Success(flutter::EncodableValue(true)); + co_return; + } else { error_msg = "Read failed: " + std::to_string((int)readResult.Status()); } + } + } + } catch (const std::exception& e) { error_msg = e.what(); } + catch (...) { error_msg = "Unknown error"; } + + co_await ui_thread_; + result_ptr->Error("readCharacteristic", error_msg); +} + +winrt::fire_and_forget FlutterBluePlusWinrtPlugin::WriteCharacteristicAsync(flutter::EncodableMap args, std::unique_ptr> result) { + auto result_ptr = std::shared_ptr>(std::move(result)); + std::string error_msg; + try { + std::string remote_id = utils::from_value(&args[flutter::EncodableValue("remote_id")]); + std::string service_uuid_str = utils::from_value(&args[flutter::EncodableValue("service_uuid")]); + std::string characteristic_uuid_str = utils::from_value(&args[flutter::EncodableValue("characteristic_uuid")]); + int instance_id = utils::from_value(&args[flutter::EncodableValue("instance_id")]); + std::vector value = utils::from_value>(&args[flutter::EncodableValue("value")]); + int write_type = utils::from_value(&args[flutter::EncodableValue("write_type")]); + std::string primary_service_uuid_str = ""; + auto it_primary = args.find(flutter::EncodableValue("primary_service_uuid")); + if (it_primary != args.end()) primary_service_uuid_str = utils::from_value(&it_primary->second); + + BluetoothLEDevice device = nullptr; + { + std::lock_guard lock(devices_mutex_); + auto it = std::find_if(connected_devices_.begin(), connected_devices_.end(), [&](const auto& pair) { return pair.first == remote_id; }); + if (it != connected_devices_.end()) device = it->second.as(); + } + if (!device) { error_msg = "device is disconnected"; } + else { + co_await winrt::resume_background(); + GattCharacteristic targetChar = co_await this->GetCharacteristicInternalAsync(device, remote_id, service_uuid_str, characteristic_uuid_str, primary_service_uuid_str, instance_id); + if (!targetChar) { error_msg = "Characteristic not found"; } + else { + auto writer = winrt::Windows::Storage::Streams::DataWriter(); + writer.WriteBytes(value); + GattWriteOption option = (write_type == 1) ? GattWriteOption::WriteWithoutResponse : GattWriteOption::WriteWithResponse; + auto writeResult = co_await targetChar.WriteValueWithResultAsync(writer.DetachBuffer(), option); + + co_await ui_thread_; + flutter::EncodableMap response; + response[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + response[flutter::EncodableValue("service_uuid")] = flutter::EncodableValue(service_uuid_str); + response[flutter::EncodableValue("characteristic_uuid")] = flutter::EncodableValue(characteristic_uuid_str); + response[flutter::EncodableValue("instance_id")] = flutter::EncodableValue(instance_id); + response[flutter::EncodableValue("value")] = flutter::EncodableValue(value); + response[flutter::EncodableValue("success")] = flutter::EncodableValue(writeResult.Status() == GattCommunicationStatus::Success ? 1 : 0); + response[flutter::EncodableValue("error_code")] = flutter::EncodableValue((int)writeResult.Status()); + response[flutter::EncodableValue("error_string")] = flutter::EncodableValue(writeResult.Status() == GattCommunicationStatus::Success ? "GATT_SUCCESS" : "Write failed"); + channel_->InvokeMethod("OnCharacteristicWritten", std::make_unique(response)); + result_ptr->Success(flutter::EncodableValue(true)); + co_return; + } + } + } catch (const std::exception& e) { error_msg = e.what(); } + catch (...) { error_msg = "Unknown error"; } + + co_await ui_thread_; + result_ptr->Error("writeCharacteristic", error_msg); +} + +winrt::fire_and_forget FlutterBluePlusWinrtPlugin::ReadDescriptorAsync(flutter::EncodableMap args, std::unique_ptr> result) { + auto result_ptr = std::shared_ptr>(std::move(result)); + std::string error_msg; + try { + std::string remote_id = utils::from_value(&args[flutter::EncodableValue("remote_id")]); + std::string service_uuid_hint = utils::from_value(&args[flutter::EncodableValue("service_uuid")]); + std::string characteristic_uuid_hint = utils::from_value(&args[flutter::EncodableValue("characteristic_uuid")]); + std::string descriptor_uuid_hint = utils::from_value(&args[flutter::EncodableValue("descriptor_uuid")]); + int instance_id = utils::from_value(&args[flutter::EncodableValue("instance_id")]); + std::string primary_service_uuid_str = ""; + auto it_primary = args.find(flutter::EncodableValue("primary_service_uuid")); + if (it_primary != args.end()) primary_service_uuid_str = utils::from_value(&it_primary->second); + + BluetoothLEDevice device = nullptr; + { + std::lock_guard lock(devices_mutex_); + auto it = std::find_if(connected_devices_.begin(), connected_devices_.end(), [&](const auto& pair) { return pair.first == remote_id; }); + if (it != connected_devices_.end()) device = it->second.as(); + } + if (!device) { error_msg = "device is disconnected"; } + else { + co_await winrt::resume_background(); + + GattDescriptor targetDesc = nullptr; + if (instance_id != 0) { + std::string d_key = remote_id + ":" + std::to_string(instance_id); + std::lock_guard cache_lock(gatt_cache_mutex_); + auto it_d = descriptor_cache_.find(d_key); + if (it_d != descriptor_cache_.end()) targetDesc = it_d->second.as(); + } + + if (!targetDesc) { + GattCharacteristic targetChar = co_await this->GetCharacteristicByDescriptorHandleAsync(device, instance_id); + if (!targetChar) { + targetChar = co_await this->GetCharacteristicInternalAsync(device, remote_id, service_uuid_hint, characteristic_uuid_hint, primary_service_uuid_str, instance_id); + } + + if (targetChar) { + targetDesc = co_await GetDescriptorAsync(targetChar, descriptor_uuid_hint); + } + } + + if (!targetDesc) { error_msg = "Descriptor not found"; } + else { + auto readResult = co_await targetDesc.ReadValueAsync(BluetoothCacheMode::Uncached); + if (readResult.Status() == GattCommunicationStatus::Success) { + flutter::EncodableMap response; + response[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + response[flutter::EncodableValue("primary_service_uuid")] = primary_service_uuid_str.empty() ? flutter::EncodableValue() : flutter::EncodableValue(primary_service_uuid_str); + response[flutter::EncodableValue("service_uuid")] = flutter::EncodableValue(service_uuid_hint); + response[flutter::EncodableValue("characteristic_uuid")] = flutter::EncodableValue(characteristic_uuid_hint); + response[flutter::EncodableValue("descriptor_uuid")] = flutter::EncodableValue(utils::to_uuid_string(targetDesc.Uuid())); + response[flutter::EncodableValue("instance_id")] = flutter::EncodableValue(instance_id); + response[flutter::EncodableValue("value")] = flutter::EncodableValue(utils::to_vector(readResult.Value())); + response[flutter::EncodableValue("success")] = flutter::EncodableValue(1); + response[flutter::EncodableValue("error_code")] = flutter::EncodableValue(0); + response[flutter::EncodableValue("error_string")] = flutter::EncodableValue("GATT_SUCCESS"); + + co_await ui_thread_; + channel_->InvokeMethod("OnDescriptorRead", std::make_unique(response)); + result_ptr->Success(flutter::EncodableValue(true)); + co_return; + } else { error_msg = "Read failed: " + std::to_string((int)readResult.Status()); } + } + } + } catch (const std::exception& e) { error_msg = e.what(); } + catch (...) { error_msg = "Unknown error"; } + + co_await ui_thread_; + result_ptr->Error("readDescriptor", error_msg); +} + +winrt::fire_and_forget FlutterBluePlusWinrtPlugin::WriteDescriptorAsync(flutter::EncodableMap args, std::unique_ptr> result) { + auto result_ptr = std::shared_ptr>(std::move(result)); + std::string error_msg; + try { + std::string remote_id = utils::from_value(&args[flutter::EncodableValue("remote_id")]); + std::string service_uuid_hint = utils::from_value(&args[flutter::EncodableValue("service_uuid")]); + std::string characteristic_uuid_hint = utils::from_value(&args[flutter::EncodableValue("characteristic_uuid")]); + std::string descriptor_uuid_hint = utils::from_value(&args[flutter::EncodableValue("descriptor_uuid")]); + std::vector value = utils::from_value>(&args[flutter::EncodableValue("value")]); + int instance_id = utils::from_value(&args[flutter::EncodableValue("instance_id")]); + std::string primary_service_uuid_str = ""; + auto it_primary = args.find(flutter::EncodableValue("primary_service_uuid")); + if (it_primary != args.end()) primary_service_uuid_str = utils::from_value(&it_primary->second); + + BluetoothLEDevice device = nullptr; + { + std::lock_guard lock(devices_mutex_); + auto it = std::find_if(connected_devices_.begin(), connected_devices_.end(), [&](const auto& pair) { return pair.first == remote_id; }); + if (it != connected_devices_.end()) device = it->second.as(); + } + if (!device) { error_msg = "device is disconnected"; } + else { + co_await winrt::resume_background(); + + GattDescriptor targetDesc = nullptr; + if (instance_id != 0) { + std::string d_key = remote_id + ":" + std::to_string(instance_id); + std::lock_guard cache_lock(gatt_cache_mutex_); + auto it_d = descriptor_cache_.find(d_key); + if (it_d != descriptor_cache_.end()) targetDesc = it_d->second.as(); + } + + if (!targetDesc) { + GattCharacteristic targetChar = co_await this->GetCharacteristicInternalAsync(device, remote_id, service_uuid_hint, characteristic_uuid_hint, primary_service_uuid_str, instance_id); + if (targetChar) { + targetDesc = co_await GetDescriptorAsync(targetChar, descriptor_uuid_hint); + } + } + + if (!targetDesc) { error_msg = "Descriptor not found"; } + else { + auto writer = winrt::Windows::Storage::Streams::DataWriter(); + writer.WriteBytes(value); + auto writeResult = co_await targetDesc.WriteValueWithResultAsync(writer.DetachBuffer()); + if (writeResult.Status() == GattCommunicationStatus::Success) { + flutter::EncodableMap response; + response[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + response[flutter::EncodableValue("primary_service_uuid")] = primary_service_uuid_str.empty() ? flutter::EncodableValue() : flutter::EncodableValue(primary_service_uuid_str); + response[flutter::EncodableValue("service_uuid")] = flutter::EncodableValue(service_uuid_hint); + response[flutter::EncodableValue("characteristic_uuid")] = flutter::EncodableValue(characteristic_uuid_hint); + response[flutter::EncodableValue("descriptor_uuid")] = flutter::EncodableValue(utils::to_uuid_string(targetDesc.Uuid())); + response[flutter::EncodableValue("instance_id")] = flutter::EncodableValue(instance_id); + response[flutter::EncodableValue("value")] = flutter::EncodableValue(value); + response[flutter::EncodableValue("success")] = flutter::EncodableValue(1); + response[flutter::EncodableValue("error_code")] = flutter::EncodableValue(0); + response[flutter::EncodableValue("error_string")] = flutter::EncodableValue("GATT_SUCCESS"); + + co_await ui_thread_; + channel_->InvokeMethod("OnDescriptorWritten", std::make_unique(response)); + result_ptr->Success(flutter::EncodableValue(true)); + co_return; + } else { error_msg = "Write failed: " + std::to_string((int)writeResult.Status()); } + } + } + } catch (const std::exception& e) { error_msg = e.what(); } + catch (...) { error_msg = "Unknown error"; } + + co_await ui_thread_; + result_ptr->Error("writeDescriptor", error_msg); +} + +void FlutterBluePlusWinrtPlugin::ClearDeviceResources(std::string remote_id) { + // Guards every cache touched below against concurrent background-thread + // access (service discovery / read / write). This function performs no + // co_await, so holding the lock for its whole body is safe. + std::lock_guard cache_lock(gatt_cache_mutex_); + + for (auto it = subscribed_characteristics_.begin(); it != subscribed_characteristics_.end(); ) { + if (it->first.find(remote_id) == 0) { + try { + auto characteristic = it->second.characteristic.as(); + if (characteristic) characteristic.ValueChanged(it->second.token); + } catch (...) {} + it = subscribed_characteristics_.erase(it); + } else { + ++it; + } + } + + for (auto it = characteristic_cache_.begin(); it != characteristic_cache_.end(); ) { + if (it->first.find(remote_id) == 0) it = characteristic_cache_.erase(it); + else ++it; + } + for (auto it = descriptor_cache_.begin(); it != descriptor_cache_.end(); ) { + if (it->first.find(remote_id) == 0) it = descriptor_cache_.erase(it); + else ++it; + } + + auto it_s = service_cache_.find(remote_id); + if (it_s != service_cache_.end()) { + for (auto& inspectable : it_s->second) { + try { + auto service = inspectable.as(); + if (service) service.Close(); + } catch (...) {} + } + service_cache_.erase(it_s); + } + + // Cleanup sessions and MTU tokens + auto it_gs = gatt_sessions_.find(remote_id); + if (it_gs != gatt_sessions_.end()) { + try { + auto session = it_gs->second.as(); + auto it_mtu = mtu_tokens_.find(remote_id); + if (it_mtu != mtu_tokens_.end()) { + session.MaxPduSizeChanged(it_mtu->second); + mtu_tokens_.erase(it_mtu); + } + session.Close(); + } catch(...) {} + gatt_sessions_.erase(it_gs); + } + + rssi_cache_.erase(remote_id); + + // The device is Closed by the caller in the same disconnect flow, so the + // handler stops firing; just drop the token entry to keep the map bounded. + connection_tokens_.erase(remote_id); +} + +winrt::fire_and_forget FlutterBluePlusWinrtPlugin::PeriodicConnectionCheck() { + // Copy the shared alive flag into the coroutine frame so it stays valid even + // after the plugin object is destroyed. Check it right after every resume, + // before touching any `this` member. + auto alive = is_alive_; + while (*alive) { + co_await winrt::resume_after(winrt::Windows::Foundation::TimeSpan(20000000)); + if (!*alive) co_return; + co_await ui_thread_; + if (!*alive) co_return; + + std::vector mac_to_remove; + { + std::lock_guard lock(devices_mutex_); + for (size_t i = 0; i < connected_devices_.size(); ++i) { + const auto& pair = connected_devices_[i]; + std::string remote_id = pair.first; + auto device = pair.second.as(); + bool is_connected = false; + try { if (device) is_connected = (device.ConnectionStatus() == BluetoothConnectionStatus::Connected); } catch (...) {} + if (!is_connected) { + Log("PeriodicCheck: Device %s disconnected, marking for removal.", remote_id.c_str()); + mac_to_remove.push_back(remote_id); + } + } + } + + for (const auto& remote_id : mac_to_remove) { + ClearDeviceResources(remote_id); + if (channel_) { + flutter::EncodableMap connection_state; + connection_state[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + connection_state[flutter::EncodableValue("connection_state")] = flutter::EncodableValue(0); + channel_->InvokeMethod("OnConnectionStateChanged", std::make_unique(connection_state)); + } + + std::vector to_close; + { + std::lock_guard lock(devices_mutex_); + auto it = connected_devices_.begin(); + while (it != connected_devices_.end()) { + if (it->first == remote_id) { + try { auto d = it->second.as(); if (d) to_close.push_back(d); } catch(...) {} + it = connected_devices_.erase(it); + } else { + ++it; + } + } + } + + for (auto& d : to_close) { try { d.Close(); } catch(...) {} } + } + } +} + +void FlutterBluePlusWinrtPlugin::HandleMethodCall(const flutter::MethodCall& method_call, std::unique_ptr> result) { + const auto& method = method_call.method_name(); + if (method == "flutterRestart") { + int count = 0; + watcher_.Stop(); + + std::vector to_close; + std::vector devices_to_clear; + { + std::lock_guard lock(devices_mutex_); + count = static_cast(connected_devices_.size()); + + for (size_t i = 0; i < connected_devices_.size(); ++i) { + const auto& pair = connected_devices_[i]; + try { if (auto device = pair.second.as()) to_close.push_back(device); } catch(...) {} + devices_to_clear.push_back(pair.first); + } + for (size_t i = 0; i < currently_connecting_devices_.size(); ++i) { + const auto& pair = currently_connecting_devices_[i]; + try { if (auto device = pair.second.as()) to_close.push_back(device); } catch(...) {} + } + + connected_devices_.clear(); + currently_connecting_devices_.clear(); + } + + for (const auto& remote_id : devices_to_clear) { + ClearDeviceResources(remote_id); + } + + { + std::lock_guard cache_lock(gatt_cache_mutex_); + rssi_cache_.clear(); + scan_results_cache_.clear(); + subscribed_characteristics_.clear(); + characteristic_cache_.clear(); + descriptor_cache_.clear(); + service_cache_.clear(); + } + + for (auto& d : to_close) { try { d.Close(); } catch(...) {} } + + result->Success(flutter::EncodableValue(count)); return; + } + if (method == "startScan") { scan_results_cache_.clear(); watcher_.Start(); result->Success(flutter::EncodableValue(true)); return; } + if (method == "stopScan") { watcher_.Stop(); result->Success(flutter::EncodableValue(true)); return; } + if (method == "getSystemDevices") { GetSystemDevicesAsync(std::move(result)); return; } + if (method == "getAdapterState") { GetAdapterStateAsync(std::move(result)); return; } + if (method == "connect") { + const auto* args = std::get_if(method_call.arguments()); + std::string remote_id = ""; if (args) { auto it = args->find(flutter::EncodableValue("remote_id")); if (it != args->end()) remote_id = utils::from_value(&it->second); } + ConnectAsync(remote_id, std::move(result)); return; + } + if (method == "disconnect") { + const auto* remote_id_val_ptr = std::get_if(method_call.arguments()); + if (remote_id_val_ptr) { + std::string remote_id = *remote_id_val_ptr; + + std::vector to_close; + + { + std::lock_guard lock(devices_mutex_); + { + auto it = currently_connecting_devices_.begin(); + while (it != currently_connecting_devices_.end()) { + if (it->first == remote_id) { + try { if (auto device = it->second.as()) to_close.push_back(device); } catch(...) {} + it = currently_connecting_devices_.erase(it); + } else { + ++it; + } + } + } + + { + auto it = connected_devices_.begin(); + while (it != connected_devices_.end()) { + if (it->first == remote_id) { + try { if (auto device = it->second.as()) to_close.push_back(device); } catch(...) {} + it = connected_devices_.erase(it); + } else { + ++it; + } + } + } + } + + for (auto& d : to_close) { try { d.Close(); } catch(...) {} } + + ClearDeviceResources(remote_id); + + flutter::EncodableMap connection_state; connection_state[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); connection_state[flutter::EncodableValue("connection_state")] = flutter::EncodableValue(0); + channel_->InvokeMethod("OnConnectionStateChanged", std::make_unique(connection_state)); + } + result->Success(flutter::EncodableValue(true)); return; + } + if (method == "discoverServices") { + const auto* remote_id_val = std::get_if(method_call.arguments()); + if (remote_id_val) DiscoverServicesAsync(*remote_id_val, std::move(result)); + else result->Error("discoverServices", "Invalid arguments"); + return; + } + if (method == "setNotifyValue") { + const auto* args = std::get_if(method_call.arguments()); + if (args) { auto args_ptr = std::make_shared(*args); result->Success(flutter::EncodableValue(true)); SetNotifyValueAsync(args_ptr); } + else result->Error("setNotifyValue", "Invalid arguments"); + return; + } + if (method == "readCharacteristic") { + const auto* args = std::get_if(method_call.arguments()); + if (args) ReadCharacteristicAsync(*args, std::move(result)); else result->Error("readCharacteristic", "Invalid arguments"); + return; + } + if (method == "writeCharacteristic") { + const auto* args = std::get_if(method_call.arguments()); + if (args) WriteCharacteristicAsync(*args, std::move(result)); else result->Error("writeCharacteristic", "Invalid arguments"); + return; + } + if (method == "readDescriptor") { + const auto* args = std::get_if(method_call.arguments()); + if (args) ReadDescriptorAsync(*args, std::move(result)); else result->Error("readDescriptor", "Invalid arguments"); + return; + } + if (method == "writeDescriptor") { + const auto* args = std::get_if(method_call.arguments()); + if (args) WriteDescriptorAsync(*args, std::move(result)); else result->Error("writeDescriptor", "Invalid arguments"); + return; + } + if (method == "setLogLevel") { result->Success(flutter::EncodableValue(true)); return; } + if (method == "setOptions") { result->Success(flutter::EncodableValue(true)); return; } + if (method == "isSupported") { result->Success(flutter::EncodableValue(true)); return; } + if (method == "requestMtu") { + const auto* args = std::get_if(method_call.arguments()); + std::string remote_id = ""; + if (args) { + auto it = args->find(flutter::EncodableValue("remote_id")); + if (it != args->end()) remote_id = utils::from_value(&it->second); + } + if (!remote_id.empty()) { + winrt::Windows::Foundation::IInspectable session_obj = nullptr; + { + std::lock_guard cache_lock(gatt_cache_mutex_); + auto it = gatt_sessions_.find(remote_id); + if (it != gatt_sessions_.end()) session_obj = it->second; + } + if (session_obj) { + auto session = session_obj.as(); + flutter::EncodableMap response; + response[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + response[flutter::EncodableValue("mtu")] = flutter::EncodableValue(static_cast(session.MaxPduSize())); + response[flutter::EncodableValue("success")] = flutter::EncodableValue(1); + response[flutter::EncodableValue("error_code")] = flutter::EncodableValue(0); + response[flutter::EncodableValue("error_string")] = flutter::EncodableValue(""); + channel_->InvokeMethod("OnMtuChanged", std::make_unique(response)); + } + } + result->Success(flutter::EncodableValue(true)); + return; + } + if (method == "requestConnectionPriority") { result->Success(flutter::EncodableValue(true)); return; } + if (method == "setPreferredPhy") { result->Success(flutter::EncodableValue(true)); return; } + if (method == "turnOn") { result->Success(flutter::EncodableValue(false)); return; } + if (method == "turnOff") { result->Success(flutter::EncodableValue(false)); return; } + if (method == "clearGattCache") { result->Success(flutter::EncodableValue(true)); return; } + if (method == "getPhySupport") { + flutter::EncodableMap response; + response[flutter::EncodableValue("le_2m")] = flutter::EncodableValue(true); + response[flutter::EncodableValue("le_coded")] = flutter::EncodableValue(true); + result->Success(flutter::EncodableValue(response)); + return; + } + if (method == "createBond") { + const auto* args = std::get_if(method_call.arguments()); + std::string remote_id = ""; if (args) { auto it = args->find(flutter::EncodableValue("remote_id")); if (it != args->end()) remote_id = utils::from_value(&it->second); } + CreateBondAsync(remote_id, std::move(result)); return; + } + if (method == "removeBond") { + const auto* remote_id_val = std::get_if(method_call.arguments()); + if (remote_id_val) RemoveBondAsync(*remote_id_val, std::move(result)); + else result->Error("removeBond", "Invalid arguments"); + return; + } + if (method == "getBondState") { + const auto* remote_id_val = std::get_if(method_call.arguments()); + if (remote_id_val) GetBondStateAsync(*remote_id_val, std::move(result)); + else result->Error("getBondState", "Invalid arguments"); + return; + } + if (method == "getBondedDevices") { + GetBondedDevicesAsync(std::move(result)); + return; + } + if (method == "connectedCount") { + int count = 0; + { + std::lock_guard lock(devices_mutex_); + count = static_cast(connected_devices_.size()); + } + result->Success(flutter::EncodableValue(count)); + return; + } + if (method == "readRssi") { + const auto* remote_id_arg = std::get_if(method_call.arguments()); + if (remote_id_arg) { + std::string remote_id = *remote_id_arg; + flutter::EncodableMap rssi_result; + rssi_result[flutter::EncodableValue("remote_id")] = flutter::EncodableValue(remote_id); + rssi_result[flutter::EncodableValue("error_code")] = flutter::EncodableValue(0); + rssi_result[flutter::EncodableValue("error_string")] = flutter::EncodableValue("GATT_SUCCESS"); + + auto it = rssi_cache_.find(remote_id); + if (it != rssi_cache_.end()) { + rssi_result[flutter::EncodableValue("rssi")] = flutter::EncodableValue(it->second); + } else { + rssi_result[flutter::EncodableValue("rssi")] = flutter::EncodableValue(0); + } + rssi_result[flutter::EncodableValue("success")] = flutter::EncodableValue(1); + rssi_result[flutter::EncodableValue("error_code")] = flutter::EncodableValue(0); + rssi_result[flutter::EncodableValue("error_string")] = flutter::EncodableValue("GATT_SUCCESS"); + channel_->InvokeMethod("OnReadRssi", std::make_unique(rssi_result)); + result->Success(flutter::EncodableValue(true)); + } else result->Error("readRssi", "Invalid arguments"); + return; + } + + result->NotImplemented(); +} + +} // namespace flutter_blue_plus_winrt diff --git a/packages/flutter_blue_plus_winrt/windows/flutter_blue_plus_winrt_plugin.h b/packages/flutter_blue_plus_winrt/windows/flutter_blue_plus_winrt_plugin.h new file mode 100644 index 00000000..85bce396 --- /dev/null +++ b/packages/flutter_blue_plus_winrt/windows/flutter_blue_plus_winrt_plugin.h @@ -0,0 +1,223 @@ +#ifndef FLUTTER_PLUGIN_FLUTTER_BLUE_PLUS_WINRT_PLUGIN_H_ +#define FLUTTER_PLUGIN_FLUTTER_BLUE_PLUS_WINRT_PLUGIN_H_ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace flutter_blue_plus_winrt { + +struct SubscribedCharacteristic { + winrt::Windows::Foundation::IInspectable characteristic = nullptr; + winrt::event_token token; +}; + +class FlutterBluePlusWinrtPlugin : public flutter::Plugin { + public: + static void RegisterWithRegistrar(flutter::PluginRegistrarWindows *registrar); + + FlutterBluePlusWinrtPlugin(flutter::PluginRegistrarWindows* registrar); + + ~FlutterBluePlusWinrtPlugin(); + + // Disallow copy and assign. + FlutterBluePlusWinrtPlugin(const FlutterBluePlusWinrtPlugin&) = delete; + FlutterBluePlusWinrtPlugin& operator=(const FlutterBluePlusWinrtPlugin&) = delete; + + private: + void HandleMethodCall( + const flutter::MethodCall &method_call, + std::unique_ptr> result); + + flutter::PluginRegistrarWindows* registrar_; + std::unique_ptr> channel_; + + winrt::Windows::Devices::Bluetooth::Advertisement::BluetoothLEAdvertisementWatcher watcher_{}; + winrt::event_token received_token_{}; + winrt::event_token stopped_token_{}; + + // Shared "alive" flag. Held by a shared_ptr so that fire-and-forget + // coroutines / event handlers can capture a copy and safely check whether + // the plugin has been destroyed after resuming, instead of dereferencing a + // dangling `this`. + std::shared_ptr> is_alive_ = std::make_shared>(true); + + // UI Thread context + winrt::apartment_context ui_thread_; + + std::mutex devices_mutex_; + std::vector> connected_devices_{}; + std::vector> currently_connecting_devices_{}; + std::map rssi_cache_{}; + + std::map> scan_results_cache_{}; + + // Map to store event tokens and characteristic objects for notifications + std::map subscribed_characteristics_{}; + + // Guards the GATT object caches below (characteristic_cache_, + // descriptor_cache_, service_cache_, subscribed_characteristics_, + // gatt_sessions_, mtu_tokens_, connection_tokens_). These are accessed both + // from background thread-pool continuations (service discovery / read / + // write) and from the UI thread (ClearDeviceResources on disconnect), so + // every access must hold this mutex. NOTE: never hold this across a + // `co_await`. + std::mutex gatt_cache_mutex_; + + // Caches for GATT objects to avoid repeated discovery + std::map characteristic_cache_{}; + std::map descriptor_cache_{}; + std::map> service_cache_{}; + + // MTU / Session Handling + std::map gatt_sessions_{}; + std::map mtu_tokens_{}; + + // Per-device ConnectionStatusChanged event tokens, so the handlers can be + // revoked (in ClearDeviceResources and the destructor) and never fire into a + // destroyed plugin. Guarded by gatt_cache_mutex_. + std::map connection_tokens_{}; + + void OnAdvertisementReceived( + const winrt::Windows::Devices::Bluetooth::Advertisement::BluetoothLEAdvertisementWatcher&, + const winrt::Windows::Devices::Bluetooth::Advertisement::BluetoothLEAdvertisementReceivedEventArgs&); + + void OnAdvertisementStopped( + const winrt::Windows::Devices::Bluetooth::Advertisement::BluetoothLEAdvertisementWatcher&, + const winrt::Windows::Devices::Bluetooth::Advertisement::BluetoothLEAdvertisementWatcherStoppedEventArgs&); + + winrt::fire_and_forget GetSystemDevicesAsync( + std::unique_ptr> result); + + winrt::fire_and_forget GetAdapterStateAsync( + std::unique_ptr> result); + + winrt::fire_and_forget GetBondedDevicesAsync( + std::unique_ptr> result); + + winrt::fire_and_forget CreateBondAsync( + std::string remote_id, + std::unique_ptr> result); + + winrt::fire_and_forget RemoveBondAsync( + std::string remote_id, + std::unique_ptr> result); + + winrt::fire_and_forget GetBondStateAsync( + std::string remote_id, + std::unique_ptr> result); + + winrt::fire_and_forget ConnectAsync( + std::string remote_id, + std::unique_ptr> result); + + winrt::fire_and_forget DiscoverServicesAsync( + std::string remote_id, + std::unique_ptr> result); + + winrt::fire_and_forget SetNotifyValueAsync( + std::shared_ptr args); + + winrt::fire_and_forget ReadCharacteristicAsync( + flutter::EncodableMap args, + std::unique_ptr> result); + + winrt::fire_and_forget WriteCharacteristicAsync( + flutter::EncodableMap args, + std::unique_ptr> result); + + winrt::fire_and_forget ReadDescriptorAsync( + flutter::EncodableMap args, + std::unique_ptr> result); + + winrt::fire_and_forget WriteDescriptorAsync( + flutter::EncodableMap args, + std::unique_ptr> result); + + void OnConnectionStatusChanged( + const winrt::Windows::Devices::Bluetooth::BluetoothLEDevice&, + const winrt::Windows::Foundation::IInspectable&); + + void OnCharacteristicValueChanged( + std::string remote_id, + const winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::GattCharacteristic& sender, + const winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::GattValueChangedEventArgs& args); + + void OnMaxPduSizeChanged( + std::string remote_id, + const winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::GattSession& sender, + const winrt::Windows::Foundation::IInspectable& args); + + winrt::fire_and_forget PeriodicConnectionCheck(); + + std::string uint64_to_mac_string(uint64_t addr); + + // --- Private GATT Helpers --- + winrt::Windows::Foundation::IAsyncOperation + GetCharacteristicInternalAsync( + winrt::Windows::Devices::Bluetooth::BluetoothLEDevice device, + std::string remote_id, + std::string service_uuid_str, + std::string characteristic_uuid_str, + std::string primary_service_uuid_str, + int instance_id); + + winrt::Windows::Foundation::IAsyncOperation + GetCharacteristicByHandleAsync( + winrt::Windows::Devices::Bluetooth::BluetoothLEDevice device, + int instance_id); + + winrt::Windows::Foundation::IAsyncOperation + GetCharacteristicAsync( + winrt::Windows::Devices::Bluetooth::BluetoothLEDevice device, + std::string service_uuid_str, + std::string characteristic_uuid_str, + std::string primary_service_uuid_str, + int instance_id); + + winrt::Windows::Foundation::IAsyncOperation + FindCharacteristicInServiceAsync( + winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::GattDeviceService service, + int instance_id, + winrt::Windows::Devices::Bluetooth::BluetoothCacheMode cacheMode); + + winrt::Windows::Foundation::IAsyncOperation + FindCharacteristicByDescriptorHandleInServiceAsync( + winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::GattDeviceService service, + int descriptor_handle, + winrt::Windows::Devices::Bluetooth::BluetoothCacheMode cacheMode); + + winrt::Windows::Foundation::IAsyncOperation + GetCharacteristicByDescriptorHandleAsync( + winrt::Windows::Devices::Bluetooth::BluetoothLEDevice device, + int descriptor_handle); + + winrt::Windows::Foundation::IAsyncOperation + GetDescriptorAsync( + winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::GattCharacteristic characteristic, + std::string descriptor_uuid_str); + + winrt::Windows::Foundation::IAsyncAction PopulateCharacteristicsAsync( + winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::GattDeviceService service, + std::string remote_id, + std::string primaryServiceUuid, + std::shared_ptr outList); + + void ClearDeviceResources(std::string remote_id); +}; + +} // namespace flutter_blue_plus_winrt + +#endif // FLUTTER_PLUGIN_FLUTTER_BLUE_PLUS_WINRT_PLUGIN_H_ diff --git a/packages/flutter_blue_plus_winrt/windows/flutter_blue_plus_winrt_plugin_c_api.cpp b/packages/flutter_blue_plus_winrt/windows/flutter_blue_plus_winrt_plugin_c_api.cpp new file mode 100644 index 00000000..8866882f --- /dev/null +++ b/packages/flutter_blue_plus_winrt/windows/flutter_blue_plus_winrt_plugin_c_api.cpp @@ -0,0 +1,12 @@ +#include "include/flutter_blue_plus_winrt/flutter_blue_plus_plugin.h" + +#include + +#include "flutter_blue_plus_winrt_plugin.h" + +void FlutterBluePlusPluginRegisterWithRegistrar( + FlutterDesktopPluginRegistrarRef registrar) { + flutter_blue_plus_winrt::FlutterBluePlusWinrtPlugin::RegisterWithRegistrar( + flutter::PluginRegistrarManager::GetInstance() + ->GetRegistrar(registrar)); +} diff --git a/packages/flutter_blue_plus_winrt/windows/include/flutter_blue_plus_winrt/flutter_blue_plus_plugin.h b/packages/flutter_blue_plus_winrt/windows/include/flutter_blue_plus_winrt/flutter_blue_plus_plugin.h new file mode 100644 index 00000000..1306e8cc --- /dev/null +++ b/packages/flutter_blue_plus_winrt/windows/include/flutter_blue_plus_winrt/flutter_blue_plus_plugin.h @@ -0,0 +1,23 @@ +#ifndef FLUTTER_PLUGIN_FLUTTER_BLUE_PLUS_WINRT_PLUGIN_C_API_H_ +#define FLUTTER_PLUGIN_FLUTTER_BLUE_PLUS_WINRT_PLUGIN_C_API_H_ + +#include + +#ifdef FLUTTER_PLUGIN_IMPL +#define FLUTTER_PLUGIN_EXPORT __declspec(dllexport) +#else +#define FLUTTER_PLUGIN_EXPORT __declspec(dllimport) +#endif + +#if defined(__cplusplus) +extern "C" { +#endif + +FLUTTER_PLUGIN_EXPORT void FlutterBluePlusPluginRegisterWithRegistrar( + FlutterDesktopPluginRegistrarRef registrar); + +#if defined(__cplusplus) +} // extern "C" +#endif + +#endif // FLUTTER_PLUGIN_FLUTTER_BLUE_PLUS_WINRT_PLUGIN_C_API_H_