platformio / platform-espressif32

Espressif 32: development platform for PlatformIO
https://registry.platformio.org/platforms/platformio/espressif32
Apache License 2.0
925 stars 624 forks source link

Help!! ESP32-S3 BLE and Windows11 BLE(Realtek Bluetooth5.0 USB Adapter) pairing Error #894

Closed Tanami21 closed 2 years ago

Tanami21 commented 2 years ago

We are using ESP32-S3-DevkitC-1 to develop BLE devices. It worked fine when connecting with the Nordic nRfConnect app on my Android phone (Galaxy S20).

However, when I replace the Android phone with Windows 11 and pair it, an error occurs.

ESP32 Error message   E (11296) BT_SMP: smp_calculate_link_key_from_long_term_key failed to update link_key. Sec Mode = 2, sm4 = 0x00   E (11296) BT_SMP: smp_derive_link_key_from_long_term_key failed   E (11302) BT_BTM: btm_proc_smp_cback received for unknown device   E (11413) BT_BTM: Device not found

Please help me about this probrem !!

[Our development environment] VSCode + PlatformIO

[platformio.ini] [env:esp32-s3-devkitc-1] platform = espressif32 board = esp32-s3-devkitc-1 framework = arduino

[main.cpp]

#include <Arduino.h>
#include "BleCtrl.h"
// BLE Control class
BleCtrl mBle;

void setup() {
    Serial.begin(115200);
    mBle.Init( );
    mBle.Start( );
    Serial.println( "### setup ##" );
}
void loop( ) {
    mBle.Interval( );
}

[BleCtrl.h]

#ifndef __BLECTRL_H__
#define __BLECTRL_H__
#include <Arduino.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLE2902.h>
// --------------------------------------------------------------------------
#define SERVICE_UUID            "9be2a470-19e1-11ed-861d-0242ac120002"
#define CHARACTERISTIC_UUID     "bc4a7558-19e1-11ed-861d-0242ac120002"

class BleCtrl {

private:
    BLEServer   *pServer;
    BLEService  *pService;
    BLEAdvertising *pAdvertising;
    BLECharacteristic   *pCharacteristic;

public:
    BleCtrl( );
    ~BleCtrl( );
    void    Init( void );
    void    Start( void );
    void    Interval( void );
};
#endif //__BLECTRL_H__

[BleCtrl.cpp : Excerpts from the required sections only]

#include "BleCtrl.h"
//    BLE callback class
static bool  deviceConnected = false, deviceConnectedSave = false;
class ServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServ) {
        deviceConnected = true;
        Serial.println( "##=> BLE Callback on Connected ##" );
    };
    void onDisconnect(BLEServer* pServ) {
        deviceConnected = false;
        Serial.println( "##=> BLE Callback on disConnected ##" );
    }
};
static ServerCallbacks  mServerCallbacks;
//    BLE Characteristic Callback class
class CharacteristicCallbacks : public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic * pCharacteristic )
    {
        size_t size = pCharacteristic->getLength( );
        uint8_t *pDat = pCharacteristic->getData( );
        Serial.printf( "##=> BLE Callback onWrite ## --> %d, %x, %x", size, pDat[ 0 ] , pDat[ 1 ] );
    }
    void onRead(BLECharacteristic * pCharacteristic )
    {
        pCharacteristic->setValue( ( uint8_t * )"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", 128 );
        Serial.println( "##=> BLE Callback onRead ##" );
    }
};
static CharacteristicCallbacks mCharacteristicCallbacks;

BleCtrl::BleCtrl( )
{

}
BleCtrl::~BleCtrl( )
{

}
void    BleCtrl::Init( void )
{

}
void    BleCtrl::Interval( void )
{
    if(( deviceConnected == true ) && ( deviceConnectedSave == false ))
    {
        deviceConnectedSave = true;
        Serial.println( "##=> BLE Connected ##" );
    }
    if(( deviceConnected == false ) && ( deviceConnectedSave == true ))
    {
        pServer->startAdvertising( );
        deviceConnectedSave = false;
        Serial.println( "##=> BLE DisConnected ##" );
    }
}
//    Start
void    BleCtrl::Start( void )
{
    BLEDevice::init( "TestBleDevice" );
    pServer = BLEDevice::createServer( );
    pServer->setCallbacks( &mServerCallbacks );
    pService = pServer->createService( SERVICE_UUID );
    pCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID,
                                BLECharacteristic::PROPERTY_WRITE |
                                BLECharacteristic::PROPERTY_READ );
    pCharacteristic->setCallbacks( &mCharacteristicCallbacks );
    pCharacteristic->addDescriptor(new BLE2902());
    pService->start( );

    // Advertising Setting
    pAdvertising = BLEDevice::getAdvertising( );
    pAdvertising->addServiceUUID( SERVICE_UUID );
    pAdvertising->setScanResponse( true );
    pAdvertising->setMinPreferred( 0x06 );
    pAdvertising->setMinPreferred( 0x12 );

    // Advertising interval -- about 100mS
    pAdvertising->setMinInterval( 145 );
    pAdvertising->setMaxInterval( 155 );
    BLEDevice::startAdvertising( );
}

[Controlled by nRfConnect app on Android (works OK) ]

### setup ##
##=> BLE Callback on Connected ## <== Connect from Android nRFConnect App
##=> BLE Connected ##
##=> BLE Callback onRead ## <== Read Opearation from Android nRFConnect App
##=> BLE Callback onWrite ## <== Write Opearation from Android nRFConnect App
##=> BLE Callback on disConnected ## <== Disconnect from Android nRFConnect App
##=> BLE DisConnected ##

[Controlled by Windows11 (not work) ]

setup

##=> BLE Callback on Connected ##
##=> BLE Connected ##
E (11296) BT_SMP: smp_calculate_link_key_from_long_term_key failed to update link_key. Sec Mode = 2, sm4 = 0x00
E (11296) BT_SMP: smp_derive_link_key_from_long_term_key failed
E (11302) BT_BTM: btm_proc_smp_cback received for unknown device
E (11413) BT_BTM: Device not found
##=> BLE Callback on disConnected ##
##=> BLE DisConnected ##

Windows11 operation is   Windows->SYSTEM-> Bluetooth and Devices-> Add Device   Buetooth mouse keyboard, pen, audio device, controller, etc.   Select TestBLeDeveice -> Paired

Although paired in Windows, but ESP32 is disconnected, so communication with Windows will not work afterwards, of course.

valeros commented 2 years ago

Hi @Tanami21! This repository is not the right place for such questions. Please forward to https://community.platformio.org/ or https://github.com/espressif/arduino-esp32