From 03f38a974d3ef13016311fc5ba3247b90b018dd5 Mon Sep 17 00:00:00 2001 From: XiongYu Date: Fri, 10 May 2019 12:04:05 +0800 Subject: [PATCH] feature(BluetoothSerial): add pinCode function --- .../SerialToSerialBT/SerialToSerialBT.ino | 5 +---- .../BluetoothSerial/src/BluetoothSerial.cpp | 20 +++++++++++++++++-- .../BluetoothSerial/src/BluetoothSerial.h | 3 +++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/libraries/BluetoothSerial/examples/SerialToSerialBT/SerialToSerialBT.ino b/libraries/BluetoothSerial/examples/SerialToSerialBT/SerialToSerialBT.ino index 9a5fa0877bc..7980432560e 100644 --- a/libraries/BluetoothSerial/examples/SerialToSerialBT/SerialToSerialBT.ino +++ b/libraries/BluetoothSerial/examples/SerialToSerialBT/SerialToSerialBT.ino @@ -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!"); } diff --git a/libraries/BluetoothSerial/src/BluetoothSerial.cpp b/libraries/BluetoothSerial/src/BluetoothSerial.cpp index a8332b8bc99..a7fa7a8ac8f 100644 --- a/libraries/BluetoothSerial/src/BluetoothSerial.cpp +++ b/libraries/BluetoothSerial/src/BluetoothSerial.cpp @@ -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(); @@ -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; } @@ -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) @@ -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) diff --git a/libraries/BluetoothSerial/src/BluetoothSerial.h b/libraries/BluetoothSerial/src/BluetoothSerial.h index 3f4372e55d2..393f371de92 100644 --- a/libraries/BluetoothSerial/src/BluetoothSerial.h +++ b/libraries/BluetoothSerial/src/BluetoothSerial.h @@ -22,6 +22,7 @@ #include "Arduino.h" #include "Stream.h" #include +#include "esp_gap_bt_api.h" class BluetoothSerial: public Stream { @@ -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); @@ -43,6 +45,7 @@ class BluetoothSerial: public Stream private: String local_name; + String pin_code; };