Improve setNotifiation completion (#42)

* Improve setNotifiation completion

* Implement Windows

* Minor android fixes

* Few more fixes

* Update changelog

---------

Co-authored-by: Foti Dim <fdimanidis@gmail.com>
This commit is contained in:
Rohit Sangwan
2024-05-10 16:29:10 +05:30
committed by GitHub
parent b07a8888e0
commit 05697b7d30
19 changed files with 507 additions and 290 deletions
+46 -30
View File
@@ -1,4 +1,4 @@
// Autogenerated from Pigeon (v17.1.1), do not edit directly.
// Autogenerated from Pigeon (v18.0.1), do not edit directly.
// See also: https://pub.dev/packages/pigeon
#undef _HAS_EXCEPTIONS
@@ -453,8 +453,16 @@ const flutter::StandardMessageCodec& UniversalBlePlatformChannel::GetCodec() {
void UniversalBlePlatformChannel::SetUp(
flutter::BinaryMessenger* binary_messenger,
UniversalBlePlatformChannel* api) {
UniversalBlePlatformChannel::SetUp(binary_messenger, api, "");
}
void UniversalBlePlatformChannel::SetUp(
flutter::BinaryMessenger* binary_messenger,
UniversalBlePlatformChannel* api,
const std::string& message_channel_suffix) {
const std::string prepended_suffix = message_channel_suffix.length() > 0 ? std::string(".") + message_channel_suffix : "";
{
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.getBluetoothAvailabilityState", &GetCodec());
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.getBluetoothAvailabilityState" + prepended_suffix, &GetCodec());
if (api != nullptr) {
channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) {
try {
@@ -476,7 +484,7 @@ void UniversalBlePlatformChannel::SetUp(
}
}
{
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.enableBluetooth", &GetCodec());
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.enableBluetooth" + prepended_suffix, &GetCodec());
if (api != nullptr) {
channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) {
try {
@@ -498,7 +506,7 @@ void UniversalBlePlatformChannel::SetUp(
}
}
{
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.startScan", &GetCodec());
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.startScan" + prepended_suffix, &GetCodec());
if (api != nullptr) {
channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) {
try {
@@ -522,7 +530,7 @@ void UniversalBlePlatformChannel::SetUp(
}
}
{
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.stopScan", &GetCodec());
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.stopScan" + prepended_suffix, &GetCodec());
if (api != nullptr) {
channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) {
try {
@@ -543,7 +551,7 @@ void UniversalBlePlatformChannel::SetUp(
}
}
{
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.connect", &GetCodec());
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.connect" + prepended_suffix, &GetCodec());
if (api != nullptr) {
channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) {
try {
@@ -571,7 +579,7 @@ void UniversalBlePlatformChannel::SetUp(
}
}
{
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.disconnect", &GetCodec());
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.disconnect" + prepended_suffix, &GetCodec());
if (api != nullptr) {
channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) {
try {
@@ -599,7 +607,7 @@ void UniversalBlePlatformChannel::SetUp(
}
}
{
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.setNotifiable", &GetCodec());
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.setNotifiable" + prepended_suffix, &GetCodec());
if (api != nullptr) {
channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) {
try {
@@ -628,14 +636,15 @@ void UniversalBlePlatformChannel::SetUp(
return;
}
const int64_t ble_input_property_arg = encodable_ble_input_property_arg.LongValue();
std::optional<FlutterError> output = api->SetNotifiable(device_id_arg, service_arg, characteristic_arg, ble_input_property_arg);
if (output.has_value()) {
reply(WrapError(output.value()));
return;
}
EncodableList wrapped;
wrapped.push_back(EncodableValue());
reply(EncodableValue(std::move(wrapped)));
api->SetNotifiable(device_id_arg, service_arg, characteristic_arg, ble_input_property_arg, [reply](std::optional<FlutterError>&& output) {
if (output.has_value()) {
reply(WrapError(output.value()));
return;
}
EncodableList wrapped;
wrapped.push_back(EncodableValue());
reply(EncodableValue(std::move(wrapped)));
});
} catch (const std::exception& exception) {
reply(WrapError(exception.what()));
}
@@ -645,7 +654,7 @@ void UniversalBlePlatformChannel::SetUp(
}
}
{
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.discoverServices", &GetCodec());
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.discoverServices" + prepended_suffix, &GetCodec());
if (api != nullptr) {
channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) {
try {
@@ -674,7 +683,7 @@ void UniversalBlePlatformChannel::SetUp(
}
}
{
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.readValue", &GetCodec());
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.readValue" + prepended_suffix, &GetCodec());
if (api != nullptr) {
channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) {
try {
@@ -715,7 +724,7 @@ void UniversalBlePlatformChannel::SetUp(
}
}
{
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.requestMtu", &GetCodec());
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.requestMtu" + prepended_suffix, &GetCodec());
if (api != nullptr) {
channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) {
try {
@@ -750,7 +759,7 @@ void UniversalBlePlatformChannel::SetUp(
}
}
{
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.writeValue", &GetCodec());
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.writeValue" + prepended_suffix, &GetCodec());
if (api != nullptr) {
channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) {
try {
@@ -803,7 +812,7 @@ void UniversalBlePlatformChannel::SetUp(
}
}
{
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.isPaired", &GetCodec());
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.isPaired" + prepended_suffix, &GetCodec());
if (api != nullptr) {
channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) {
try {
@@ -832,7 +841,7 @@ void UniversalBlePlatformChannel::SetUp(
}
}
{
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.pair", &GetCodec());
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.pair" + prepended_suffix, &GetCodec());
if (api != nullptr) {
channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) {
try {
@@ -860,7 +869,7 @@ void UniversalBlePlatformChannel::SetUp(
}
}
{
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.unPair", &GetCodec());
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.unPair" + prepended_suffix, &GetCodec());
if (api != nullptr) {
channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) {
try {
@@ -888,7 +897,7 @@ void UniversalBlePlatformChannel::SetUp(
}
}
{
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.getConnectedDevices", &GetCodec());
BasicMessageChannel<> channel(binary_messenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.getConnectedDevices" + prepended_suffix, &GetCodec());
if (api != nullptr) {
channel.SetMessageHandler([api](const EncodableValue& message, const flutter::MessageReply<EncodableValue>& reply) {
try {
@@ -963,7 +972,14 @@ void UniversalBleCallbackChannelCodecSerializer::WriteValue(
// Generated class from Pigeon that represents Flutter messages that can be called from C++.
UniversalBleCallbackChannel::UniversalBleCallbackChannel(flutter::BinaryMessenger* binary_messenger)
: binary_messenger_(binary_messenger) {}
: binary_messenger_(binary_messenger),
message_channel_suffix_("") {}
UniversalBleCallbackChannel::UniversalBleCallbackChannel(
flutter::BinaryMessenger* binary_messenger,
const std::string& message_channel_suffix)
: binary_messenger_(binary_messenger),
message_channel_suffix_(message_channel_suffix.length() > 0 ? std::string(".") + message_channel_suffix : "") {}
const flutter::StandardMessageCodec& UniversalBleCallbackChannel::GetCodec() {
return flutter::StandardMessageCodec::GetInstance(&UniversalBleCallbackChannelCodecSerializer::GetInstance());
@@ -973,7 +989,7 @@ void UniversalBleCallbackChannel::OnAvailabilityChanged(
int64_t state_arg,
std::function<void(void)>&& on_success,
std::function<void(const FlutterError&)>&& on_error) {
const std::string channel_name = "dev.flutter.pigeon.universal_ble.UniversalBleCallbackChannel.onAvailabilityChanged";
const std::string channel_name = "dev.flutter.pigeon.universal_ble.UniversalBleCallbackChannel.onAvailabilityChanged" + message_channel_suffix_;
BasicMessageChannel<> channel(binary_messenger_, channel_name, &GetCodec());
EncodableValue encoded_api_arguments = EncodableValue(EncodableList{
EncodableValue(state_arg),
@@ -1000,7 +1016,7 @@ void UniversalBleCallbackChannel::OnPairStateChange(
const std::string* error_arg,
std::function<void(void)>&& on_success,
std::function<void(const FlutterError&)>&& on_error) {
const std::string channel_name = "dev.flutter.pigeon.universal_ble.UniversalBleCallbackChannel.onPairStateChange";
const std::string channel_name = "dev.flutter.pigeon.universal_ble.UniversalBleCallbackChannel.onPairStateChange" + message_channel_suffix_;
BasicMessageChannel<> channel(binary_messenger_, channel_name, &GetCodec());
EncodableValue encoded_api_arguments = EncodableValue(EncodableList{
EncodableValue(device_id_arg),
@@ -1027,7 +1043,7 @@ void UniversalBleCallbackChannel::OnScanResult(
const UniversalBleScanResult& result_arg,
std::function<void(void)>&& on_success,
std::function<void(const FlutterError&)>&& on_error) {
const std::string channel_name = "dev.flutter.pigeon.universal_ble.UniversalBleCallbackChannel.onScanResult";
const std::string channel_name = "dev.flutter.pigeon.universal_ble.UniversalBleCallbackChannel.onScanResult" + message_channel_suffix_;
BasicMessageChannel<> channel(binary_messenger_, channel_name, &GetCodec());
EncodableValue encoded_api_arguments = EncodableValue(EncodableList{
CustomEncodableValue(result_arg),
@@ -1054,7 +1070,7 @@ void UniversalBleCallbackChannel::OnValueChanged(
const std::vector<uint8_t>& value_arg,
std::function<void(void)>&& on_success,
std::function<void(const FlutterError&)>&& on_error) {
const std::string channel_name = "dev.flutter.pigeon.universal_ble.UniversalBleCallbackChannel.onValueChanged";
const std::string channel_name = "dev.flutter.pigeon.universal_ble.UniversalBleCallbackChannel.onValueChanged" + message_channel_suffix_;
BasicMessageChannel<> channel(binary_messenger_, channel_name, &GetCodec());
EncodableValue encoded_api_arguments = EncodableValue(EncodableList{
EncodableValue(device_id_arg),
@@ -1082,7 +1098,7 @@ void UniversalBleCallbackChannel::OnConnectionChanged(
int64_t state_arg,
std::function<void(void)>&& on_success,
std::function<void(const FlutterError&)>&& on_error) {
const std::string channel_name = "dev.flutter.pigeon.universal_ble.UniversalBleCallbackChannel.onConnectionChanged";
const std::string channel_name = "dev.flutter.pigeon.universal_ble.UniversalBleCallbackChannel.onConnectionChanged" + message_channel_suffix_;
BasicMessageChannel<> channel(binary_messenger_, channel_name, &GetCodec());
EncodableValue encoded_api_arguments = EncodableValue(EncodableList{
EncodableValue(device_id_arg),
+12 -3
View File
@@ -1,4 +1,4 @@
// Autogenerated from Pigeon (v17.1.1), do not edit directly.
// Autogenerated from Pigeon (v18.0.1), do not edit directly.
// See also: https://pub.dev/packages/pigeon
#ifndef PIGEON_UNIVERSAL_BLE_G_H_
@@ -280,11 +280,12 @@ class UniversalBlePlatformChannel {
virtual std::optional<FlutterError> StopScan() = 0;
virtual std::optional<FlutterError> Connect(const std::string& device_id) = 0;
virtual std::optional<FlutterError> Disconnect(const std::string& device_id) = 0;
virtual std::optional<FlutterError> SetNotifiable(
virtual void SetNotifiable(
const std::string& device_id,
const std::string& service,
const std::string& characteristic,
int64_t ble_input_property) = 0;
int64_t ble_input_property,
std::function<void(std::optional<FlutterError> reply)> result) = 0;
virtual void DiscoverServices(
const std::string& device_id,
std::function<void(ErrorOr<flutter::EncodableList> reply)> result) = 0;
@@ -319,6 +320,10 @@ class UniversalBlePlatformChannel {
static void SetUp(
flutter::BinaryMessenger* binary_messenger,
UniversalBlePlatformChannel* api);
static void SetUp(
flutter::BinaryMessenger* binary_messenger,
UniversalBlePlatformChannel* api,
const std::string& message_channel_suffix);
static flutter::EncodableValue WrapError(std::string_view error_message);
static flutter::EncodableValue WrapError(const FlutterError& error);
@@ -351,6 +356,9 @@ class UniversalBleCallbackChannelCodecSerializer : public flutter::StandardCodec
class UniversalBleCallbackChannel {
public:
UniversalBleCallbackChannel(flutter::BinaryMessenger* binary_messenger);
UniversalBleCallbackChannel(
flutter::BinaryMessenger* binary_messenger,
const std::string& message_channel_suffix);
static const flutter::StandardMessageCodec& GetCodec();
void OnAvailabilityChanged(
int64_t state,
@@ -380,6 +388,7 @@ class UniversalBleCallbackChannel {
private:
flutter::BinaryMessenger* binary_messenger_;
std::string message_channel_suffix_;
};
} // namespace universal_ble
+22 -10
View File
@@ -206,17 +206,21 @@ namespace universal_ble
}
}
std::optional<FlutterError> UniversalBlePlugin::SetNotifiable(
void UniversalBlePlugin::SetNotifiable(
const std::string &device_id,
const std::string &service,
const std::string &characteristic,
int64_t ble_input_property)
int64_t ble_input_property,
std::function<void(std::optional<FlutterError> reply)> result)
{
try
{
auto it = connectedDevices.find(_str_to_mac_address(device_id));
if (it == connectedDevices.end())
return FlutterError("IllegalArgument", "Unknown devicesId:" + device_id);
{
result(FlutterError("IllegalArgument", "Unknown devicesId:" + device_id));
return;
}
auto bluetoothAgent = *it->second;
GattCharacteristicObject &gatt_characteristic_holder = bluetoothAgent._fetch_characteristic(service, characteristic);
GattCharacteristic gattCharacteristic = gatt_characteristic_holder.obj;
@@ -228,7 +232,8 @@ namespace universal_ble
descriptorValue = GattClientCharacteristicConfigurationDescriptorValue::Notify;
if ((properties & GattCharacteristicProperties::Notify) == GattCharacteristicProperties::None)
{
return FlutterError("Characteristic does not support notify");
result(FlutterError("Characteristic does not support notify"));
return;
}
}
else if (ble_input_property == static_cast<int>(BleInputProperty::indication))
@@ -236,21 +241,21 @@ namespace universal_ble
descriptorValue = GattClientCharacteristicConfigurationDescriptorValue::Indicate;
if ((properties & GattCharacteristicProperties::Indicate) == GattCharacteristicProperties::None)
{
return FlutterError("Characteristic does not support indicate");
result(FlutterError("Characteristic does not support indicate"));
return;
}
}
SetNotifiableAsync(bluetoothAgent, service, characteristic, descriptorValue);
return std::nullopt;
SetNotifiableAsync(bluetoothAgent, service, characteristic, descriptorValue, result);
}
catch (const FlutterError &err)
{
return err;
return result(err);
}
catch (...)
{
std::cout << "SetNotifiableLog: Unknown error" << std::endl;
return FlutterError("Unknown error");
return result(FlutterError("Unknown error"));
}
};
@@ -1178,7 +1183,9 @@ namespace universal_ble
};
winrt::fire_and_forget UniversalBlePlugin::SetNotifiableAsync(BluetoothDeviceAgent &bluetoothDeviceAgent, const std::string &service,
const std::string &characteristic, GattClientCharacteristicConfigurationDescriptorValue descriptorValue)
const std::string &characteristic,
GattClientCharacteristicConfigurationDescriptorValue descriptorValue,
std::function<void(std::optional<FlutterError> reply)> result)
{
GattCharacteristicObject &gatt_characteristic_holder = bluetoothDeviceAgent._fetch_characteristic(service, characteristic);
GattCharacteristic gattCharacteristic = gatt_characteristic_holder.obj;
@@ -1205,11 +1212,15 @@ namespace universal_ble
std::cout << "FailedToSubscribe: Unknown" << to_uuidstr(gattCharacteristic.Uuid()) << std::endl;
}
if (status != GattCommunicationStatus::Success)
{
result(FlutterError("Failed to update notification state"));
co_return;
}
}
catch (...)
{
std::cout << "FailedToPerformThisOperationOn: " << to_uuidstr(gattCharacteristic.Uuid()) << std::endl;
result(FlutterError("Failed to update notification state"));
co_return;
}
// create uniqKey with uuid and deviceId
@@ -1239,6 +1250,7 @@ namespace universal_ble
}
characteristicsTokens[uniqTokenKey] = gattCharacteristic.ValueChanged({this, &UniversalBlePlugin::GattCharacteristic_ValueChanged});
}
result(std::nullopt);
}
void UniversalBlePlugin::GattCharacteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
+5 -3
View File
@@ -113,7 +113,8 @@ namespace universal_ble
void CleanConnection(uint64_t bluetoothAddress);
void DiscoverServicesAsync(BluetoothDeviceAgent &bluetoothDeviceAgent, std::function<void(ErrorOr<flutter::EncodableList> reply)>);
winrt::fire_and_forget SetNotifiableAsync(BluetoothDeviceAgent &bluetoothDeviceAgent, const std::string &service,
const std::string &characteristic, GattClientCharacteristicConfigurationDescriptorValue descriptorValue);
const std::string &characteristic, GattClientCharacteristicConfigurationDescriptorValue descriptorValue,
std::function<void(std::optional<FlutterError> reply)> result );
void GattCharacteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args);
AvailabilityState getAvailabilityStateFromRadio(RadioState radioState);
std::string parsePairingFailError(Enumeration::DevicePairingResult result);
@@ -135,11 +136,12 @@ namespace universal_ble
void DiscoverServices(
const std::string &device_id,
std::function<void(ErrorOr<flutter::EncodableList> reply)> result) override;
std::optional<FlutterError> SetNotifiable(
void SetNotifiable(
const std::string &device_id,
const std::string &service,
const std::string &characteristic,
int64_t ble_input_property) override;
int64_t ble_input_property,
std::function<void(std::optional<FlutterError> reply)> result) override;
void ReadValue(
const std::string &device_id,
const std::string &service,