esp32-snippets icon indicating copy to clipboard operation
esp32-snippets copied to clipboard

Cant connect from Flutter app

Open lgLindstrom opened this issue 2 years ago • 7 comments

Hi

I've tried to set upp a BLE communcation between a flutter app and ESP32 using Arduino library. Cant get it to work,, so I think I am missing out on something.

Below is two logs.

  1. From ESP Starting up,, and initiation of BLE. My flutter app is still not started.
  2. From ESP when flutter app is connecting.

I've tried to find information on internet but with no luck. Can anyone help ??

  1. From ESP Starting up
[V][BLEDevice.cpp:76] createServer(): >> createServer
[V][BLEServer.cpp:287] registerApp(): >> registerApp - 0
[V][FreeRTOS.cpp:189] take(): Semaphore taking: name: RegisterAppEvt (0x3ffdd4ac), owner: <N/A> for registerApp
[V][FreeRTOS.cpp:198] take(): Semaphore taken:  name: RegisterAppEvt (0x3ffdd4ac), owner: registerApp
[V][FreeRTOS.cpp:63] wait(): >> wait: Semaphore waiting: name: RegisterAppEvt (0x3ffdd4ac), owner: registerApp for registerApp
[D][BLEDevice.cpp:102] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... Unknown
[V][BLEUtils.cpp:1530] dumpGattServerEvent(): GATT ServerEvent: Unknown
[V][BLEUtils.cpp:1720] dumpGattServerEvent(): dumpGattServerEvent: *** NOT CODED ***
[V][BLEServer.cpp:144] handleGATTServerEvent(): >> handleGATTServerEvent: Unknown
[V][FreeRTOS.cpp:143] give(): Semaphore giving: name: RegisterAppEvt (0x3ffdd4ac), owner: registerApp
[V][BLEServer.cpp:277] handleGATTServerEvent(): << handleGATTServerEvent
[V][FreeRTOS.cpp:77] wait(): << wait: Semaphore released: name: RegisterAppEvt (0x3ffdd4ac), owner: <N/A>
[V][BLEServer.cpp:291] registerApp(): << registerApp
[V][BLEDevice.cpp:83] createServer(): << createServer
[V][BLEServer.cpp:67] createService(): >> createService - 4fafc202-1fb5-459e-8fca-c5c9c331914b
[V][FreeRTOS.cpp:189] take(): Semaphore taking: name: CreateEvt (0x3ffdd76c), owner: <N/A> for createService
[V][FreeRTOS.cpp:198] take(): Semaphore taken:  name: CreateEvt (0x3ffdd76c), owner: createService
[V][BLEService.cpp:60] executeCreate(): >> executeCreate() - Creating service (esp_ble_gatts_create_service) service uuid: 4fafc202-1fb5-459e-8fca-c5c9c331914b
[V][FreeRTOS.cpp:189] take(): Semaphore taking: name: CreateEvt (0x3ffdd9d8), owner: <N/A> for executeCreate
[V][FreeRTOS.cpp:198] take(): Semaphore taken:  name: CreateEvt (0x3ffdd9d8), owner: executeCreate
[V][FreeRTOS.cpp:63] wait(): >> wait: Semaphore waiting: name: CreateEvt (0x3ffdd9d8), owner: executeCreate for executeCreate
[D][BLEDevice.cpp:102] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... Unknown
[V][BLEUtils.cpp:1530] dumpGattServerEvent(): GATT ServerEvent: Unknown
[V][BLEUtils.cpp:1720] dumpGattServerEvent(): dumpGattServerEvent: *** NOT CODED ***
[V][BLEServer.cpp:144] handleGATTServerEvent(): >> handleGATTServerEvent: Unknown
[V][FreeRTOS.cpp:143] give(): Semaphore giving: name: CreateEvt (0x3ffdd76c), owner: createService
[V][BLEService.cpp:194] setHandle(): >> setHandle - Handle=0x28, service UUID=4fafc202-1fb5-459e-8fca-c5c9c331914b)
[V][BLEService.cpp:200] setHandle(): << setHandle
[V][FreeRTOS.cpp:143] give(): Semaphore giving: name: CreateEvt (0x3ffdd9d8), owner: executeCreate
[V][BLEServer.cpp:277] handleGATTServerEvent(): << handleGATTServerEvent
[V][FreeRTOS.cpp:77] wait(): << wait: Semaphore released: name: CreateEvt (0x3ffdd9d8), owner: <N/A>
[V][BLEService.cpp:76] executeCreate(): << executeCreate
[V][FreeRTOS.cpp:63] wait(): >> wait: Semaphore waiting: name: CreateEvt (0x3ffdd76c), owner: <N/A> for createService
[V][FreeRTOS.cpp:77] wait(): << wait: Semaphore released: name: CreateEvt (0x3ffdd76c), owner: <N/A>
[V][BLEServer.cpp:83] createService(): << createService
[V][BLEService.cpp:222] addCharacteristic(): >> addCharacteristic()
[D][BLEService.cpp:225] addCharacteristic(): Adding characteristic: uuid=beb5483e-36e1-468a-b7f5-ea07361b27a8 to service: UUID: 4fafc202-1fb5-459e-8fca-c5c9c331914b, handle: 0x0028
[V][BLEService.cpp:237] addCharacteristic(): << addCharacteristic()
[V][BLECharacteristic.cpp:574] setCallbacks(): >> setCallbacks: 0x3ffba7fc
[V][BLECharacteristic.cpp:580] setCallbacks(): << setCallbacks
[V][BLECharacteristic.cpp:68] addDescriptor(): >> addDescriptor(): Adding UUID: 00002902-0000-1000-8000-00805f9b34fb, handle: 0xffff to UUID: beb5483e-36e1-468a-b7f5-ea07361b27a8, handle : 0xffff Read Write
[V][BLECharacteristic.cpp:70] addDescriptor(): << addDescriptor()
[I][ble_wrapper.cpp:51] begin(): Starting BLE Service
[V][BLEService.cpp:134] start(): >> start(): Starting service (esp_ble_gatts_start_service): UUID: 4fafc202-1fb5-459e-8fca-c5c9c331914b, 
handle: 0x0028
[V][BLECharacteristic.cpp:79] executeCreate(): >> executeCreate()
[D][BLECharacteristic.cpp:90] executeCreate(): Registering characteristic (esp_ble_gatts_add_char): uuid: beb5483e-36e1-468a-b7f5-ea07361b27a8, service: UUID: 4fafc202-1fb5-459e-8fca-c5c9c331914b, handle: 0x0028
[V][FreeRTOS.cpp:189] take(): Semaphore taking: name: CreateEvt (0x3ffddc98), owner: <N/A> for executeCreate
[V][FreeRTOS.cpp:198] take(): Semaphore taken:  name: CreateEvt (0x3ffddc98), owner: executeCreate
[V][FreeRTOS.cpp:63] wait(): >> wait: Semaphore waiting: name: CreateEvt (0x3ffddc98), owner: executeCreate for executeCreate
[D][BLEDevice.cpp:102] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... Unknown
[V][BLEUtils.cpp:1530] dumpGattServerEvent(): GATT ServerEvent: Unknown
[V][BLEUtils.cpp:1720] dumpGattServerEvent(): dumpGattServerEvent: *** NOT CODED ***
[V][BLEServer.cpp:144] handleGATTServerEvent(): >> handleGATTServerEvent: Unknown
[V][BLECharacteristic.cpp:595] setHandle(): >> setHandle: handle=0x2a, characteristic uuid=beb5483e-36e1-468a-b7f5-ea07361b27a8
[V][BLECharacteristic.cpp:597] setHandle(): << setHandle
[V][BLECharacteristic.cpp:198] handleGATTServerEvent(): >> handleGATTServerEvent: Unknown
[V][FreeRTOS.cpp:143] give(): Semaphore giving: name: CreateEvt (0x3ffddc98), owner: executeCreate
[V][BLECharacteristic.cpp:456] handleGATTServerEvent(): << handleGATTServerEvent
[V][FreeRTOS.cpp:77] wait(): << wait: Semaphore released: name: CreateEvt (0x3ffddc98), owner: <N/A>
[V][BLEServer.cpp:277] handleGATTServerEvent(): << handleGATTServerEvent
[V][BLEDescriptor.cpp:57] executeCreate(): >> executeCreate(): UUID: 00002902-0000-1000-8000-00805f9b34fb, handle: 0xffff
[V][FreeRTOS.cpp:189] take(): Semaphore taking: name: CreateEvt (0x3ffdeb3c), owner: <N/A> for executeCreate
[V][FreeRTOS.cpp:198] take(): Semaphore taken:  name: CreateEvt (0x3ffdeb3c), owner: executeCreate
[V][FreeRTOS.cpp:63] wait(): >> wait: Semaphore waiting: name: CreateEvt (0x3ffdeb3c), owner: executeCreate for executeCreate
[D][BLEDevice.cpp:102] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... Unknown
[V][BLEUtils.cpp:1530] dumpGattServerEvent(): GATT ServerEvent: Unknown
[V][BLEUtils.cpp:1720] dumpGattServerEvent(): dumpGattServerEvent: *** NOT CODED ***
[V][BLEServer.cpp:144] handleGATTServerEvent(): >> handleGATTServerEvent: Unknown
[V][BLECharacteristic.cpp:198] handleGATTServerEvent(): >> handleGATTServerEvent: Unknown
[V][BLEDescriptor.cpp:220] setHandle(): >> setHandle(0x2b): Setting descriptor handle to be 0x2b
[V][BLEDescriptor.cpp:222] setHandle(): << setHandle()
[V][FreeRTOS.cpp:143] give(): Semaphore giving: name: CreateEvt (0x3ffdeb3c), owner: executeCreate
[V][BLECharacteristic.cpp:456] handleGATTServerEvent(): << handleGATTServerEvent
[V][FreeRTOS.cpp:77] wait(): << wait: Semaphore released: name: CreateEvt (0x3ffdeb3c), owner: <N/A>
[V][BLEServer.cpp:277] handleGATTServerEvent(): << handleGATTServerEvent
[V][BLEDescriptor.cpp:81] executeCreate(): << executeCreate
[V][BLECharacteristic.cpp:116] executeCreate(): << executeCreate
[V][FreeRTOS.cpp:189] take(): Semaphore taking: name: StartEvt (0x3ffddaa0), owner: <N/A> for start
[V][FreeRTOS.cpp:198] take(): Semaphore taken:  name: StartEvt (0x3ffddaa0), owner: start
[V][FreeRTOS.cpp:63] wait(): >> wait: Semaphore waiting: name: StartEvt (0x3ffddaa0), owner: start for start
[D][BLEDevice.cpp:102] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... Unknown
[V][BLEUtils.cpp:1530] dumpGattServerEvent(): GATT ServerEvent: Unknown
[V][BLEUtils.cpp:1720] dumpGattServerEvent(): dumpGattServerEvent: *** NOT CODED ***
[V][BLEServer.cpp:144] handleGATTServerEvent(): >> handleGATTServerEvent: Unknown
[V][FreeRTOS.cpp:143] give(): Semaphore giving: name: StartEvt (0x3ffddaa0), owner: start
[V][BLECharacteristic.cpp:198] handleGATTServerEvent(): >> handleGATTServerEvent: Unknown
[V][FreeRTOS.cpp:77] wait(): << wait: Semaphore released: name: StartEvt (0x3ffddaa0), owner: <N/A>
[V][BLECharacteristic.cpp:456] handleGATTServerEvent(): << handleGATTServerEvent
[V][BLEService.cpp:159] start(): << start()
[V][BLEServer.cpp:277] handleGATTServerEvent(): << handleGATTServerEvent
[I][BLEDevice.cpp:569] getAdvertising(): create advertising
[D][BLEDevice.cpp:571] getAdvertising(): get advertising
[I][ble_wrapper.cpp:61] begin(): Advertising BLE Service
[V][BLEDevice.cpp:576] startAdvertising(): >> startAdvertising
[D][BLEDevice.cpp:571] getAdvertising(): get advertising
[V][BLEAdvertising.cpp:187] start(): >> start: customAdvData: 0, customScanResponseData: 0
[D][BLEAdvertising.cpp:199] start(): - advertising service: 4fafc202-1fb5-459e-8fca-c5c9c331914b
[V][BLEAdvertising.cpp:252] start(): << start
[V][BLEDevice.cpp:578] startAdvertising(): << startAdvertising
[V][BLEUtils.cpp:1817] gapEventToString(): gapEventToString: Unknown event type 0 0x00
[I][ble_wrapper.cpp:65] begin(): Waiting for BLE client
[V][BLEUtils.cpp:1049] dumpGapEvent(): Received a GAP event: Unknown event type
[V][BLEUtils.cpp:1264] dumpGapEvent(): *** dumpGapEvent: Logger not coded ***
[D][BLEDevice.cpp:571] getAdvertising(): get advertising
[D][BLEAdvertising.cpp:506] handleGAPEvent(): handleGAPEvent [event no: 0]
[V][BLEUtils.cpp:1817] gapEventToString(): gapEventToString: Unknown event type 1 0x01
[V][BLEUtils.cpp:1049] dumpGapEvent(): Received a GAP event: Unknown event type
[V][BLEUtils.cpp:1264] dumpGapEvent(): *** dumpGapEvent: Logger not coded ***
[D][BLEDevice.cpp:571] getAdvertising(): get advertising
[D][BLEAdvertising.cpp:506] handleGAPEvent(): handleGAPEvent [event no: 1]
[V][BLEUtils.cpp:1817] gapEventToString(): gapEventToString: Unknown event type 6 0x06
[V][BLEUtils.cpp:1049] dumpGapEvent(): Received a GAP event: Unknown event type
[V][BLEUtils.cpp:1264] dumpGapEvent(): *** dumpGapEvent: Logger not coded ***
[D][BLEDevice.cpp:571] getAdvertising(): get advertising
[D][BLEAdvertising.cpp:506] handleGAPEvent(): handleGAPEvent [event no: 6]
BLE .
BLE .
BLE .
  1. From ESP when flutter app is connecting.
