Enhance getSystemDevice API on macOS with Broader Default Services Fi… (#121)

* Enhance getSystemDevice API on macOS with Broader Default Services Filter

* Update docs

* Update darwin/Classes/UniversalBlePlugin.swift

Co-authored-by: Foti Dim <foti@navideck.com>

* Reorder fields

* Use ScanFilter ui for getting system devices

---------

Co-authored-by: Foti Dim <fdimanidis@gmail.com>
Co-authored-by: Foti Dim <foti@navideck.com>
This commit is contained in:
Rohit Sangwan
2025-01-02 10:28:51 +00:00
committed by GitHub
parent dd75894244
commit f3411280e9
9 changed files with 40 additions and 25 deletions
+3
View File
@@ -1,3 +1,6 @@
## 0.14.0
* `getSystemDevices(withServices:)` now sets several generic services by default as filter
## 0.14.0
* BREAKING CHANGE: `bleDevice.name` now filters out non-printable characters
* Add `bleDevice.rawName`
+3 -3
View File
@@ -102,9 +102,9 @@ See the [Bluetooth Availability](#bluetooth-availability) section for more.
Already connected devices, connected either through previous sessions, other apps or through system settings, won't show up as scan results. You can get those using `getSystemDevices()`.
```dart
// Get already connected devices
// You can set `withServices` to narrow down the results
// On `Apple`, `withServices` is required to get connected devices, else [1800] service will be used as default filter.
// Get already connected devices.
// You can set `withServices` to narrow down the results.
// On `Apple`, `withServices` is required to get any connected devices. If not passed, several [18XX] generic services will be set by default.
List<BleDevice> devices = await UniversalBle.getSystemDevices(withServices: []);
```
+8 -5
View File
@@ -52,11 +52,11 @@ private class BleCentralDarwin: NSObject, UniversalBlePlatformChannel, CBCentral
func enableBluetooth(completion: @escaping (Result<Bool, Error>) -> Void) {
completion(Result.failure(PigeonError(code: "NotSupported", message: nil, details: nil)))
}
func disableBluetooth(completion: @escaping (Result<Bool, any Error>) -> Void) {
completion(Result.failure(PigeonError(code: "NotSupported", message: nil, details: nil)))
}
func startScan(filter: UniversalScanFilter?) throws {
// If filter has any other filter other than official one
let usesCustomFilters = filter?.usesCustomFilters ?? false
@@ -291,9 +291,12 @@ private class BleCentralDarwin: NSObject, UniversalBlePlatformChannel, CBCentral
}
func getSystemDevices(withServices: [String], completion: @escaping (Result<[UniversalBleScanResult], Error>) -> Void) {
var filterCBUUID = withServices.map { CBUUID(string: $0) }
// We can't keep this filter empty, so adding a default filter
if filterCBUUID.isEmpty { filterCBUUID.append(CBUUID(string: "1800")) }
var servicesFilter = withServices
if servicesFilter.isEmpty {
// 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"]
}
var filterCBUUID = servicesFilter.map { CBUUID(string: $0) }
let bleDevices = manager.retrieveConnectedPeripherals(withServices: filterCBUUID)
bleDevices.forEach { discoveredPeripherals[$0.uuid.uuidString] = $0 }
completion(Result.success(bleDevices.map {
+2
View File
@@ -5,9 +5,11 @@
*.swp
.DS_Store
.atom/
.build/
.buildlog/
.history
.svn/
.swiftpm/
migrate_working_dir/
# IntelliJ related
+14 -11
View File
@@ -71,6 +71,19 @@ class _MyAppState extends State<MyApp> {
);
}
Future<void> _getSystemDevices() async {
List<BleDevice> devices = await UniversalBle.getSystemDevices(
withServices: scanFilter?.withServices,
);
if (devices.isEmpty) {
showSnackbar("No Connected Devices Found");
}
setState(() {
_bleDevices.clear();
_bleDevices.addAll(devices);
});
}
void _showScanFilterBottomSheet() {
showModalBottomSheet(
isScrollControlled: true,
@@ -179,17 +192,7 @@ class _MyAppState extends State<MyApp> {
if (BleCapabilities.supportsConnectedDevicesApi)
PlatformButton(
text: 'Connected Devices',
onPressed: () async {
List<BleDevice> devices =
await UniversalBle.getSystemDevices();
if (devices.isEmpty) {
showSnackbar("No Connected Devices Found");
}
setState(() {
_bleDevices.clear();
_bleDevices.addAll(devices);
});
},
onPressed: _getSystemDevices,
),
PlatformButton(
text: 'Queue: ${_queueType.name}',
@@ -115,19 +115,19 @@ class _ScanFilterWidgetState extends State<ScanFilterWidget> {
const Divider(),
const SizedBox(height: 10),
TextFormField(
controller: widget.servicesFilterController,
controller: widget.namePrefixController,
maxLines: 2,
decoration: const InputDecoration(
labelText: "Services",
labelText: "Name Prefixes",
border: OutlineInputBorder(),
),
),
const SizedBox(height: 10),
TextFormField(
controller: widget.namePrefixController,
controller: widget.servicesFilterController,
maxLines: 2,
decoration: const InputDecoration(
labelText: "Name Prefixes",
labelText: "Services",
border: OutlineInputBorder(),
),
),
+4
View File
@@ -6,4 +6,8 @@ class AppDelegate: FlutterAppDelegate {
override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
override func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
return true
}
}
+1 -1
View File
@@ -292,7 +292,7 @@ class UniversalBle {
/// Get connected devices to the system (connected by any app).
/// Use [withServices] to filter devices by services.
/// On `Apple`, [withServices] is required to get connected devices, else [1800] service will be used as default filter.
/// On `Apple`, [withServices] is required to get any connected devices. If not passed, several [18XX] generic services will be set by default.
/// On `Android`, `Linux` and `Windows`, if [withServices] is used, then internally all services will be discovered for each device first (either by connecting or by using cached services).
/// Not supported on `Web`.
static Future<List<BleDevice>> getSystemDevices({
+1 -1
View File
@@ -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.14.0
version: 0.15.0
homepage: https://navideck.com
repository: https://github.com/Navideck/universal_ble
issue_tracker: https://github.com/Navideck/universal_ble/issues