41 explicit TinyCTSClient(
const char* device =
nullptr, uint32_t scanMs = 5000)
43 if (device !=
nullptr) _device = device;
61 BLEClient* client = connect();
62 if (client ==
nullptr)
return false;
65 if (service !=
nullptr) {
69 storeLocalInfo(data, len);
72 storeReferenceInfo(data, len);
75 uint32_t t0 = TINY_CTS_MILLIS();
76 if (read(chr, data, len)) {
90 BLEClient* client = connect();
91 if (client ==
nullptr)
return false;
94 BLERemoteCharacteristic* chr =
96 if (chr !=
nullptr && chr->canWrite()) {
98 ok = chr->setValue(data,
timeToWrite(unixMs, data));
100 client->disconnect();
105 BLEClient* connect() {
106 uint32_t seconds = (_scanMs + 999) / 1000;
107 if (seconds == 0) seconds = 1;
109 BLEScanReport* report =
110 _device.empty() ? BLE.scan(BLEUUID(
CTS_SERVICE_UUID), seconds) : BLE.scan(seconds);
111 BLEClient* client = BLE.client();
112 bool connected =
false;
113 for (
auto& item : *report) {
114 std::string address = item.getAddress().toString().c_str();
115 std::string name = item.getName();
116 if (!_device.empty() && !
isWanted(address, name))
continue;
117 connected = client->connect(item, 10);
125 return connected ? client :
nullptr;
130 static bool read(BLERemoteCharacteristic* chr, uint8_t* out,
size_t& len) {
131 if (chr ==
nullptr || !chr->canRead())
return false;
133 const void* value = chr->valueData();
134 len = chr->valueLen();
135 if (value ==
nullptr || len == 0)
return false;
136 if (len > 16) len = 16;
137 memcpy(out, value, len);
The logic of the Current Time Service client, independent of the Bluetooth stack: decoding of the cha...
Starts the BLE stack of the Raspberry Pi Pico (W) core once, for the client and the server.
bool ctsPicoBegin(const char *name, bool pairing)
Definition TinyCTSPicoBLE.h:20
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_REFERENCE_TIME_INFO_UUID
16 bit UUID of the Reference Time Information characteristic (read)
Definition TinyCTSTypes.h:23
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
bool begin()
Start the BLE hardware, find the device and read the time.
Definition TinyCTSClientPico.h:50
TinyCTSClient(const char *device=nullptr, uint32_t scanMs=5000)
Definition TinyCTSClientPico.h:41
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 TinyCTSClientPico.h:89
bool update()
Read the time (and the other characteristics) again: the device is connected, read and disconnected.
Definition TinyCTSClientDesktop.h:58
static bool read(BLERemoteCharacteristic *chr, uint8_t *out, size_t &len)
Definition TinyCTSClientPico.h:130
static constexpr size_t Size
Size of the encoded characteristic.
Definition TinyCTSTypes.h:70