Arduino-esp32: How to pair with ESP32 Bluetooth device using passkey/PIN from smartphone?

Created on 2 Jun 2018  路  36Comments  路  Source: espressif/arduino-esp32

Hardware:

Board: ESP-WROOM-32
Core Installation/update date: 01/JUN/2018
IDE name: Arduino IDE
Flash Frequency: 80Mhz
Upload Speed: 921600

Description:

I am using the SerialToSerialBT example. I am able to scan and discover the Bluetooth device from my smartphone. But the pairing is without any security/passcode. It simply pairs immediately. How to make the pairing using PIN/Passkey entry?

Sketch:

#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);
  delay(5000);
  Serial.println("Starting..");
  SerialBT.begin("MyESP32"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");

}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  delay(20);
}

Debug Messages:

Starting..
The device started, now you can pair it with bluetooth!

to be implemented

Most helpful comment

@copercini feature was implemented in commit https://github.com/espressif/esp-idf/commit/8571170abe3b2b7e5344e9603dadf2e6f74916e9

Any idea on when it could be available for esp-idf ?

Thank you

All 36 comments

It depends of some IDF/Bluetooth core functions that aren't available yet... https://github.com/espressif/esp-idf/issues/1541

once it is released, it will be implemented here as well

I am also working on a Bluetooth device that needs this feature. Is there an estimate on its release date?

@copercini feature was implemented in commit https://github.com/espressif/esp-idf/commit/8571170abe3b2b7e5344e9603dadf2e6f74916e9

Any idea on when it could be available for esp-idf ?

Thank you

would be great to have this feature.

Hi,
Any idea on when it could be available for Arduino ?
Thank You

I know its not the best solution, but you can try to use esp-idf functions in your app. Just saying.

Anything more on this? It's a feature i am needing badly.

No help.
I am using Lolin D32 with Arduino IDE. I have tried all the ways many examples, still serial terminal app is connecting to esp32 without facing any passkey authentication in bluetooth classic mode.

Looking forward for support.

Thanks

There is no option to build classic bluetooth with static key pairing (or any key pairing). It is because it requires to change settings in menuconfig. Classic bluetooth can be configured to use SSP (secure simple pairing) or legacy pairing which is controlled in menuconfig.
Only option is to use arduino as component.

Hi everyone,
Thanks to all.
I have successfully implemented pass key aka pin code authentication functionality in my lolin 32 using Arduino as IDE in classic Bluetooth mode for sending data over serial Bluetooth profile.
What I did is as following.

  1. I uninstall latest version of of espressif esp32 Arduino 1.0.4 release.
  2. Install espressif/ Arduino ver. 1.0.1 from board manager.
  3. Select board.
  4. Download disable_ssp.zip from https://github.com/espressif/arduino-esp32/issues/2320#issuecomment-477288545
  5. Follow the readme instruction available in above zip file.
  6. Upload the compiled program.

Open Bluetooth setting in my phone , unpair esp32 device from my old paired list. Rescan for new device then click on the my esp32 device ... Here m the miracle happened .... THE PIN CODE AUTHENTICATION APPEARED .... to pair my Bluetooth esp32 device....

Thanks for all brilliant people who saved my hard work done by providing this solution.

Regards
Harpreet Singh

Thanks for sharing @HarpreetHeera
As you said, you are using arduino-esp32 v1.0.1, which is very old version and probably at the time it was compiled option i mentioned earlier was not implemented in esp-idf, but glad you achieved what you are looking for.

Thanks for sharing @HarpreetHeera
As you said, you are using arduino-esp32 v1.0.1, which is very old version and probably at the time it was compiled option i mentioned earlier was not implemented in esp-idf, but glad you achieved what you are looking for.

Thanks Chegewara ...
for your support and valuable response.
Have a great time.

Hi guys. I found some code and I uploaded my repository.
come and try my code. it might be worked !
It should be Arduino IDF and ESP-IDF installed
No need to downgrade version
https://github.com/choichangjun/ESP32_arduino

Hi guys. I found some code and I uploaded my repository.
come and try my code. it might be worked !
It should be Arduino IDF and ESP-IDF installed
No need to downgrade version
https://github.com/choichangjun/ESP32_arduino

Its wonderfully working. Thanks

https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLETests/Arduino/security/StaticPIN/StaticPIN.ino

https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/BLESecurity.cpp#L67

Its here over a year, and its working, just for some reasons is not updated in arduino-essp32.

BTW this issue was about PIN in classic BT, not BLE

Thanks Chegewara,
Now we have working pin key authentication in latest version of Arduino-Esp32.

Thanks for your efforts. Great to know. Now we are able to do more stuff on latest release .

Thanks again.

So... I've tried Bluetooth classic serial on the ESP32 and pairing does not work for me with Ubuntu 19.10. The ESP32 always connects and directly disconnects again. Is this the same problem? What can I do about it?

Only option is to use arduino as component.

What does that mean?

Thanks Chegewara,
Now we have working pin key authentication in latest version of Arduino-Esp32.
Thanks for your efforts. Great to know. Now we are able to do more stuff on latest release .
Thanks again.

sir, can you please provide me the code to set password for esp-32 classic Bluetooth.

Thanks Chegewara,
Now we have working pin key authentication in latest version of Arduino-Esp32.
Thanks for your efforts. Great to know. Now we are able to do more stuff on latest release .
Thanks again.

sir, can you please provide me the code to set password for esp-32 classic Bluetooth.

What specific problem are you facing? Need little explanation.

Thanks Chegewara,
Now we have working pin key authentication in latest version of Arduino-Esp32.
Thanks for your efforts. Great to know. Now we are able to do more stuff on latest release .
Thanks again.

sir, can you please provide me the code to set password for esp-32 classic Bluetooth.

What specific problem are you facing? Need little explanation.

I am unable to set a paring password for esp32 board. The board is automatically getting connected to mobile phone without asking for any passkey.

I am in need of help regarding setting passkey for ESP-32 classic Bluetooth please somebody help me out.

I am in need of help regarding setting passkey for ESP-32 classic Bluetooth please somebody help me out.

You need to use arduino as component and to change some settings in bluetooth menuconfig or rebuild bluetooth library in esp-idf with new settings and replace the one in arduino.

Hi,I麓m also needing to set a passkey to classic Bluetooth .Use arduino as a component?How can we do that?I麓m learning ,please help!Thanks.

https://github.com/espressif/arduino-esp32/blob/master/docs/esp-idf_component.md

Thanks for the answer.But is it possible to make it easier?How can a simple thing like this give so much work.I don麓t know how to do it .Can you help?

https://github.com/espressif/arduino-esp32/blob/master/docs/esp-idf_component.md

Thanks for the answer.But is it possible to make it easier?How can a simple thing like this give so much work.I don麓t know how to do it .Can you help?

I am also in need of it very much. Please help me out.

If you are still not able to figure out with latest update.
Then try this.

https://github.com/espressif/arduino-esp32/issues/1458#issuecomment-567611934

If you are still not able to figure out with latest update.
Then try this.

#1458 (comment)

Thanks for reply ,but I麓m designing a commercial product with possible future updates so I can麓t depend on older releases of arduino.I want something that I can work in the future .

If you are still not able to figure out with latest update.
Then try this.
#1458 (comment)

Thanks for reply ,but I麓m designing a commercial product with possible future updates so I can麓t depend on older releases of arduino.I want something that I can work in the future .

Yes sir, I am also doing the similar thing please help us out in easiest way.
Thank you

I'm new to ESP32. I used to work with Arduino Bluno Mega, which is a Mega with a BT BLE built in.
I succeed to exchange over BT with Andro茂d App sp茅cialy designed, but never to go trought the Andro茂d BT standard pairing process to exchange with existing Apps. I understand this standard pairing can be done with ESP32 (please confirm my understanding). And by the way, Flashbulbtech, as you are very knowledgeable, do you know if a solution exists for the Bluno board? Thank you in advance.

Hello I am new on ESP32,
I am working with nrf52832 bluetooth modules . I prepared static passkey "123456" on nrf modules I can connect on nrf connect app after I write static passkey .Now I am trying to connect with ESP32 DEV Module .Is there any example Arduino code for ESP32 for pairing my nrf module with passkey? Can you advise me any example?

hello @chegewara ,
I can connect on nrf connect app after I write static passkey with my nrf module but ESP32 looking can not connect with passkey . I tried example that you advised me above, I write nrf52832 service uuid , my nrf ble password and I build successfully after I opened the serial port ,I saw this messages
I am new on ESP32 Coding can you tell me what should I do for connecting with passkey nrf52832?

If I remove the password setting part in the nrf code and then try to connect esp32, I can see that the connection is established immediately. not connecting when I enable encryption in nrf code

BLE Advertised Device found: Name: NoQey, Address: cd:59:6d:14:44:fa, serviceUUID: f3641400-00b0-4240-ba50-05ca45bf8abc
Found our device! address: Forming a connection to cd:59:6d:14:44:fa

  • Created client
  • Connected to server
    Failed to find our service UUID: f3641400-00b0-4240-ba50-05ca45bf8abc
    We have failed to connect to the server; there is nothin more we will do.

You have to add this to your code too to tell nRF you are able to input numbers:

BLESecurity *pSecurity = new BLESecurity();
pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_ONLY);
pSecurity->setCapability(ESP_IO_CAP_KBDISP);

https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLETests/security/SampleServer_authentication_passkey.cpp#L101

BLESecurity *pSecurity = new BLESecurity();
pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_ONLY);
pSecurity->setCapability(ESP_IO_CAP_KBDISP);

@chegewara
this code part that you said me already exist in my arduino code you advised me above. Altough this part exist in code , I can not connect to esp32.
I couldn't understand what the problem was

Hardware:

Board: ESP-WROOM-32
Core Installation/update date: 01/JUN/2018
IDE name: Arduino IDE
Flash Frequency: 80Mhz
Upload Speed: 921600

Description:

I am using the SerialToSerialBT example. I am able to scan and discover the Bluetooth device from my smartphone. But the pairing is without any security/passcode. It simply pairs immediately. How to make the pairing using PIN/Passkey entry?

Sketch:

#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);
  delay(5000);
  Serial.println("Starting..");
  SerialBT.begin("MyESP32"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");

}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  delay(20);
}

Debug Messages:

Starting..
The device started, now you can pair it with bluetooth!

Hi, is there any arduino example on how to pair bluetooth with PIN?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

0x1abin picture 0x1abin  路  3Comments

maxgerhardt picture maxgerhardt  路  3Comments

jhowkis picture jhowkis  路  3Comments

lonerzzz picture lonerzzz  路  3Comments

OAXP picture OAXP  路  4Comments