Skip to content

Commit 33a11ec

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

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
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("3456");
1815
Serial.println("The device started, now you can pair it with bluetooth!");
1916
}
2017

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

+16
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,22 @@ bool BluetoothSerial::begin(String localName)
384384
return _init_bt(local_name.c_str());
385385
}
386386

387+
bool BluetoothSerial::pinCode( char* pswd)
388+
{
389+
if(strlen(pswd)==4){
390+
esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED;
391+
esp_bt_pin_code_t pin_code;
392+
pin_code[0] = pswd[0];
393+
pin_code[1] = pswd[1] ;
394+
pin_code[2] = pswd[2];
395+
pin_code[3] = pswd[3];
396+
esp_bt_gap_set_pin(pin_type, 4, pin_code);
397+
return true;
398+
}
399+
else
400+
return false;
401+
}
402+
387403
int BluetoothSerial::available(void)
388404
{
389405
if (_spp_rx_queue == NULL){

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

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
#include "Arduino.h"
2323
#include "Stream.h"
2424
#include <esp_spp_api.h>
25+
#include "esp_gap_bt_api.h"
26+
27+
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
28+
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
29+
#endif
2530

2631
class BluetoothSerial: public Stream
2732
{
@@ -31,6 +36,7 @@ class BluetoothSerial: public Stream
3136
~BluetoothSerial(void);
3237

3338
bool begin(String localName=String());
39+
bool pinCode(char* pswd);
3440
int available(void);
3541
int peek(void);
3642
bool hasClient(void);

0 commit comments

Comments
 (0)