Created
August 15, 2018 10:29
-
-
Save marcteys/29531745535e8267978962af9b9a7617 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef void(*SendBluetoothMessageCallback)(const char* objectName, const char* methodName,const char* message); | |
... | |
void PluginWrite(std::string message) { | |
Log(message.c_str()); // ici ça marche | |
sendMessageCallback("truc", "PluginMessageReceived", message.c_str()); // !!! ici ça marche pas !! | |
} | |
std::string currentBufferString; | |
void AddToInBuffer(std::string inBuffer) { | |
currentBufferString.append(inBuffer); | |
// Je split par \r\n | |
std::vector<std::string> newLines; // (optionaly) Here I want to store the results without \r\n | |
std::regex rx("(?:\\r\\n|\\r|\\n)"); | |
std::sregex_token_iterator iter(currentBufferString.begin(), currentBufferString.end(), rx, -1); | |
for (std::sregex_token_iterator end; iter != end; ++iter) | |
{ | |
newLines.push_back(iter->str()); | |
} | |
bool removeOffsed = 1; | |
if (ends_with(currentBufferString, "\r\n") || ends_with(currentBufferString, "\n")) { // || ends_with(currentBufferString, "\r") | |
removeOffsed = 0; // go until the end !! | |
} | |
LogToFile(removeOffsed ? "true" : "false"); | |
for (int i = 0; i < newLines.size() - removeOffsed; i++) { | |
if (newLines[i].compare("\r\n") != 0 && newLines[i].compare("\r") != 0 && newLines[i].compare("\n") != 0 && newLines[i].compare("") != 0) { | |
std::string newString; | |
newString = newLines[i]; | |
size_t size = newLines[i].size(); | |
std::vector<char> v(newLines[i].length() + 1); | |
std::strcpy(&v[0], newLines[i].c_str()); | |
char* pc = &v[0]; | |
PluginWrite(pc); // la j'envoi un const char * . Les qq lignes qu'il y a avant j'essaye de faire une copie | |
} | |
} | |
if (removeOffsed && newLines.size() > 0) { | |
currentBufferString = newLines[newLines.size()-1]; | |
// on prends le "rest" | |
} | |
else if (!removeOffsed) { | |
// ici on clear le buffer. | |
currentBufferString.clear(); | |
} | |
} | |
// ici c'est un thread qui reçoit des données BLE | |
void __stdcall HandleBLENotification(BTH_LE_GATT_EVENT_TYPE EventType, PVOID EventOutParameter, PVOID Context) | |
{ | |
PBLUETOOTH_GATT_VALUE_CHANGED_EVENT ValueChangedEventParameters = (PBLUETOOTH_GATT_VALUE_CHANGED_EVENT)EventOutParameter; | |
int size = (unsigned int)ValueChangedEventParameters->CharacteristicValueDataSize; | |
std::string readCharacteristicMessage = ""; | |
std::string stroutput(reinterpret_cast<const char *>(ValueChangedEventParameters->CharacteristicValue->Data), size); | |
/* | |
ici on reçoit les différents bundle de character s | |
Par exmple je vais recevoir : | |
- ceci e | |
- st une lign | |
- e complete coupee p | |
- ar le ble \r\n ceci | |
- est une au | |
- tre ligne \r\n | |
chaque bout de string est ajoutée à AddToInBuffer(), et dans AddToInBuffer() je vais tout concaténer puis splitter par \r\n, | |
ex : | |
- ceci est une ligne complete coupee par le ble \r\n ceci est une autre ligne \r\n | |
=> | |
- ceci est une ligne complete coupee par le ble \r\n | |
- ceci est une autre ligne \r\n | |
puis, chaque ligne va etre envoyée a PluginWrite | |
*/ | |
AddToInBuffer(readCharacteristicMessage); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment