nkolban / esp32-snippets

Sample ESP32 snippets and code fragments
https://leanpub.com/kolban-ESP32
Apache License 2.0
2.36k stars 711 forks source link

arduino BLE scan for specific beacon and count the No of packets received #460

Open megazero1316 opened 6 years ago

megazero1316 commented 6 years ago

hello i want to ask if there`s a way to make the BLEScan example scan for only specific beacon based on UUID and make it count how many time it receive a packet from it

thanks

megazero1316 commented 6 years ago

i already managed to solve the problem referring to this video on YouTube : https://www.youtube.com/watch?v=HqX3pZm6dd8 github code : https://github.com/pcbreflux/espressif/blob/master/esp32/arduino/sketchbook/ESP32_BLE_beaconscan/ESP32_BLE_beaconscan.ino

and changing pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); to pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks(),true);

but there's a problem. i found that when i use oBeacon.getProximityUUID() it gives me wrong uuid or in another word the uuid i flipped

my beacon uuid is

01122334-4556-6778-899a-abbccddeeff0

the results from esp32 :

UUID: f0efdecd-bcab-9a89-7867-564534231201

and the problem is coming from BLEUUID.cpp starting from line 390:

ss << std::hex << std::setfill('0') <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[15] <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[14] <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[13] <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[12] << "-" <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[11] <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[10] << "-" <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[9]  <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[8]  << "-" <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[7]  <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[6]  << "-" <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[5]  <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[4]  <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[3]  <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[2]  <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[1]  <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[0];

and the fixing is :

// ss << std::hex << std::setfill('0') <<
    //  std::setw(2) << (int)m_uuid.uuid.uuid128[15] <<
    //  std::setw(2) << (int)m_uuid.uuid.uuid128[14] <<
    //  std::setw(2) << (int)m_uuid.uuid.uuid128[13] <<
    //  std::setw(2) << (int)m_uuid.uuid.uuid128[12] << "-" <<
    //  std::setw(2) << (int)m_uuid.uuid.uuid128[11] <<
    //  std::setw(2) << (int)m_uuid.uuid.uuid128[10] << "-" <<
    //  std::setw(2) << (int)m_uuid.uuid.uuid128[9]  <<
    //  std::setw(2) << (int)m_uuid.uuid.uuid128[8]  << "-" <<
    //  std::setw(2) << (int)m_uuid.uuid.uuid128[7]  <<
    //  std::setw(2) << (int)m_uuid.uuid.uuid128[6]  << "-" <<
    //  std::setw(2) << (int)m_uuid.uuid.uuid128[5]  <<
    //  std::setw(2) << (int)m_uuid.uuid.uuid128[4]  <<
    //  std::setw(2) << (int)m_uuid.uuid.uuid128[3]  <<
    //  std::setw(2) << (int)m_uuid.uuid.uuid128[2]  <<
    //  std::setw(2) << (int)m_uuid.uuid.uuid128[1]  <<
    //  std::setw(2) << (int)m_uuid.uuid.uuid128[0];
    ss << std::hex << std::setfill('0') <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[0] <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[1] <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[2] <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[3] << "-" <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[4] <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[5] << "-" <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[6]  <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[7]  << "-" <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[8]  <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[9]  << "-" <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[10]  <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[11]  <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[12]  <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[13]  <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[14]  <<
        std::setw(2) << (int)m_uuid.uuid.uuid128[15];

and now i can get the correct results