espd

bt with mac address - WITH ssp

Mar 16th, 2025 (edited)
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 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) and use PIN
  58. esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED;
  59. esp_bt_gap_set_pin(pin_type, 4, (uint8_t*)BT_PIN_CODE);
  60.  
  61. // Start discovery and attempt connection
  62. esp_spp_start_discovery(target_bd_addr);
  63. break;
  64.  
  65. case ESP_SPP_DISCOVERY_COMP_EVT:
  66. if (param->disc_comp.status == ESP_SPP_SUCCESS) {
  67. Serial.println("Device discovered, attempting connection...");
  68. esp_spp_connect(ESP_SPP_SEC_AUTHENTICATE, ESP_SPP_ROLE_MASTER, param->disc_comp.scn[0], target_bd_addr);
  69. } else {
  70. Serial.println("Device discovery failed.");
  71. }
  72. break;
  73.  
  74. case ESP_SPP_OPEN_EVT:
  75. Serial.println("SPP connection established!");
  76. connected = true;
  77. break;
  78.  
  79. case ESP_SPP_CLOSE_EVT:
  80. Serial.println("SPP connection closed.");
  81. connected = false;
  82. break;
  83.  
  84. default:
  85. break;
  86. }
  87. }
  88.  
  89. // Function to initialize Bluetooth
  90. void setup_bluetooth() {
  91. if (!btStart()) {
  92. Serial.println("Failed to initialize Bluetooth.");
  93. return;
  94. }
  95.  
  96. if (esp_bluedroid_init() != ESP_OK) {
  97. Serial.println("Failed to initialize bluedroid.");
  98. return;
  99. }
  100.  
  101. if (esp_bluedroid_enable() != ESP_OK) {
  102. Serial.println("Failed to enable bluedroid.");
  103. return;
  104. }
  105.  
  106. Serial.println("Bluetooth initialized.");
  107.  
  108. esp_spp_register_callback(bt_spp_callback);
  109. esp_spp_init(ESP_SPP_MODE_CB);
  110.  
  111. str2bda(TARGET_MAC_ADDRESS, target_bd_addr);
  112. }
  113.  
  114. void setup() {
  115. Serial.begin(115200);
  116. setup_bluetooth();
  117. }
  118.  
  119. void loop() {
  120. if (connected) {
  121. Serial.println("Connected to Bluetooth device.");
  122. } else if (paired) {
  123. Serial.println("Paired but not connected.");
  124. } else {
  125. Serial.println("Waiting for pairing...");
  126. }
  127. delay(5000);
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment