BLE Connection Issue

Hello,

I'm running into some issues connecting an Arduino Nano 33 BLE to the app I created with MIT App Inventor. I can successfully scan for nearby devices in the mobile application, but after selecting the desired device from the list, the program remains in the "Connecting" state until it disconnects itself shortly after. However, my Arduino program shows that the connection is made and that data is being advertised. How can I fix this problem? I've attached my App Inventor design below. Any help is much appreciated.

Hello yali

I have moved your Topic to the Help Forum section. The Extensions section is for developers who have created an Extension.

Concerning your issue, do not split or modify the ListView selection. Call BLE1 to connect with the ListView Selection directly - the BLE function will extract the address.

Hi Chris,

Thank you for your reply. I modified my design so that BLE1 connects with the ListView selection directly, but I am still facing the same issues. The program is unable to enter the BLE1.Connected block. My modified design is shown below.

Hi yali

Tricky. Where do the UUIDs come from? Are they both 100% correct in both the App and the Arduino Sketch?

Can you post your Sketch here? Change the extension from .ino to .txt

I am mostly away from my desk for the next three days but hopefully a fellow Power User can help you.

Hi Chris,

Yes, the UUIDs match in both the App and Arduino sketch. They denote the weight scale service / weight scale measurement characteristic.

My Arduino sketch is attached below.

nano33ble.txt (1.2 KB)

... In your Sketch, you are only using one serial channel to send data to the Arduino Serial Monitor and the Characteristic to send one float value to your App, right?

Since you are using a float, set the number of decimal places. You have a loop inside a loop (owch).

Try this - I can't test it here but I have removed all the bugs I could find.
nano33bleMod2.txt (2.2 KB)

Edit: Improved the loop.

#include <ArduinoBLE.h>

//vars
float x = 185.3;
unsigned long lgUpdateTime;
#define BLEService weightService("0000181d-0000-1000-8000-00805f9b34fb");
#define BLEFloatCharacteristic weight("00002a9d-0000-1000-8000-00805f9b34fb", BLERead | BLENotify);


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

         // begin initialization
         if (!BLE.begin())
         {
               Serial.println("starting BLE failed!");

               while (1);
         }

         BLE.setLocalName("MH_Wgt_Scale");
         BLE.setAdvertisedService(weightService);
         weightService.addCharacteristic(weight);
         BLE.addService(weightService);
         weight.setValue(0.0);

         BLE.advertise();
         Serial.println("Bluetooth device active, waiting for connections...");

         lgUpdateTime = millis();
}

void loop()
{
         //Execute Comms every 2000 milli seconds
	     if(millis() - lgUpdateTime > 2000)
         {
               lgUpdateTime = millis();

               // wait for a BLE central
               BLEDevice central = BLE.central();

               // if a central is connected to the peripheral:
               if (central)
               {
                         Serial.println("Connected to central: ");

                         // print the central's BT address:
                         Serial.println(central.address());

                         digitalWrite(LED_BUILTIN, HIGH);

                         //central is connected:
                         if (central.connected())
                         {
                              x = x + 1;
                              Serial.print("The weight is: ");
                              Serial.println(x,2);

                              weight.writeValue(x,2);
                         }
               }
               else
               {
                         // when the central disconnects, turn off the LED:
                         digitalWrite(LED_BUILTIN, LOW);
                         Serial.println("Disconnected from central: ");
                         Serial.println(central.address());
               }
         }
}

Hi Chris,

I receive an error when I input two parameters into the writeValue function, so I removed the 2 following the x. After making this change and uploading the code, I am still facing the same problem as before: the App jumps from the "Connecting" to the "Disconnected" state.

The Arduino code indicates that it makes the connection temporarily, so it begins to print the data in the Serial Monitor (and transmit data via BLE), and then it disconnects shortly after. However, I never reach the "Connected" state in the App in order to start receiving the data.

Hello Yali

