BLE arduino send values over 20 bytes

zombodotcom
Posts: 8
Joined: Wed Jan 31, 2018 4:02 am

BLE arduino send values over 20 bytes

Postby zombodotcom » 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.

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) {

  }

}

Valerii
Posts: 16
Joined: Wed Dec 27, 2017 4:20 pm

Re: BLE arduino send values over 20 bytes

Postby Valerii » Wed Jan 31, 2018 7:46 am

Hi, the maximum size of single data packet determined by MTU size which is 23bytes for BLE 4.0 (20b of data + 3b protocol wrapper). The MTU size is usually set during connection establishment with "MTU Request" command. I don't know if IDF has the API for this setting but first of all check your android device for BLE version. To work with bigger MTU size both devices have to be BLE 4.2+ compatible. As option you could consider your own packet fragmentation protocol on top of GATT, it will enable older devices to work with your ESP based solution.

zombodotcom
Posts: 8
Joined: Wed Jan 31, 2018 4:02 am

Re: BLE arduino send values over 20 bytes

Postby zombodotcom » Wed Jan 31, 2018 3:17 pm

Valerii wrote:Hi, the maximum size of single data packet determined by MTU size which is 23bytes for BLE 4.0 (20b of data + 3b protocol wrapper). The MTU size is usually set during connection establishment with "MTU Request" command. I don't know if IDF has the API for this setting but first of all check your android device for BLE version. To work with bigger MTU size both devices have to be BLE 4.2+ compatible. As option you could consider your own packet fragmentation protocol on top of GATT, it will enable older devices to work with your ESP based solution.
Could you give me an example of a packet fragmentation protocol for my code?

chegewara
Posts: 2230
Joined: Wed Jun 14, 2017 9:00 pm

Re: BLE arduino send values over 20 bytes

Postby chegewara » Wed Jan 31, 2018 5:26 pm

Like Valerii said, its all depend on MTU value. From your client app, and only client app, you can ask esp32 to use higher mtu. By default in esp-idf mtu is setup to 500 bytes, but you can change it with BLE library and set it to any value between 23 and 512 (in server app).

In esp32 app (server or client app) you can use this command to setup mtu request:

Code: Select all

BLEDevice::setMTU(uint16_t mtu)
PS one more thing, maybe Valerii is right, but i think that you can setup mtu in android v4.0+ app, at least my nRF connect on android v4.0 lets me do it

zombodotcom
Posts: 8
Joined: Wed Jan 31, 2018 4:02 am

Re: BLE arduino send values over 20 bytes

Postby zombodotcom » Wed Jan 31, 2018 10:23 pm

I am currently doing the android app in mit App inventor. I probably need to switch to android studios or something.

When I try and set the mtu size

Code: Select all

uint16_t mtu = 128;
  BLEDevice::setMTU(128);
I get this

Code: Select all

sketch\LBC.ino.cpp.o: In function `void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct_aux<char const*>(char const*, char const*, std::__false_type)':

c:\users\******\documents\arduino\hardware\espressif\esp32\arduino-esp32\tools\xtensa-esp32-elf\xtensa-esp32-elf\include\c++\5.2.0\bits/basic_string.h:195: undefined reference to `BLEDevice::setMTU(unsigned short)'

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board ESP32 Dev Module.
I don't really know how I am supposed to set the MTU size. I am sorry if this is an easy fix, I am kind of new to BLE and android.

chegewara
Posts: 2230
Joined: Wed Jun 14, 2017 9:00 pm

Re: BLE arduino send values over 20 bytes

Postby chegewara » Thu Feb 01, 2018 8:45 pm

I dont think so its issue with mtu this time. I checked it out with BLE_server example and it works (its compiling with no issue) with your lines:

Code: Select all

uint16_t mtu = 128;
  BLEDevice::setMTU(128);
It may be some issue with other part of your code or you need to update esp-idf and BLE library with git pull (git clone).

zombodotcom
Posts: 8
Joined: Wed Jan 31, 2018 4:02 am

Re: BLE arduino send values over 20 bytes

Postby zombodotcom » Thu Feb 01, 2018 9:25 pm

Are you using the

Code: Select all

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
from the esp32-snippets git?

just redid the esp32 git clone with get.exe and still getting the error, going to try the esp32-snippits headers

Edit: same errors with the BLE_Server example from esp32 snippets, are you using the ESP32 Dev Module as the board?

zombodotcom
Posts: 8
Joined: Wed Jan 31, 2018 4:02 am

Re: BLE arduino send values over 20 bytes

Postby zombodotcom » Fri Feb 02, 2018 12:49 am

I think I got it, I just need to wait for a response from a github post on a FastLED bug,

https://github.com/FastLED/FastLED/issu ... -338513421

cant figure out how to get clockless esp32 working in fastled, even cloned the git to make sure it had all the new files
Hi @glararan, take a look at #504, there's some good work being done to overcome this issue. As far as I'm aware, the current solution is to take https://github.com/samguyer/FastLED/blo ... ss_esp32.h and use that in your local FastLED installation until it gets merged in (assuming it does).
EDIT: Didn't get it, getting this in the arduino serial monitor

Code: Select all

E (69195) BT: attribute value too long, to be truncated to 20

chegewara
Posts: 2230
Joined: Wed Jun 14, 2017 9:00 pm

Re: BLE arduino send values over 20 bytes

Postby chegewara » Fri Feb 02, 2018 5:24 am

Edit: same errors with the BLE_Server example from esp32 snippets, are you using the ESP32 Dev Module as the board?
You get it and you can do nothing about it unless your client app (ie android app) will send to esp342 server mtu request. This is not esp32-snippets or esp-idf dependent, its bluetooth LE functionality. You can test it with nRF connect. When you connect to esp32 just change from nRF connect (its upper right corner options). Then you wont see message about truncating notification.

App inventor 2 is nice tool to quick write android app but the problem is i dont see option to change mtu with it.

zombodotcom
Posts: 8
Joined: Wed Jan 31, 2018 4:02 am

Re: BLE arduino send values over 20 bytes

Postby zombodotcom » Fri Feb 02, 2018 4:24 pm

Yeah, yesterday I changed the MTU through nrf connect and it let the string go past 20 bytes, but as you said, mit app inventor/thunkable's BLE extension does not have a change MTU option. I might just download the apk, then edit the BLE section to include a getMTU part in it. or just code it from the ground up and learn something.


thanks for the help, you can consider this closed because it's an issue with MIT app inventor /thunkable beacause you cant change the MTU unless you use nrfConnect to adjust the MTU

Who is online

Users browsing this forum: No registered users and 70 guests