Skip to content

Commit 03f38a9

Browse files
committed
feature(BluetoothSerial): add pinCode function
1 parent 7dd537f commit 03f38a9

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

Diff for: libraries/BluetoothSerial/examples/SerialToSerialBT/SerialToSerialBT.ino

+1-4
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@
66

77
#include "BluetoothSerial.h"
88

9-
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
10-
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
11-
#endif
12-
139
BluetoothSerial SerialBT;
1410

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

Diff for: libraries/BluetoothSerial/src/BluetoothSerial.cpp

+18-2
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
235235
if(custom_spp_callback)(*custom_spp_callback)(event, param);
236236
}
237237

238-
static bool _init_bt(const char *deviceName)
238+
static bool _init_bt(const char *deviceName, const char *pswd)
239239
{
240240
if(!_spp_event_group){
241241
_spp_event_group = xEventGroupCreate();
@@ -319,6 +319,11 @@ static bool _init_bt(const char *deviceName)
319319
return false;
320320
}
321321

322+
if (esp_bt_gap_set_pin(ESP_BT_PIN_TYPE_FIXED, strlen(pswd), (uint8_t *)pswd) != ESP_OK) {
323+
log_e("set pincode failed");
324+
return false;
325+
}
326+
322327
return true;
323328
}
324329

@@ -369,6 +374,7 @@ static bool _stop_bt()
369374
BluetoothSerial::BluetoothSerial()
370375
{
371376
local_name = "ESP32"; //default bluetooth name
377+
pin_code = "0000"; //default bluetooth password
372378
}
373379

374380
BluetoothSerial::~BluetoothSerial(void)
@@ -381,7 +387,17 @@ bool BluetoothSerial::begin(String localName)
381387
if (localName.length()){
382388
local_name = localName;
383389
}
384-
return _init_bt(local_name.c_str());
390+
return _init_bt(local_name.c_str(), pin_code.c_str());
391+
}
392+
393+
bool BluetoothSerial::pinCode(String pswd)
394+
{
395+
if (pswd.length() == 0 || pswd.length() > ESP_BT_PIN_CODE_LEN) {
396+
return false;
397+
}
398+
399+
esp_bt_gap_set_pin(ESP_BT_PIN_TYPE_FIXED, pswd.length(), (uint8_t *)pswd.c_str());
400+
return true;
385401
}
386402

387403
int BluetoothSerial::available(void)

Diff for: libraries/BluetoothSerial/src/BluetoothSerial.h

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "Arduino.h"
2323
#include "Stream.h"
2424
#include <esp_spp_api.h>
25+
#include "esp_gap_bt_api.h"
2526

2627
class BluetoothSerial: public Stream
2728
{
@@ -31,6 +32,7 @@ class BluetoothSerial: public Stream
3132
~BluetoothSerial(void);
3233

3334
bool begin(String localName=String());
35+
bool pinCode(String pswd);
3436
int available(void);
3537
int peek(void);
3638
bool hasClient(void);
@@ -43,6 +45,7 @@ class BluetoothSerial: public Stream
4345

4446
private:
4547
String local_name;
48+
String pin_code;
4649

4750
};
4851

0 commit comments

Comments
 (0)