espd

esp32 cyd BT

Mar 15th, 2025
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. #include "BluetoothSerial.h"
  2.  
  3. BluetoothSerial SerialBT;
  4.  
  5. const char* realDeviceName = "EMUCANBT_SPP"; // Replace with actual Bluetooth device name
  6. //uint8_t canDeviceMAC[] =
  7.  
  8. void setup() {
  9. Serial.begin(115200);
  10. SerialBT.begin("ESP32-CYD", true); // True = ESP32 as a client
  11. //SerialBT.setPin("1234");
  12. Serial.println("Attempting to connect to real device...");
  13.  
  14. if (SerialBT.connect(realDeviceName)) {
  15. Serial.println("Connected successfully!");
  16. } else {
  17. Serial.println("Connection failed. Retrying...");
  18. }
  19. }
  20.  
  21. void loop() {
  22. if (SerialBT.connected()) {
  23. if (SerialBT.available()) {
  24. Serial.print("Received: ");
  25. Serial.write(SerialBT.read());
  26. }
  27. } else {
  28. Serial.println("Disconnected. Attempting reconnect...");
  29. SerialBT.connect(realDeviceName);
  30. delay(5000);
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment