Trying the app I succed to scan and connect to the arduino Nano but when I push a button in order to write 1 or 0 nothing is received on board (as from serial monitor on Arduino IDE).
/*
LED
This example creates a BLE peripheral with service that contains a
characteristic to control an LED.
The circuit:
- Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.
You can use a generic BLE central app, like LightBlue (iOS and Android) or
nRF Connect (Android), to interact with the services and characteristics
created in this sketch.
This example code is in the public domain.
*/
#include <ArduinoBLE.h>
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
const int ledPin = LED_BUILTIN; // pin to use for the LED
void setup() {
Serial.begin(9600);
while (!Serial);
// set LED pin to output mode
pinMode(ledPin, OUTPUT);
// begin initialization
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
// set advertised local name and service UUID:
BLE.setLocalName("LED");
BLE.setAdvertisedService(ledService);
// add the characteristic to the service
ledService.addCharacteristic(switchCharacteristic);
// add service
BLE.addService(ledService);
// set the initial value for the characeristic:
switchCharacteristic.writeValue(0);
// start advertising
BLE.advertise();
Serial.println("BLE LED Peripheral");
}
void loop() {
// listen for BLE peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
// while the central is still connected to peripheral:
while (central.connected()) {
// if the remote device wrote to the characteristic,
// use the value to control the LED:
if (switchCharacteristic.written()) {
if (switchCharacteristic.value()) { // any value other than 0
Serial.println("LED on");
digitalWrite(ledPin, HIGH); // will turn the LED on
} else { // a 0 value
Serial.println(F("LED off"));
digitalWrite(ledPin, LOW); // will turn the LED off
}
}
}
// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}
LightBlue send the value that I set, which is 1 to turn on and 0 to turn off.
I verified in the serial console that these are the value received by the board.
Hello,
I have also had the same problem with an Arduino Nano 33 BLE Sense and would greatly appreciate anyone who can give us an explanation of why this happens.
Thank you
In my case, neither of the WriteStrings or WriteStringsWithResponse or WriteBytes or any of the Write functions seem to “send” the right value. On the other end (Arduino) am trying to print the received value using - switchcharacteristic.value() ; the nrfConnect app seems to deliver the exact values when we use it’s “write” ability to send Byte or Char. So, am guessing the problem is not at the Arduino end. I’ve tried both CurieBLE.h and ArduinoBLE.h and am sure am missing something at the App inventor end. If anyone has a similar problem where their “values” are not reaching the Nano BLE, pls shout.
absolutely … it is the same one from the examples i borrowed from the gentleman who had it done for HM10 i guess. I’ve tried all the “Write” functions and sending variables and “lists” in various ways but no luck ARD_HM10_AI2_Single_LED_04 (1).aia (176.5 KB)
once again - kindly note - the nrfConnect app works just fine; am able to send bytes or UINT or an SINT works just fine. The code on the Arduino side is similar to the one pasted above but i’ll just paste the part of code that matters, here.
while (central.connected())
{
if (switchCharacteristic.written())
{
byte Value = 0;
Serial.print("swith char value:"); Serial.println(switchCharacteristic.value());
digitalWrite(LED_BUILTIN, HIGH);
switchCharacteristic.readValue(Value);
Serial.print("BLE value using readValue:"); Serial.println(Value);
Serial.print("value length=");
Serial.println(switchCharacteristic.valueLength());
}
I had the same problem with the LED Arduino example sketch (on a nano 33 IoT) shown above in that it worked using the nRFConnect and Light Blue android apps. Both said the characteristic was read & write. In AppInventor, if I used the WriteBytes method, it did not work. Further, it returned the result of the 'CanReadCharacteristic' call as FALSE. However, if I changed the WriteBytes call to WriteBytesWithResponse, the 'CanReadCharacteristic' now returns TRUE and the app responds when writing. One more interesting observation was that I ran the following on the Arduino;
if(switchCharacteristic.canWrite())
Serial.println("led can be written");
else
Serial.println("led canNOT be written");
byte led_properties = switchCharacteristic.properties();
if(led_properties & BLERead)
Serial.println("led is readable");
if(led_properties & BLEWrite)
Serial.println("led is writable");
if(led_properties & BLENotify)
Serial.println("led is notifiable");
Serial.print("led_properties value is = ");
Serial.println(led_properties);
with the following results;
led canNOT be written
led is readable
led is writable
led_properties value = 10 // 0x0a
not sure therefore if it's an appinventor issue or an Arduino library issue.
And the same App Inventor problem for me, along with another snag.
The 'CallbackLED' example in ArduinoBLE library receives data only if:
BluetoothLE.WriteBytesWithResponse() block used, not the more obvious (in context of library example) BluetoothLE.WriteBytes() block.
serviceUuid and characteristicUuid 'sockets' were plugged into text entry blocks, not global variable getter menu blocks.
Correspondents in other forums addressing Arduino/AI2 snags also mention 'hardwiring' Uuid workarounds, which is strange because menu-driven variable getter blocks seem to work OK elsewhere.