From 163e99b3addb65975acb1c046424e895f2e3e034 Mon Sep 17 00:00:00 2001 From: Rohit Sangwan Date: Sun, 19 Jun 2022 17:00:30 +0530 Subject: [PATCH] Add onWriteHandler and Errors --- .../quick_blue/darwin/QuickBlueDarwin.swift | 20 ++++++++++++++++++- packages/quick_blue/lib/quick_blue.dart | 4 ++++ .../lib/src/method_channel_quick_blue.dart | 17 +++++++++------- .../quick_blue/lib/src/quick_blue_linux.dart | 6 +++--- .../src/quick_blue_platform_interface.dart | 8 ++++++-- 5 files changed, 42 insertions(+), 13 deletions(-) diff --git a/packages/quick_blue/darwin/QuickBlueDarwin.swift b/packages/quick_blue/darwin/QuickBlueDarwin.swift index d8ff321..1c46d00 100644 --- a/packages/quick_blue/darwin/QuickBlueDarwin.swift +++ b/packages/quick_blue/darwin/QuickBlueDarwin.swift @@ -79,7 +79,7 @@ public class QuickBlueDarwin: NSObject, FlutterPlugin { case "stopScan": manager.stopScan() result(nil) - case "connect": + case "connect": let arguments = call.arguments as! Dictionary let deviceId = arguments["deviceId"] as! String guard let peripheral = discoveredPeripherals[deviceId] else { @@ -200,12 +200,22 @@ extension QuickBlueDarwin: CBCentralManagerDelegate { "ConnectionState": "connected", ]) } + + public func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral,error: Error?) { + print("centralManager:didFailToConnect \(peripheral.uuid.uuidString) error: \(String(describing: error))") + messageConnector.sendMessage([ + "deviceId": peripheral.uuid.uuidString, + "ConnectionState": "disconnected", + "error": String(describing: error) + ]) + } public func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) { print("centralManager:didDisconnectPeripheral: \(peripheral.uuid.uuidString) error: \(String(describing: error))") messageConnector.sendMessage([ "deviceId": peripheral.uuid.uuidString, "ConnectionState": "disconnected", + "error": String(describing: error) ]) } } @@ -248,6 +258,7 @@ extension QuickBlueDarwin: CBPeripheralDelegate { } self.messageConnector.sendMessage([ "deviceId": peripheral.uuid.uuidString, + "error": String(describing: error), "ServiceState": "discovered", "service": service.uuid.uuidStr, "characteristics": service.characteristics!.map { $0.uuid.uuidStr } @@ -257,6 +268,12 @@ extension QuickBlueDarwin: CBPeripheralDelegate { public func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) { let data = characteristic.value as NSData? print("peripheral:didWriteValueForCharacteristic \(characteristic.uuid.uuidStr) \(String(describing: data)) error: \(String(describing: error))") + self.messageConnector.sendMessage([ + "deviceId": peripheral.uuid.uuidString, + "write": "Success", + "characteristic": characteristic.uuid.uuidStr, + "error": String(describing: error) + ]) } public func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { @@ -264,6 +281,7 @@ extension QuickBlueDarwin: CBPeripheralDelegate { print("peripheral:didUpdateValueForCharacteristic \(characteristic.uuid) \(String(describing: data)) error: \(String(describing: error))") self.messageConnector.sendMessage([ "deviceId": peripheral.uuid.uuidString, + "error": String(describing: error), "characteristicValue": [ "characteristic": characteristic.uuid.uuidStr, "value": FlutterStandardTypedData(bytes: characteristic.value!) diff --git a/packages/quick_blue/lib/quick_blue.dart b/packages/quick_blue/lib/quick_blue.dart index 4bb5139..f29f312 100644 --- a/packages/quick_blue/lib/quick_blue.dart +++ b/packages/quick_blue/lib/quick_blue.dart @@ -39,6 +39,10 @@ class QuickBlue { _platform.onConnectionChanged = onConnectionChanged; } + static void setWriteValueHandler(OnWrite? onWrite) { + _platform.onWrite = onWrite; + } + static void discoverServices(String deviceId) => _platform.discoverServices(deviceId); static void setServiceHandler(OnServiceDiscovered? onServiceDiscovered) { diff --git a/packages/quick_blue/lib/src/method_channel_quick_blue.dart b/packages/quick_blue/lib/src/method_channel_quick_blue.dart index 87d3ef1..54341e3 100644 --- a/packages/quick_blue/lib/src/method_channel_quick_blue.dart +++ b/packages/quick_blue/lib/src/method_channel_quick_blue.dart @@ -64,18 +64,19 @@ class MethodChannelQuickBlue extends QuickBluePlatform { } @override - void discoverServices(String deviceId) { - _method.invokeMethod('discoverServices', { + Future discoverServices(String deviceId) async{ + await _method.invokeMethod('discoverServices', { 'deviceId': deviceId, }).then((_) => _log('discoverServices invokeMethod success')); } Future _handleConnectorMessage(dynamic message) async { _log('_handleConnectorMessage $message', logLevel: Level.ALL); + String? error = (message['error'] == null || message['error'] == 'nil') ? null : message['error']; if (message['ConnectionState'] != null) { String deviceId = message['deviceId']; BlueConnectionState connectionState = BlueConnectionState.parse(message['ConnectionState']); - onConnectionChanged?.call(deviceId, connectionState); + onConnectionChanged?.call(deviceId, connectionState,error); } else if (message['ServiceState'] != null) { if (message['ServiceState'] == 'discovered') { String deviceId = message['deviceId']; @@ -91,12 +92,14 @@ class MethodChannelQuickBlue extends QuickBluePlatform { onValueChanged?.call(deviceId, characteristic, value); } else if (message['mtuConfig'] != null) { _mtuConfigController.add(message['mtuConfig']); + }else if (message['write'] != null) { + onWrite?.call(message['deviceId'],message['characteristic'],error); } } @override Future setNotifiable(String deviceId, String service, String characteristic, BleInputProperty bleInputProperty) async { - _method.invokeMethod('setNotifiable', { + await _method.invokeMethod('setNotifiable', { 'deviceId': deviceId, 'service': service, 'characteristic': characteristic, @@ -106,7 +109,7 @@ class MethodChannelQuickBlue extends QuickBluePlatform { @override Future readValue(String deviceId, String service, String characteristic) async { - _method.invokeMethod('readValue', { + await _method.invokeMethod('readValue', { 'deviceId': deviceId, 'service': service, 'characteristic': characteristic, @@ -115,7 +118,7 @@ class MethodChannelQuickBlue extends QuickBluePlatform { @override Future writeValue(String deviceId, String service, String characteristic, Uint8List value, BleOutputProperty bleOutputProperty) async { - _method.invokeMethod('writeValue', { + await _method.invokeMethod('writeValue', { 'deviceId': deviceId, 'service': service, 'characteristic': characteristic, @@ -134,7 +137,7 @@ class MethodChannelQuickBlue extends QuickBluePlatform { @override Future requestMtu(String deviceId, int expectedMtu) async { - _method.invokeMethod('requestMtu', { + await _method.invokeMethod('requestMtu', { 'deviceId': deviceId, 'expectedMtu': expectedMtu, }).then((_) => _log('requestMtu invokeMethod success')); diff --git a/packages/quick_blue/lib/src/quick_blue_linux.dart b/packages/quick_blue/lib/src/quick_blue_linux.dart index 3cf1211..3c73be8 100644 --- a/packages/quick_blue/lib/src/quick_blue_linux.dart +++ b/packages/quick_blue/lib/src/quick_blue_linux.dart @@ -103,19 +103,19 @@ class QuickBlueLinux extends QuickBluePlatform { @override void connect(String deviceId) { _findDeviceById(deviceId).connect().then((_) { - onConnectionChanged?.call(deviceId, BlueConnectionState.connected); + onConnectionChanged?.call(deviceId, BlueConnectionState.connected,null); }); } @override void disconnect(String deviceId) { _findDeviceById(deviceId).disconnect().then((_) { - onConnectionChanged?.call(deviceId, BlueConnectionState.disconnected); + onConnectionChanged?.call(deviceId, BlueConnectionState.disconnected,null); }); } @override - void discoverServices(String deviceId) { + Future discoverServices(String deviceId) async{ var device = _findDeviceById(deviceId); for (var service in device.gattServices) { diff --git a/packages/quick_blue/lib/src/quick_blue_platform_interface.dart b/packages/quick_blue/lib/src/quick_blue_platform_interface.dart index 7f1f5a3..1874fb7 100644 --- a/packages/quick_blue/lib/src/quick_blue_platform_interface.dart +++ b/packages/quick_blue/lib/src/quick_blue_platform_interface.dart @@ -11,12 +11,14 @@ export 'models.dart'; typedef QuickLogger = Logger; -typedef OnConnectionChanged = void Function(String deviceId, BlueConnectionState state); +typedef OnConnectionChanged = void Function(String deviceId, BlueConnectionState state,String? error); typedef OnServiceDiscovered = void Function(String deviceId, String serviceId, List characteristicIds); typedef OnValueChanged = void Function(String deviceId, String characteristicId, Uint8List value); +typedef OnWrite = void Function(String deviceId, String characteristicId, String? error); + abstract class QuickBluePlatform extends PlatformInterface { QuickBluePlatform() : super(token: _token); @@ -47,7 +49,7 @@ abstract class QuickBluePlatform extends PlatformInterface { OnConnectionChanged? onConnectionChanged; - void discoverServices(String deviceId); + Future discoverServices(String deviceId); OnServiceDiscovered? onServiceDiscovered; @@ -55,6 +57,8 @@ abstract class QuickBluePlatform extends PlatformInterface { OnValueChanged? onValueChanged; + OnWrite? onWrite; + Future readValue(String deviceId, String service, String characteristic); Future writeValue(String deviceId, String service, String characteristic, Uint8List value, BleOutputProperty bleOutputProperty);