Pixels Plugin for Unity
Enable communications with Pixels dice using Bluetooth Low Energy.
Loading...
Searching...
No Matches
Service.h
Go to the documentation of this file.
1
6#pragma once
7
9{
10 class Peripheral;
11 class Characteristic;
12
20 class Service
21 {
22 using GattDeviceService = winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::GattDeviceService;
23
24 // Keep a weak pointer to the peripheral so it can be accessed
25 std::weak_ptr<Peripheral> _peripheral;
26
27 // Service
28 GattDeviceService _service{ nullptr };
29
30 // Characteristics (there may be more than one characteristic instance for a given UUID)
31 std::unordered_map<winrt::guid, std::vector<std::shared_ptr<Characteristic>>> _characteristics{};
32
33 public:
36
41 {
42 _characteristics.clear();
43
44 if (_service)
45 {
46 _service.Close();
47 _service = nullptr;
48 }
49 }
50
54
60 std::uint16_t handle() const
61 {
62 return _service.AttributeHandle();
63 }
64
70 winrt::guid uuid() const
71 {
72 return _service.Uuid();
73 }
74
80 std::shared_ptr<const Peripheral> peripheral() const
81 {
82 return _peripheral.lock();
83 }
84
90 std::shared_ptr<Peripheral> peripheral()
91 {
92 return _peripheral.lock();
93 }
94
98
105 std::shared_ptr<Characteristic> getCharacteristic(const winrt::guid& uuid)
106 {
107 auto it = _characteristics.find(uuid);
108 bool found = (it != _characteristics.end()) && (!it->second.empty());
109 return found ? it->second[0] : nullptr;
110 }
111
120 const std::vector<std::shared_ptr<Characteristic>> getCharacteristics(const winrt::guid& uuid)
121 {
122 return _characteristics.find(uuid)->second;
123 }
124
130 void copyCharacteristics(std::vector<std::shared_ptr<Characteristic>>& outCharacteristics)
131 {
132 size_t _characteristicsCount = 0;
133 for (auto& [_, instances] : _characteristics)
134 {
135 _characteristicsCount += instances.size();
136 }
137 outCharacteristics.reserve(outCharacteristics.size() + _characteristicsCount);
138 for (auto& [_, instances] : _characteristics)
139 {
140 for (auto& c : instances)
141 {
142 outCharacteristics.emplace_back(c);
143 }
144 }
145 }
146
148
149 private:
150 friend Peripheral;
151
152 // Initializes a new instance of Service for a Peripheral and GattDeviceService,
153 // and with a list of characteristics.
154 Service(
155 std::weak_ptr<Peripheral> peripheral,
156 GattDeviceService service,
157 std::unordered_map<winrt::guid, std::vector<std::shared_ptr<Characteristic>>> characteristics)
158 :
159 _peripheral{ peripheral },
160 _service{ service },
161 _characteristics{ characteristics }
162 {
163 }
164 };
165}
Represents a Bluetooth Low Energy (BLE) peripheral.
Definition Peripheral.h:33
Represents a primary service on a Bluetooth Low Energy (BLE) peripheral.
Definition Service.h:21
std::shared_ptr< const Peripheral > peripheral() const
Gets the peripheral to which the service belongs.
Definition Service.h:80
~Service()
Closes and destroys the Service instance.
Definition Service.h:40
const std::vector< std::shared_ptr< Characteristic > > getCharacteristics(const winrt::guid &uuid)
Gets the all the Characteristic instances with the given UUID.
Definition Service.h:120
winrt::guid uuid() const
Gets the UUID of the service.
Definition Service.h:70
std::shared_ptr< Peripheral > peripheral()
Gets the peripheral to which the service belongs.
Definition Service.h:90
std::shared_ptr< Characteristic > getCharacteristic(const winrt::guid &uuid)
Gets the first Characteristic instance with the given UUID.
Definition Service.h:105
std::uint16_t handle() const
Gets the 16 bits handle of the BLE service.
Definition Service.h:60
void copyCharacteristics(std::vector< std::shared_ptr< Characteristic > > &outCharacteristics)
Copy the discovered characteristics to the given std::vector.
Definition Service.h:130
A collection of C++ classes that provides a simplified access to Bluetooth Low Energy peripherals.
Definition bletypes.h:36