Skip to content

Commit

Permalink
Return pair result
Browse files Browse the repository at this point in the history
  • Loading branch information
fotiDim committed Aug 12, 2024
1 parent 7606e8f commit 40819d5
Show file tree
Hide file tree
Showing 29 changed files with 572 additions and 656 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
## 0.12.0
* BREAKING CHANGE: `unPair` is now `unpair`
* Add isPaired support on Apple
* `connect` will now return the connection result
* Add `pair()`, `isPaired` and `onPairingStateChange` support for Apple and web
* `connect()` and `pair()` now return a bool result
* Add `PlatformConfig` property in `StartScan`
* Add `WebConfig` property in `PlatformConfig`
* Fix notifications for characteristics without cccd on Android
* Add `connectionStream` API to get connection updates as stream
* Promote Linux to stable

## 0.11.1
* Trim spaces in UUIDs
Expand Down
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ A cross-platform (Android/iOS/macOS/Windows/Linux/Web) Bluetooth Low Energy (BLE

### API Support Matrix

| API | Android | iOS | macOS | Windows | Linux (beta) | Web |
| API | Android | iOS | macOS | Windows | Linux | Web |
| :------------------- | :-----: | :-: | :---: | :-----: | :----------: | :-: |
| startScan/stopScan | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| connect/disconnect | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
Expand All @@ -31,11 +31,11 @@ A cross-platform (Android/iOS/macOS/Windows/Linux/Web) Bluetooth Low Energy (BLE
| readValue | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| writeValue | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| setNotifiable | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| pair | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | |
| pair | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | |
| unpair | ✔️ ||| ✔️ | ✔️ ||
| isPaired | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ? |
| getBluetoothAvailabilityState | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | |
| onPairingStateChange | ✔️ | | | ✔️ | ✔️ ||
| isPaired | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| onPairingStateChange | ✔️ | | | ✔️ | ✔️ | |
| getBluetoothAvailabilityState | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ ||
| enableBluetooth | ✔️ ||| ✔️ | ✔️ ||
| onAvailabilityChange | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| requestMtu | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ ||
Expand Down Expand Up @@ -196,18 +196,22 @@ UniversalBle.setNotifiable(deviceId, serviceId, characteristicId, BleInputProper

```dart
// Pair
UniversalBle.pair(deviceId);
bool? isPaired = await UniversalBle.pair(deviceId); // Returns true if successful
// Get the pairing result
// For Apple and Web, you can optionally pass a pairingCommand if you know an encrypted read or write characteristic.
// Not supported on Web/Windows
UniversalBle.pair(deviceId, pairingCommand: BleCommand(service:"SERVICE", characteristic:"ENCRYPTED_CHARACTERISTIC",));
// Receive pairing state changes
UniversalBle.onPairingStateChange = (String deviceId, bool isPaired, String? error) {
// Handle Pairing state change
// Handle pairing state change
}
// Unpair
UniversalBle.unpair(deviceId);
// Check current pairing state
bool isPaired = UniversalBle.isPaired(deviceId);
bool? isPaired = UniversalBle.isPaired(deviceId);
```

### Bluetooth Availability
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Autogenerated from Pigeon (v18.0.1), do not edit directly.
// Autogenerated from Pigeon (v21.1.0), do not edit directly.
// See also: https://pub.dev/packages/pigeon
@file:Suppress("UNCHECKED_CAST", "ArrayInDataClass")

Expand Down Expand Up @@ -72,7 +72,7 @@ data class UniversalBleScanResult (
}
}
fun toList(): List<Any?> {
return listOf<Any?>(
return listOf(
deviceId,
name,
isPaired,
Expand All @@ -99,7 +99,7 @@ data class UniversalBleService (
}
}
fun toList(): List<Any?> {
return listOf<Any?>(
return listOf(
uuid,
characteristics,
)
Expand All @@ -121,7 +121,7 @@ data class UniversalBleCharacteristic (
}
}
fun toList(): List<Any?> {
return listOf<Any?>(
return listOf(
uuid,
properties,
)
Expand All @@ -147,7 +147,7 @@ data class UniversalScanFilter (
}
}
fun toList(): List<Any?> {
return listOf<Any?>(
return listOf(
withServices,
withManufacturerData,
)
Expand All @@ -171,22 +171,16 @@ data class UniversalManufacturerDataFilter (
}
}
fun toList(): List<Any?> {
return listOf<Any?>(
return listOf(
companyIdentifier,
data,
mask,
)
}
}

private object UniversalBlePlatformChannelCodec : StandardMessageCodec() {
private object UniversalBlePigeonCodec : StandardMessageCodec() {
override fun readValueOfType(type: Byte, buffer: ByteBuffer): Any? {
return when (type) {
128.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
UniversalBleCharacteristic.fromList(it)
}
}
129.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
UniversalBleScanResult.fromList(it)
Expand All @@ -199,23 +193,24 @@ private object UniversalBlePlatformChannelCodec : StandardMessageCodec() {
}
131.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
UniversalManufacturerDataFilter.fromList(it)
UniversalBleCharacteristic.fromList(it)
}
}
132.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
UniversalScanFilter.fromList(it)
}
}
133.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
UniversalManufacturerDataFilter.fromList(it)
}
}
else -> super.readValueOfType(type, buffer)
}
}
override fun writeValue(stream: ByteArrayOutputStream, value: Any?) {
when (value) {
is UniversalBleCharacteristic -> {
stream.write(128)
writeValue(stream, value.toList())
}
is UniversalBleScanResult -> {
stream.write(129)
writeValue(stream, value.toList())
Expand All @@ -224,19 +219,24 @@ private object UniversalBlePlatformChannelCodec : StandardMessageCodec() {
stream.write(130)
writeValue(stream, value.toList())
}
is UniversalManufacturerDataFilter -> {
is UniversalBleCharacteristic -> {
stream.write(131)
writeValue(stream, value.toList())
}
is UniversalScanFilter -> {
stream.write(132)
writeValue(stream, value.toList())
}
is UniversalManufacturerDataFilter -> {
stream.write(133)
writeValue(stream, value.toList())
}
else -> super.writeValue(stream, value)
}
}
}


/**
* Flutter -> Native
*
Expand All @@ -255,24 +255,25 @@ interface UniversalBlePlatformChannel {
fun requestMtu(deviceId: String, expectedMtu: Long, callback: (Result<Long>) -> Unit)
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 pair(deviceId: String, callback: (Result<Boolean>) -> Unit)
fun unPair(deviceId: String)
fun getSystemDevices(withServices: List<String>, callback: (Result<List<UniversalBleScanResult>>) -> Unit)
fun getConnectionState(deviceId: String): Long

companion object {
/** The codec used by UniversalBlePlatformChannel. */
val codec: MessageCodec<Any?> by lazy {
UniversalBlePlatformChannelCodec
UniversalBlePigeonCodec
}
/** Sets up an instance of `UniversalBlePlatformChannel` to handle messages through the `binaryMessenger`. */
@JvmOverloads
fun setUp(binaryMessenger: BinaryMessenger, api: UniversalBlePlatformChannel?, messageChannelSuffix: String = "") {
val separatedMessageChannelSuffix = if (messageChannelSuffix.isNotEmpty()) ".$messageChannelSuffix" else ""
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.getBluetoothAvailabilityState$separatedMessageChannelSuffix", codec)
if (api != null) {
channel.setMessageHandler { _, reply ->
api.getBluetoothAvailabilityState() { result: Result<Long> ->
api.getBluetoothAvailabilityState{ result: Result<Long> ->
val error = result.exceptionOrNull()
if (error != null) {
reply.reply(wrapError(error))
Expand All @@ -290,7 +291,7 @@ interface UniversalBlePlatformChannel {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.enableBluetooth$separatedMessageChannelSuffix", codec)
if (api != null) {
channel.setMessageHandler { _, reply ->
api.enableBluetooth() { result: Result<Boolean> ->
api.enableBluetooth{ result: Result<Boolean> ->
val error = result.exceptionOrNull()
if (error != null) {
reply.reply(wrapError(error))
Expand All @@ -312,7 +313,7 @@ interface UniversalBlePlatformChannel {
val filterArg = args[0] as UniversalScanFilter?
val wrapped: List<Any?> = try {
api.startScan(filterArg)
listOf<Any?>(null)
listOf(null)
} catch (exception: Throwable) {
wrapError(exception)
}
Expand All @@ -328,7 +329,7 @@ interface UniversalBlePlatformChannel {
channel.setMessageHandler { _, reply ->
val wrapped: List<Any?> = try {
api.stopScan()
listOf<Any?>(null)
listOf(null)
} catch (exception: Throwable) {
wrapError(exception)
}
Expand All @@ -346,7 +347,7 @@ interface UniversalBlePlatformChannel {
val deviceIdArg = args[0] as String
val wrapped: List<Any?> = try {
api.connect(deviceIdArg)
listOf<Any?>(null)
listOf(null)
} catch (exception: Throwable) {
wrapError(exception)
}
Expand All @@ -364,7 +365,7 @@ interface UniversalBlePlatformChannel {
val deviceIdArg = args[0] as String
val wrapped: List<Any?> = try {
api.disconnect(deviceIdArg)
listOf<Any?>(null)
listOf(null)
} catch (exception: Throwable) {
wrapError(exception)
}
Expand Down Expand Up @@ -508,13 +509,15 @@ interface UniversalBlePlatformChannel {
channel.setMessageHandler { message, reply ->
val args = message as List<Any?>
val deviceIdArg = args[0] as String
val wrapped: List<Any?> = try {
api.pair(deviceIdArg)
listOf<Any?>(null)
} catch (exception: Throwable) {
wrapError(exception)
api.pair(deviceIdArg) { result: Result<Boolean> ->
val error = result.exceptionOrNull()
if (error != null) {
reply.reply(wrapError(error))
} else {
val data = result.getOrNull()
reply.reply(wrapResult(data))
}
}
reply.reply(wrapped)
}
} else {
channel.setMessageHandler(null)
Expand All @@ -528,7 +531,7 @@ interface UniversalBlePlatformChannel {
val deviceIdArg = args[0] as String
val wrapped: List<Any?> = try {
api.unPair(deviceIdArg)
listOf<Any?>(null)
listOf(null)
} catch (exception: Throwable) {
wrapError(exception)
}
Expand Down Expand Up @@ -565,7 +568,7 @@ interface UniversalBlePlatformChannel {
val args = message as List<Any?>
val deviceIdArg = args[0] as String
val wrapped: List<Any?> = try {
listOf<Any?>(api.getConnectionState(deviceIdArg))
listOf(api.getConnectionState(deviceIdArg))
} catch (exception: Throwable) {
wrapError(exception)
}
Expand All @@ -578,28 +581,6 @@ interface UniversalBlePlatformChannel {
}
}
}
private object UniversalBleCallbackChannelCodec : StandardMessageCodec() {
override fun readValueOfType(type: Byte, buffer: ByteBuffer): Any? {
return when (type) {
128.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
UniversalBleScanResult.fromList(it)
}
}
else -> super.readValueOfType(type, buffer)
}
}
override fun writeValue(stream: ByteArrayOutputStream, value: Any?) {
when (value) {
is UniversalBleScanResult -> {
stream.write(128)
writeValue(stream, value.toList())
}
else -> super.writeValue(stream, value)
}
}
}

/**
* Native -> Flutter
*
Expand All @@ -609,7 +590,7 @@ class UniversalBleCallbackChannel(private val binaryMessenger: BinaryMessenger,
companion object {
/** The codec used by UniversalBleCallbackChannel. */
val codec: MessageCodec<Any?> by lazy {
UniversalBleCallbackChannelCodec
UniversalBlePigeonCodec
}
}
fun onAvailabilityChanged(stateArg: Long, callback: (Result<Unit>) -> Unit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import java.util.UUID
private const val TAG = "UniversalBlePlugin"

val knownGatts = mutableListOf<BluetoothGatt>()
val ccdCharacteristic = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")
val ccdCharacteristic: UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")

enum class BleConnectionState(val value: Long) {
Connected(0),
Expand Down Expand Up @@ -157,8 +157,7 @@ fun Int.parseGattErrorCode(): String? {
}

fun UniversalScanFilter.toScanFilters(): List<ScanFilter> {
var scanFilters: ArrayList<ScanFilter> = arrayListOf()

val scanFilters: ArrayList<ScanFilter> = arrayListOf()
// Add withServices Filter
for (service in this.withServices) {
try {
Expand Down
Loading

0 comments on commit 40819d5

Please sign in to comment.