That shouldn't happen, but the connection problem is the issue of interest. Your Sketch code is in fact similar to other sketches I have seen, the difference is that you are using a Nano 33 BLE, a relatively new board (2018) with a different architecture to the more common boards such as the Uno. I assume you are an experienced User of Arduino as the differences between this board and the others is rather stark.

Is there a hardware issue? Is the Ardunio disconnecting because it is overheating for example? You could temporarily cool it with a desktop fan and see if that makes a difference.

Hi Chris,

I tested the same Arduino code and board with the nRF Connect app, and it was successfully able to make the BLE connection and update the data values on the mobile app. For this reason, I do not believe there is a hardware issue.

Right, that is good news. One thing I haven't asked is what phone are you using? Make/Model/Bluetooth Version.

Also, what version of the MIT BLE Extension are you using?

I have asked Evan Patton to take a look at your Topic. I'm sure he would like to see your Project File. Can you export the Project as an .aia file and upload it here.

Hi Chris,

I am using a Samsung Galaxy A20s (Bluetooth v4.2) and version 20200616 of the MIT BLE Extension.

My project file is attached below.

bluetoothLE.aia (197.7 KB)

Well, the good news is, you are using the wrong extension :thinking:

On my website:
https://www.professorcad.co.uk/appinventortips#TipsBluetooth

Strange, the default spec for the Galaxy A20 is Bluetooth v5.0......

To install the latest BLE Extension, you will need to remove the one you currently have - but to do so will lose the related Blocks. So:

  1. Save a Backup of your Project to your PC.
  2. Empty the BackPack
  3. Copy ALL Blocks to the BackPack
  4. Delete the current BLE Extension
  5. Install the new BLE Extension
  6. Restore the Blocks from the BackPack. Some may have the red warning symbol on them, but clicking the Block should be the only remedy required.

Although we are confident there isn't a hardware problem causing the connection issue, it is wise to take good care of your hardware. The most common ways to destroy a board are wrong voltage and overheating. The Nano33 BLE is much smaller than other boards and stacked with heat emitting components - you really do need to deal with that (RIP my little UNO). If it was mounted in a box, it would most likely require it's own fan, similar to any computer.

Hi Chris,

I'm using a Galaxy A20s, which has slightly different specs than the A20. I installed the latest BLE Extension and placed a fan near the Nano33 BLE, but I'm still facing the same issue. The only difference I notice is that the BLE connection does not break in the Arduino code once I select my Arduino from the App's list view. Previously, the Arduino would disconnect after a few seconds of transmitting data, but now it remains connected and continuously transmits, which ideally is what I want. The App, however, does not indicate that a connection has been made, even though the Arduino no longer shows up in the list view after it is selected.

My project file with the updated Extension is attached below.
BLE.aia (200.9 KB)

Yali, are you setting your phone to "fine location" before starting the BLE Scan?

Although I can't see anything particularly wrong with your Block code, we can see that when you run it, Register For Floats apparently never happens.

The App can be given a bit more time at each stage of the process to ensure everything is the best it can be. No doubt the nRF App does so. Attached is a basic Project that receives Strings via BLE. The principle is the same for receiving any data type.

BLE_RECEIVE_STRINGS.aia (195.5 KB)

... I think we might thank the fan for that.

Can you screenshot your device list before picking the Nano? We want to see the exact contents.

Hi Chris,

It seems like I'm facing the same issue when running the BLE_RECEIVE_STRINGS project. I tested the project with my current Arduino code just to see if it could make the connection, and although the Arduino Serial Monitor indicates that the connection is made and data is being transmitted, the App remains in the "connecting" state, just like it does in my App project. Could it be a problem with the particular Android device I'm using? I'm unfamiliar with the Android OS and don't know how to set the phone to "fine location". However, I did configure the location service's settings to improve its accuracy as much as possible.

A screenshot of the device list before picking the Nano is attached below.

That should do it - but ensure Location is actually switched on. It would be nice if we could do this in-App but currently we cannot.