[D][BLEDevice.cpp:102] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... Unknown
[V][BLEUtils.cpp:1530] dumpGattServerEvent(): GATT ServerEvent: Unknown
[V][BLEUtils.cpp:1720] dumpGattServerEvent(): dumpGattServerEvent: *** NOT CODED ***
[V][BLEServer.cpp:144] handleGATTServerEvent(): >> handleGATTServerEvent: Unknown   
[V][BLECharacteristic.cpp:198] handleGATTServerEvent(): >> handleGATTServerEvent: Unknown
[V][BLECharacteristic.cpp:456] handleGATTServerEvent(): << handleGATTServerEvent
[V][BLEServer.cpp:277] handleGATTServerEvent(): << handleGATTServerEvent
[V][BLEUtils.cpp:1817] gapEventToString(): gapEventToString: Unknown event type 20 0x14
[V][BLEUtils.cpp:1049] dumpGapEvent(): Received a GAP event: Unknown event type        
[V][BLEUtils.cpp:1264] dumpGapEvent(): *** dumpGapEvent: Logger not coded ***
[D][BLEDevice.cpp:571] getAdvertising(): get advertising
[D][BLEAdvertising.cpp:506] handleGAPEvent(): handleGAPEvent [event no: 20]  
BLE .
[V][BLEUtils.cpp:1817] gapEventToString(): gapEventToString: Unknown event type 20 0x14
[V][BLEUtils.cpp:1049] dumpGapEvent(): Received a GAP event: Unknown event type
[V][BLEUtils.cpp:1264] dumpGapEvent(): *** dumpGapEvent: Logger not coded ***  
[D][BLEDevice.cpp:571] getAdvertising(): get advertising
[D][BLEAdvertising.cpp:506] handleGAPEvent(): handleGAPEvent [event no: 20]
BLE .
BLE .

