From 3be945c3d59968e10ecde8eca024091218df23ac Mon Sep 17 00:00:00 2001 From: Foti Dim Date: Thu, 2 May 2024 00:09:44 +0200 Subject: [PATCH] Improve manufacturer data discovery on Windows (#38) * Improve manufacturer data discovery on Windows * Switch pairing protection level to encrypted in Windows * Minor UI improvement in example app * Update windows/src/universal_ble_plugin.h * Remove minimum DevicePairingProtectionLevel * Improve logging * Bump version --------- Co-authored-by: Rohit Sangwan --- CHANGELOG.md | 4 +++ .../lib/home/widgets/scanned_item_widget.dart | 10 +++---- .../peripheral_detail_page.dart | 4 +-- pubspec.yaml | 2 +- windows/src/universal_ble_plugin.cpp | 27 ++++++++++--------- windows/src/universal_ble_plugin.h | 2 +- 6 files changed, 27 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5c5139..2c37aac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.9.8 +* Improve manufacturer data discovery on Windows +* Example app improvements + ## 0.9.7 * Fix characteristic keying on linux allowing receiving data from multiple BLE devices at the same time diff --git a/example/lib/home/widgets/scanned_item_widget.dart b/example/lib/home/widgets/scanned_item_widget.dart index aea9106..54589d8 100644 --- a/example/lib/home/widgets/scanned_item_widget.dart +++ b/example/lib/home/widgets/scanned_item_widget.dart @@ -22,13 +22,13 @@ class ScannedItemWidget extends StatelessWidget { crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(scanResult.deviceId), - // Show manufacturer data only on web and desktop Visibility( - visible: (Platform.isWeb || Platform.isDesktop) && - scanResult.manufacturerData?.isNotEmpty == true, + visible: scanResult.manufacturerData?.isNotEmpty == true, child: Text( - ManufacturerData.fromData(scanResult.manufacturerData!) - .toString(), + Platform.isWeb || Platform.isDesktop + ? ManufacturerData.fromData(scanResult.manufacturerData!) + .toString() + : 'ManufacturerCompanyId: ${ManufacturerData.fromData(scanResult.manufacturerData!).companyIdRadix16}', ), ), Visibility( diff --git a/example/lib/peripheral_details/peripheral_detail_page.dart b/example/lib/peripheral_details/peripheral_detail_page.dart index dca3e64..1f2326e 100644 --- a/example/lib/peripheral_details/peripheral_detail_page.dart +++ b/example/lib/peripheral_details/peripheral_detail_page.dart @@ -56,7 +56,7 @@ class _PeripheralDetailPageState extends State { void _addLog(String type, dynamic data) { setState(() { - _logs.add('$type : ${data.toString()}'); + _logs.add('$type: ${data.toString()}'); }); } @@ -85,7 +85,7 @@ class _PeripheralDetailPageState extends State { void _handlePairingStateChange( String deviceId, bool isPaired, String? error) { print('OnPairStateChange $deviceId, $isPaired'); - if (error != null) { + if (error != null && error.isNotEmpty) { _addLog("PairStateChangeError", "(Paired: $isPaired): $error "); } else { _addLog("PairStateChange", isPaired); diff --git a/pubspec.yaml b/pubspec.yaml index 2ef8cdb..e756284 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: universal_ble description: A cross-platform (Android/iOS/macOS/Windows/Linux/Web) Bluetooth Low Energy (BLE) plugin for Flutter -version: 0.9.7 +version: 0.9.8 homepage: https://navideck.com repository: https://github.com/Navideck/universal_ble issue_tracker: https://github.com/Navideck/universal_ble/issues diff --git a/windows/src/universal_ble_plugin.cpp b/windows/src/universal_ble_plugin.cpp index 0b5db1d..de7609e 100644 --- a/windows/src/universal_ble_plugin.cpp +++ b/windows/src/universal_ble_plugin.cpp @@ -411,6 +411,7 @@ namespace universal_ble auto customPairing = deviceInformation.Pairing().Custom(); winrt::event_token token = customPairing.PairingRequested([this](const Enumeration::DeviceInformationCustomPairing &sender, const Enumeration::DevicePairingRequestedEventArgs &eventArgs) { + std::cout << "PairLog: Pairing requested" << std::endl; // eventArgs.AcceptWithPasswordCredential(nullptr, nullptr); // eventArgs.Pin(); // Accept all pairing request @@ -419,8 +420,7 @@ namespace universal_ble // DevicePairingProtectionLevel => Default, None, Encryption, EncryptionAndAuthentication std::cout << "PairLog: Trying to pair" << std::endl; auto async_c = customPairing.PairAsync( - Enumeration::DevicePairingKinds::ConfirmOnly, - Enumeration::DevicePairingProtectionLevel::None); + Enumeration::DevicePairingKinds::ConfirmOnly); async_c.Completed([this, customPairing, token, device_id](IAsyncOperation const &sender, AsyncStatus const args) { auto result = sender.GetResults(); @@ -549,7 +549,7 @@ namespace universal_ble // Send device to callback channel // if device is already discovered in deviceWatcher then merge the scan result - void UniversalBlePlugin::pushUniversalScanResult(UniversalBleScanResult scanResult) + void UniversalBlePlugin::pushUniversalScanResult(UniversalBleScanResult scanResult, bool isConnectable) { auto it = scanResults.find(scanResult.device_id()); if (it != scanResults.end()) @@ -590,8 +590,11 @@ namespace universal_ble scanResults.insert(std::make_pair(scanResult.device_id(), scanResult)); } - uiThreadHandler_.Post([scanResult] - { callbackChannel->OnScanResult(scanResult, SuccessCallback, ErrorCallback); }); + if (isConnectable) + { + uiThreadHandler_.Post([scanResult] + { callbackChannel->OnScanResult(scanResult, SuccessCallback, ErrorCallback); }); + } } void UniversalBlePlugin::setupDeviceWatcher() @@ -690,7 +693,7 @@ namespace universal_ble universalScanResult.set_rssi(rssi); } - pushUniversalScanResult(universalScanResult); + pushUniversalScanResult(universalScanResult, true); } } @@ -699,10 +702,6 @@ namespace universal_ble { try { - // Avoid devices if they are not connectable - if (!args.IsConnectable()) - return; - // Apply ManufacturerData filter if (!manufacturerScanFilter.empty()) { @@ -717,6 +716,7 @@ namespace universal_ble auto deviceId = _mac_address_to_str(args.BluetoothAddress()); auto universalScanResult = UniversalBleScanResult(deviceId); std::string name = winrt::to_string(args.Advertisement().LocalName()); + auto manufacturerData = parseManufacturerDataHead(args.Advertisement(), deviceId); auto dataSection = args.Advertisement().DataSections(); for (auto &&data : dataSection) @@ -737,8 +737,9 @@ namespace universal_ble if (!name.empty()) universalScanResult.set_name(name); - auto manufacturerData = parseManufacturerDataHead(args.Advertisement(), deviceId); - universalScanResult.set_manufacturer_data_head(manufacturerData); + if (!manufacturerData.empty()) + universalScanResult.set_manufacturer_data_head(manufacturerData); + universalScanResult.set_rssi(args.RawSignalStrengthInDBm()); // Add services @@ -765,7 +766,7 @@ namespace universal_ble universalScanResult.set_name(winrt::to_string(deviceInfo.Name())); } - pushUniversalScanResult(universalScanResult); + pushUniversalScanResult(universalScanResult, args.IsConnectable()); } catch (...) { diff --git a/windows/src/universal_ble_plugin.h b/windows/src/universal_ble_plugin.h index 50e62eb..daf2683 100644 --- a/windows/src/universal_ble_plugin.h +++ b/windows/src/universal_ble_plugin.h @@ -101,7 +101,7 @@ namespace universal_ble void setupDeviceWatcher(); void disposeDeviceWatcher(); - void pushUniversalScanResult(UniversalBleScanResult scanResult); + void pushUniversalScanResult(UniversalBleScanResult scanResult, bool isConnectable); void BluetoothLEWatcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args); void onDeviceInfoReceived(DeviceInformation deviceInfo);