Skip to content

feature(BluetoothSerial): add pinCode function #2765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
SerialBT.pinCode("1234"); //Bluetooth device password
Serial.println("The device started, now you can pair it with bluetooth!");
}

Expand Down
20 changes: 18 additions & 2 deletions libraries/BluetoothSerial/src/BluetoothSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
if(custom_spp_callback)(*custom_spp_callback)(event, param);
}

static bool _init_bt(const char *deviceName)
static bool _init_bt(const char *deviceName, const char *pswd)
{
if(!_spp_event_group){
_spp_event_group = xEventGroupCreate();
Expand Down Expand Up @@ -319,6 +319,11 @@ static bool _init_bt(const char *deviceName)
return false;
}

if (esp_bt_gap_set_pin(ESP_BT_PIN_TYPE_FIXED, strlen(pswd), (uint8_t *)pswd) != ESP_OK) {
log_e("set pincode failed");
return false;
}

return true;
}

Expand Down Expand Up @@ -369,6 +374,7 @@ static bool _stop_bt()
BluetoothSerial::BluetoothSerial()
{
local_name = "ESP32"; //default bluetooth name
pin_code = "0000"; //default bluetooth password
}

BluetoothSerial::~BluetoothSerial(void)
Expand All @@ -381,7 +387,17 @@ bool BluetoothSerial::begin(String localName)
if (localName.length()){
local_name = localName;
}
return _init_bt(local_name.c_str());
return _init_bt(local_name.c_str(), pin_code.c_str());
}

bool BluetoothSerial::pinCode(String pswd)
{
if (pswd.length() == 0 || pswd.length() > ESP_BT_PIN_CODE_LEN) {
return false;
}

esp_bt_gap_set_pin(ESP_BT_PIN_TYPE_FIXED, pswd.length(), (uint8_t *)pswd.c_str());
return true;
}

int BluetoothSerial::available(void)
Expand Down
3 changes: 3 additions & 0 deletions libraries/BluetoothSerial/src/BluetoothSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "Arduino.h"
#include "Stream.h"
#include <esp_spp_api.h>
#include "esp_gap_bt_api.h"

class BluetoothSerial: public Stream
{
Expand All @@ -31,6 +32,7 @@ class BluetoothSerial: public Stream
~BluetoothSerial(void);

bool begin(String localName=String());
bool pinCode(String pswd);
int available(void);
int peek(void);
bool hasClient(void);
Expand All @@ -43,6 +45,7 @@ class BluetoothSerial: public Stream

private:
String local_name;
String pin_code;

};

Expand Down