lgLindstrom avatar Aug 27 '21 14:08 lgLindstrom

Hi,

I've some experience working with BLE with flutter and the ESP32. Can you be more specific? or maybe share some code.

It's hard to know what you mean by not working, and the logs without context don't mean much

CLDiego avatar Aug 27 '21 14:08 CLDiego

The second log shows whats happening when trying to connect. I get a lot of unknowns ....

ble_wrapper.h

#pragma once
 #include <string>
#include <sstream>
#include <vector>
#include <iterator>
#include <arduino.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include "esp_log.h"
#include "commons.h"



class BleWrapper : public BLECharacteristicCallbacks , public BLEServerCallbacks {
  constexpr static const char *TAG = "BLE_WRAPPER";
  
  
  public:
    BleWrapper() : BLECharacteristicCallbacks(), BLEServerCallbacks()  {};
    ~BleWrapper()  = default; 

    void begin();
  
    virtual void write (std::string s1, std::string s2, std::string s3, std::string s4, std::string s5){};

    // BLECharacteristicCallback
    void onWrite(BLECharacteristic *pCharacteristic) override;

    // Ble server callbacks
    void  onConnect(BLEServer* pServer) override;  
    void onDisconnect(BLEServer* pServer) override;
      
 

    template <typename Out>
    void split(const std::string &s, char delim, Out result) {
      std::istringstream iss(s);
      std::string item;
      while (std::getline(iss, item, delim)) {
        *result++ = item;
      }
    }

