27 using GattCharacteristic = winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::GattCharacteristic;
28 using GattValueChangedEventArgs = winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::GattValueChangedEventArgs;
31 GattCharacteristic _characteristic{
nullptr };
34 std::function<void(
const std::vector<std::uint8_t>&)> _onValueChanged{};
37 winrt::event_token _valueChangedToken{};
38 std::recursive_mutex _subscribeMtx{};
51 _characteristic.ValueChanged(_valueChangedToken);
52 _characteristic =
nullptr;
67 return _characteristic.AttributeHandle();
77 return _characteristic.Uuid();
86 std::underlying_type<CharacteristicProperties>::type
properties()
const
88 return static_cast<std::underlying_type<CharacteristicProperties>::type
>(
89 _characteristic.CharacteristicProperties());
99 return properties() & CharacteristicProperties::Write;
109 return properties() & CharacteristicProperties::Read;
119 return properties() & CharacteristicProperties::Notify;
138 auto result =
co_await _characteristic.ReadValueAsync();
139 co_return Internal::dataBufferToBytesVector(result.Value());
151 std::future<BleRequestStatus>
writeAsync(
const std::vector<std::uint8_t>& data,
bool withoutResponse =
false)
154 using namespace winrt::Windows::Devices::Bluetooth::GenericAttributeProfile;
157 auto options = withoutResponse ? GattWriteOption::WriteWithoutResponse : GattWriteOption::WriteWithResponse;
158 auto result =
co_await _characteristic.WriteValueAsync(Internal::bytesVectorToDataBuffer(data), options);
160 co_return result == GattCommunicationStatus::Success ? BleRequestStatus::Success : BleRequestStatus::Error;
172 std::future<BleRequestStatus>
subscribeAsync(
const std::function<
void(
const std::vector<std::uint8_t>&)>& onValueChanged)
174 using namespace winrt::Windows::Devices::Bluetooth::GenericAttributeProfile;
177 if (!onValueChanged)
co_return BleRequestStatus::InvalidParameters;
178 if (!
canNotify())
co_return BleRequestStatus::NotSupported;
181 std::lock_guard lock{ _subscribeMtx };
184 _onValueChanged = onValueChanged;
185 if (!_valueChangedToken)
187 _valueChangedToken = _characteristic.ValueChanged({
this, &Characteristic::onValueChanged });
192 auto result =
co_await _characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(
193 GattClientCharacteristicConfigurationDescriptorValue::Notify);
195 co_return result == GattCommunicationStatus::Success ? BleRequestStatus::Success : BleRequestStatus::Error;
205 using namespace winrt::Windows::Devices::Bluetooth::GenericAttributeProfile;
209 std::lock_guard lock{ _subscribeMtx };
210 if (!_onValueChanged)
212 co_return BleRequestStatus::Success;
216 _onValueChanged =
nullptr;
220 _characteristic.ValueChanged(_valueChangedToken);
221 _valueChangedToken = winrt::event_token{};
224 auto result =
co_await _characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(
225 GattClientCharacteristicConfigurationDescriptorValue::None);
227 co_return result == GattCommunicationStatus::Success ? BleRequestStatus::Success : BleRequestStatus::Error;
235 : _characteristic{ characteristic }
240 void onValueChanged(GattCharacteristic _, GattValueChangedEventArgs args)
243 std::function<void(
const std::vector<std::uint8_t>&)> callback{};
245 std::lock_guard lock{ _subscribeMtx };
246 callback = _onValueChanged;
251 callback(Internal::dataBufferToBytesVector(args.CharacteristicValue()));
Represents a service's characteristic of a Bluetooth Low Energy (BLE) peripheral.
Definition Characteristic.h:26
std::future< std::vector< std::uint8_t > > readValueAsync()
Reads the value from the characteristic.
Definition Characteristic.h:133
winrt::guid uuid() const
Gets the UUID of the characteristic.
Definition Characteristic.h:75
bool canRead() const
Indicates whether the characteristic can be read.
Definition Characteristic.h:107
std::uint16_t handle() const
Gets the 16 bits handle of the BLE characteristic.
Definition Characteristic.h:65
std::future< BleRequestStatus > unsubscribeAsync()
Unsubscribes from value changes of the characteristic.
Definition Characteristic.h:203
std::future< BleRequestStatus > subscribeAsync(const std::function< void(const std::vector< std::uint8_t > &)> &onValueChanged)
Subscribes for value changes of the characteristic.
Definition Characteristic.h:172
~Characteristic()
Destroys the Characteristic instance.
Definition Characteristic.h:47
std::underlying_type< CharacteristicProperties >::type properties() const
Gets the standard BLE properties of the characteristic.
Definition Characteristic.h:86
bool canWrite() const
Indicates whether the characteristic can be written.
Definition Characteristic.h:97
bool canNotify() const
Indicates whether the characteristic can notify its value changes.
Definition Characteristic.h:117
std::future< BleRequestStatus > writeAsync(const std::vector< std::uint8_t > &data, bool withoutResponse=false)
Writes the given data to the value of the characteristic.
Definition Characteristic.h:151
Represents a Bluetooth Low Energy (BLE) peripheral.
Definition Peripheral.h:33
A collection of C++ classes that provides a simplified access to Bluetooth Low Energy peripherals.
Definition bletypes.h:36