From a428d40058909a174369fc429408f619b9584418 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Byszewski?= <4048063+pawelByszewski@users.noreply.github.com> Date: Fri, 18 Oct 2019 18:15:40 +0200 Subject: [PATCH] =?UTF-8?q?monitor=20transactionId=20of=20monitoring=20cha?= =?UTF-8?q?racteristics=20-=20remove=20doubled=20=E2=80=A6=20(#227)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * monitor transactionId of monitoring characteristics - remove doubled events * refactor * refactor * revert * Hide internal types from the user * Remove unnecessary mixin --- .../SingleCharacteristicResponse.java | 9 ++- ...leCharacteristicResponseJsonConverter.java | 2 + .../delegate/CharacteristicsDelegate.java | 41 ++++++----- .../device_details/device_details_bloc.dart | 8 +++ .../device_details/view/manual_test_view.dart | 14 ++++ .../peripheral_test_operations.dart | 15 ++++ lib/characteristic.dart | 1 + .../bridge/characteristics_mixin.dart | 72 +++++++++++++------ 8 files changed, 123 insertions(+), 39 deletions(-) diff --git a/android/src/main/java/com/polidea/flutter_ble_lib/SingleCharacteristicResponse.java b/android/src/main/java/com/polidea/flutter_ble_lib/SingleCharacteristicResponse.java index 9e13f0e4..e3303d5b 100644 --- a/android/src/main/java/com/polidea/flutter_ble_lib/SingleCharacteristicResponse.java +++ b/android/src/main/java/com/polidea/flutter_ble_lib/SingleCharacteristicResponse.java @@ -8,11 +8,14 @@ public class SingleCharacteristicResponse { private final Characteristic characteristic; private int serviceId; private UUID serviceUuid; + private String transactionId; - public SingleCharacteristicResponse(Characteristic characteristics, int serviceId, UUID serviceUuid) { + public SingleCharacteristicResponse(Characteristic characteristics, int serviceId, UUID serviceUuid, + String transactionId) { this.characteristic = characteristics; this.serviceId = serviceId; this.serviceUuid = serviceUuid; + this.transactionId = transactionId; } public Characteristic getCharacteristic() { @@ -26,4 +29,8 @@ public int getServiceId() { public UUID getServiceUuid() { return serviceUuid; } + + public String getTransactionId() { + return transactionId; + } } diff --git a/android/src/main/java/com/polidea/flutter_ble_lib/converter/SingleCharacteristicResponseJsonConverter.java b/android/src/main/java/com/polidea/flutter_ble_lib/converter/SingleCharacteristicResponseJsonConverter.java index 07eea3f9..528a6106 100644 --- a/android/src/main/java/com/polidea/flutter_ble_lib/converter/SingleCharacteristicResponseJsonConverter.java +++ b/android/src/main/java/com/polidea/flutter_ble_lib/converter/SingleCharacteristicResponseJsonConverter.java @@ -13,6 +13,7 @@ private interface Metadata { String UUID = "serviceUuid"; String ID = "serviceId"; String CHARACTERISTIC = "characteristic"; + String TRANSACTION_ID = "transactionId"; } private CharacteristicJsonConverter characteristicJsonConverter = new CharacteristicJsonConverter(); @@ -24,6 +25,7 @@ public String toJson(SingleCharacteristicResponse value) throws JSONException { jsonObject.put(Metadata.UUID, value.getServiceUuid()); jsonObject.put(Metadata.ID, value.getServiceId()); + jsonObject.put(Metadata.TRANSACTION_ID, value.getTransactionId()); jsonObject.put(Metadata.CHARACTERISTIC, characteristicJsonConverter.toJsonObject(value.getCharacteristic())); return jsonObject.toString(); diff --git a/android/src/main/java/com/polidea/flutter_ble_lib/delegate/CharacteristicsDelegate.java b/android/src/main/java/com/polidea/flutter_ble_lib/delegate/CharacteristicsDelegate.java index 9ad5408c..45f80f3c 100644 --- a/android/src/main/java/com/polidea/flutter_ble_lib/delegate/CharacteristicsDelegate.java +++ b/android/src/main/java/com/polidea/flutter_ble_lib/delegate/CharacteristicsDelegate.java @@ -134,14 +134,14 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result private void readCharacteristicForIdentifier( int characteristicIdentifier, - String transactionId, + final String transactionId, final MethodChannel.Result result) { final SafeMainThreadResolver safeMainThreadResolver = new SafeMainThreadResolver<>( new OnSuccessCallback() { @Override public void onSuccess(Characteristic data) { try { - result.success(characteristicsResponseJsonConverter.toJson(createCharacteristicResponse(data))); + result.success(characteristicsResponseJsonConverter.toJson(createCharacteristicResponse(data, transactionId))); } catch (JSONException e) { e.printStackTrace(); result.error(null, e.getMessage(), null); @@ -173,7 +173,7 @@ private void readCharacteristicForDevice( String deviceIdentifier, String serviceUuid, String characteristicUuid, - String transactionId, + final String transactionId, final MethodChannel.Result result) { final SafeMainThreadResolver safeMainThreadResolver = new SafeMainThreadResolver<>( @@ -209,13 +209,13 @@ public void onError(BleError error) { }); } - private void readCharacteristicForService(int serviceIdentifier, String characteristicUuid, String transactionId, final MethodChannel.Result result) { + private void readCharacteristicForService(int serviceIdentifier, String characteristicUuid, final String transactionId, final MethodChannel.Result result) { final SafeMainThreadResolver safeMainThreadResolver = new SafeMainThreadResolver<>( new OnSuccessCallback() { @Override public void onSuccess(Characteristic data) { try { - result.success(characteristicsResponseJsonConverter.toJson(createCharacteristicResponse(data))); + result.success(characteristicsResponseJsonConverter.toJson(createCharacteristicResponse(data, transactionId))); } catch (JSONException e) { e.printStackTrace(); result.error(null, e.getMessage(), null); @@ -250,14 +250,14 @@ public void onError(BleError error) { private void writeCharacteristicForIdentifier(int characteristicIdentifier, byte[] bytesToWrite, boolean withResponse, - String transactionId, + final String transactionId, final MethodChannel.Result result) { final SafeMainThreadResolver safeMainThreadResolver = new SafeMainThreadResolver<>( new OnSuccessCallback() { @Override public void onSuccess(Characteristic data) { try { - result.success(characteristicsResponseJsonConverter.toJson(createCharacteristicResponse(data))); + result.success(characteristicsResponseJsonConverter.toJson(createCharacteristicResponse(data, transactionId))); } catch (JSONException e) { e.printStackTrace(); result.error(null, e.getMessage(), null); @@ -294,14 +294,14 @@ private void writeCharacteristicForDevice(String deviceIdentifier, String characteristicUuid, byte[] bytesToWrite, boolean withResponse, - String transactionId, + final String transactionId, final MethodChannel.Result result) { final SafeMainThreadResolver safeMainThreadResolver = new SafeMainThreadResolver<>( new OnSuccessCallback() { @Override public void onSuccess(Characteristic data) { try { - result.success(characteristicsResponseJsonConverter.toJson(createCharacteristicResponse(data))); + result.success(characteristicsResponseJsonConverter.toJson(createCharacteristicResponse(data, transactionId))); } catch (JSONException e) { e.printStackTrace(); result.error(null, e.getMessage(), null); @@ -339,14 +339,14 @@ private void writeCharacteristicForService(int serviceIdentifier, String characteristicUuid, byte[] bytesToWrite, boolean withResponse, - String transactionId, + final String transactionId, final MethodChannel.Result result) { final SafeMainThreadResolver safeMainThreadResolver = new SafeMainThreadResolver<>( new OnSuccessCallback() { @Override public void onSuccess(Characteristic data) { try { - result.success(characteristicsResponseJsonConverter.toJson(createCharacteristicResponse(data))); + result.success(characteristicsResponseJsonConverter.toJson(createCharacteristicResponse(data, transactionId))); } catch (JSONException e) { e.printStackTrace(); result.error(null, e.getMessage(), null); @@ -380,7 +380,7 @@ public void onError(BleError error) { } private void monitorCharacteristicForIdentifier(final int characteristicIdentifier, - String transactionId, + final String transactionId, final MethodChannel.Result result) { bleAdapter.monitorCharacteristic( characteristicIdentifier, @@ -392,7 +392,7 @@ public void onEvent(final Characteristic data) { public void run() { try { characteristicsMonitorStreamHandler.onCharacteristicsUpdate( - createCharacteristicResponse(data) + createCharacteristicResponse(data, transactionId) ); } catch (JSONException e) { e.printStackTrace(); @@ -418,7 +418,7 @@ public void run() { private void monitorCharacteristicForDevice(String deviceIdentifier, String serviceUuid, String characteristicUuid, - String transactionId, + final String transactionId, final MethodChannel.Result result) { bleAdapter.monitorCharacteristicForDevice( deviceIdentifier, @@ -433,7 +433,7 @@ public void onEvent(final Characteristic data) { public void run() { try { characteristicsMonitorStreamHandler.onCharacteristicsUpdate( - createCharacteristicResponse(data) + createCharacteristicResponse(data, transactionId) ); } catch (JSONException e) { e.printStackTrace(); @@ -458,7 +458,7 @@ public void run() { private void monitorCharacteristicForService(int serviceIdentifier, String characteristicUuid, - String transactionId, + final String transactionId, final MethodChannel.Result result) { bleAdapter.monitorCharacteristicForService( serviceIdentifier, @@ -472,7 +472,7 @@ public void onEvent(final Characteristic data) { public void run() { try { characteristicsMonitorStreamHandler.onCharacteristicsUpdate( - createCharacteristicResponse(data) + createCharacteristicResponse(data, transactionId) ); } catch (JSONException e) { e.printStackTrace(); @@ -496,9 +496,14 @@ public void run() { } private SingleCharacteristicResponse createCharacteristicResponse(Characteristic characteristic) { + return createCharacteristicResponse(characteristic, null); + } + + private SingleCharacteristicResponse createCharacteristicResponse(Characteristic characteristic, String transactionId) { return new SingleCharacteristicResponse( characteristic, characteristic.getServiceID(), - characteristic.getServiceUUID()); + characteristic.getServiceUUID(), + transactionId); } } diff --git a/example/lib/device_details/device_details_bloc.dart b/example/lib/device_details/device_details_bloc.dart index 3877f921..b8c81bd4 100644 --- a/example/lib/device_details/device_details_bloc.dart +++ b/example/lib/device_details/device_details_bloc.dart @@ -159,6 +159,14 @@ class DeviceDetailsBloc { }); } + void monitorCharacteristicForPeripheral() { + _clearLogs(); + _deviceController.stream.listen((bleDevice) async { + PeripheralTestOperations(_bleManager, bleDevice.peripheral, log, logError) + .monitorCharacteristicForPeripheral(); + }); + } + void disableBluetooth() { _clearLogs(); _deviceController.stream.listen((bleDevice) async { diff --git a/example/lib/device_details/view/manual_test_view.dart b/example/lib/device_details/view/manual_test_view.dart index 4ca3e9f8..b669ac1a 100644 --- a/example/lib/device_details/view/manual_test_view.dart +++ b/example/lib/device_details/view/manual_test_view.dart @@ -76,6 +76,10 @@ class ManualTestView extends StatelessWidget { _deviceDetailsBloc.writeCharacteristicDirectly(); } + void _monitorCharacteristicForPeripheral() { + _deviceDetailsBloc.monitorCharacteristicForPeripheral(); + } + void _disableBluetooth() { _deviceDetailsBloc.disableBluetooth(); } @@ -149,6 +153,16 @@ class ManualTestView extends StatelessWidget { ], ), ), + Padding( + padding: const EdgeInsets.symmetric(vertical: 2.0), + child: Row( + children: [ + ButtonView("Monitor temp", action: _monitorCharacteristicForPeripheral), + ButtonView("Turn on temp", action: _writeCharacteristicForPeripheral), + ButtonView("Read temp", action: _readCharacteristicForPeripheral), + ], + ), + ), Padding( padding: const EdgeInsets.symmetric(vertical: 2.0), child: Row( diff --git a/example/lib/test_scenarios/peripheral_test_operations.dart b/example/lib/test_scenarios/peripheral_test_operations.dart index e277a624..f40721d5 100644 --- a/example/lib/test_scenarios/peripheral_test_operations.dart +++ b/example/lib/test_scenarios/peripheral_test_operations.dart @@ -196,6 +196,21 @@ class PeripheralTestOperations { return peripheral; } + static int monitorCounter = 0; + + Future monitorCharacteristicForPeripheral() async { + log("Reading temperature"); + int id = monitorCounter; + Stream characteristicStream = await peripheral.monitorCharacteristic( + SensorTagTemperatureUuids.temperatureService, + SensorTagTemperatureUuids.temperatureDataCharacteristic, + transactionId: "$id"); + characteristicStream.listen((characteristicValue) { + log("Read temperature [$id] value ${_convertToTemperature(characteristicValue.value)}C"); + }); + ++monitorCounter; + } + Future readWriteMonitorCharacteristicForService() async { log("Test read/write/monitor characteristic on service"); log("Fetching service"); diff --git a/lib/characteristic.dart b/lib/characteristic.dart index e7d2a788..6f9acb38 100644 --- a/lib/characteristic.dart +++ b/lib/characteristic.dart @@ -76,4 +76,5 @@ class CharacteristicWithValue extends Characteristic with WithValue { ) : super.fromJson(jsonObject, service, manager) { value = base64Decode(jsonObject[_CharacteristicMetadata.value]); } + } diff --git a/lib/internal/bridge/characteristics_mixin.dart b/lib/internal/bridge/characteristics_mixin.dart index e19e68ba..cdb1d251 100644 --- a/lib/internal/bridge/characteristics_mixin.dart +++ b/lib/internal/bridge/characteristics_mixin.dart @@ -21,7 +21,9 @@ mixin CharacteristicsMixin on FlutterBLE { .catchError((errorJson) => Future.error(BleError.fromJson(jsonDecode(errorJson.details)))) .then((rawJsonValue) => - _parseCharacteristicWithValueResponse(peripheral, rawJsonValue).value); + _parseCharacteristicWithValueWithTransactionIdResponse( + peripheral, rawJsonValue) + .value); Future readCharacteristicForDevice( Peripheral peripheral, @@ -43,7 +45,8 @@ mixin CharacteristicsMixin on FlutterBLE { Future.error(BleError.fromJson(jsonDecode(errorJson.details)))) .then( (rawJsonValue) => - _parseCharacteristicWithValueResponse(peripheral, rawJsonValue), + _parseCharacteristicWithValueWithTransactionIdResponse( + peripheral, rawJsonValue), ); Future readCharacteristicForService( @@ -65,7 +68,8 @@ mixin CharacteristicsMixin on FlutterBLE { Future.error(BleError.fromJson(jsonDecode(errorJson.details)))) .then( (rawJsonValue) => - _parseCharacteristicWithValueResponse(peripheral, rawJsonValue), + _parseCharacteristicWithValueWithTransactionIdResponse( + peripheral, rawJsonValue), ); Future writeCharacteristicForIdentifier( @@ -153,7 +157,8 @@ mixin CharacteristicsMixin on FlutterBLE { yield* _characteristicsMonitoringEvents .map( (rawJsonValue) => - _parseCharacteristicWithValueResponse(peripheral, rawJsonValue), + _parseCharacteristicWithValueWithTransactionIdResponse( + peripheral, rawJsonValue), ) .where( (characteristic) => characteristic._id == characteristicIdentifier, @@ -180,10 +185,18 @@ mixin CharacteristicsMixin on FlutterBLE { ); yield* _characteristicsMonitoringEvents .map((rawJsonValue) => - _parseCharacteristicWithValueResponse(peripheral, rawJsonValue)) + _parseCharacteristicWithValueWithTransactionIdResponse( + peripheral, rawJsonValue)) + .map((characteristic) { + print( + "flutter TransactionId [${transactionId}], native id [${characteristic.transactionId}]"); + return characteristic; + }) .where((characteristic) => equalsIgnoreAsciiCase(characteristicUUID, characteristic.uuid) && - equalsIgnoreAsciiCase(serviceUuid, characteristic.service.uuid)) + equalsIgnoreAsciiCase(serviceUuid, characteristic.service.uuid) && + equalsIgnoreAsciiCase( + transactionId ?? "", characteristic.transactionId ?? "")) .handleError((errorJson) => throw BleError.fromJson(jsonDecode(errorJson.details))); } @@ -205,7 +218,8 @@ mixin CharacteristicsMixin on FlutterBLE { yield* _characteristicsMonitoringEvents .map( (rawJsonValue) => - _parseCharacteristicWithValueResponse(peripheral, rawJsonValue), + _parseCharacteristicWithValueWithTransactionIdResponse( + peripheral, rawJsonValue), ) .where( (characteristic) => @@ -216,22 +230,40 @@ mixin CharacteristicsMixin on FlutterBLE { throw BleError.fromJson(jsonDecode(errorJson.details))); } -CharacteristicWithValue _parseCharacteristicWithValueResponse( - Peripheral peripheral, rawJsonValue) { - Map rootObject = jsonDecode(rawJsonValue); - Service service = Service.fromJson(rootObject, peripheral, _manager); + CharacteristicWithValueAndTransactionId + _parseCharacteristicWithValueWithTransactionIdResponse( + Peripheral peripheral, rawJsonValue) { + Map rootObject = jsonDecode(rawJsonValue); + Service service = Service.fromJson(rootObject, peripheral, _manager); - return CharacteristicWithValue.fromJson( - rootObject["characteristic"], service, _manager); -} + var transactionId = rootObject["transactionId"]; + return CharacteristicWithValueAndTransactionId.fromJson( + rootObject["characteristic"], service, _manager) + .setTransactionId(transactionId); + } -Characteristic _parseCharacteristicResponse( - Peripheral peripheral, rawJsonValue) { - Map rootObject = jsonDecode(rawJsonValue); - Service service = Service.fromJson(rootObject, peripheral, _manager); + Characteristic _parseCharacteristicResponse( + Peripheral peripheral, rawJsonValue) { + Map rootObject = jsonDecode(rawJsonValue); + Service service = Service.fromJson(rootObject, peripheral, _manager); - return Characteristic.fromJson( - rootObject["characteristic"], service, _manager); + return Characteristic.fromJson( + rootObject["characteristic"], service, _manager); + } } +class CharacteristicWithValueAndTransactionId extends CharacteristicWithValue { + String transactionId; + + CharacteristicWithValueAndTransactionId.fromJson( + Map jsonObject, + Service service, + ManagerForCharacteristic manager, + ) : super.fromJson(jsonObject, service, manager); + + CharacteristicWithValueAndTransactionId setTransactionId( + String transactionId) { + this.transactionId = transactionId; + return this; + } }