How do I send long strings to BLE from AI2?

Good morning all.
I am looking for the procedure to send a long text using Bluetooth BLE like classic Bluetooth. BLE doesn't allow more than 20 characters and I need the double. I can't find any tutorial on this topic.
Anyone know the solution?
Merry Christmas! :christmas_tree:

Hello Slegg

Depends what device is sending the string - If it's MTU (maximum transmission unit) can be defined (via a script for example) then the payload limit is far greater @ 247 bytes.

Otherwise, you could chop the string up into 20 Byte packages and assemble them into one string in the App.

Hi Chris!
And thanks for helping me.
It's Chrismas every day on MIT !
Indeed, I found a solution at this level for the output.
Now I have a new problem to settle on the reception.
For data reception, they arrive with [""].
Before, with "classic" bluetooth, I simply received them.
I try to erase them. An idea ?
(Sorry for my bad English...)

If you can tell more? Is the App sending or Receiving? What is the Bluetooth device - is it a programmable microprocessor?

It's a project inspired by the awesome Retroetgeek !!!!
https://retroetgeek.com/arduino/creer-une-application-android-avec-appinventor-2-reception-et-emission-de-donnees-en-bluetooth/

Strubg Values is a list.
(Notice the trailing 's' in the name, implying plural.)

So you need a for each loop

for each item in stringValues
  set templocal to split item at '='
  if length of list templocal = 2
    etc.
end for each

Oulala !!!
I will work hard all night long !!!!
But I love it since I discovered App Inventor and Arduino .... it's been a month!
I'll tell you tomorrow!
Thank you very much ABG!

Sorry ABC,
I'm not yet big enough to build as you generously suggested ...

It's ABG :grimacing:

Slegg, you are using an Ardunio, so the values do not have to arrive like that, it's more to do with the way you receives them. Do not send value labels, just the values. If you post your Ardunio Sketch here (change the extension from .ino to .txt), I can take a look at it for you.

Thanks ChrisWard for taking the time to watch.
I simplified my sketch to be more understandable.
The HC-05 version therefore works very well.
I want the same with the Bluetooth BLE HM-10. But it does not work ...MitSendReceiveBT.txt (2.4 KB)

How can I send the .apk to you?

I just need the .ino file, the APK is of no use.

The blocks you have just posted, which are practically unreadable, are BT Classic and not BLE?

To produce a Blocks image correctly so that we can zoom it to 1:1, right - click in the Blocks work area and from the pop-up menu select "download blocks as image".

Yes I did it ! I sent 2 images. first MitSendreceiveHC05 and 2nd MiSendReceiveBLE.
From "download blocks as image" !!!

Hi Slegg

Ah yes - I had to view them in another Tab (do not know why).

I can see you have really lost your way with the BLE code. Can you upload the BLE project? (.aia file)

Export Project

Also, can you explain what is going on in your Sketch?

  1. What is all that play one time, play 2x, play 3x? (with delays in between)?
  2. Do you just require to send the integer values ar and jr to the App?
  3. ar and jr are derived from aa and jj respectively via the decoupeur procedure, which should therefore run first - but aa and jj are resolved as floats and not integers. So what do you want to arrive in the App, floats or integers? :upside_down_face:

Well anyway, essentially you are sending from the App and then back to the App so the details are not so specific in terms of units required (but typos and syntax errors cause havoc).

I'm just going to correct your Sketch, except the strtok() code which at the minute I assume is correct because I have not studied the sending side of your App, and adapt a "template" Project in App Inventor to see if we can get you going. :koala:

OK, although I can't test the following files here, they are very similar to other successful projects (of other App Inventor Users) that have based their App on my basic template.

There are inline notes in the Blocks and the Sketch, but you should be able to follow the logic. The App enables the Scan Button when BT is available and the Send Button when there is a BT Connection with the target device (Arduino). The App receives text from the Arduino whenever it is sent via a 'Strings Received' Block, so no need for a timer or loop on the App side to receive data. You will need to check that the UUIDs in my file match yours and correct them accordingly. The .aia has the latest official release of the MIT BLE extension 20200828 but Evan Patton has very recently put out an enhancement.

BLE_SEND_RECEIVE.aia (195.6 KB)

I don't know how you have initialized your BLE server for the HM-10 in your Sketch. You may need to delete my BLE Server code if you have different code that already works for you. Again, the code I have included has worked successfully on other App Inventor User's projects.

AppSendReceiveBLE.txt (3.3 KB)

// App Send and Receive via BLE

// BluetoothLE
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

float aa;
float jj;
unsigned int state = 0;
unsigned long lgUpdateTime;
String message;

#define        SERVICE_UUID = "0000FFE0-0000-1000-8000-00805F9B34FB"
#define CHARACTERISTIC_UUID = "0000FFE1-0000-1000-8000-00805F9B34FB"

BLEServer *pServer = NULL;
BLECharacteristic *pTxCharacteristic;
bool deviceConnected = false;

class MyServerCallbacks: public BLEServerCallbacks
{

        void onConnect(BLEServer *pServer)
        {
             deviceConnected = true;
        }

        void onDisconnect(BLEServer *pServer)
        {
             deviceConnected = false;
        }
}

void setup()
{
          Serial.begin(9600);
         Serial1.begin(9600);

         // Create the BLE Server
         pServer = BLEDevice::createServer();
         pServer->setCallbacks(new MyServerCallbacks());

         // Create the BLE Service
         BLEService *pService = pServer->createService(SERVICE_UUID);

         // Create a BLE Characteristic
         pTxCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_NOTIFY);
         pTxCharacteristic->addDescriptor(new BLE2902());

         // Start the service
         pService->start();

         // Start advertising
         pServer->getAdvertising()->start();

         Serial.println("Bluetooth device active, waiting for connection...");

         lgUpdateTime = millis();
}

void loop()
{
         if ((millis() - lgUpdateTime) > 600) //Loop every 600 milliseconds

               lgUpdateTime = millis();

               //From App
               if (Serial.available() > 0)
               {
                     message = Serial.readString();

                     Serial.println("Received");

                     decoupeur(message);
               }

               //To App       Note, Serial sends numbers as ASCII text
               if(state == 1)
               {
                      Serial1.print(aa,2);
                      Serial1.print("|");  //Value delimiter expected by App
                      Serial1.print(jj,2);
                      Serial1.println();   //End Of Data delimiter expected by App

                      state = 0;
               }
         }
}

void decoupeur(String inputString)
{
         char inputChar[inputString.length()+1] ;

         inputString.toCharArray(inputChar,inputString.length()+1);

         char* command = strtok(inputChar, "&");
                                                    // boucle sur toutes les commandes
         while (command != 0)
         {

               char* valueCommand = strchr(command, '=');
               if (valueCommand != 0)
               {
                   *valueCommand = 0;
                   ++valueCommand;

                     if(String(command) == "a"){
                       aa = String(valueCommand).toFloat();
                     }
                     if(String(command) == "j"){
                       jj = String(valueCommand).toFloat();
                     }

               }

               command = strtok(0, "&");
         }

         Serial.print ("aa = ");
         Serial.print (aa);
         Serial.print (     " jj = ");
         Serial.println (jj);

         state = 1;
}