Skip to content

Commit

Permalink
Add onWriteHandler and Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohit Sangwan authored and Rohit Sangwan committed Jun 19, 2022
1 parent e5c4803 commit 163e99b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
20 changes: 19 additions & 1 deletion packages/quick_blue/darwin/QuickBlueDarwin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Any>
let deviceId = arguments["deviceId"] as! String
guard let peripheral = discoveredPeripherals[deviceId] else {
Expand Down Expand Up @@ -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)
])
}
}
Expand Down Expand Up @@ -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 }
Expand All @@ -257,13 +268,20 @@ 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?) {
let data = characteristic.value as NSData?
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!)
Expand Down
4 changes: 4 additions & 0 deletions packages/quick_blue/lib/quick_blue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 10 additions & 7 deletions packages/quick_blue/lib/src/method_channel_quick_blue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,19 @@ class MethodChannelQuickBlue extends QuickBluePlatform {
}

@override
void discoverServices(String deviceId) {
_method.invokeMethod('discoverServices', {
Future<void> discoverServices(String deviceId) async{
await _method.invokeMethod('discoverServices', {
'deviceId': deviceId,
}).then((_) => _log('discoverServices invokeMethod success'));
}

Future<void> _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'];
Expand All @@ -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<void> setNotifiable(String deviceId, String service, String characteristic, BleInputProperty bleInputProperty) async {
_method.invokeMethod('setNotifiable', {
await _method.invokeMethod('setNotifiable', {
'deviceId': deviceId,
'service': service,
'characteristic': characteristic,
Expand All @@ -106,7 +109,7 @@ class MethodChannelQuickBlue extends QuickBluePlatform {

@override
Future<void> readValue(String deviceId, String service, String characteristic) async {
_method.invokeMethod('readValue', {
await _method.invokeMethod('readValue', {
'deviceId': deviceId,
'service': service,
'characteristic': characteristic,
Expand All @@ -115,7 +118,7 @@ class MethodChannelQuickBlue extends QuickBluePlatform {

@override
Future<void> 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,
Expand All @@ -134,7 +137,7 @@ class MethodChannelQuickBlue extends QuickBluePlatform {

@override
Future<int> requestMtu(String deviceId, int expectedMtu) async {
_method.invokeMethod('requestMtu', {
await _method.invokeMethod('requestMtu', {
'deviceId': deviceId,
'expectedMtu': expectedMtu,
}).then((_) => _log('requestMtu invokeMethod success'));
Expand Down
6 changes: 3 additions & 3 deletions packages/quick_blue/lib/src/quick_blue_linux.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> discoverServices(String deviceId) async{
var device = _findDeviceById(deviceId);

for (var service in device.gattServices) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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);

Expand Down Expand Up @@ -47,14 +49,16 @@ abstract class QuickBluePlatform extends PlatformInterface {

OnConnectionChanged? onConnectionChanged;

void discoverServices(String deviceId);
Future<void> discoverServices(String deviceId);

OnServiceDiscovered? onServiceDiscovered;

Future<void> setNotifiable(String deviceId, String service, String characteristic, BleInputProperty bleInputProperty);

OnValueChanged? onValueChanged;

OnWrite? onWrite;

Future<void> readValue(String deviceId, String service, String characteristic);

Future<void> writeValue(String deviceId, String service, String characteristic, Uint8List value, BleOutputProperty bleOutputProperty);
Expand Down

0 comments on commit 163e99b

Please sign in to comment.