Skip to content

Commit

Permalink
Improve example app for system connected devices (#123)
Browse files Browse the repository at this point in the history
* Improve example app for system connected devices

* Add print when no filter is set for system devices

---------

Co-authored-by: Foti Dim <[email protected]>
  • Loading branch information
rohitsangwan01 and fotiDim authored Jan 7, 2025
1 parent 07498db commit 17f834e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions darwin/Classes/UniversalBlePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ private class BleCentralDarwin: NSObject, UniversalBlePlatformChannel, CBCentral
func getSystemDevices(withServices: [String], completion: @escaping (Result<[UniversalBleScanResult], Error>) -> Void) {
var servicesFilter = withServices
if servicesFilter.isEmpty {
print("No services filter was set for getting system connected devices. Using default services...")

// Add several generic services
servicesFilter = ["1800", "1801", "180A", "180D", "1810", "181B", "1808", "181D", "1816", "1814", "181A", "1802", "1803", "1804", "1815", "1805", "1807", "1806", "1848", "185E", "180F", "1812", "180E", "1813"]
}
Expand Down
13 changes: 11 additions & 2 deletions example/lib/home/home.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:universal_ble/universal_ble.dart';
import 'package:universal_ble_example/data/mock_universal_ble.dart';
Expand Down Expand Up @@ -72,11 +73,19 @@ class _MyAppState extends State<MyApp> {
}

Future<void> _getSystemDevices() async {
// For macOS and iOS, it is recommended to set a filter to get system devices
if (defaultTargetPlatform == TargetPlatform.macOS ||
defaultTargetPlatform == TargetPlatform.iOS &&
(scanFilter?.withServices ?? []).isEmpty) {
showSnackbar(
"No services filter was set for getting system connected devices. Using default services...");
}

List<BleDevice> devices = await UniversalBle.getSystemDevices(
withServices: scanFilter?.withServices,
);
if (devices.isEmpty) {
showSnackbar("No Connected Devices Found");
showSnackbar("No System Connected Devices Found");
}
setState(() {
_bleDevices.clear();
Expand Down Expand Up @@ -191,7 +200,7 @@ class _MyAppState extends State<MyApp> {
),
if (BleCapabilities.supportsConnectedDevicesApi)
PlatformButton(
text: 'Connected Devices',
text: 'System Devices',
onPressed: _getSystemDevices,
),
PlatformButton(
Expand Down

0 comments on commit 17f834e

Please sign in to comment.