espd

bt with NAME - WITH ssp

Mar 17th, 2025
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include "esp_bt.h"
  3. #include "esp_bt_main.h"
  4. #include "esp_gap_bt_api.h"
  5. #include "esp_spp_api.h"
  6. #include "esp_bt_device.h"
  7. #include "esp_err.h"
  8.  
  9. #define TARGET_DEVICE_NAME "YourDeviceName" // Replace with actual Bluetooth device name
  10.  
  11. static esp_bd_addr_t target_bd_addr;
  12. static bool paired = false;
  13. static bool connected = false;
  14. static uint32_t spp_handle = 0; // Store SPP session handle
  15.  
  16. // GAP callback function (handles pairing, discovery, and authentication)
  17. void bt_gap_callback(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param) {
  18. switch (event) {
  19. case ESP_BT_GAP_DISC_RES_EVT: {
  20. Serial.println("Device discovered.");
  21. for (int i = 0; i < param->disc_res.num_prop; i++) {
  22. if (param->disc_res.prop[i].type == ESP_BT_GAP_DEV_PROP_BDNAME) {
  23. char *device_name = (char *)param->disc_res.prop[i].val;
  24. Serial.print("Found device: ");
  25. Serial.println(device_name);
  26.  
  27. if (strcmp(device_name, TARGET_DEVICE_NAME) == 0) {
  28. memcpy(target_bd_addr, param->disc_res.bda, sizeof(esp_bd_addr_t));
  29. Serial.println("Target device found! Connecting...");
  30. esp_spp_connect(ESP_SPP_SEC_AUTHENTICATE, ESP_SPP_ROLE_MASTER, 1, target_bd_addr);
  31. esp_bt_gap_cancel_discovery(); // Stop discovery once found
  32. }
  33. }
  34. }
  35. break;
  36. }
  37.  
  38. case ESP_BT_GAP_AUTH_CMPL_EVT:
  39. if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) {
  40. Serial.println("Bluetooth pairing successful!");
  41. paired = true;
  42. } else {
  43. Serial.println("Bluetooth pairing failed!");
  44. }
  45. break;
  46.  
  47. default:
  48. break;
  49. }
  50. }
  51.  
  52. // SPP callback function (handles connection status and data reception)
  53. void bt_spp_callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
  54. switch (event) {
  55. case ESP_SPP_INIT_EVT:
  56. Serial.println("SPP initialized.");
  57. esp_bt_dev_set_device_name("ESP32_BT_Client");
  58. esp_bt_gap_register_callback(bt_gap_callback);
  59.  
  60. // Enable Secure Simple Pairing (SSP)
  61. esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
  62. esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_IO;
  63. esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t));
  64.  
  65. // Start discovery and attempt connection
  66. esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, 10, 0);
  67. break;
  68.  
  69. case ESP_SPP_OPEN_EVT:
  70. Serial.println("SPP connection established!");
  71. connected = true;
  72. spp_handle = param->open.handle; // Store session handle for data transfer
  73. break;
  74.  
  75. case ESP_SPP_CLOSE_EVT:
  76. Serial.println("SPP connection closed.");
  77. connected = false;
  78. spp_handle = 0;
  79. break;
  80.  
  81. default:
  82. break;
  83. }
  84. }
  85.  
  86. // Function to initialize Bluetooth
  87. void setup_bluetooth() {
  88. if (!btStart()) {
  89. Serial.println("Failed to initialize Bluetooth.");
  90. return;
  91. }
  92.  
  93. if (esp_bluedroid_init() != ESP_OK) {
  94. Serial.println("Failed to initialize bluedroid.");
  95. return;
  96. }
  97.  
  98. if (esp_bluedroid_enable() != ESP_OK) {
  99. Serial.println("Failed to enable bluedroid.");
  100. return;
  101. }
  102.  
  103. Serial.println("Bluetooth initialized.");
  104.  
  105. esp_spp_register_callback(bt_spp_callback);
  106. esp_spp_init(ESP_SPP_MODE_CB);
  107. }
  108.  
  109. void setup() {
  110. Serial.begin(115200);
  111. setup_bluetooth();
  112. }
  113.  
  114. void loop() {
  115. if (connected) {
  116. Serial.println("Connected to Bluetooth device. Waiting for data...");
  117. uint8_t buffer[128];
  118. int len = esp_spp_read(spp_handle, buffer, sizeof(buffer));
  119. if (len > 0) {
  120. Serial.print("Received Data: ");
  121. for (int i = 0; i < len; i++) {
  122. Serial.write(buffer[i]);
  123. }
  124. Serial.println();
  125. }
  126. } else if (paired) {
  127. Serial.println("Paired but not connected.");
  128. } else {
  129. Serial.println("Waiting for pairing...");
  130. }
  131. delay(5000);
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment