Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement getPairedDevices api #67

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ interface UniversalBlePlatformChannel {
fun writeValue(deviceId: String, service: String, characteristic: String, value: ByteArray, bleOutputProperty: Long, callback: (Result<Unit>) -> Unit)
fun isPaired(deviceId: String, callback: (Result<Boolean>) -> Unit)
fun pair(deviceId: String)
fun getPairedDevices(): List<UniversalBleScanResult>
fun unPair(deviceId: String)
fun getSystemDevices(withServices: List<String>, callback: (Result<List<UniversalBleScanResult>>) -> Unit)
fun getConnectionState(deviceId: String): Long
Expand Down Expand Up @@ -520,6 +521,21 @@ interface UniversalBlePlatformChannel {
channel.setMessageHandler(null)
}
}
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.getPairedDevices$separatedMessageChannelSuffix", codec)
if (api != null) {
channel.setMessageHandler { _, reply ->
val wrapped: List<Any?> = try {
listOf<Any?>(api.getPairedDevices())
} catch (exception: Throwable) {
wrapError(exception)
}
reply.reply(wrapped)
}
} else {
channel.setMessageHandler(null)
}
}
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.unPair$separatedMessageChannelSuffix", codec)
if (api != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,20 @@ class UniversalBlePlugin : UniversalBlePlatformChannel, BluetoothGattCallback(),
}
}

override fun getPairedDevices(): List<UniversalBleScanResult> {
val bondedDevices: Set<BluetoothDevice> = bluetoothManager.adapter.bondedDevices;
return bondedDevices.map {
UniversalBleScanResult(
name = it.name,
deviceId = it.address,
isPaired = it.bondState == BOND_BONDED,
manufacturerDataHead = null,
manufacturerData = null,
rssi = null,
)
}
}

override fun unPair(deviceId: String) {
val remoteDevice: BluetoothDevice =
bluetoothManager.adapter.getRemoteDevice(deviceId)
Expand Down
14 changes: 14 additions & 0 deletions darwin/Classes/UniversalBle.g.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ protocol UniversalBlePlatformChannel {
func writeValue(deviceId: String, service: String, characteristic: String, value: FlutterStandardTypedData, bleOutputProperty: Int64, completion: @escaping (Result<Void, Error>) -> Void)
func isPaired(deviceId: String, completion: @escaping (Result<Bool, Error>) -> Void)
func pair(deviceId: String) throws
func getPairedDevices() throws -> [UniversalBleScanResult]
func unPair(deviceId: String) throws
func getSystemDevices(withServices: [String], completion: @escaping (Result<[UniversalBleScanResult], Error>) -> Void)
func getConnectionState(deviceId: String) throws -> Int64
Expand Down Expand Up @@ -484,6 +485,19 @@ class UniversalBlePlatformChannelSetup {
} else {
pairChannel.setMessageHandler(nil)
}
let getPairedDevicesChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.getPairedDevices\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
if let api = api {
getPairedDevicesChannel.setMessageHandler { _, reply in
do {
let result = try api.getPairedDevices()
reply(wrapResult(result))
} catch {
reply(wrapError(error))
}
}
} else {
getPairedDevicesChannel.setMessageHandler(nil)
}
let unPairChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.unPair\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
if let api = api {
unPairChannel.setMessageHandler { message, reply in
Expand Down
6 changes: 5 additions & 1 deletion darwin/Classes/UniversalBlePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,11 @@ private class BleCentralDarwin: NSObject, UniversalBlePlatformChannel, CBCentral
func unPair(deviceId _: String) throws {
throw FlutterError(code: "NotSupported", message: nil, details: nil)
}


func getPairedDevices() throws -> [UniversalBleScanResult] {
throw FlutterError(code: "NotSupported", message: nil, details: nil)
}

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
Expand Down
5 changes: 5 additions & 0 deletions example/lib/data/mock_universal_ble.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,9 @@ class MockUniversalBle extends UniversalBlePlatform {
Future<BleConnectionState> getConnectionState(String deviceId) {
throw UnimplementedError();
}

@override
Future<List<BleDevice>> getPairedDevices() async {
return <BleDevice>[];
}
}
8 changes: 8 additions & 0 deletions lib/src/universal_ble_linux/universal_ble_linux.dart
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@ class UniversalBleLinux extends UniversalBlePlatform {
}
}

@override
Future<List<BleDevice>> getPairedDevices() async {
return _client.devices
.where((e) => e.paired)
.map((e) => e.toBleDevice())
.toList();
}

@override
Future<bool> isPaired(String deviceId) async {
return _findDeviceById(deviceId).paired;
Expand Down
Loading
Loading