1

I am making BLE echo system between Arduino and Android. I have finished to make read and write function. But I want Android can automatically detect value when BluetoothGattCharacteristic value change. So I made set notification on Android. When I tried to check the fucntion is working properly or not, It was not working properly. And I use nRFConnect App to check what is the problem. the problem is the status of descriptor on Arduino is Notification and indications disabled. And I googled how to enable notification and indication on Arduino. But on the Arduino's document, I cannot find the function which can make enable descriptor's notification and indication. this is the document link. So is it possible to enable notification and indication on Arduino BLE??

Code of Arduino nano 33 IOT.

#include <ArduinoBLE.h>

BLEService echoService("00000000-0000-1000-8000-00805f9b34fb");
BLEStringCharacteristic charac ("741c12b9-e13c-4992-8a5e-fce46dec0bff", BLERead | BLEWrite | BLENotify,40);
BLEDescriptor Descriptor("beca6057-955c-4f8a-e1e3-56a1633f04b1","Descriptor");
String var = "";

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

  if(!BLE.begin()){
    Serial.println("starting BLE failed.");
    while(1);
  }

  
  BLE.setLocalName("Arduino BLE Echo");
  BLE.setAdvertisedService(echoService);
  charac.addDescriptor(Descriptor);
  echoService.addCharacteristic(charac);
  BLE.addService(echoService);
  BLE.advertise();
  Serial.println("Bluetooth device active, waiting for connections...");
  Serial.println(" ");

}

void loop(){
  BLEDevice central = BLE.central();
  if(central){
    Serial.println("* Connected to central device!");
    Serial.print("Connected to central : ");
    Serial.println(central.address());
    Serial.println(" ");

    while(central.connected()){
      if(charac.written()){
        var = charac.value();
        Serial.println(String(var));
        delay(500);
        charac.writeValue(var);
        Serial.println("write stringCharacteristic");
      }
      
    }
    Serial.print("Disconnected from central: ");
    Serial.println(central.address());   
  }
}

picture of nRFConnection APP. enter image description here

tonykrjhc
  • 129
  • 6

1 Answers1

1

You need to enable the notifications from the Android side in order to receive the BLE notifications. This is because according to the BLE specification, BLE notifications are disabled by default. If you want BLE notifications to remain enabled after the first time you enable them, you can bond with the Arduino device. This way, the status of the notification will persist across power cycles and disconnection/reconnection, and as soon as you reconnect to the Arduino device, notifications will be enabled by default (but again, you still have to enable them the first time).

This can be seen in the paragraph below (Bluetooth Specification v5.3, Vol 3, Part G, Section 3.3.3.3 - Clinet Characteristic Configuration - Page 1489):-

"The Client Characteristic Configuration declaration is an optional characteristic descriptor that defines how the characteristic may be configured by a specific client. The Client Characteristic Configuration descriptor value shall be persistent across connections for bonded devices. The Client Characteristic Configuration descriptor value shall be set to the default value at each connection with non-bonded devices.The characteristic descriptor value is a bit field. When a bit is set, that action shall be enabled, otherwise it will not be used. The Client Characteristic Configuration descriptor may occur in any position within the characteristic definition after the Characteristic Value. Only one Client Characteristic Configuration declaration shall exist in a characteristic definition.

The default value for the Client Characteristic Configuration descriptor value shall be 0x0000."

Note that the Client Characteristic Descriptor (CCCD) is the attribute in the characteristic that handles notification/indications. You can read more about this here:-

Youssif Saeed
  • 11,789
  • 4
  • 44
  • 72