Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include "esp_bt.h"
- #include "esp_bt_main.h"
- #include "esp_gap_bt_api.h"
- #include "esp_spp_api.h"
- #include "esp_bt_device.h"
- #include "esp_err.h"
- #define TARGET_DEVICE_NAME "YourDeviceName" // Replace with actual Bluetooth device name
- static esp_bd_addr_t target_bd_addr;
- static bool paired = false;
- static bool connected = false;
- static uint32_t spp_handle = 0; // Store SPP session handle
- // GAP callback function (handles pairing, discovery, and authentication)
- void bt_gap_callback(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param) {
- switch (event) {
- case ESP_BT_GAP_DISC_RES_EVT: {
- Serial.println("Device discovered.");
- for (int i = 0; i < param->disc_res.num_prop; i++) {
- if (param->disc_res.prop[i].type == ESP_BT_GAP_DEV_PROP_BDNAME) {
- char *device_name = (char *)param->disc_res.prop[i].val;
- Serial.print("Found device: ");
- Serial.println(device_name);
- if (strcmp(device_name, TARGET_DEVICE_NAME) == 0) {
- memcpy(target_bd_addr, param->disc_res.bda, sizeof(esp_bd_addr_t));
- Serial.println("Target device found! Connecting...");
- esp_spp_connect(ESP_SPP_SEC_AUTHENTICATE, ESP_SPP_ROLE_MASTER, 1, target_bd_addr);
- esp_bt_gap_cancel_discovery(); // Stop discovery once found
- }
- }
- }
- break;
- }
- case ESP_BT_GAP_AUTH_CMPL_EVT:
- if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) {
- Serial.println("Bluetooth pairing successful!");
- paired = true;
- } else {
- Serial.println("Bluetooth pairing failed!");
- }
- break;
- default:
- break;
- }
- }
- // SPP callback function (handles connection status and data reception)
- void bt_spp_callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
- switch (event) {
- case ESP_SPP_INIT_EVT:
- Serial.println("SPP initialized.");
- esp_bt_dev_set_device_name("ESP32_BT_Client");
- esp_bt_gap_register_callback(bt_gap_callback);
- // Enable Secure Simple Pairing (SSP)
- esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
- esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_IO;
- esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t));
- // Start discovery and attempt connection
- esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, 10, 0);
- break;
- case ESP_SPP_OPEN_EVT:
- Serial.println("SPP connection established!");
- connected = true;
- spp_handle = param->open.handle; // Store session handle for data transfer
- break;
- case ESP_SPP_CLOSE_EVT:
- Serial.println("SPP connection closed.");
- connected = false;
- spp_handle = 0;
- break;
- default:
- break;
- }
- }
- // Function to initialize Bluetooth
- void setup_bluetooth() {
- if (!btStart()) {
- Serial.println("Failed to initialize Bluetooth.");
- return;
- }
- if (esp_bluedroid_init() != ESP_OK) {
- Serial.println("Failed to initialize bluedroid.");
- return;
- }
- if (esp_bluedroid_enable() != ESP_OK) {
- Serial.println("Failed to enable bluedroid.");
- return;
- }
- Serial.println("Bluetooth initialized.");
- esp_spp_register_callback(bt_spp_callback);
- esp_spp_init(ESP_SPP_MODE_CB);
- }
- void setup() {
- Serial.begin(115200);
- setup_bluetooth();
- }
- void loop() {
- if (connected) {
- Serial.println("Connected to Bluetooth device. Waiting for data...");
- uint8_t buffer[128];
- int len = esp_spp_read(spp_handle, buffer, sizeof(buffer));
- if (len > 0) {
- Serial.print("Received Data: ");
- for (int i = 0; i < len; i++) {
- Serial.write(buffer[i]);
- }
- Serial.println();
- }
- } else if (paired) {
- Serial.println("Paired but not connected.");
- } else {
- Serial.println("Waiting for pairing...");
- }
- delay(5000);
- }
Advertisement
Add Comment
Please, Sign In to add comment