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.