63 using GattCharacteristic = winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::GattCharacteristic;
64 using GattValueChangedEventArgs = winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::GattValueChangedEventArgs;
67 GattCharacteristic _characteristic{
nullptr };
70 std::function<void(
const std::vector<std::uint8_t>&)> _onValueChanged{};
73 winrt::event_token _valueChangedToken{};
74 std::recursive_mutex _subscribeMtx{};
87 _characteristic.ValueChanged(_valueChangedToken);
88 _characteristic =
nullptr;
103 return _characteristic.AttributeHandle();
113 return _characteristic.Uuid();
122 std::underlying_type<CharacteristicProperties>::type
properties()
const
124 return static_cast<std::underlying_type<CharacteristicProperties>::type
>(
125 _characteristic.CharacteristicProperties());
174 auto result =
co_await _characteristic.ReadValueAsync();
175 co_return Internal::dataBufferToBytesVector(result.Value());
187 std::future<BleRequestStatus>
writeAsync(
const std::vector<std::uint8_t>& data,
bool withoutResponse =
false)
190 using namespace winrt::Windows::Devices::Bluetooth::GenericAttributeProfile;
193 auto options = withoutResponse ? GattWriteOption::WriteWithoutResponse : GattWriteOption::WriteWithResponse;
194 auto result =
co_await _characteristic.WriteValueAsync(Internal::bytesVectorToDataBuffer(data), options);
208 std::future<BleRequestStatus>
subscribeAsync(
const std::function<
void(
const std::vector<std::uint8_t>&)>& onValueChanged)
210 using namespace winrt::Windows::Devices::Bluetooth::GenericAttributeProfile;
217 std::lock_guard lock{ _subscribeMtx };
220 _onValueChanged = onValueChanged;
221 if (!_valueChangedToken)
223 _valueChangedToken = _characteristic.ValueChanged({
this, &Characteristic::onValueChanged });
228 auto result =
co_await _characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(
229 GattClientCharacteristicConfigurationDescriptorValue::Notify);
241 using namespace winrt::Windows::Devices::Bluetooth::GenericAttributeProfile;
245 std::lock_guard lock{ _subscribeMtx };
246 if (!_onValueChanged)
252 _onValueChanged =
nullptr;
256 _characteristic.ValueChanged(_valueChangedToken);
257 _valueChangedToken = winrt::event_token{};
260 auto result =
co_await _characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(
261 GattClientCharacteristicConfigurationDescriptorValue::None);
271 : _characteristic{ characteristic }
276 void onValueChanged(GattCharacteristic _, GattValueChangedEventArgs args)
279 std::function<void(
const std::vector<std::uint8_t>&)> callback{};
281 std::lock_guard lock{ _subscribeMtx };
282 callback = _onValueChanged;
287 callback(Internal::dataBufferToBytesVector(args.CharacteristicValue()));
Represents a service's characteristic of a Bluetooth Low Energy (BLE) peripheral.
Definition: Characteristic.h:62
std::future< std::vector< std::uint8_t > > readValueAsync()
Reads the value from the characteristic.
Definition: Characteristic.h:169
winrt::guid uuid() const
Gets the UUID of the characteristic.
Definition: Characteristic.h:111
bool canRead() const
Indicates whether the characteristic can be read.
Definition: Characteristic.h:143
std::uint16_t handle() const
Gets the 16 bits handle of the BLE characteristic.
Definition: Characteristic.h:101
std::future< BleRequestStatus > unsubscribeAsync()
Unsubscribes from value changes of the characteristic.
Definition: Characteristic.h:239
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:208
~Characteristic()
Destroys the Characteristic instance.
Definition: Characteristic.h:83
std::underlying_type< CharacteristicProperties >::type properties() const
Gets the standard BLE properties of the characteristic.
Definition: Characteristic.h:122
bool canWrite() const
Indicates whether the characteristic can be written.
Definition: Characteristic.h:133
bool canNotify() const
Indicates whether the characteristic can notify its value changes.
Definition: Characteristic.h:153
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:187
std::future< BleRequestStatus > connectAsync(std::vector< winrt::guid > requiredServices=std::vector< winrt::guid >{}, bool maintainConnection=false)
Connects to the BLE peripheral.
A collection of C++ classes that provides a simplified access to Bluetooth Low Energy peripherals.
Definition: BleTypes.h:38
@ InvalidParameters
The request did not run because some of its parameters are invalid.
@ Success
The request completed successfully.
@ Error
The request completed with a non-specific error.
@ NotSupported
The request failed because of the operation is not supported by the peripheral.
CharacteristicProperties
Standard BLE values for characteristic properties, those are flags that can be combined.
Definition: Characteristic.h:16
@ SignedWrite
Characteristic supports write with signature.
Definition: Characteristic.h:38
@ Write
Characteristic can be written.
Definition: Characteristic.h:29
@ WriteWithoutResponse
Characteristic can be written without response.
Definition: Characteristic.h:26
@ Indicate
Characteristic supports indication.
Definition: Characteristic.h:35
@ NotifyEncryptionRequired
Characteristic notification uses encryption.
Definition: Characteristic.h:44
@ ExtendedProperties
Characteristic has extended properties.
Definition: Characteristic.h:41
@ Notify
Characteristic supports notification.
Definition: Characteristic.h:32
@ IndicateEncryptionRequired
Characteristic indication uses encryption.
Definition: Characteristic.h:47
@ Broadcast
Characteristic is broadcastable.
Definition: Characteristic.h:20
@ Read
Characteristic is readable.
Definition: Characteristic.h:23
@ Peripheral
Disconnection was initiated by peripheral.