Skip to content

Commit

Permalink
Merge pull request #440 from PhilipsHue/fix-reconnection-problems
Browse files Browse the repository at this point in the history
Revert "Merge pull request #406 from plyobits/queue_messages"
  • Loading branch information
remonh87 committed Nov 8, 2021
2 parents f352c14 + fe136d6 commit c25bedf
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 43 deletions.
4 changes: 2 additions & 2 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -402,14 +402,14 @@ packages:
path: "../packages/reactive_ble_mobile"
relative: true
source: path
version: "5.0.1"
version: "5.0.2"
reactive_ble_platform_interface:
dependency: transitive
description:
name: reactive_ble_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "5.0.1"
version: "5.0.2"
shelf:
dependency: transitive
description:
Expand Down
4 changes: 2 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_reactive_ble_example
description: Demonstrates how to use the flutter_reactive_ble plugin.
version: 3.1.1+1
version: 5.0.2
publish_to: 'none'

environment:
Expand All @@ -11,7 +11,7 @@ dependencies:
flutter:
sdk: flutter
flutter_lints: ^1.0.4
flutter_reactive_ble: ^5.0.1
flutter_reactive_ble: ^5.0.2
functional_data: ^1.0.0
intl: ^0.17.0

Expand Down
4 changes: 4 additions & 0 deletions packages/flutter_reactive_ble/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 5.0.2

* Revert Queue up messages on iOS until event channel is ready. Fix #439

## 5.0.1

* Bump protobuf so it includes binaries for Mac M1 #396.
Expand Down
4 changes: 2 additions & 2 deletions packages/flutter_reactive_ble/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -374,14 +374,14 @@ packages:
name: reactive_ble_mobile
url: "https://pub.dartlang.org"
source: hosted
version: "5.0.1"
version: "5.0.2"
reactive_ble_platform_interface:
dependency: "direct main"
description:
name: reactive_ble_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "5.0.1"
version: "5.0.2"
shelf:
dependency: transitive
description:
Expand Down
6 changes: 3 additions & 3 deletions packages/flutter_reactive_ble/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_reactive_ble
description: Reactive Bluetooth Low Energy (BLE) plugin that can communicate with multiple devices
version: 5.0.1
version: 5.0.2
homepage: https://github.com/PhilipsHue/flutter_reactive_ble

environment:
Expand All @@ -21,8 +21,8 @@ dependencies:
sdk: flutter
functional_data: ^1.0.0
meta: ^1.3.0
reactive_ble_mobile: ^5.0.1
reactive_ble_platform_interface: ^5.0.1
reactive_ble_mobile: ^5.0.2
reactive_ble_platform_interface: ^5.0.2
dev_dependencies:
build_runner: ^2.1.2
flutter_lints: ^1.0.4
Expand Down
4 changes: 4 additions & 0 deletions packages/reactive_ble_mobile/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 5.0.2

* Revert Queue up messages on iOS until event channel is ready. Fix #439

## 5.0.1

* Bump protobuf so it includes binaries for Mac M1 #396.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,10 @@ final class PluginController {
}
}
}
var discoveryQueue: [PlatformMethodResult] = [];
var messageQueue: [CharacteristicValueInfo] = [];
var connectedDeviceSink: EventSink?

var valueQueue: [PlatformMethodResult] = [];
var characteristicValueUpdateSink: EventSink?

func flushQueue(sink: EventSink, queue: inout [PlatformMethodResult]) {
queue.forEach(sink.add)
queue.removeAll()
}

func sinkOrQueue(sink: EventSink?, queue: inout [PlatformMethodResult], value: PlatformMethodResult ) {
if let sink = sink {
flushQueue(sink: sink, queue: &queue)
sink.add(value)
} else {
queue.append(value)
}
}

func initialize(name: String, completion: @escaping PlatformMethodCompletionHandler) {
if let central = central {
central.stopScan()
Expand Down Expand Up @@ -99,9 +83,12 @@ final class PluginController {
}
}

context.sinkOrQueue(sink: context.connectedDeviceSink, queue: &context.discoveryQueue, value: .success(message))
context.connectedDeviceSink?.add(.success(message))
},
onServicesWithCharacteristicsInitialDiscovery: papply(weak: self) { context, central, peripheral, errors in
guard let sink = context.connectedDeviceSink
else { assert(false); return }

let message = DeviceInfo.with {
$0.id = peripheral.identifier.uuidString
$0.connectionState = encode(peripheral.state)
Expand All @@ -112,7 +99,8 @@ final class PluginController {
}
}
}
context.sinkOrQueue(sink: context.connectedDeviceSink, queue: &context.discoveryQueue, value: .success(message))