    std::vector<std::string> split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    split(s, delim, std::back_inserter(elems));
    return elems;
}
  
  private:

    BLEServer* pServer = NULL;
    BLECharacteristic* pCharacteristic = NULL;
    static void _write (String s1, String s2, String s3, String s4, String s5);
 


};

ble_wrapper.cpp

#include "ble_wrapper.h"

  
  void BleWrapper::begin(){
   
   ESP_LOGI(TAG, "Starting BLE Wrapper");

    BLEDevice::init(BLE_EXPOSED_BLE_DEVICE);

    // Create the BLE Server
    pServer = BLEDevice::createServer();
     
    // Create the BLE Service
    BLEService *pService = pServer->createService(BLE_SERVICE_UUID);

    // Create a BLE Characteristic
    pCharacteristic = pService->createCharacteristic(
                        BLE_CHARACTERISTIC_UUID,
                        BLECharacteristic::PROPERTY_READ   |
                        BLECharacteristic::PROPERTY_WRITE  
 
                      );

    pCharacteristic->setCallbacks( this );
    // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
    // Create a BLE Descriptor
    pCharacteristic->addDescriptor(new BLE2902());

    // Start the service
    ESP_LOGI(TAG, "Starting BLE Service");
    pService->start();

    // Start advertising
    BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
    pAdvertising->addServiceUUID(BLE_SERVICE_UUID);
    pAdvertising->setScanResponse(true);
    pAdvertising->setMinPreferred(0x6); 
    pAdvertising->setMinPreferred(0x12); // set value to 0x00 to not advertise this parameter
    
    ESP_LOGI(TAG,"Advertising BLE Service");
    
    BLEDevice::startAdvertising();

    ESP_LOGI(TAG,"Waiting for BLE client");
  };

  void BleWrapper:: onConnect(BLEServer* pServer)  {
    ESP_LOGI(TAG,"BLE connected");
        
    BLEDevice::startAdvertising();
  };

  void BleWrapper::onDisconnect(BLEServer* pServer) {
    ESP_LOGI(TAG, "BLE Disconnected");   //TODO: Should do something here
  }

  void BleWrapper::onWrite(BLECharacteristic *pCharacteristic){
    
    std::string value = pCharacteristic->getValue();

    ESP_LOGI(TAG, "Data revived %s   with length %i",value.c_str(), value.length());

     if(value.length() > 0){
        std::vector<std::string> values = BleWrapper::split(value, BLE_SEPARATOR);
        ESP_LOGI(TAG, "Got:  %s , %s , %s , %s , %s", values[0].c_str(),values[1].c_str(),values[2].c_str(), values[3].c_str(),values[4].c_str());
        write(values[0],values[1],values[2], values[3],values[4]);
    } else {
      ESP_LOGW(TAG, "Recived data was empty");
    } 

  }


