Skip to content

Commit

Permalink
monitor transactionId of monitoring characteristics - remove doubled … (
Browse files Browse the repository at this point in the history
#227)

* monitor transactionId of monitoring characteristics - remove doubled events

* refactor

* refactor

* revert

* Hide internal types from the user

* Remove unnecessary mixin
  • Loading branch information
pawelByszewski authored and mikolak committed Oct 18, 2019
1 parent 7942816 commit a428d40
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 39 deletions.
Expand Up @@ -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() {
Expand All @@ -26,4 +29,8 @@ public int getServiceId() {
public UUID getServiceUuid() {
return serviceUuid;
}

public String getTransactionId() {
return transactionId;
}
}
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
Expand Up @@ -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<Characteristic> safeMainThreadResolver = new SafeMainThreadResolver<>(
new OnSuccessCallback<Characteristic>() {
@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);
Expand Down Expand Up @@ -173,7 +173,7 @@ private void readCharacteristicForDevice(
String deviceIdentifier,
String serviceUuid,
String characteristicUuid,
String transactionId,
final String transactionId,
final MethodChannel.Result result) {

final SafeMainThreadResolver<Characteristic> safeMainThreadResolver = new SafeMainThreadResolver<>(
Expand Down Expand Up @@ -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<Characteristic> safeMainThreadResolver = new SafeMainThreadResolver<>(
new OnSuccessCallback<Characteristic>() {
@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);
Expand Down Expand Up @@ -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<Characteristic> safeMainThreadResolver = new SafeMainThreadResolver<>(
new OnSuccessCallback<Characteristic>() {
@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);
Expand Down Expand Up @@ -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<Characteristic> safeMainThreadResolver = new SafeMainThreadResolver<>(
new OnSuccessCallback<Characteristic>() {
@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);
Expand Down Expand Up @@ -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<Characteristic> safeMainThreadResolver = new SafeMainThreadResolver<>(
new OnSuccessCallback<Characteristic>() {
@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);
Expand Down Expand Up @@ -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,
Expand All @@ -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();
Expand All @@ -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,
Expand All @@ -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();
Expand All @@ -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,
Expand All @@ -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();
Expand All @@ -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);
}
}
8 changes: 8 additions & 0 deletions example/lib/device_details/device_details_bloc.dart
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions example/lib/device_details/view/manual_test_view.dart
Expand Up @@ -76,6 +76,10 @@ class ManualTestView extends StatelessWidget {
_deviceDetailsBloc.writeCharacteristicDirectly();
}

void _monitorCharacteristicForPeripheral() {
_deviceDetailsBloc.monitorCharacteristicForPeripheral();
}

void _disableBluetooth() {
_deviceDetailsBloc.disableBluetooth();
}
Expand Down Expand Up @@ -149,6 +153,16 @@ class ManualTestView extends StatelessWidget {
],
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 2.0),
child: Row(
children: <Widget>[
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(
Expand Down
15 changes: 15 additions & 0 deletions example/lib/test_scenarios/peripheral_test_operations.dart
Expand Up @@ -196,6 +196,21 @@ class PeripheralTestOperations {
return peripheral;
}

static int monitorCounter = 0;

Future<void> monitorCharacteristicForPeripheral() async {
log("Reading temperature");
int id = monitorCounter;
Stream<CharacteristicWithValue> characteristicStream = await peripheral.monitorCharacteristic(
SensorTagTemperatureUuids.temperatureService,
SensorTagTemperatureUuids.temperatureDataCharacteristic,
transactionId: "$id");
characteristicStream.listen((characteristicValue) {
log("Read temperature [$id] value ${_convertToTemperature(characteristicValue.value)}C");
});
++monitorCounter;
}

Future<void> readWriteMonitorCharacteristicForService() async {
log("Test read/write/monitor characteristic on service");
log("Fetching service");
Expand Down
1 change: 1 addition & 0 deletions lib/characteristic.dart
Expand Up @@ -76,4 +76,5 @@ class CharacteristicWithValue extends Characteristic with WithValue {
) : super.fromJson(jsonObject, service, manager) {
value = base64Decode(jsonObject[_CharacteristicMetadata.value]);
}

}

0 comments on commit a428d40

Please sign in to comment.