-
Notifications
You must be signed in to change notification settings - Fork 64
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
Add on write handler and errors #36
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -209,10 +209,12 @@ class QuickBluePlugin: FlutterPlugin, MethodCallHandler, EventChannel.StreamHand | |
"ConnectionState" to "connected" | ||
)) | ||
} else { | ||
// TODO Parse status code and send correct error messages | ||
cleanConnection(gatt) | ||
sendMessage(messageConnector, mapOf( | ||
"deviceId" to gatt.device.address, | ||
"ConnectionState" to "disconnected" | ||
"ConnectionState" to "disconnected", | ||
"error" to "Disconnection Reason : Status $status" | ||
)) | ||
} | ||
} | ||
|
@@ -260,6 +262,13 @@ class QuickBluePlugin: FlutterPlugin, MethodCallHandler, EventChannel.StreamHand | |
|
||
override fun onCharacteristicWrite(gatt: BluetoothGatt?, characteristic: BluetoothGattCharacteristic, status: Int) { | ||
Log.v(TAG, "onCharacteristicWrite ${characteristic.uuid}, ${characteristic.value.contentToString()} $status") | ||
val deviceID = gatt?.device?.address ?: "" | ||
// TODO Parse status code and send correct error messages | ||
sendMessage(messageConnector, mapOf( | ||
"deviceId" to deviceID, | ||
"characteristic" to characteristic.uuid.toString(), | ||
"error" to "Write Command Status : $status" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
An |
||
)) | ||
} | ||
|
||
override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -200,12 +200,22 @@ extension QuickBlueDarwin: CBCentralManagerDelegate { | |
"ConnectionState": "connected", | ||
]) | ||
} | ||
|
||
public func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral,error: Error?) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to connection error's PR |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to |
||
]) | ||
} | ||
} | ||
|
@@ -248,6 +258,7 @@ extension QuickBlueDarwin: CBPeripheralDelegate { | |
} | ||
self.messageConnector.sendMessage([ | ||
"deviceId": peripheral.uuid.uuidString, | ||
"error": String(describing: error), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to |
||
"ServiceState": "discovered", | ||
"service": service.uuid.uuidStr, | ||
"characteristics": service.characteristics!.map { $0.uuid.uuidStr } | ||
|
@@ -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!) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ class _PeripheralDetailPageState extends State<PeripheralDetailPage> { | |
QuickBlue.setConnectionHandler(_handleConnectionChange); | ||
QuickBlue.setServiceHandler(_handleServiceDiscovery); | ||
QuickBlue.setValueHandler(_handleValueChange); | ||
QuickBlue.setWriteValueHandler(_handlerWriteValue); | ||
} | ||
|
||
@override | ||
|
@@ -48,10 +49,15 @@ class _PeripheralDetailPageState extends State<PeripheralDetailPage> { | |
QuickBlue.setValueHandler(null); | ||
QuickBlue.setServiceHandler(null); | ||
QuickBlue.setConnectionHandler(null); | ||
QuickBlue.setWriteValueHandler(null); | ||
} | ||
|
||
void _handleConnectionChange(String deviceId, BlueConnectionState state) { | ||
debugPrint('_handleConnectionChange $deviceId, $state'); | ||
void _handlerWriteValue(String deviceId, String characteristicId, String? error) { | ||
debugPrint('_handlerWriteValue $deviceId, State : $characteristicId ,Error : $error'); | ||
} | ||
|
||
void _handleConnectionChange(String deviceId, BlueConnectionState state,String? error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to |
||
debugPrint('_handleConnectionChange $deviceId,State : ${state.value} , Error : $error'); | ||
} | ||
|
||
void _handleServiceDiscovery(String deviceId, String serviceId, List<String> characteristicIds) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,18 +64,19 @@ class MethodChannelQuickBlue extends QuickBluePlatform { | |
} | ||
|
||
@override | ||
void discoverServices(String deviceId) { | ||
_method.invokeMethod('discoverServices', { | ||
Future<void> discoverServices(String deviceId) async{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to |
||
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']; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle platform-specific type/string on platform. DO NOT handle in Dart/Flutter |
||
if (message['ConnectionState'] != null) { | ||
String deviceId = message['deviceId']; | ||
BlueConnectionState connectionState = BlueConnectionState.parse(message['ConnectionState']); | ||
onConnectionChanged?.call(deviceId, connectionState); | ||
onConnectionChanged?.call(deviceId, connectionState,error); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to |
||
} 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<void> setNotifiable(String deviceId, String service, String characteristic, BleInputProperty bleInputProperty) async { | ||
_method.invokeMethod('setNotifiable', { | ||
await _method.invokeMethod('setNotifiable', { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to |
||
'deviceId': deviceId, | ||
'service': service, | ||
'characteristic': characteristic, | ||
|
@@ -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', { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to |
||
'deviceId': deviceId, | ||
'service': service, | ||
'characteristic': characteristic, | ||
|
@@ -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, | ||
|
@@ -134,7 +137,7 @@ class MethodChannelQuickBlue extends QuickBluePlatform { | |
|
||
@override | ||
Future<int> requestMtu(String deviceId, int expectedMtu) async { | ||
_method.invokeMethod('requestMtu', { | ||
await _method.invokeMethod('requestMtu', { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to |
||
'deviceId': deviceId, | ||
'expectedMtu': expectedMtu, | ||
}).then((_) => _log('requestMtu invokeMethod success')); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,19 +103,24 @@ class QuickBlueLinux extends QuickBluePlatform { | |
@override | ||
void connect(String deviceId) { | ||
_findDeviceById(deviceId).connect().then((_) { | ||
onConnectionChanged?.call(deviceId, BlueConnectionState.connected); | ||
onConnectionChanged?.call(deviceId, BlueConnectionState.connected,null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to |
||
}); | ||
//TODO : Listen for PropertisChanged event , to update Connection Status on Disconnection from BleDevice , | ||
|
||
// _findDeviceById(deviceId).propertiesChanged.listen((event) { | ||
// print(event); | ||
// }); | ||
} | ||
|
||
@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) { | ||
|
@@ -173,13 +178,17 @@ class QuickBlueLinux extends QuickBluePlatform { | |
@override | ||
Future<void> writeValue(String deviceId, String service, String characteristic, Uint8List value, BleOutputProperty bleOutputProperty) async { | ||
var c = _getCharacteristic(deviceId, service, characteristic); | ||
|
||
if (bleOutputProperty == BleOutputProperty.withResponse) { | ||
await c.writeValue(value, type: BlueZGattCharacteristicWriteType.request); | ||
} else { | ||
await c.writeValue(value, type: BlueZGattCharacteristicWriteType.command); | ||
try{ | ||
if (bleOutputProperty == BleOutputProperty.withResponse) { | ||
await c.writeValue(value, type: BlueZGattCharacteristicWriteType.request); | ||
} else { | ||
await c.writeValue(value, type: BlueZGattCharacteristicWriteType.command); | ||
} | ||
_log('writeValue $characteristic, ${hex.encode(value)}'); | ||
onWrite?.call(deviceId,characteristic,null); | ||
}catch(e){ | ||
onWrite?.call(deviceId,characteristic,e.toString()); | ||
} | ||
_log('writeValue $characteristic, ${hex.encode(value)}'); | ||
} | ||
|
||
@override | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OnWriteValue |
||
|
||
Future<void> readValue(String deviceId, String service, String characteristic); | ||
|
||
Future<void> writeValue(String deviceId, String service, String characteristic, Uint8List value, BleOutputProperty bleOutputProperty); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move to
connection error
's PR