### NuGet Package Installation and C++/WinRT Integration Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/windows/CMakeLists.txt This snippet demonstrates how to fetch, install, and integrate the Microsoft.Windows.CppWinRT NuGet package using CMake. It ensures the package is available, installs it into a 'packages' directory, and configures the project to use its build properties and targets. ```cmake cmake_minimum_required(VERSION 3.21) set(PROJECT_NAME "ble_peripheral") project(${PROJECT_NAME} LANGUAGES CXX) cmake_policy(VERSION 3.14...3.25) include(FetchContent) set(CPPWINRT_VERSION "2.0.220418.1") set(PLUGIN_NAME "ble_peripheral_plugin") # ############### NuGet intall begin ################ FetchContent_Declare(nuget URL "https://dist.nuget.org/win-x86-commandline/v6.0.0/nuget.exe" URL_HASH SHA256=04eb6c4fe4213907e2773e1be1bbbd730e9a655a3c9c58387ce8d4a714a5b9e1 DOWNLOAD_NO_EXTRACT true ) find_program(NUGET nuget) if(NOT NUGET) message("Nuget.exe not found, trying to download or use cached version.") FetchContent_MakeAvailable(nuget) set(NUGET ${nuget_SOURCE_DIR}/nuget.exe) endif() execute_process(COMMAND ${NUGET} install "Microsoft.Windows.CppWinRT" -Version ${CPPWINRT_VERSION} -OutputDirectory packages WORKING_DIRECTORY ${CMAKE_BINARY_DIR} RESULT_VARIABLE ret) if(NOT ret EQUAL 0) message(FATAL_ERROR "Failed to install nuget package Microsoft.Windows.CppWinRT.${CPPWINRT_VERSION}") endif() # ############### NuGet install end ################ # ############### NuGet import begin ################ set_target_properties(${PLUGIN_NAME} PROPERTIES VS_PROJECT_IMPORT ${CMAKE_BINARY_DIR}/packages/Microsoft.Windows.CppWinRT.${CPPWINRT_VERSION}/build/native/Microsoft.Windows.CppWinRT.props ) target_link_libraries(${PLUGIN_NAME} PRIVATE ${CMAKE_BINARY_DIR}/packages/Microsoft.Windows.CppWinRT.${CPPWINRT_VERSION}/build/native/Microsoft.Windows.CppWinRT.targets ) # ############### NuGet import end ################ ``` -------------------------------- ### Installation Rules Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/example/windows/CMakeLists.txt Configures the installation process for the project, specifying where the executable, data files, libraries, and assets should be placed relative to the installation prefix. It ensures that assets are re-copied on each build and installs the AOT library for non-Debug builds. ```cmake set(BUILD_BUNDLE_DIR "$") set(CMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD 1) if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) set(CMAKE_INSTALL_PREFIX "${BUILD_BUNDLE_DIR}" CACHE PATH "..." FORCE) endif() set(INSTALL_BUNDLE_DATA_DIR "${CMAKE_INSTALL_PREFIX}/data") set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}") install(TARGETS ${BINARY_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}" COMPONENT Runtime) install(FILES "${FLUTTER_ICU_DATA_FILE}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime) install(FILES "${FLUTTER_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" COMPONENT Runtime) if(PLUGIN_BUNDLED_LIBRARIES) install(FILES "${PLUGIN_BUNDLED_LIBRARIES}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" COMPONENT Runtime) endif() set(FLUTTER_ASSET_DIR_NAME "flutter_assets") install(CODE " file(REMOVE_RECURSE \"${INSTALL_BUNDLE_DATA_DIR}/${FLUTTER_ASSET_DIR_NAME}\") " COMPONENT Runtime) install(DIRECTORY "${PROJECT_BUILD_DIR}/${FLUTTER_ASSET_DIR_NAME}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime) install(FILES "${AOT_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" CONFIGURATIONS Profile;Release COMPONENT Runtime) ``` -------------------------------- ### CMake Project Setup and Executable Definition Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/example/windows/runner/CMakeLists.txt Configures the CMake project, defines the minimum required version, sets the project name, and adds the main executable target with its source files and Windows-specific resources. ```cmake cmake_minimum_required(VERSION 3.14) project(runner LANGUAGES CXX) add_executable(${BINARY_NAME} WIN32 "flutter_window.cpp" "main.cpp" "utils.cpp" "win32_window.cpp" "${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc" "Runner.rc" "runner.exe.manifest" ) ``` -------------------------------- ### Flutter Library Setup Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/example/windows/flutter/CMakeLists.txt Configures the main Flutter library and its associated headers and data files. It defines the Flutter library target, including include directories and linking to the Flutter library. ```cmake cmake_minimum_required(VERSION 3.14) set(EPHEMERAL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ephemeral") # Configuration provided via flutter tool. include(${EPHEMERAL_DIR}/generated_config.cmake) # TODO: Move the rest of this into files in ephemeral. See # https://github.com/flutter/flutter/issues/57146. set(WRAPPER_ROOT "${EPHEMERAL_DIR}/cpp_client_wrapper") # Set fallback configurations for older versions of the flutter tool. if (NOT DEFINED FLUTTER_TARGET_PLATFORM) set(FLUTTER_TARGET_PLATFORM "windows-x64") endif() # === Flutter Library === set(FLUTTER_LIBRARY "${EPHEMERAL_DIR}/flutter_windows.dll") # Published to parent scope for install step. set(FLUTTER_LIBRARY ${FLUTTER_LIBRARY} PARENT_SCOPE) set(FLUTTER_ICU_DATA_FILE "${EPHEMERAL_DIR}/icudtl.dat" PARENT_SCOPE) set(PROJECT_BUILD_DIR "${PROJECT_DIR}/build/" PARENT_SCOPE) set(AOT_LIBRARY "${PROJECT_DIR}/build/windows/app.so" PARENT_SCOPE) list(APPEND FLUTTER_LIBRARY_HEADERS "flutter_export.h" "flutter_windows.h" "flutter_messenger.h" "flutter_plugin_registrar.h" "flutter_texture_registrar.h" ) list(TRANSFORM FLUTTER_LIBRARY_HEADERS PREPEND "${EPHEMERAL_DIR}/") add_library(flutter INTERFACE) target_include_directories(flutter INTERFACE "${EPHEMERAL_DIR}" ) target_link_libraries(flutter INTERFACE "${FLUTTER_LIBRARY}.lib") add_dependencies(flutter flutter_assemble) ``` -------------------------------- ### Start and Stop BLE Advertising Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/README.md Initiates BLE advertising with specified services and a local name. It also includes setting up a callback to monitor the advertising status (started, stopped, or failed). ```dart /// set callback for advertising state BlePeripheral.setAdvertisingStatusUpdateCallback((bool advertising, String? error) { print("AdvertisingStatus: $advertising Error $error") }); // Start advertising await BlePeripheral.startAdvertising( services: [serviceBattery], localName: "TestBle", ); await BlePeripheral.stopAdvertising(); ``` -------------------------------- ### Open Xcode Workspace Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/README.md Instructions on how to open the Flutter project's Xcode workspace to access and modify launch screen assets. ```bash open ios/Runner.xcworkspace ``` -------------------------------- ### Project Configuration and Build Settings Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/example/windows/CMakeLists.txt Sets the minimum CMake version, project name, executable name, and manages build configurations (Debug, Profile, Release) based on whether the generator is multi-config. It also defines standard compiler flags and includes. ```cmake cmake_minimum_required(VERSION 3.14) project(example LANGUAGES CXX) set(BINARY_NAME "example") cmake_policy(SET CMP0063 NEW) get_property(IS_MULTICONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) if(IS_MULTICONFIG) set(CMAKE_CONFIGURATION_TYPES "Debug;Profile;Release" CACHE STRING "" FORCE) else() if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Flutter build mode" FORCE) set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Profile" "Release") endif() endif() set(CMAKE_EXE_LINKER_FLAGS_PROFILE "${CMAKE_EXE_LINKER_FLAGS_RELEASE}") set(CMAKE_SHARED_LINKER_FLAGS_PROFILE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE}") set(CMAKE_C_FLAGS_PROFILE "${CMAKE_C_FLAGS_RELEASE}") set(CMAKE_CXX_FLAGS_PROFILE "${CMAKE_CXX_FLAGS_RELEASE}") add_definitions(-DUNICODE -D_UNICODE) ``` -------------------------------- ### Link Libraries and Include Directories Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/example/windows/runner/CMakeLists.txt Links the necessary libraries (flutter, flutter_wrapper_app, dwmapi.lib) and sets the include directory for the project's executable target. This ensures all dependencies are correctly resolved during the build process. ```cmake target_link_libraries(${BINARY_NAME} PRIVATE flutter flutter_wrapper_app) target_link_libraries(${BINARY_NAME} PRIVATE "dwmapi.lib") target_include_directories(${BINARY_NAME} PRIVATE "${CMAKE_SOURCE_DIR}") ``` -------------------------------- ### Apply Standard Build Settings Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/example/windows/runner/CMakeLists.txt Applies a standard set of build settings to the project's executable target. This is a convenience function that can be customized for specific build requirements. ```cmake apply_standard_settings(${BINARY_NAME}) ``` -------------------------------- ### Shared Library Definition and Linking Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/windows/CMakeLists.txt This snippet defines a C++ shared library named 'ble_peripheral_plugin'. It includes source files, applies standard CMake settings, sets visibility presets, defines compile-time macros, and links against necessary libraries like Flutter and its wrapper. ```cmake list(APPEND PLUGIN_SOURCES "Utils.cpp" "Utils.h" "BlePeripheral.g.cpp" "BlePeripheral.g.h" "ble_peripheral_plugin.cpp" "ble_peripheral_plugin.h" "ui_thread_handler.hpp" ) add_library(${PLUGIN_NAME} SHARED "include/ble_peripheral/ble_peripheral_plugin_c_api.h" "ble_peripheral_plugin_c_api.cpp" ${PLUGIN_SOURCES} ) apply_standard_settings(${PLUGIN_NAME}) set_target_properties(${PLUGIN_NAME} PROPERTIES CXX_VISIBILITY_PRESET hidden) target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL) target_include_directories(${PLUGIN_NAME} INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include") target_link_libraries(${PLUGIN_NAME} PRIVATE flutter flutter_wrapper_plugin) set(ble_peripheral_bundled_libraries "" PARENT_SCOPE ) ``` -------------------------------- ### Flutter and Runner Integration Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/example/windows/CMakeLists.txt Includes the Flutter managed directory and the runner subdirectory, integrating Flutter's build system and application logic into the main project build. It also includes generated plugin configurations. ```cmake set(FLUTTER_MANAGED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/flutter") add_subdirectory(${FLUTTER_MANAGED_DIR}) add_subdirectory("runner") include(flutter/generated_plugins.cmake) ``` -------------------------------- ### Standard Compilation Settings Function Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/example/windows/CMakeLists.txt Defines a reusable CMake function `APPLY_STANDARD_SETTINGS` to apply common compilation features, options, and definitions to a target, ensuring consistent build settings across different parts of the project. ```cmake function(APPLY_STANDARD_SETTINGS TARGET) target_compile_features(${TARGET} PUBLIC cxx_std_17) target_compile_options(${TARGET} PRIVATE /W4 /WX /wd"4100") target_compile_options(${TARGET} PRIVATE /EHsc) target_compile_definitions(${TARGET} PRIVATE "_HAS_EXCEPTIONS=0") target_compile_definitions(${TARGET} PRIVATE "$:_DEBUG") endfunction() ``` -------------------------------- ### Flutter Tool Backend Custom Command Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/example/windows/flutter/CMakeLists.txt Configures a custom command to execute the Flutter tool backend. This command is designed to run every time by using a phony output file, ensuring that build artifacts are generated by the Flutter tool. ```cmake # === Flutter tool backend === # _phony_ is a non-existent file to force this command to run every time, # since currently there's no way to get a full input/output list from the # flutter tool. set(PHONY_OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/_phony_") set_source_files_properties("${PHONY_OUTPUT}" PROPERTIES SYMBOLIC TRUE) add_custom_command( OUTPUT ${FLUTTER_LIBRARY} ${FLUTTER_LIBRARY_HEADERS} ${CPP_WRAPPER_SOURCES_CORE} ${CPP_WRAPPER_SOURCES_PLUGIN} ${CPP_WRAPPER_SOURCES_APP} ${PHONY_OUTPUT} COMMAND ${CMAKE_COMMAND} -E env ${FLUTTER_TOOL_ENVIRONMENT} "${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.bat" ${FLUTTER_TARGET_PLATFORM} $ VERBATIM ) add_custom_target(flutter_assemble DEPENDS "${FLUTTER_LIBRARY}" ${FLUTTER_LIBRARY_HEADERS} ${CPP_WRAPPER_SOURCES_CORE} ${CPP_WRAPPER_SOURCES_PLUGIN} ${CPP_WRAPPER_SOURCES_APP} ) ``` -------------------------------- ### C++ Wrapper Library for Plugins Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/example/windows/flutter/CMakeLists.txt Defines a static C++ library for Flutter plugins, including core implementations and plugin registrar sources. It sets up independent compilation and visibility, linking against the Flutter library. ```cmake # === Wrapper === list(APPEND CPP_WRAPPER_SOURCES_CORE "core_implementations.cc" "standard_codec.cc" ) list(TRANSFORM CPP_WRAPPER_SOURCES_CORE PREPEND "${WRAPPER_ROOT}/") list(APPEND CPP_WRAPPER_SOURCES_PLUGIN "plugin_registrar.cc" ) list(TRANSFORM CPP_WRAPPER_SOURCES_PLUGIN PREPEND "${WRAPPER_ROOT}/") list(APPEND CPP_WRAPPER_SOURCES_APP "flutter_engine.cc" "flutter_view_controller.cc" ) list(TRANSFORM CPP_WRAPPER_SOURCES_APP PREPEND "${WRAPPER_ROOT}/") # Wrapper sources needed for a plugin. add_library(flutter_wrapper_plugin STATIC ${CPP_WRAPPER_SOURCES_CORE} ${CPP_WRAPPER_SOURCES_PLUGIN} ) apply_standard_settings(flutter_wrapper_plugin) set_target_properties(flutter_wrapper_plugin PROPERTIES POSITION_INDEPENDENT_CODE ON) set_target_properties(flutter_wrapper_plugin PROPERTIES CXX_VISIBILITY_PRESET hidden) target_link_libraries(flutter_wrapper_plugin PUBLIC flutter) target_include_directories(flutter_wrapper_plugin PUBLIC "${WRAPPER_ROOT}/include" ) add_dependencies(flutter_wrapper_plugin flutter_assemble) ``` -------------------------------- ### Replace Launch Screen Images in Xcode Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/README.md Steps to replace launch screen images within the Xcode Project Navigator by dropping in new image files. ```text 1. Open your Flutter project's Xcode project with `open ios/Runner.xcworkspace`. 2. Select `Runner/Assets.xcassets` in the Project Navigator. 3. Drop in the desired images to replace existing ones. ``` -------------------------------- ### Initialize Ble Peripheral Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/README.md Initializes the BLE peripheral functionality. Requires Bluetooth permissions to be granted beforehand. ```dart await BlePeripheral.initialize(); ``` -------------------------------- ### C++ Wrapper Library for Runner Application Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/example/windows/flutter/CMakeLists.txt Defines a static C++ library for the Flutter runner application, incorporating core implementations and application-specific sources. It links against the Flutter library and specifies include directories. ```cmake # Wrapper sources needed for the runner. add_library(flutter_wrapper_app STATIC ${CPP_WRAPPER_SOURCES_CORE} ${CPP_WRAPPER_SOURCES_APP} ) apply_standard_settings(flutter_wrapper_app) target_link_libraries(flutter_wrapper_app PUBLIC flutter) target_include_directories(flutter_wrapper_app PUBLIC "${WRAPPER_ROOT}/include" ) add_dependencies(flutter_wrapper_app flutter_assemble) ``` -------------------------------- ### Add Flutter Assemble Dependency Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/example/windows/runner/CMakeLists.txt Ensures that the Flutter assembly process is completed before the main application target is built, guaranteeing that all Flutter-related assets and code are available. ```cmake add_dependencies(${BINARY_NAME} flutter_assemble) ``` -------------------------------- ### Define Flutter Version Compile Definitions Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/example/windows/runner/CMakeLists.txt Adds preprocessor definitions to the build for various components of the Flutter version, allowing the application to access version information at compile time. ```cmake target_compile_definitions(${BINARY_NAME} PRIVATE "FLUTTER_VERSION=\"${FLUTTER_VERSION}\"") target_compile_definitions(${BINARY_NAME} PRIVATE "FLUTTER_VERSION_MAJOR=${FLUTTER_VERSION_MAJOR}") target_compile_definitions(${BINARY_NAME} PRIVATE "FLUTTER_VERSION_MINOR=${FLUTTER_VERSION_MINOR}") target_compile_definitions(${BINARY_NAME} PRIVATE "FLUTTER_VERSION_PATCH=${FLUTTER_VERSION_PATCH}") target_compile_definitions(${BINARY_NAME} PRIVATE "FLUTTER_VERSION_BUILD=${FLUTTER_VERSION_BUILD}") ``` -------------------------------- ### Add and Manage BLE Services Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/README.md Demonstrates how to add a BLE service with characteristics, retrieve a list of all added services, remove a specific service, and clear all services. Services and characteristics are defined using UUIDs and properties. ```dart String serviceBattery = "0000180F-0000-1000-8000-00805F9B34FB"; await BlePeripheral.addService( BleService( uuid: serviceBattery, primary: true, characteristics: [ BleCharacteristic( uuid: "00002A19-0000-1000-8000-00805F9B34FB", properties: [ CharacteristicProperties.read.index, CharacteristicProperties.notify.index ], value: null, permissions: [ AttributePermissions.readable.index ], ), ], ), ); // To get list of added services await BlePeripheral.getServices(); // To remove any specific services await BlePeripheral.removeService(String serviceId); // To remove all added services await BlePeripheral.clearServices(); ``` -------------------------------- ### Disable Windows Macros Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/example/windows/runner/CMakeLists.txt Disables specific Windows macros (NOMINMAX) that might conflict with standard C++ library functions, preventing potential naming collisions. ```cmake target_compile_definitions(${BINARY_NAME} PRIVATE "NOMINMAX") ``` -------------------------------- ### Android Bluetooth Permissions Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/README.md Specifies the necessary Bluetooth permissions required in the `AndroidManifest.xml` file for Android devices to utilize BLE peripheral functionalities. This includes permissions for general Bluetooth, administration, connection, advertising, and scanning. ```xml ``` -------------------------------- ### iOS/macOS Bluetooth Permissions Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/README.md Details the required Bluetooth permission entry for `info.plist` on iOS and macOS. This key is essential for the application to advertise as a BLE peripheral. ```xml NSBluetoothAlwaysUsageDescription For advertise as ble peripheral ``` -------------------------------- ### BLE Communication Callbacks Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/README.md Provides essential callbacks for managing BLE communication with central devices. This includes notifications when a central device subscribes to a characteristic, connection state changes (Android only), and when a central device attempts to read or write to a characteristic. ```dart // Called when central subscribes to a characteristic BlePeripheral.setCharacteristicSubscriptionChangeCallback(CharacteristicSubscriptionChangeCallback callback); // Android only, Called when central connected/disconnected BlePeripheral.setConnectionStateChangeCallback(ConnectionStateChangeCallback callback); // Called when advertisement started, stopped or failed BlePeripheral.setAdvertisingStatusUpdateCallback(AdvertisementStatusUpdateCallback callback); // Called when Bluetooth radio on device turned on/off BlePeripheral.setBleStateChangeCallback(BleStateCallback callback); // Called when Central device tries to read a characteristics BlePeripheral.setReadRequestCallback(ReadRequestCallback callback); // When central tries to write to a characteristic BlePeripheral.setWriteRequestCallback(WriteRequestCallback callback); // Called when service added successfully BlePeripheral.setServiceAddedCallback(ServiceAddedCallback callback); // Called when mtu changed, on Apple and Windows, this will be called when a device subscribes to a characteristic BlePeripheral.setMtuChangeCallback(MtuChangeCallback callback); // Only available on Android, Called when central paired/unpaired BlePeripheral.setBondStateChangeCallback(BondStateCallback callback); ``` -------------------------------- ### Update Characteristic Value Source: https://github.com/rohitsangwan01/ble_peripheral/blob/main/README.md Allows updating the value of a characteristic. This update can be targeted to a specific connected central device by providing a `deviceId`, or if `deviceId` is omitted, all subscribed devices will receive the update. ```dart BlePeripheral.updateCharacteristic(characteristicId: characteristicTest,value: utf8.encode("Test Data")); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.