espd

bt with mac address - NO ssp

Mar 16th, 2025 (edited)
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 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_MAC_ADDRESS "XX:XX:XX:XX:XX:XX" // Set the MAC address of your target device
  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.  
  16. // Convert MAC address string to esp_bd_addr_t format
  17. void str2bda(const char *str, esp_bd_addr_t bda) {
  18. int values[6];
  19. if (sscanf(str, "%x:%x:%x:%x:%x:%x", &values[0], &values[1], &values[2], &values[3], &values[4], &values[5]) == 6) {
  20. for (int i = 0; i < 6; ++i) {
  21. bda[i] = (uint8_t)values[i];
  22. }
  23. }
  24. }
  25.  
  26. // GAP callback function (handles pairing and authentication)
  27. void bt_gap_callback(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param) {
  28. switch (event) {
  29. case ESP_BT_GAP_AUTH_CMPL_EVT:
  30. if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) {
  31. Serial.println("Bluetooth pairing successful!");
  32. paired = true;
  33. } else {
  34. Serial.println("Bluetooth pairing failed!");
  35. }
  36. break;
  37.  
  38. case ESP_BT_GAP_PIN_REQ_EVT:
  39. Serial.println("PIN requested...");
  40. esp_bt_gap_pin_reply(param->pin_req.bda, true, strlen(BT_PIN_CODE), (uint8_t *)BT_PIN_CODE);
  41. Serial.println("PIN sent.");
  42. break;
  43.  
  44. default:
  45. break;
  46. }
  47. }
  48.  
  49. // SPP callback function (handles connection status)
  50. void bt_spp_callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
  51. switch (event) {
  52. case ESP_SPP_INIT_EVT:
  53. Serial.println("SPP initialized.");
  54. esp_bt_dev_set_device_name("ESP32_BT_Client");
  55. esp_bt_gap_register_callback(bt_gap_callback);
  56.  
  57. // Disable Secure Simple Pairing (SSP)
  58. esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
  59. esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_NONE; // Disable SSP completely
  60. esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t));
  61.  
  62. // Set PIN-based authentication
  63. esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED;
  64. esp_bt_gap_set_pin(pin_type, 4, (uint8_t*)BT_PIN_CODE);
  65.  
  66. // Start discovery and attempt connection
  67. esp_spp_start_discovery(target_bd_addr);
  68. break;
  69.  
  70. case ESP_SPP_DISCOVERY_COMP_EVT:
  71. if (param->disc_comp.status == ESP_SPP_SUCCESS) {
  72. Serial.println("Device discovered, attempting connection...");
  73. esp_spp_connect(ESP_SPP_SEC_AUTHENTICATE, ESP_SPP_ROLE_MASTER, param->disc_comp.scn[0], target_bd_addr);
  74. } else {
  75. Serial.println("Device discovery failed.");
  76. }
  77. break;
  78.  
  79. case ESP_SPP_OPEN_EVT:
  80. Serial.println("SPP connection established!");
  81. connected = true;
  82. break;
  83.  
  84. case ESP_SPP_CLOSE_EVT:
  85. Serial.println("SPP connection closed.");
  86. connected = false;
  87. break;
  88.  
  89. default:
  90. break;
  91. }
  92. }
  93.  
  94. // Function to initialize Bluetooth
  95. void setup_bluetooth() {
  96. if (!btStart()) {
  97. Serial.println("Failed to initialize Bluetooth.");
  98. return;
  99. }
  100.  
  101. if (esp_bluedroid_init() != ESP_OK) {
  102. Serial.println("Failed to initialize bluedroid.");
  103. return;
  104. }
  105.  
  106. if (esp_bluedroid_enable() != ESP_OK) {
  107. Serial.println("Failed to enable bluedroid.");
  108. return;
  109. }
  110.  
  111. Serial.println("Bluetooth initialized.");
  112.  
  113. esp_spp_register_callback(bt_spp_callback);
  114. esp_spp_init(ESP_SPP_MODE_CB);
  115.  
  116. str2bda(TARGET_MAC_ADDRESS, target_bd_addr);
  117. }
  118.  
  119. void setup() {
  120. Serial.begin(115200);
  121. setup_bluetooth();
  122. }
  123.  
  124. void loop() {
  125. if (connected) {
  126. Serial.println("Connected to Bluetooth device.");
  127. } else if (paired) {
  128. Serial.println("Paired but not connected.");
  129. } else {
  130. Serial.println("Waiting for pairing...");
  131. }
  132. delay(5000);
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment