ESP32 with ESP-IDF Classic Bluetooth - Mobile Phone - Serial Communication

Edgar1
Posts: 6
Joined: Fri Oct 11, 2019 7:53 am

ESP32 with ESP-IDF Classic Bluetooth - Mobile Phone - Serial Communication

Postby Edgar1 » Fri Oct 11, 2019 8:20 am

I was looking for sample code to create a simple application for serial communication between the ESP32 and a mobile phone.
It took a long time before I figured out how things work.

Here is some information - for a start without code.

I looked at the

ESP-IDF BT-SPP-ACCEPTOR demo
https://github.com/espressif/esp-idf/tr ... p_acceptor

ESP-IDF BT-SPP-INITATOR demo
https://github.com/espressif/esp-idf/tr ... _initiator

and first I could not figure out which I should use to connect my mobile phone (Android) to an ESP32
After some time I figured out that I can connect from my phone to the Acceptor demo.
I have to pair my phone with the ESP_SPP_ACCEPTOR
After that is done I can open a terminal application on the mobile phone (i.e. Serial Bluetooth) and I can send data from my phone to the ESP32.
If the Acceptor is opened with a monitor program I can see that what I send on the phone is received by the ESP32. It is shown in HEX format.
That works with the Acceptor demo without any changes. It's only necessary to set the following to see the received data.

Code: Select all

#define SPP_SHOW_MODE SPP_SHOW_DATA
With that the communication from the phone to the ESP32 worked. But I also want that data is send from the ESP to the mobile phone.
To be able to do that I analyzed how the communication is established and the code for sending and receiving data in the Acceptor and Initiator.
Then I copied the sending code from the Initiator to the Acceptor and the receiving code from the Acceptor to the Initiator.
Now finally that works. I can send and receive data in the Initiator and the Acceptor.

Currently the code which I worked on has lots of additional comment - which I added for my own understanding.
If anybody is interested I can publish that code here.
But maybe it would be better if I do that at GitHub. I have to admit I don't have much experience with them.

Now that this works in principle with the modified Initiator and Acceptor code my next step will be that I will work on the communication between my mobile phone and a modified version of the Acceptor.

Please let me know if you are interested in further info and which info.

entropio
Posts: 2
Joined: Tue Nov 05, 2019 2:39 pm

Re: ESP32 with ESP-IDF Classic Bluetooth - Mobile Phone - Serial Communication

Postby entropio » Thu Nov 07, 2019 8:43 pm

You could put the code here in the forum to get out of my doubts I am trying the same as you but apart I must send the data by the uart :D
Ing. Damian Lugo Electronic Design

Edgar1
Posts: 6
Joined: Fri Oct 11, 2019 7:53 am

Re: ESP32 with ESP-IDF Classic Bluetooth - Mobile Phone - Serial Communication

Postby Edgar1 » Fri Nov 08, 2019 1:48 am

Ok, I will do that. But currently I am a little sick so maybe next week.

emanuel36
Posts: 1
Joined: Tue Dec 24, 2019 12:45 pm

Re: ESP32 with ESP-IDF Classic Bluetooth - Mobile Phone - Serial Communication

Postby emanuel36 » Tue Dec 24, 2019 12:47 pm

Hi, currently I'm trying to do exactly this, could you share your code?

Edgar1
Posts: 6
Joined: Fri Oct 11, 2019 7:53 am

Re: ESP32 with ESP-IDF Classic Bluetooth - Mobile Phone - Serial Communication

Postby Edgar1 » Wed Dec 25, 2019 2:55 am

Sorry for the long delay.
Below is "my" code. I used initially the ESP-IDF sample code example_spp_acceptor_demo.c and modified it.
This is not clean code, I didn't have time to clean it up yet. But it works.
I pair my phone with the ESP_SPP_ACCEPTOR (ESP32 running with this code).
And then I use on the phone one of the free apps Serial Bluetooth or Bluetooth Terminal.
  1. /*
  2.    This example code is in the Public Domain (or CC0 licensed, at your option.)
  3.  
  4.    Unless required by applicable law or agreed to in writing, this
  5.    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  6.    CONDITIONS OF ANY KIND, either express or implied.
  7. */
  8.  
  9. #include <stdint.h>
  10. #include <string.h>
  11. #include <stdbool.h>
  12. #include <stdio.h>
  13. //#include "nvs.h"
  14. #include "nvs_flash.h"
  15. //#include "freertos/FreeRTOS.h"
  16. //#include "freertos/task.h"
  17. #include "esp_log.h"
  18. #include "esp_bt.h"
  19. #include "esp_bt_main.h"
  20. #include "esp_gap_bt_api.h"
  21. #include "esp_bt_device.h"
  22. #include "esp_spp_api.h"
  23.  
  24. #define SPP_TAG "SPP_ACCEPTOR_DEMO"
  25. #define SPP_SERVER_NAME "SPP_SERVER"
  26. #define EXAMPLE_DEVICE_NAME "ESP_SPP_ACCEPTOR"
  27.  
  28. static bool bWriteAfterOpenEvt = true;
  29. static bool bWriteAfterWriteEvt = false;
  30. static bool bWriteAfterSvrOpenEvt = true;
  31. static bool bWriteAfterDataReceived = true;
  32.  
  33. static const esp_spp_mode_t esp_spp_mode = ESP_SPP_MODE_CB;
  34.  
  35. //static long data_num = 0;
  36.  
  37. static const esp_spp_sec_t sec_mask = ESP_SPP_SEC_AUTHENTICATE;
  38. static const esp_spp_role_t role_slave = ESP_SPP_ROLE_SLAVE;
  39.  
  40. #define SPP_DATA_LEN 20
  41. static uint8_t spp_data[SPP_DATA_LEN];
  42.  
  43.  
  44. //Testing
  45. //https://stackoverflow.com/questions/23597259/returning-a-pointer-to-an-array-of-uint8-t-from-a-c-function-get-compiler-erro
  46. uint8_t *createTestBuffer(void){
  47.     static const uint8_t buffer[] = { 5, 7, 3, 4, 9, 1, 3 };
  48.     uint8_t *rv = malloc(sizeof(buffer));
  49.     if (rv != 0)
  50.         memmove(rv, buffer, sizeof(buffer));
  51.     return rv;
  52. }
  53.  
  54. static char charArrayLastReceivedData[18];
  55.  
  56. static void saveReceivedData(int len, uint8_t *p_data){
  57.     //
  58.     char array1[18] = "abcdefg";
  59.     char array2[18];
  60.     strncpy(array2, array1, 18);
  61.    
  62.     //Better: strncpy_s();
  63.  
  64.     strncpy(charArrayLastReceivedData, array1, 18);
  65.  
  66. }
  67.  
  68. static int getDataToSend(int *len, uint8_t *p_data){
  69.     //
  70.     return 0;   //ok
  71. }
  72.  
  73.  
  74.  
  75. static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param){
  76.     //Used in app_main() to setup the BT configuration in the ESP32 and used for communication with device
  77.     ESP_LOGI(SPP_TAG, "Start of: static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)");
  78.     switch (event) {
  79.     case ESP_SPP_INIT_EVT:
  80.         ESP_LOGI(SPP_TAG, "ESP_SPP_INIT_EVT");
  81.         ESP_LOGI(SPP_TAG, "Call esp_bt_dev_set_device_name(EXAMPLE_DEVICE_NAME)");
  82.         esp_bt_dev_set_device_name(EXAMPLE_DEVICE_NAME);
  83.         ESP_LOGI(SPP_TAG, "Call esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE)");
  84.         esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
  85.         ESP_LOGI(SPP_TAG, "Call esp_spp_start_srv(sec_mask,role_slave, 0, SPP_SERVER_NAME)");
  86.         esp_spp_start_srv(sec_mask,role_slave, 0, SPP_SERVER_NAME);
  87.         break;
  88.     case ESP_SPP_DISCOVERY_COMP_EVT:
  89.         ESP_LOGI(SPP_TAG, "ESP_SPP_DISCOVERY_COMP_EVT");
  90.         break;
  91.     case ESP_SPP_OPEN_EVT:
  92.         //When SPP Client connection open, the event comes
  93.         //In use in Initiator
  94.         ESP_LOGI(SPP_TAG, "ESP_SPP_OPEN_EVT");
  95.        
  96.         ESP_LOGI(SPP_TAG, "bWriteAfterOpenEvt: %d: ", bWriteAfterOpenEvt);
  97.         //Added code from Initiator - Start
  98.         if (bWriteAfterOpenEvt){
  99.             ESP_LOGI(SPP_TAG, "bWriteAfterOpenEvt = true");
  100.             ESP_LOGI(SPP_TAG, "Call esp_spp_write(param->srv_open.handle, SPP_DATA_LEN, spp_data)");
  101.  
  102.             //esp_err_tesp_spp_write(uint32_t handle, int len, uint8_t *p_data)
  103.             esp_spp_write(param->srv_open.handle, SPP_DATA_LEN, spp_data);
  104.  
  105.            
  106.             //Test 1
  107.             //uint8_t myData[10];
  108.  
  109.             //Test 2
  110.             char * c = "Hello";
  111.             // "Hello" is in fact just { 'H', 'e', 'l', 'l', 'o', '\0' }
  112.             uint8_t * u = (uint8_t *)c;
  113.             //uint8_t x = u[1];
  114.             // x is 101, which is the ASCII char code of 'e'
  115.  
  116.             esp_spp_write(param->srv_open.handle, 6, u);
  117.  
  118.             //Test, compiles ok
  119.             int intTest = 6;
  120.             intTest = 2 + 4;
  121.             esp_spp_write(param->srv_open.handle, intTest, u);
  122.  
  123.            
  124.         }
  125.         else{
  126.             ESP_LOGI(SPP_TAG, "bWriteAfterOpenEvt = false");
  127.         }
  128.         //Added code from Initiator - End
  129.  
  130.         break;
  131.     case ESP_SPP_CLOSE_EVT:
  132.         ESP_LOGI(SPP_TAG, "ESP_SPP_CLOSE_EVT");
  133.         break;
  134.     case ESP_SPP_START_EVT:                                         //Short before connection is established
  135.         ESP_LOGI(SPP_TAG, "ESP_SPP_START_EVT");
  136.         break;
  137.     case ESP_SPP_CL_INIT_EVT:
  138.         ESP_LOGI(SPP_TAG, "ESP_SPP_CL_INIT_EVT");
  139.         break;
  140.     case ESP_SPP_DATA_IND_EVT:                                      //When SPP connection received data, the event comes, only for ESP_SPP_MODE_CB
  141.         ESP_LOGI(SPP_TAG, "ESP_SPP_DATA_IND_EVT len=%d handle=%d", param->data_ind.len, param->data_ind.handle);
  142.         ESP_LOGI(SPP_TAG, "Call esp_log_buffer_hex("",param->data_ind.data,param->data_ind.len)");
  143.  
  144.         //ESP_LOG_BUFFER_HEX(tag, buffer, buff_len)
  145.         //tag: description tag
  146.         //buffer: Pointer to the buffer array
  147.         //buff_len: length of buffer in bytes
  148.  
  149.         esp_log_buffer_hex("Received HEX Data",param->data_ind.data,param->data_ind.len);
  150.         esp_log_buffer_char("Received String Data",param->data_ind.data,param->data_ind.len);
  151.         saveReceivedData(param->data_ind.len, param->data_ind.data);
  152.  
  153.         //New Test - Start
  154.         if (bWriteAfterDataReceived){
  155.             ESP_LOGI(SPP_TAG, "bWriteAfterDataReceived = true");
  156.  
  157.             //New data
  158.             //abc
  159.             for (int i = 0; i < SPP_DATA_LEN; ++i) {
  160.                 //spp_data[i] = i;
  161.                 spp_data[i] = i + 97;
  162.             }
  163.             //ESP_LOGI(SPP_TAG, "Call esp_spp_write(param->write.handle, SPP_DATA_LEN, spp_data)");
  164.             //esp_spp_write(param->write.handle, SPP_DATA_LEN, spp_data);
  165.  
  166.             ESP_LOGI(SPP_TAG, "Call esp_spp_write(param->write.handle, 8, Received)");
  167.             char * c = "Received";
  168.             uint8_t * u = (uint8_t *)c;
  169.             //uint8_t x = u[1];
  170.             esp_spp_write(param->srv_open.handle, 8, u);
  171.  
  172.         }
  173.         else{
  174.             ESP_LOGI(SPP_TAG, "bWriteAfterDataReceived = false");
  175.         }
  176.         //New Test - End
  177.  
  178.  
  179.         break;
  180.     case ESP_SPP_CONG_EVT:
  181.         ESP_LOGI(SPP_TAG, "ESP_SPP_CONG_EVT");
  182.         break;
  183.     case ESP_SPP_WRITE_EVT:
  184.         //When SPP write operation completes, the event comes, only for ESP_SPP_MODE_CB
  185.         //In use in Initiator
  186.  
  187.         //Original Acceptor Code - Start
  188.         //ESP_LOGI(SPP_TAG, "ESP_SPP_WRITE_EVT");
  189.         //Original Acceptor Code - End
  190.  
  191.         //Code copied from Initiator - Start
  192.         ESP_LOGI(SPP_TAG, "ESP_SPP_WRITE_EVT len=%d cong=%d", param->write.len , param->write.cong);
  193.  
  194.         //ToDo: Now the next line is incorrect, it shows not the data which was sent!
  195.         esp_log_buffer_hex("HEX Data was sent",spp_data,SPP_DATA_LEN);
  196.  
  197.         ESP_LOGI(SPP_TAG, "if param->write.cong ...");
  198.         if (param->write.cong == 0) {
  199.             ESP_LOGI(SPP_TAG, "param->write.cong == 0");
  200.             if (bWriteAfterWriteEvt){
  201.                 ESP_LOGI(SPP_TAG, "bWriteAfterWriteEvt = true");
  202.                 ESP_LOGI(SPP_TAG, "Call esp_spp_write(param->write.handle, SPP_DATA_LEN, spp_data)");
  203.                 esp_spp_write(param->write.handle, SPP_DATA_LEN, spp_data);
  204.             }
  205.             else{
  206.                 ESP_LOGI(SPP_TAG, "bWriteAfterWriteEvt = false");
  207.             }
  208.         }
  209.         else {
  210.             ESP_LOGI(SPP_TAG, "param->write.cong <> 0");
  211.         }
  212.         //Code copied from Initiator - End
  213.  
  214.         break;
  215.     case ESP_SPP_SRV_OPEN_EVT:                                      //After connection is established, short before data is received
  216.         //When SPP Server connection open, the event comes
  217.         //In use in Acceptor
  218.         ESP_LOGI(SPP_TAG, "ESP_SPP_SRV_OPEN_EVT");
  219.  
  220.         ESP_LOGI(SPP_TAG, "bWriteAfterOpenEvt: %d: ", bWriteAfterSvrOpenEvt);
  221.         //Added code from Initiator - Start
  222.         if (bWriteAfterSvrOpenEvt){
  223.             ESP_LOGI(SPP_TAG, "bWriteAfterSvrOpenEvt = true");
  224.             //ESP_LOGI(SPP_TAG, "Call esp_spp_write(param->srv_open.handle, SPP_DATA_LEN, spp_data)");
  225.             //esp_spp_write(param->srv_open.handle, SPP_DATA_LEN, spp_data);
  226.  
  227.            
  228.             //https://stackoverflow.com/questions/40579902/how-to-turn-a-character-array-into-uint8-t
  229.             char * c = "Hello";             // "Hello" is in fact just { 'H', 'e', 'l', 'l', 'o', '\0' }
  230.             uint8_t * u = (uint8_t *)c;
  231.             //uint8_t x = u[1];
  232.             ESP_LOGI(SPP_TAG, "Call esp_spp_write(param->srv_open.handle, 5, Hello)");
  233.             //esp_spp_write(param->srv_open.handle, 6, u);    //Works but shows special character after Hello
  234.             esp_spp_write(param->srv_open.handle, 5, u);    //Works, but maybe it needs something like CR
  235.  
  236.         }
  237.         else{
  238.             ESP_LOGI(SPP_TAG, "bWriteAfterSvrOpenEvt = false");
  239.         }
  240.         //Added code from Initiator - End
  241.  
  242.         break;
  243.     default:
  244.         break;
  245.     }
  246.     ESP_LOGI(SPP_TAG, "End of: static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)");
  247. }
  248.  
  249. void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param){
  250.     //Used in app_main() to setup the BT configuration in the ESP32
  251.     ESP_LOGI(SPP_TAG, "Start of: void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)");
  252.     switch (event) {
  253.     case ESP_BT_GAP_AUTH_CMPL_EVT:{
  254.         if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) {
  255.             ESP_LOGI(SPP_TAG, "authentication success: %s", param->auth_cmpl.device_name);
  256.             esp_log_buffer_hex(SPP_TAG, param->auth_cmpl.bda, ESP_BD_ADDR_LEN);
  257.         } else {
  258.             ESP_LOGE(SPP_TAG, "authentication failed, status:%d", param->auth_cmpl.stat);
  259.         }
  260.         break;
  261.     }
  262.     case ESP_BT_GAP_PIN_REQ_EVT:{
  263.         ESP_LOGI(SPP_TAG, "ESP_BT_GAP_PIN_REQ_EVT min_16_digit:%d", param->pin_req.min_16_digit);
  264.         if (param->pin_req.min_16_digit) {
  265.             ESP_LOGI(SPP_TAG, "Input pin code: 0000 0000 0000 0000");
  266.             esp_bt_pin_code_t pin_code = {0};
  267.             esp_bt_gap_pin_reply(param->pin_req.bda, true, 16, pin_code);
  268.         } else {
  269.             ESP_LOGI(SPP_TAG, "Input pin code: 1234");
  270.             esp_bt_pin_code_t pin_code;
  271.             pin_code[0] = '1';
  272.             pin_code[1] = '2';
  273.             pin_code[2] = '3';
  274.             pin_code[3] = '4';
  275.             esp_bt_gap_pin_reply(param->pin_req.bda, true, 4, pin_code);
  276.         }
  277.         break;
  278.     }
  279.  
  280.     //Must be set in sdkconfig.h: CONFIG_BT_SSP_ENABLED == true
  281.     //This enables the Secure Simple Pairing.
  282.     case ESP_BT_GAP_CFM_REQ_EVT:
  283.         ESP_LOGI(SPP_TAG, "ESP_BT_GAP_CFM_REQ_EVT Please compare the numeric value: %d", param->cfm_req.num_val);
  284.         esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true);
  285.         break;
  286.     case ESP_BT_GAP_KEY_NOTIF_EVT:
  287.         ESP_LOGI(SPP_TAG, "ESP_BT_GAP_KEY_NOTIF_EVT passkey:%d", param->key_notif.passkey);
  288.         break;
  289.     case ESP_BT_GAP_KEY_REQ_EVT:
  290.         ESP_LOGI(SPP_TAG, "ESP_BT_GAP_KEY_REQ_EVT Please enter passkey!");
  291.         break;
  292.  
  293.  
  294.     default: {
  295.         ESP_LOGI(SPP_TAG, "event: %d", event);
  296.         //  0 ESP_BT_GAP_DISC_RES_EVT
  297.         //  1 ESP_BT_GAP_DISC_STATE_CHANGED_EVT
  298.         //  2 ESP_BT_GAP_RMT_SRVCS_EVT
  299.         //  3 ESP_BT_GAP_RMT_SRVC_REC_EVT
  300.         //  4 ESP_BT_GAP_AUTH_CMPL_EVT
  301.         //  5 ESP_BT_GAP_PIN_REQ_EVT
  302.         //  6 ESP_BT_GAP_CFM_REQ_EVT
  303.         //  7 ESP_BT_GAP_KEY_NOTIF_EVT
  304.         //  8 ESP_BT_GAP_KEY_REQ_EVT
  305.         //  9 ESP_BT_GAP_READ_RSSI_DELTA_EVT
  306.         // 10 ESP_BT_GAP_CONFIG_EIR_DATA_EVT
  307.         // 11 ESP_BT_GAP_EVT_MAX
  308.         break;
  309.     }
  310.     }
  311.     return;
  312.     ESP_LOGI(SPP_TAG, "End of: void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)");
  313. }
  314.  
  315. void startClassicBtSpp(void){
  316.     ESP_LOGI(SPP_TAG, "void startClassicBtSpp(void) - Start");
  317.     //code
  318.     //Dummy data to send
  319.     for (int i = 0; i < SPP_DATA_LEN; ++i) {
  320.         //spp_data[i] = i;
  321.         spp_data[i] = i + 65;
  322.     }
  323.  
  324.  
  325.     //Non-volatile storage (NVS) library is designed to store key-value pairs in flash.
  326.     esp_err_t ret = nvs_flash_init();   //Initialize the default NVS partition.
  327.     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  328.         ESP_ERROR_CHECK(nvs_flash_erase());
  329.         ret = nvs_flash_init();
  330.     }
  331.     ESP_ERROR_CHECK( ret );
  332.  
  333.     ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_BLE));        //release the controller memory as per the mode
  334.  
  335.     esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
  336.     ESP_LOGI(SPP_TAG, "Call esp_bt_controller_init(&bt_cfg)");
  337.     if ((ret = esp_bt_controller_init(&bt_cfg)) != ESP_OK) {
  338.         ESP_LOGE(SPP_TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(ret));
  339.         return;
  340.     }
  341.     else{
  342.         ESP_LOGI(SPP_TAG, "Initialize controller ok");
  343.     }
  344.    
  345.     ESP_LOGI(SPP_TAG, "Call esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT)");
  346.     if ((ret = esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT)) != ESP_OK) {
  347.         ESP_LOGE(SPP_TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
  348.         return;
  349.     }
  350.     else{
  351.         ESP_LOGI(SPP_TAG, "Enable controller ok");
  352.     }
  353.    
  354.     ESP_LOGI(SPP_TAG, "Call esp_bluedroid_init()");
  355.     if ((ret = esp_bluedroid_init()) != ESP_OK) {
  356.         ESP_LOGE(SPP_TAG, "%s initialize bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
  357.         return;
  358.     }
  359.     else{
  360.         ESP_LOGI(SPP_TAG, "Initialize bluedroid ok");
  361.     }
  362.    
  363.     ESP_LOGI(SPP_TAG, "Call esp_bluedroid_enable()");
  364.     if ((ret = esp_bluedroid_enable()) != ESP_OK) {
  365.         ESP_LOGE(SPP_TAG, "%s enable bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
  366.         return;
  367.     }
  368.     else{
  369.         ESP_LOGI(SPP_TAG, "Enable bluedroid ok");
  370.     }
  371.    
  372.     ESP_LOGI(SPP_TAG, "Call esp_bt_gap_register_callback(esp_bt_gap_cb)");
  373.     if ((ret = esp_bt_gap_register_callback(esp_bt_gap_cb)) != ESP_OK) {
  374.         ESP_LOGE(SPP_TAG, "%s gap register failed: %s\n", __func__, esp_err_to_name(ret));
  375.         return;
  376.     }
  377.     else{
  378.         ESP_LOGI(SPP_TAG, "Gap register ok");
  379.     }
  380.    
  381.     ESP_LOGI(SPP_TAG, "Call esp_spp_register_callback(esp_spp_cb)");
  382.     if ((ret = esp_spp_register_callback(esp_spp_cb)) != ESP_OK) {
  383.         ESP_LOGE(SPP_TAG, "%s spp register failed: %s\n", __func__, esp_err_to_name(ret));
  384.         return;
  385.     }
  386.     else{
  387.         ESP_LOGI(SPP_TAG, "spp register ok");
  388.     }
  389.    
  390.     ESP_LOGI(SPP_TAG, "Call esp_spp_init(esp_spp_mode)");
  391.     if ((ret = esp_spp_init(esp_spp_mode)) != ESP_OK) {
  392.         ESP_LOGE(SPP_TAG, "%s spp init failed: %s\n", __func__, esp_err_to_name(ret));
  393.         return;
  394.     }
  395.     else{
  396.         ESP_LOGI(SPP_TAG, "spp init ok");
  397.     }
  398.    
  399.  
  400.  
  401.  
  402.     //Must be set in sdkconfig.h: CONFIG_BT_SSP_ENABLED == true
  403.     //This enables the Secure Simple Pairing.
  404.     ESP_LOGI(SPP_TAG, "CONFIG_BT_SSP_ENABLED == true");
  405.     /* Set default parameters for Secure Simple Pairing */
  406.     ESP_LOGI(SPP_TAG, "Set default parameters for Secure Simple Pairing");
  407.     esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
  408.     esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_IO;
  409.     esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t));
  410.  
  411.     /*
  412.      * Set default parameters for Legacy Pairing
  413.      * Use variable pin, input pin code when pairing
  414.      */
  415.     ESP_LOGI(SPP_TAG, "Set default parameters");
  416.     esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_VARIABLE;
  417.     esp_bt_pin_code_t pin_code;
  418.     esp_bt_gap_set_pin(pin_type, 0, pin_code);
  419.  
  420.     ESP_LOGI(SPP_TAG, "void startClassicBtSpp(void) - End");
  421. }
  422.  
  423. void app_main(void){
  424.     ESP_LOGI(SPP_TAG, "void app_main(void) - Start");
  425.  
  426.     startClassicBtSpp();
  427.  
  428.     ESP_LOGI(SPP_TAG, "void app_main(void) - End\n");
  429. }
  430.  
  431.  
  432.  

entropio
Posts: 2
Joined: Tue Nov 05, 2019 2:39 pm

Re: ESP32 with ESP-IDF Classic Bluetooth - Mobile Phone - Serial Communication

Postby entropio » Fri Oct 30, 2020 5:55 pm

Hello I have made a program that uses bluetooth classic spp and the uart2, the purpose is to pass data through bluetooth and pass it to uart2 transparently, your code is very similar to mine, the whole trick is in the functions esp_spp_cb () and esp_bt_gap_cb ( ) in the events that they handle, and find the data in the esp_spp_cb_param_t * param structure, where the obtained data is found, I got a bit complicated if you leave the functions and work with the esp_spp_cb_param_t * param structure since it loses the data writing the data from another function was complicated, could you help me.
Ing. Damian Lugo Electronic Design

Edgar1
Posts: 6
Joined: Fri Oct 11, 2019 7:53 am

Re: ESP32 with ESP-IDF Classic Bluetooth - Mobile Phone - Serial Communication

Postby Edgar1 » Sun Nov 01, 2020 9:58 am

I didn't do anything with this for a long time.
At some stage I changed to BLE. It works differently and in my opinion it's easier to use once you understand it.
I bought two of these for testing.
https://www.nordicsemi.com/Software-and ... 840-Dongle
That was a good, and not expensive, investment.

jmotisariya
Posts: 1
Joined: Tue Nov 10, 2020 4:47 pm

Re: ESP32 with ESP-IDF Classic Bluetooth - Mobile Phone - Serial Communication

Postby jmotisariya » Tue Nov 10, 2020 4:54 pm

Thank you for source code of ESP-IDF @Edgar1.
Can you please help me with the following issue.!
I require to read the string length of 32 characters from android device in my ESP32. I am able to read the string for 32 characters but the string is getting divided into 16 characters. Above that, I am also receiving 5 characters in single callback in my android platform from ESP32. I am required to get the whole string as a single string rather than split into 5 letter per callback in my android platform.

tiger.qi
Posts: 1
Joined: Mon Nov 16, 2020 1:13 am

Re: ESP32 with ESP-IDF Classic Bluetooth - Mobile Phone - Serial Communication

Postby tiger.qi » Mon Nov 16, 2020 1:34 am

There is an error in running the bt_spp_indiector demo program,anybody know why?

ChrisJ7903
Posts: 2
Joined: Tue Sep 03, 2024 10:45 am

Re: ESP32 with ESP-IDF Classic Bluetooth - Mobile Phone - Serial Communication

Postby ChrisJ7903 » Sat Oct 05, 2024 11:42 pm

Hi @Edgar1,
It's a very long time since you posted, but I'm hoping you might answer my questions:

Do you remember which ESP32 module you were using? (e.g. was it an ESP32-WROOM-32 ?)
Did you have to re-flash the ESP32 to add Classic Bluetooth functions?
Or did the module come with Classic bluetooth built in?

thank you

Who is online

Users browsing this forum: No registered users and 10 guests