32 class Peripheral :
public std::enable_shared_from_this<Peripheral>
34 using BluetoothLEDevice = winrt::Windows::Devices::Bluetooth::BluetoothLEDevice;
35 using GattSession = winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::GattSession;
44 BluetoothLEDevice _device{
nullptr };
45 GattSession _session{
nullptr };
46 winrt::event_token _connectionStatusChangedToken{};
49 std::unordered_map<winrt::guid, std::shared_ptr<Service>> _services{};
52 volatile bool _isReady{};
55 mutable std::recursive_mutex _connectOpMtx{};
56 volatile size_t _connectCounter{};
57 volatile bool _connecting{};
60 std::vector<std::tuple<ConnectionEvent, ConnectionEventReason>> _connectionEventsQueue{};
74 : _address{ bluetoothAddress }, _onConnectionEvent{ onConnectionEvent }
76 assert(bluetoothAddress);
77 assert(onConnectionEvent);
78 _connectionEventsQueue.reserve(16);
105 std::vector<winrt::guid> requiredServices = std::vector<winrt::guid>{},
106 bool maintainConnection =
false);
115 internalDisconnect(ConnectionEventReason::Success);
116 notifyQueuedConnectionEvents();
143 using namespace winrt::Windows::Devices::Bluetooth;
145 auto dev = safeGetDevice();
146 return dev ? (dev.ConnectionStatus() == BluetoothConnectionStatus::Connected) :
false;
174 auto dev = safeGetDevice();
175 return dev ? dev.DeviceId().data() :
nullptr;
185 auto dev = safeGetDevice();
186 return dev ? dev.Name().data() :
nullptr;
197 std::lock_guard lock{ _connectOpMtx };
198 return _session ? _session.MaxPduSize() : 0;
214 std::lock_guard lock{ _connectOpMtx };
215 auto it = _services.find(uuid);
216 return it != _services.end() ? it->second :
nullptr;
226 std::lock_guard lock{ _connectOpMtx };
227 outServices.reserve(outServices.size() + _services.size());
228 for (
auto& [_, s] : _services)
230 outServices.emplace_back(s);
238 BluetoothLEDevice safeGetDevice()
const
241 std::lock_guard lock{ _connectOpMtx };
249 void notifyQueuedConnectionEvents()
252 decltype(_connectionEventsQueue) queue{};
254 std::lock_guard lock{ _connectOpMtx };
255 queue = _connectionEventsQueue;
256 _connectionEventsQueue.clear();
258 if (_onConnectionEvent)
260 for (
auto& [ev, reason] : queue)
262 _onConnectionEvent(ev, reason);
268 void onDeviceConnectionStatusChanged(BluetoothLEDevice device, winrt::Windows::Foundation::IInspectable _)
270 using namespace winrt::Windows::Devices::Bluetooth;
272 if (device.ConnectionStatus() == BluetoothConnectionStatus::Disconnected)
274 bool linkLoss = (_session !=
nullptr) && _session.MaintainConnection();
276 notifyQueuedConnectionEvents();
Represents a Bluetooth Low Energy (BLE) peripheral.
Definition Peripheral.h:33
bluetooth_address_t address() const
Gets the Bluetooth address of the peripheral.
Definition Peripheral.h:129
const wchar_t * name() const
Gets the name of the peripheral.
Definition Peripheral.h:183
Peripheral(bluetooth_address_t bluetoothAddress, const std::function< void(ConnectionEvent, ConnectionEventReason)> &onConnectionEvent)
Initializes a new instance of Peripheral with the given Bluetooth address and a callback for notifyin...
Definition Peripheral.h:73
std::shared_ptr< Service > getDiscoveredService(const winrt::guid &uuid) const
Gets the Service instance with the given UUID.
Definition Peripheral.h:212
void copyDiscoveredServices(std::vector< std::shared_ptr< Service > > &outServices) const
Copy the discovered services to the given std::vector.
Definition Peripheral.h:224
bool isConnected() const
Indicates whether the peripheral is connected.
Definition Peripheral.h:141
std::future< BleRequestStatus > connectAsync(std::vector< winrt::guid > requiredServices=std::vector< winrt::guid >{}, bool maintainConnection=false)
Connects to the BLE peripheral.
Definition Peripheral.cpp:47
const wchar_t * deviceId() const
Gets the device id assigned by the system for the peripheral.
Definition Peripheral.h:172
uint16_t mtu() const
Gets the Maximum Transmission Unit (MTU).
Definition Peripheral.h:194
void disconnect()
Immediately disconnects the peripheral.
Definition Peripheral.h:113
bool isReady() const
Indicates whether the peripheral is ready.
Definition Peripheral.h:157
~Peripheral()
Disconnects and destroys the Peripheral instance.
Definition Peripheral.h:84
A collection of C++ classes that provides a simplified access to Bluetooth Low Energy peripherals.
Definition bletypes.h:36
std::uint64_t bluetooth_address_t
Type for a Bluetooth address.
Definition bletypes.h:54
@ Timeout
The request did not succeed after the timeout period.
ConnectionEvent
Peripheral connection events.
Definition bletypes.h:101
ConnectionEventReason
Peripheral connection event reasons.
Definition bletypes.h:123