BluetoothLE don't complete the connection with Arduino nano 33 BLE

Can anybody help me? Thanks in advance.

I am testing the BluetoothLE from APP Inventor extension with an arduino nano 33 BLE but the connection does not complete.

I have created a simple App (download from here:simpleBLE.aia) following some tutorials (see figure):

The code for arduino nano 33 BLE is as follows:

#include <ArduinoBLE.h>

float sensor;
                         
// Custom Sensing Service 1000f000-181A-0000-0001-a4ad9fa0c43b
BLEService customSensingService("1000f000-181A-0000-0001-a4ad9fa0c43b");

// Custom Characteristic Sensor 1000f000-181A-0001-0001-a4ad9fa0c43b
BLEUnsignedIntCharacteristic sensorCharacteristic("1000f000-181A-0001-0001-a4ad9fa0c43b", BLERead);


void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(5000);
  
  Serial.begin(9600);

  Serial.println("Testing BLE");

  Serial.println("Initializing Bluetooth communication.");
  if (!BLE.begin()) {
    Serial.println("Failed.");
    while(1);
  }

  // Set up Bluetooth Environmental Sensing service.
  Serial.println("Setting up service with characteristics ....");
  BLE.setLocalName("Nano33BLE");
  BLE.setAdvertisedService(customSensingService);

  // Add characteristics for barometric pressure, temperature, and humidity.
  customSensingService.addCharacteristic(sensorCharacteristic);

  // Make the service available.
  BLE.addService(customSensingService);
  BLE.setConnectable(true);
  Serial.println("Advertising services.");
  BLE.advertise();  

  digitalWrite(LED_BUILTIN, LOW);
}

void loop() {

  BLEDevice central = BLE.central();


  if (central) {
    digitalWrite(LED_BUILTIN, HIGH);
    Serial.print("Incoming connection from: ");
    Serial.println(central.address());

    while (central.connected()) {
      // Get readings from sensors and update the charcteristic values.
      sensor = (float) random(100, 200) / 10.0;

      // Update Bluetooth characteristics with new values.
      sensorCharacteristic.writeValue((uint32_t) sensor); 

      Serial.print("Value: ");
      Serial.println(sensor);

      // Delay between updates. (Don't make too long of connections start to timeout.)
      delay(1000);
    }
    // Turn off LED when connection is dropped. 
    digitalWrite(LED_BUILTIN, LOW);
    Serial.println("Connection terminated.");
  }

}

Arduino works fine with LightBlue application: it connects to the Nano33BLE device, I see the service and I can get the data from the sensorCharacteristic.

However, the application developed with App Inventor (simpleBLE): it correctly scans the devices, when I select Nano33BLE the connection starts (arduino identifies the central and starts showing data from the sensor on serial monitor), but after about 10 seconds it rejcts the conection (does not complete the connection ) and simpleBLE throws "conectionFailed" event displaying the message "Connection timeout reached".

The block "when BluetoothLE1.Connected" never is executed.

I have tested the app with two android devices: Xiaomi Mi A2 and Samsung Galaxy Alpha, both LightBlue App works fine.

Where am I making the mistake? any ideas?. Thank you.

Try this block to disconnect:
component_method (2)

If I push the Disconnect button during the 10 seconds in which the arduino has identified the central, it disconnects perfectly.

But, this block never is executed.

image

I am not sure if the UUID characteristics can be the same as the UUID service. Verify it, change one of these values so that they are not the same.

Edit:
Now I can see that they are different: D

They are not the same:

Sevice UUID: 1000f000-181A-0000-0001-a4ad9fa0c43b
Characteristic UUID: 1000f000-181A-0001-0001-a4ad9fa0c43b

In case it helps!, in one of my tests I got the message "Connection Failed: service discovery failed with operating system code 129".

SOLVED: I extended the BluetoothLE1 ConnectionTimeout to 20

image

app is not getting any value. can you please provide the last working arduino code, and app inventor.aia.
thank you

Connect to the Companion/Emulator, right-click the blocks you need to debug, and click on Do It. Try to see if you can get the value that way;

I am uploading the code of the project I developed to a Github repository (link below). If you have any questions, do not hesitate to ask me.
AirQuality GitHub Repository

1 Like

Ahh! Now that I remember! I believe that the initial code bug is that the return data type of the characteristic (BLEUnsignedInt) on Arduino and the expected data type (IntegerReceived) on Mobile App do not match. Try changing that.

3 Likes