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_MAC_ADDRESS "XX:XX:XX:XX:XX:XX" // Set the MAC address of your target device
- #define BT_PIN_CODE "1234" // Change this to your correct PIN
- static esp_bd_addr_t target_bd_addr;
- static bool paired = false;
- static bool connected = false;
- // Convert MAC address string to esp_bd_addr_t format
- void str2bda(const char *str, esp_bd_addr_t bda) {
- int values[6];
- if (sscanf(str, "%x:%x:%x:%x:%x:%x", &values[0], &values[1], &values[2], &values[3], &values[4], &values[5]) == 6) {
- for (int i = 0; i < 6; ++i) {
- bda[i] = (uint8_t)values[i];
- }
- }
- }
- // GAP callback function (handles pairing 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_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;
- case ESP_BT_GAP_PIN_REQ_EVT:
- Serial.println("PIN requested...");
- esp_bt_gap_pin_reply(param->pin_req.bda, true, strlen(BT_PIN_CODE), (uint8_t *)BT_PIN_CODE);
- Serial.println("PIN sent.");
- break;
- default:
- break;
- }
- }
- // SPP callback function (handles connection status)
- 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);
- // Disable Secure Simple Pairing (SSP) and use PIN
- esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED;
- esp_bt_gap_set_pin(pin_type, 4, (uint8_t*)BT_PIN_CODE);
- // Start discovery and attempt connection
- esp_spp_start_discovery(target_bd_addr);
- break;
- case ESP_SPP_DISCOVERY_COMP_EVT:
- if (param->disc_comp.status == ESP_SPP_SUCCESS) {
- Serial.println("Device discovered, attempting connection...");
- esp_spp_connect(ESP_SPP_SEC_AUTHENTICATE, ESP_SPP_ROLE_MASTER, param->disc_comp.scn[0], target_bd_addr);
- } else {
- Serial.println("Device discovery failed.");
- }
- break;
- case ESP_SPP_OPEN_EVT:
- Serial.println("SPP connection established!");
- connected = true;
- break;
- case ESP_SPP_CLOSE_EVT:
- Serial.println("SPP connection closed.");
- connected = false;
- 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);
- str2bda(TARGET_MAC_ADDRESS, target_bd_addr);
- }
- void setup() {
- Serial.begin(115200);
- setup_bluetooth();
- }
- void loop() {
- if (connected) {
- Serial.println("Connected to Bluetooth device.");
- } 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