BLE arduino send values over 20 bytes
Posted: Wed Jan 31, 2018 4:06 am
I am trying to send values over 20 bytes to an android program,
the values get cut off at 20 bytes, and I don't know how to implement write long or the other suggestions I have found on the internet. I am new to this so I don't really know how to fix this.
the values get cut off at 20 bytes, and I don't know how to implement write long or the other suggestions I have found on the internet. I am new to this so I don't really know how to fix this.
Code: Select all
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
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++) {
Serial.print(rxValue[i]);
}
Serial.println();
Serial.println("*********");
}
// Do stuff based on the command received from the app
// For some reason using rxValue.compare("A") == 0 doesn't work. Maybe
// there are hidden characters I'm not seeing?
}
};
void setup() {
Serial.begin(115200);
// Create the BLE Device
BLEDevice::init("Lumos Board"); // 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
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_NOTIFY
);
pCharacteristic->addDescriptor(new BLE2902());
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_RX,
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
// Start the service
pService->start();
// Start advertising
pServer->getAdvertising()->start();
Serial.println("Waiting a client connection to notify...");
}
void loop() {
EVERY_N_MILLISECONDS( 20 ) {
gHue++; // slowly cycle the "base color" through the rainbow
}
unsigned long currentMillis = millis();
if (deviceConnected) {
if (currentMillis - previousMillis >= interval) {
// save the last time you sent the message
previousMillis = currentMillis;
pCharacteristic->setValue("Hi, I am trying to send a string over 20 bytes to Android"); // Sending a test message
pCharacteristic->notify(); // Send the value to the app!
}
}
if (!deviceConnected) {
}
}