Skip to content

Commit 813585b

Browse files
Implement getPairedDevices api
1 parent 28b0bc6 commit 813585b

File tree

15 files changed

+222
-165
lines changed

15 files changed

+222
-165
lines changed

android/src/main/kotlin/com/navideck/universal_ble/UniversalBle.g.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ interface UniversalBlePlatformChannel {
256256
fun writeValue(deviceId: String, service: String, characteristic: String, value: ByteArray, bleOutputProperty: Long, callback: (Result<Unit>) -> Unit)
257257
fun isPaired(deviceId: String, callback: (Result<Boolean>) -> Unit)
258258
fun pair(deviceId: String)
259+
fun getPairedDevices(): List<UniversalBleScanResult>
259260
fun unPair(deviceId: String)
260261
fun getSystemDevices(withServices: List<String>, callback: (Result<List<UniversalBleScanResult>>) -> Unit)
261262
fun getConnectionState(deviceId: String): Long
@@ -520,6 +521,21 @@ interface UniversalBlePlatformChannel {
520521
channel.setMessageHandler(null)
521522
}
522523
}
524+
run {
525+
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.getPairedDevices$separatedMessageChannelSuffix", codec)
526+
if (api != null) {
527+
channel.setMessageHandler { _, reply ->
528+
val wrapped: List<Any?> = try {
529+
listOf<Any?>(api.getPairedDevices())
530+
} catch (exception: Throwable) {
531+
wrapError(exception)
532+
}
533+
reply.reply(wrapped)
534+
}
535+
} else {
536+
channel.setMessageHandler(null)
537+
}
538+
}
523539
run {
524540
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.unPair$separatedMessageChannelSuffix", codec)
525541
if (api != null) {

android/src/main/kotlin/com/navideck/universal_ble/UniversalBlePlugin.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,20 @@ class UniversalBlePlugin : UniversalBlePlatformChannel, BluetoothGattCallback(),
496496
}
497497
}
498498

499+
override fun getPairedDevices(): List<UniversalBleScanResult> {
500+
val bondedDevices: Set<BluetoothDevice> = bluetoothManager.adapter.bondedDevices;
501+
return bondedDevices.map {
502+
UniversalBleScanResult(
503+
name = it.name,
504+
deviceId = it.address,
505+
isPaired = it.bondState == BOND_BONDED,
506+
manufacturerDataHead = null,
507+
manufacturerData = null,
508+
rssi = null,
509+
)
510+
}
511+
}
512+
499513
override fun unPair(deviceId: String) {
500514
val remoteDevice: BluetoothDevice =
501515
bluetoothManager.adapter.getRemoteDevice(deviceId)

darwin/Classes/UniversalBle.g.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ protocol UniversalBlePlatformChannel {
257257
func writeValue(deviceId: String, service: String, characteristic: String, value: FlutterStandardTypedData, bleOutputProperty: Int64, completion: @escaping (Result<Void, Error>) -> Void)
258258
func isPaired(deviceId: String, completion: @escaping (Result<Bool, Error>) -> Void)
259259
func pair(deviceId: String) throws
260+
func getPairedDevices() throws -> [UniversalBleScanResult]
260261
func unPair(deviceId: String) throws
261262
func getSystemDevices(withServices: [String], completion: @escaping (Result<[UniversalBleScanResult], Error>) -> Void)
262263
func getConnectionState(deviceId: String) throws -> Int64
@@ -484,6 +485,19 @@ class UniversalBlePlatformChannelSetup {
484485
} else {
485486
pairChannel.setMessageHandler(nil)
486487
}
488+
let getPairedDevicesChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.getPairedDevices\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
489+
if let api = api {
490+
getPairedDevicesChannel.setMessageHandler { _, reply in
491+
do {
492+
let result = try api.getPairedDevices()
493+
reply(wrapResult(result))
494+
} catch {
495+
reply(wrapError(error))
496+
}
497+
}
498+
} else {
499+
getPairedDevicesChannel.setMessageHandler(nil)
500+
}
487501
let unPairChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.unPair\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
488502
if let api = api {
489503
unPairChannel.setMessageHandler { message, reply in

darwin/Classes/UniversalBlePlugin.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,11 @@ private class BleCentralDarwin: NSObject, UniversalBlePlatformChannel, CBCentral
287287
func unPair(deviceId _: String) throws {
288288
throw FlutterError(code: "NotSupported", message: nil, details: nil)
289289
}
290-
290+
291+
func getPairedDevices() throws -> [UniversalBleScanResult] {
292+
throw FlutterError(code: "NotSupported", message: nil, details: nil)
293+
}
294+
291295
func getSystemDevices(withServices: [String], completion: @escaping (Result<[UniversalBleScanResult], Error>) -> Void) {
292296
var filterCBUUID = withServices.map { CBUUID(string: $0) }
293297
// We can't keep this filter empty, so adding a default filter

example/lib/data/mock_universal_ble.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,9 @@ class MockUniversalBle extends UniversalBlePlatform {
111111
Future<BleConnectionState> getConnectionState(String deviceId) {
112112
throw UnimplementedError();
113113
}
114+
115+
@override
116+
Future<List<BleDevice>> getPairedDevices() async {
117+
return <BleDevice>[];
118+
}
114119
}

lib/src/universal_ble_linux/universal_ble_linux.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,14 @@ class UniversalBleLinux extends UniversalBlePlatform {
286286
}
287287
}
288288

289+
@override
290+
Future<List<BleDevice>> getPairedDevices() async {
291+
return _client.devices
292+
.where((e) => e.paired)
293+
.map((e) => e.toBleDevice())
294+
.toList();
295+
}
296+
289297
@override
290298
Future<bool> isPaired(String deviceId) async {
291299
return _findDeviceById(deviceId).paired;

0 commit comments

Comments
 (0)