-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLE.ino
139 lines (115 loc) · 3.73 KB
/
BLE.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "SPIFFS.h"
BLEServer* pServer = NULL;
BLECharacteristic* pcLatestDate = NULL;
BLEDescriptor *pDescr;
BLECharacteristic* pcSync = NULL;
BLEDescriptor *pDescrSync;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint32_t value = 0;
#define SERVICE_UUID "8292fed4-e037-4dd7-b0c8-c8d7c80feaae"
#define CHARACTERISTIC_UUID "d5641f9f-1d6f-4f8e-94db-49fbfe9192ab"
#define CHARACTERISTIC_UUID2 "870e104f-ba63-4de3-a3ac-106969eac292"
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
Serial.println("Device connected");
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
Serial.println("Device disconnected");
}
};
class LatestDate : public BLECharacteristicCallbacks {
void onRead(BLECharacteristic *pcLatestDate) {
pcLatestDate->setValue(findLatestDateFile("/data").c_str());
}
};
class syncData: public BLECharacteristicCallbacks {
File dataFile;
String fileName = findLatestDateFile("/data");
String filePath = "/data/"+fileName;
bool open = 0;
void onWrite(BLECharacteristic *pCharacteristic) {
fileName = pCharacteristic->getValue();
filePath = "/data/"+fileName;
Serial.println("Write value: "+fileName);
}
void onRead(BLECharacteristic *pcSync) {
if (!open) {
dataFile = SPIFFS.open(filePath.c_str());
if (!dataFile) {
Serial.println("Could not open file for reading!");
pcSync->setValue("failed");
return;
} else {
pcSync->setValue("ok");
Serial.println("File opened.");
open = 1;
}
} else {
if (dataFile.available()) {
String value = dataFile.readStringUntil('\n');
pcSync->setValue(value.c_str());
Serial.println(value.c_str());
} else {
pcSync->setValue("end");
dataFile.close();
open = 0;
Serial.println("File has been read.");
}
}
}
};
void BLE_init() {
BLEDevice::init("APOC RECEIVER");
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID);
pcLatestDate = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ
);
pcSync = pService->createCharacteristic(
CHARACTERISTIC_UUID2,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pcLatestDate->addDescriptor(new BLE2902());
pDescr = new BLEDescriptor((uint16_t)0x2901);
pDescr->setValue("latest date");
pcLatestDate->addDescriptor(pDescr);
pcSync->addDescriptor(new BLE2902());
pDescrSync = new BLEDescriptor((uint16_t)0x2901);
pDescrSync->setValue("sync data");
pcSync->addDescriptor(pDescrSync);
pcLatestDate->setCallbacks(new LatestDate());
pcSync->setCallbacks(new syncData());
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(false);
pAdvertising->setMinPreferred(0x0);
BLEDevice::startAdvertising();
Serial.println("Waiting for a client connection to notify...");
}
void BLE_services(void *pvParameters) {
for (;;) {
if (deviceConnected) {
pcLatestDate->setValue((uint8_t*)&value, 4);
pcLatestDate->notify();
value++;
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
if (!deviceConnected && oldDeviceConnected) {
vTaskDelay(500 / portTICK_PERIOD_MS);
pServer->startAdvertising();
Serial.println("Start advertising");
oldDeviceConnected = deviceConnected;
}
if (deviceConnected && !oldDeviceConnected) {
oldDeviceConnected = deviceConnected;
}
vTaskDelay(10 / portTICK_PERIOD_MS); // Yield to other tasks
}
}