-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbluetooth.ino
119 lines (98 loc) · 3.69 KB
/
bluetooth.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
// define Daly Smart BMS UUIDs for bluetooth
#define SERVICE_UUID "0000fff0-0000-1000-8000-00805f9b34fb"
#define CHARACTERISTIC_UUID_RX "0000fff1-0000-1000-8000-00805f9b34fb"
#define CHARACTERISTIC_UUID_TX "0000fff2-0000-1000-8000-00805f9b34fb"
// define bluetooth constants
//#define BLE_DATA_CHUNK_SIZE 200
bool SEND_IN_CHUNKS_OF_MTU = false;
// variables
BLECharacteristic *pCharacteristicWrite;
BLECharacteristic *pCharacteristicRead;
bool deviceConnected = false;
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
Serial.println("Connection established");
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
Serial.println("Connection lost");
deviceConnected = false;
// start advertising again
pServer->getAdvertising()->start();
Serial.println("Advertising started");
}
};
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0) {
Serial.println("*********");
Serial.print("Received Value: ");
for (int i = 0; i < rxValue.length(); i++) {
if (rxValue[i]<0x10) {
Serial.print("0");
}
Serial.print(rxValue[i], HEX);
Serial.print(' ');
}
Serial.println();
if (SEND_CONTINIOUSLY == 0) {
// send response accordingly to request
if (smartbmsutilIsCommandRunInfo(rxValue.data(), sizeof(rxValue))) {
// received command to send RunInfo -> send RunInfo data
smartbmsdemoSendRunInfo();
/*delay(500);
smartbmsdemoSendVersionInfo();*/
} else if (smartbmsutilIsCommandSetDataInfo(rxValue.data(), sizeof(rxValue))) {
smartbmsdemoSendSetDataInfo();
}
}
}
}
};
void bluetoothSetupServer() {
// Create the BLE Device
BLEDevice::init("DL-40D63C3223A2"); // Give it a name
// Create the BLE Server
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pCharacteristicWrite = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristicWrite->setCallbacks(new MyCallbacks());
pCharacteristicRead = pService->createCharacteristic(
CHARACTERISTIC_UUID_RX,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY
);
pCharacteristicRead->addDescriptor(new BLE2902());
// Start the service
pService->start();
// Start advertising
pServer->getAdvertising()->addServiceUUID(BLEUUID(SERVICE_UUID));
pServer->getAdvertising()->start();
Serial.println("Waiting a client connection to notify...");
}
void bluetoothSendByteArray(byte *buffer, int dataLength) {
int sentBytes = 0;
int chunkSize = dataLength;
if (SEND_IN_CHUNKS_OF_MTU) {
chunkSize = BLEDevice::getMTU() - 3;
}
byte tmpBuffer[chunkSize];
while (sentBytes < dataLength) {
int countBytes = (dataLength - sentBytes) > chunkSize ? chunkSize : (dataLength - sentBytes);
for (int i = 0; i < countBytes; i++) {
tmpBuffer[i] = buffer[sentBytes + i];
}
pCharacteristicRead->setValue(tmpBuffer, countBytes);
pCharacteristicRead->notify();
sentBytes = sentBytes + countBytes;
}
}
bool bluetoothIsDeviceConnected() {
return deviceConnected;
}