8#include <NimBLEDevice.h>
39 explicit TinyCTSClient(
const char* device =
nullptr, uint32_t scanMs = 5000)
41 if (device !=
nullptr) _device = device;
50 if (!NimBLEDevice::isInitialized()) NimBLEDevice::init(
"");
60 NimBLEClient* client = connect();
61 if (client ==
nullptr)
return false;
63 NimBLERemoteService* service = client->getService(NimBLEUUID(
CTS_SERVICE_UUID));
64 if (service !=
nullptr) {
66 NimBLERemoteCharacteristic* chr =
68 if (chr !=
nullptr && chr->canRead()) {
69 uint32_t t0 = TINY_CTS_MILLIS();
70 NimBLEAttValue v = chr->readValue();
71 uint32_t t1 = TINY_CTS_MILLIS();
85 NimBLEClient* client = connect();
86 if (client ==
nullptr)
return false;
88 NimBLERemoteService* service = client->getService(NimBLEUUID(
CTS_SERVICE_UUID));
89 NimBLERemoteCharacteristic* chr =
92 if (chr !=
nullptr && chr->canWrite()) {
94 ok = chr->writeValue(data,
timeToWrite(unixMs, data),
true);
101 NimBLEClient* connect() {
102 NimBLEScan*
scan = NimBLEDevice::getScan();
103 scan->setActiveScan(
true);
104 NimBLEScanResults results =
scan->getResults(_scanMs,
false);
105 const NimBLEAdvertisedDevice* found =
nullptr;
106 for (
int i = 0; i < results.getCount() && found ==
nullptr; i++) {
107 const NimBLEAdvertisedDevice* d = results.getDevice(i);
108 if (_device.empty()) {
110 }
else if (
isWanted(d->getAddress().toString(), d->getName())) {
114 if (found ==
nullptr)
return nullptr;
115 NimBLEClient* client = NimBLEDevice::createClient();
116 if (client ==
nullptr)
return nullptr;
117 if (!client->connect(found)) {
118 NimBLEDevice::deleteClient(client);
121 _name = found->getName();
122 _address = found->getAddress().toString();
128 client->disconnect();
129 NimBLEDevice::deleteClient(client);
132 void readInfo(NimBLERemoteService* service) {
133 NimBLERemoteCharacteristic* chr =
135 if (chr !=
nullptr && chr->canRead()) {
136 NimBLEAttValue v = chr->readValue();
137 storeLocalInfo(v.data(), v.size());
139 chr = service->getCharacteristic(NimBLEUUID(CTS_REFERENCE_TIME_INFO_UUID));
140 if (chr !=
nullptr && chr->canRead()) {
141 NimBLEAttValue v = chr->readValue();
142 storeReferenceInfo(v.data(), v.size());
The logic of the Current Time Service client, independent of the Bluetooth stack: decoding of the cha...
constexpr uint16_t CTS_CURRENT_TIME_UUID
16 bit UUID of the Current Time characteristic (read, notify, write)
Definition TinyCTSTypes.h:19
constexpr uint16_t CTS_SERVICE_UUID
16 bit UUID of the Current Time Service
Definition TinyCTSTypes.h:17
constexpr uint16_t CTS_LOCAL_TIME_INFO_UUID
16 bit UUID of the Local Time Information characteristic (read, write)
Definition TinyCTSTypes.h:21
The data of a TinyCTSClient. Use TinyCTSClient, which adds the Bluetooth part.
Definition TinyCTSClientBase.h:25
size_t timeToWrite(uint64_t unixMs, uint8_t *out) const
Definition TinyCTSClientBase.h:111
bool isWanted(const std::string &address, const std::string &name) const
Definition TinyCTSClientBase.h:117
bool storeCurrentTime(const uint8_t *data, size_t len, uint32_t startMs, uint32_t endMs)
Definition TinyCTSClientBase.h:91
Reads the time from a BLE device that offers the Current Time Service (a phone, a watch,...
Definition TinyCTSClientDesktop.h:30
static void release(NimBLEClient *client)
Definition TinyCTSClientNimBLE.h:127
bool begin()
Find the device and read the time. NimBLEDevice::init() is called, if the NimBLE stack is not initial...
Definition TinyCTSClientNimBLE.h:49
TinyCTSClient(const char *device=nullptr, uint32_t scanMs=5000)
Definition TinyCTSClientNimBLE.h:39
bool setTimeMs(uint64_t unixMs) override
Set the time of the device by writing the Current Time. It works if the device allows it (and the con...
Definition TinyCTSClientNimBLE.h:84
std::vector< TinyCTSScanEntry > scan(uint32_t timeoutMs)
List the devices that are in range.
Definition TinyCTSClientDesktop.h:126
bool update()
Read the time (and the other characteristics) again: the device is connected, read and disconnected.
Definition TinyCTSClientDesktop.h:58
static constexpr size_t Size
Size of the encoded characteristic.
Definition TinyCTSTypes.h:70