sink.add(.success(message))
},
onCharacteristicValueUpdate: papply(weak: self) { context, central, characteristic, value, error in
let message = CharacteristicValueInfo.with {
Expand All @@ -131,7 +119,14 @@ final class PluginController {
}
}
}
context.sinkOrQueue(sink: context.characteristicValueUpdateSink, queue: &context.valueQueue, value: .success(message))
let sink = context.characteristicValueUpdateSink
if (sink != nil) {
sink!.add(.success(message))
} else {
// In case message arrives before sink is created
context.messageQueue.append(message);
}

}
)

Expand Down Expand Up @@ -218,12 +213,16 @@ final class PluginController {
let timeout = args.timeoutInMs > 0 ? TimeInterval(args.timeoutInMs) / 1000 : nil

completion(.success(nil))

let message = DeviceInfo.with {
$0.id = args.deviceID
$0.connectionState = encode(.connecting)

if let sink = connectedDeviceSink {
let message = DeviceInfo.with {
$0.id = args.deviceID
$0.connectionState = encode(.connecting)
}
sink.add(.success(message))
} else {
print("Warning! No event channel set up to report a connection update")
}
sinkOrQueue(sink: connectedDeviceSink, queue: &discoveryQueue, value: .success(message))

do {
try central.connect(
Expand All @@ -232,6 +231,12 @@ final class PluginController {
timeout: timeout
)
} catch {
guard let sink = connectedDeviceSink
else {
print("Warning! No event channel set up to report a connection failure: \(error)")
return
}

let message = DeviceInfo.with {
$0.id = args.deviceID
$0.connectionState = encode(.disconnected)
Expand All @@ -240,7 +245,8 @@ final class PluginController {
$0.message = "\(error)"
}
}
sinkOrQueue(sink: connectedDeviceSink, queue: &discoveryQueue, value: .success(message))

sink.add(.success(message))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,10 @@ public class SwiftReactiveBlePlugin: NSObject, FlutterPlugin {
context: context,
onListen: { context, sink in
context.connectedDeviceSink = sink
context.flushQueue(sink: sink, queue: &context.discoveryQueue)
return nil
},
onCancel: { context in
context.connectedDeviceSink = nil
context.discoveryQueue.removeAll()
return nil
}
)
Expand All @@ -69,11 +67,14 @@ public class SwiftReactiveBlePlugin: NSObject, FlutterPlugin {
context: context,
onListen: { context, sink in
context.characteristicValueUpdateSink = sink
context.flushQueue(sink: sink, queue: &context.valueQueue)
context.messageQueue.forEach { msg in
sink.add(.success(msg))
}
context.messageQueue.removeAll()
return nil
},
onCancel: { context in
context.valueQueue.removeAll()
context.messageQueue.removeAll()
context.characteristicValueUpdateSink = nil
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion packages/reactive_ble_mobile/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ packages:
name: reactive_ble_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "5.0.1"
version: "5.0.2"
shelf:
dependency: transitive
description:
Expand Down
4 changes: 2 additions & 2 deletions packages/reactive_ble_mobile/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: reactive_ble_mobile
description: Official Android and iOS implementation for the flutter_reactive_ble plugin.
version: 5.0.1
version: 5.0.2
homepage: https://github.com/PhilipsHue/flutter_reactive_ble

environment:
Expand All @@ -11,7 +11,7 @@ dependencies:
flutter:
sdk: flutter
protobuf: ^2.0.0
reactive_ble_platform_interface: ^5.0.1
reactive_ble_platform_interface: ^5.0.2
dev_dependencies:
build_runner: ^2.1.2
flutter_test:
Expand Down
4 changes: 4 additions & 0 deletions packages/reactive_ble_platform_interface/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 5.0.2

* Revert Queue up messages on iOS until event channel is ready. Fix #439

## 5.0.1

* Bump protobuf so it includes binaries for Mac M1 #396.
Expand Down
2 changes: 1 addition & 1 deletion packages/reactive_ble_platform_interface/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: reactive_ble_platform_interface
description: Platform interface for the flutter_reactive_ble_project
version: 5.0.1
version: 5.0.2
homepage: https://github.com/PhilipsHue/flutter_reactive_ble

environment:
Expand Down

0 comments on commit c25bedf

Please sign in to comment.