lgLindstrom avatar Aug 27 '21 14:08 lgLindstrom

As far as I know the unknown events are just at the debugging level (I think at some point to make the library smaller, the debugging messages got affected).

The wrapper seems to be fine, but again there is no context. So it's hard to tell if the issue is on your esp32 code or the app. You are creating a descriptor, but don't seem to be handling notifications.

Try to scan for your device with the nRF Connect app, see if it pops there with your configuration details

CLDiego avatar Aug 27 '21 15:08 CLDiego

Hi,,, apoligize for late answer.

I used the nRF connect app.

  • It finds my device properly.
  • Using the app I sent 'hello' to my device. This is the response I got on my device.
[D][BLEDevice.cpp:102] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... Unknown
[V][BLEUtils.cpp:1530] dumpGattServerEvent(): GATT ServerEvent: Unknown
[V][BLEUtils.cpp:1720] dumpGattServerEvent(): dumpGattServerEvent: *** NOT CODED ***
[V][BLEServer.cpp:144] handleGATTServerEvent(): >> handleGATTServerEvent: Unknown
[V][BLECharacteristic.cpp:198] handleGATTServerEvent(): >> handleGATTServerEvent: Unknown
[V][BLECharacteristic.cpp:456] handleGATTServerEvent(): << handleGATTServerEvent
[V][BLEServer.cpp:277] handleGATTServerEvent(): << handleGATTServerEvent
[V][BLEUtils.cpp:1817] gapEventToString(): gapEventToString: Unknown event type 20 0x14
[V][BLEUtils.cpp:1049] dumpGapEvent(): Received a GAP event: Unknown event type
[V][BLEUtils.cpp:1264] dumpGapEvent(): *** dumpGapEvent: Logger not coded ***
[D][BLEDevice.cpp:571] getAdvertising(): get advertising
[D][BLEAdvertising.cpp:506] handleGAPEvent(): handleGAPEvent [event no: 20]
[V][BLEUtils.cpp:1817] gapEventToString(): gapEventToString: Unknown event type 20 0x14
[V][BLEUtils.cpp:1049] dumpGapEvent(): Received a GAP event: Unknown event type
[V][BLEUtils.cpp:1264] dumpGapEvent(): *** dumpGapEvent: Logger not coded ***
[D][BLEDevice.cpp:571] getAdvertising(): get advertising
[D][BLEAdvertising.cpp:506] handleGAPEvent(): handleGAPEvent [event no: 20]
[D][BLEDevice.cpp:102] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... Unknown
[V][BLEUtils.cpp:1530] dumpGattServerEvent(): GATT ServerEvent: Unknown
[V][BLEUtils.cpp:1720] dumpGattServerEvent(): dumpGattServerEvent: *** NOT CODED ***
[V][BLEServer.cpp:144] handleGATTServerEvent(): >> handleGATTServerEvent: Unknown
[V][BLECharacteristic.cpp:198] handleGATTServerEvent(): >> handleGATTServerEvent: Unknown
[V][FreeRTOS.cpp:143] give(): Semaphore giving: name: ConfEvt (0x3ffdd930), owner: <N/A>
[V][BLECharacteristic.cpp:456] handleGATTServerEvent(): << handleGATTServerEvent
[V][BLEServer.cpp:277] handleGATTServerEvent(): << handleGATTServerEvent

