BLE Device not detected

Hey, If any one used BLE using sample code

I found that the ble device (gr-lychee) not showing in mobile ble list using android app

my serial logs,

Bluetooth sample started
ESP32 ready
GATT initialized
srv_index is 1
Advertising started. Please connect to any client

Parents
  • I tried the sample, even I am not getting Gr-lychee as Board BLE name, but I am getting name as N/A using that I can do Advertising & send the string from mobile using BLE Scanner app.

  • have you checked with other Android devices with GPS ON if available?

    I am getting something like this image

  • i am getting like above only, but not able to connect if connect clicks
    it shows connection fail
  • Can you try this Code once,

    #include <Arduino.h>
    #include "ESP32.h"
     
    BufferedSerial esp32(P7_1, P0_1, 1024);
    ATParser_os esp_parser(esp32);
    DigitalOut esp_en(P5_3);
    DigitalOut esp_io0(P3_14);
    DigitalOut led_green(LED1);
    DigitalOut led_yellow(LED2);
    DigitalOut led_orange(LED3);
    DigitalOut led_red(LED4);
    InterruptIn button0(USER_BUTTON0);
    InterruptIn button1(USER_BUTTON1);
    Semaphore event_sem(0);
     
    const char ble_name[] = "GR-LYCHEE";
    bool ble_advertizing;
    bool ble_connected;
    uint8_t ble_conn_index;
    uint8_t ble_srv_index;
     
    bool button0_flag = false;
    bool button1_flag = false;
    uint8_t ble_notification_on = false;
     
    void esp32_event() {
        event_sem.release();
    }
     
    void ble_client_read() {
        uint8_t mac[6] = {0};
        led_yellow = 1;
        esp_parser.recv("%hhd,\"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx\"", &ble_conn_index, &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);
        printf("%d, mac[%x:%x:%x:%x:%x:%x]\r\n", ble_conn_index, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
        led_yellow = 0;
    }
     
    void ble_client_write() {
        uint8_t char_index, desc_index;
        led_orange = 1;
        esp_parser.recv("%hhd,%hhd,%hhd,", &ble_conn_index, &ble_srv_index, &char_index);
        printf("conn=%d srv=%d char=%d\r\n", ble_conn_index, ble_srv_index, char_index);
        char c = esp_parser.getc();
        if( c != ','){
            desc_index = c;
            esp_parser.getc(); // to read ',' after desc_index.
            printf("desc=%d\r\n", desc_index);
        }
        uint16_t len;
        esp_parser.recv("%hhd,", &len);
        printf("length=%d\r\n", len);
        for (int i = 0; i < len; i++) {
            uint8_t value = esp_parser.getc();
            printf("%x\r\n", value);
        }
        led_orange = 0;
    }
     
    void ble_client_disconn(){
        led_green = 0;
        ble_connected = false;
    }
     
    void ble_client_conn(){
        uint8_t mac[6] = {0};
        esp_parser.recv("%hhd,\"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx\"", &ble_conn_index, &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);
        printf("connected client %d, mac[%x:%x:%x:%x:%x:%x]\r\n", ble_conn_index, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
     
        led_green = 1;
        ble_advertizing = false;
        ble_connected = true;
    }
     
    void ub0_interrupt(){
        if(button0_flag == false){
            button0_flag = true;
            event_sem.release();
        }
    }
     
    void ub1_interrupt(){
        if(button1_flag == false){
            button1_flag = true;
            event_sem.release();
        }
    }
     
    void setup() {
     
        printf("Bluetooth sample started\r\n");
        led_green = 0;
        led_yellow = 0;
        led_orange = 0;
        led_red = 0;
        ble_advertizing = false;
        ble_connected = false;
        button0.fall(ub0_interrupt);
        button1.fall(ub1_interrupt);
     
        // Initializing esp32 access
        esp32.baud(115200);
        esp32.attach(Callback<void()>(esp32_event));
        esp_io0 = 1;
        esp_en = 0;
        Thread::wait(10);
        esp_en = 1;
        esp_parser.setTimeout(1500);
        if (esp_parser.recv("ready")) {
            printf("ESP32 ready\r\n");
        } else {
            printf("ESP32 error\r\n");
            led_red = 1;
            while (1);
        }
     
        // Initializing esp32 as a server with GATT service
        esp_parser.setTimeout(5000);
        if (esp_parser.send("AT+BLEINIT=2") && esp_parser.recv("OK")
                && esp_parser.send("AT+BLENAME=\"%s\"", ble_name) && esp_parser.recv("OK")
                && esp_parser.send("AT+BLEGATTSSRVCRE") && esp_parser.recv("OK")
                && esp_parser.send("AT+BLEGATTSSRVSTART") && esp_parser.recv("OK")){
            printf("GATT initialized\r\n");
        } else {
            printf("fail to initialize");
            led_red = 1;
            while (1);
        }
        esp_parser.oob("+READ:", ble_client_read);
        esp_parser.oob("+WRITE:", ble_client_write);
        esp_parser.oob("+BLEDISCONN:", ble_client_disconn);
        esp_parser.oob("+BLECONN:", ble_client_conn);
     
        while (1) {
            if (esp_parser.send("AT+BLEADVSTART") && esp_parser.recv("OK")){
                printf("Advertising started, please connect to any client\r\n");
            } else {
                printf("fail to start advertising");
                led_red = 1;
                while (1);
            }
     
            ble_advertizing = true;
     
            uint8_t start, type;
            uint16_t uuid;
            esp_parser.send("AT+BLEGATTSSRV?");
            if(esp_parser.recv("+BLEGATTSSRV:%hhd,%hhd,%hhx,%hhd\r\nOK", &ble_srv_index, &start, &uuid, &type)){
                printf("srv_index=%d\r\n", ble_srv_index);
            } else {
                printf("fail to get service");
                led_red = 1;
            }
     
            while (ble_connected || ble_advertizing) {
                event_sem.wait();
                esp_parser.setTimeout(5);
                esp_parser.recv(" ");  //dummy read for parser callback
     
                if(button0_flag){
                    static uint8_t data = 1; // write data
                    esp_parser.setTimeout(5000);
                    if(esp_parser.send("AT+BLEGATTSSETATTR=%d,1,,1", ble_srv_index) && esp_parser.recv(">")){
                        if(esp_parser.putc(data) && esp_parser.recv("OK")){
                            printf("success to send\r\n");
                        } else {
                            printf("fail to send\r\n");
                        }
                    } else {
                        printf("fail to command AT");
                    }
                    esp_parser.flush();
                    button0_flag = false;
                    data++;
                }
            }
        }
    }
     
    void loop() {
     
    }
  • I tried the code uploaded by Selva on web compiler. As a result, the LightBlue on my iPhone display as below.

    The above image show "GR-LYCHEE", but first it diplays "Unnamed". I'm not sure the cause, sorry for that.

    Then I click "Unnamed", the below image is displayed.

     

    Then the console displays as following image. I think the code itself is no problem, it depends on apps.

    I'm sorry, but I don't have android, so I've not tried that.

  • Did you getting "connected client 0, mac[ ] " print from 'ble_client_conn' function
  • Actually, I am getting this

    connected client conn_index is 0, mac[73:c0:5f:16:a3:6d]
  • Did you got prints "connected client , mac[]" from ble_client_conn() function
  • i tried renesas gatt browser available on playstore it also detects the BLE but its not showing the name.Im only able to see the mac id of BLE.
  • Hi,
    I too tried for the above ble code, but my device is not getting recognised..
    but i get this on the serial monitor


    Bluetooth sample started
    ESP32 ready
    GATT initialized
    Advertising started, please connect to any client

    Is the issue solved ?
Reply Children
No Data