Repository navigation
Expand file tree
/
Copy pathTemplateForEsp32BLE.cpp
More file actions
123 lines (81 loc) · 4.78 KB
/
Copy pathTemplateForEsp32BLE.cpp
File metadata and controls
123 lines (81 loc) · 4.78 KB
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
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <BLE2901.h> //this is the name descriptor
/*Defines*/
#define DEVICE_NAME "GLOVE SENSOR"
bool deviceConnected = false;
bool oldDeviceConnected = false;
#define SERVICE_1_UUID "186ff4e3-45c7-4b73-8db2-6612a3278c2b"
#define CHARACTERISTIC_1A_UUID "ed0fe5c3-1064-47e5-b9d9-f6912652e16d"
/*Callbacks*/
class MyServerCallbacks : public BLEServerCallbacks { // we are writing a class and it is a subclass of BLEServerCallbacks
void onConnect(BLEServer *pServer) {
deviceConnected = true; // these are our function signatures
digitalWrite(2, HIGH);
Serial.println("Client Connected");
}
void onDisconnect(BLEServer *pServer) { // these are our function signatures
deviceConnected = false;
digitalWrite(2, LOW);
delay(500);
Serial.println("Client Disconnected");
}
};
class MyCharacteristicCallbacks: public BLECharacteristicCallbacks{
void onRead(BLECharacteristic *pCharacteristic){
uint32_t currentMillis = millis()/1000;
pCharacteristic -> setValue(currentMillis);
}
};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("ESP32 BLE Server setup beginning...");
//Pin mode
pinMode(2,OUTPUT);
//initilizating device
BLEDevice::init(DEVICE_NAME); //BLEDevice is a class and they want only one device to be initilized, library uses a lot of static function
//creating server
BLEServer *pServer = BLEDevice::createServer(); // holds the address of the server
pServer -> setCallbacks(new MyServerCallbacks()); // when we use a pointer we have to use the -> instead of . operator, just creates a new server call back ahandler to the server
//Services
BLEService *pService = pServer->createService(SERVICE_1_UUID); //we are making a new pointer called pServer which is pointing to a BLEService Object which we are using the createServer class to create a service
//characteristics
BLECharacteristic *pCharacteristic = pService ->createCharacteristic( // pCharacteristic is pointing to the BLECharacteristic Object and the refrence is being provided by pService & a refrence is just attached to what variable we are using and cannot be detached
CHARACTERISTIC_1A_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE // we need to take the UUID and it wants some properties like a read, write, and notify by changing the bits on different bits
);
pCharacteristic->setCallbacks(new MyCharacteristicCallbacks());
//descriptors
BLE2901 *pDescriptor_2901 = new BLE2901();//creating a new descriptor using 2901
pDescriptor_2901 ->setDescription("Time"); //having the pointer descriptor have the function of counting time
pCharacteristic ->addDescriptor(pDescriptor_2901); //since it is nested ble the pCharacteristic is pointed to the pDescriptor using the function of addDescriptor
pService->start(); // starting from the pService class
//start advertisng
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); // we have a pointer that is pointing to the class BLEAdvertising and we are getting the BLEDevice to get the getAdvertising object
// there is passive scan and active scan the passive scan is basically for the bare minimum information while an active scan is for more information by getting a querey w/o connecting to it
BLEAdvertisementData advertismentData;// this is for passive scannning we are making an instance of the BLEAdvertismentData
advertismentData.setName(DEVICE_NAME); // the advertismentData has some functions like setName and manufactureData
advertismentData.setManufacturerData("Chinese Factory");
BLEAdvertisementData scanResponseData; //active scanning creating a new object for active sccanning from the BLEAdvertisment data
scanResponseData.setName("Extra Scan Data");
pAdvertising->setAdvertisementData(advertismentData); // this is what data is going to be spat out by the device
pAdvertising->setScanResponseData(scanResponseData);
pAdvertising ->addServiceUUID(SERVICE_1_UUID); //we are adding our SERVICE_1_UUID to our pointer for advertising
pAdvertising ->setScanResponse(true); // when someone qurries/asks for data we will send them data
BLEDevice::startAdvertising();
}
void loop(){
//handle reconnection
if(!deviceConnected && oldDeviceConnected){ // if the device isn't connected & the oldDevice isnt connected due to it being false then we start advertising again after a delay
delay(500);
BLEDevice::startAdvertising();
Serial.println("Restarting...");
oldDeviceConnected = deviceConnected;
}
else if(deviceConnected && !oldDeviceConnected) { //else then the old device is the same as the device that is currently connected
oldDeviceConnected = deviceConnected;
}
}