The app gives a messages,,

Device does not support SMP Services

So,, my conclusion is that something is wrong with my wrapper,,, although,, I cant see it at the moment.

lgLindstrom avatar Aug 31 '21 10:08 lgLindstrom

I must miss some important here,,, or maybe I am so stuck in my own ideas so I cant see what I am doing wrong.

  1. I created a new project using VsCode and platform IO.
  2. Configered platformio.ini to use Arduino framework and EspressifDevkitC development board.
  3. Added code to main.cpp (see below)
  4. Uploaded and started.
  5. On my tablet I have 'nrF connect' installed. It discovered my ESP32devkit board nicely
  6. I selected the device and pressed send (hello)
  7. ===> Nothing<=== I had expected that "hello" should have been printed on the console.................................

So, what am I doing wrong????

#include <arduino.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

bool deviceConnected = false;


class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      Serial.println("Connected");
      deviceConnected = true;
      BLEDevice::startAdvertising();
    };

    void onDisconnect(BLEServer* pServer) {
      Serial.println("Disconnected");
      deviceConnected = false;
    }
};

class MyCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pCharacteristic) {
      std::string rxValue = pCharacteristic->getValue();

      if (rxValue.length() > 0) {
        Serial.println("*********");
        Serial.print("Received Value: ");
        for (int i = 0; i < rxValue.length(); i++)
          Serial.print(rxValue[i]);

        Serial.println();
        Serial.println("*********");
      }
    }
};

void setup() {
  Serial.begin(115200);
  Serial.println("Starting BLE work!");

  BLEDevice::init("Long name works now");
  BLEServer *pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  BLEService *pService = pServer->createService(SERVICE_UUID);
  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setValue("Hello World says Neil");
  pCharacteristic->setCallbacks(new MyCallbacks());
  pService->start();
  // BLEAdvertising *pAdvertising = pServer->getAdvertising();  // this still is working for backward compatibility
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  pAdvertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue
  pAdvertising->setMinPreferred(0x12);
  BLEDevice::startAdvertising();
  Serial.println("Characteristic defined! Now you can read it in your phone!");
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(2000);
}`

`

lgLindstrom avatar Sep 02 '21 09:09 lgLindstrom

Make sure you are sending the message to the right characteristic. BLE is different from traditional Bluetooth. With BLE you must connect to the device and then select a characteristic from which to read or send data.

On the app search for the characteristic that has the "Hello World says Neil" message and then write your message there

CLDiego avatar Sep 02 '21 10:09 CLDiego

F#(¤%(W)"#

Seems like it have been working for a long time... only me that not using that BLE application on the tablet correctly ...

Many, Many thanks for opening my eyes.

lgLindstrom avatar Sep 02 '21 11:09 lgLindstrom