espd

bt with NAME - NO ssp

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