/* * BLEScan.cpp * * Created on: Jul 1, 2017 * Author: kolban */ #include "sdkconfig.h" #if defined(CONFIG_BT_ENABLED) #include #include #include #include "BLEAdvertisedDevice.h" #include "BLEScan.h" #include "BLEUtils.h" #include "GeneralUtils.h" static const char* LOG_TAG = "BLEScan"; BLEScan::BLEScan() { m_scan_params.scan_type = BLE_SCAN_TYPE_PASSIVE; // Default is a passive scan. m_scan_params.own_addr_type = BLE_ADDR_TYPE_PUBLIC; m_scan_params.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL; setInterval(100); setWindow(100); m_pAdvertisedDeviceCallbacks = nullptr; m_stopped = true; } // BLEScan /** * @brief Clear the history of previously detected advertised devices. * @return N/A */ /* void BLEScan::clearAdvertisedDevices() { for (int i=0; iscan_rst.search_evt) { case ESP_GAP_SEARCH_INQ_CMPL_EVT: { m_stopped = true; m_semaphoreScanEnd.give(); break; } // ESP_GAP_SEARCH_INQ_CMPL_EVT case ESP_GAP_SEARCH_INQ_RES_EVT: { if (m_stopped) { // If we are not scanning, nothing to do with the extra results. break; } // Examine our list of previously scanned addresses and, if we found this one already, // ignore it. BLEAddress advertisedAddress(param->scan_rst.bda); bool found = false; /* for (int i=0; igetAddress().equals(advertisedAddress)) { found = true; break; } } */ for (int i=0; iscan_rst.rssi); advertisedDevice.setAdFlag(param->scan_rst.flag); advertisedDevice.parseAdvertisement((uint8_t*)param->scan_rst.ble_adv); advertisedDevice.setScan(this); //m_vectorAvdertisedDevices.push_back(pAdvertisedDevice); if (m_pAdvertisedDeviceCallbacks) { m_pAdvertisedDeviceCallbacks->onResult(advertisedDevice); } m_scanResults.m_vectorAdvertisedDevices.push_back(advertisedDevice); break; } // ESP_GAP_SEARCH_INQ_RES_EVT default: { break; } } // switch - search_evt break; } // ESP_GAP_BLE_SCAN_RESULT_EVT default: { break; } // default } // End switch } // gapEventHandler /* void BLEScan::onResults() { ESP_LOGD(LOG_TAG, ">> onResults: default"); ESP_LOGD(LOG_TAG, "<< onResults"); } // onResults */ /** * @brief Should we perform an active or passive scan? * The default is a passive scan. An active scan means that we will wish a scan response. * @param [in] active If true, we perform an active scan otherwise a passive scan. * @return N/A. */ void BLEScan::setActiveScan(bool active) { if (active) { m_scan_params.scan_type = BLE_SCAN_TYPE_ACTIVE; } else { m_scan_params.scan_type = BLE_SCAN_TYPE_PASSIVE; } } // setActiveScan /** * @brief Set the call backs to be invoked. * @param [in] pAdvertisedDeviceCallbacks Call backs to be invoked. */ void BLEScan::setAdvertisedDeviceCallbacks(BLEAdvertisedDeviceCallbacks* pAdvertisedDeviceCallbacks) { m_pAdvertisedDeviceCallbacks = pAdvertisedDeviceCallbacks; } // setAdvertisedDeviceCallbacks /** * @brief Set the interval to scan. * @param [in] The interval in msecs. */ void BLEScan::setInterval(uint16_t intervalMSecs) { m_scan_params.scan_interval = intervalMSecs / 0.625; } // setInterval /** * @brief Set the window to actively scan. * @param [in] windowMSecs How long to actively scan. */ void BLEScan::setWindow(uint16_t windowMSecs) { m_scan_params.scan_window = windowMSecs / 0.625; } // setWindow /** * @brief Start scanning. * @param [in] duration The duration in seconds for which to scan. * @return N/A. */ BLEScanResults BLEScan::start(uint32_t duration) { ESP_LOGD(LOG_TAG, ">> start(%d)", duration); m_semaphoreScanEnd.take("start"); m_scanResults.m_vectorAdvertisedDevices.clear(); esp_err_t errRc = ::esp_ble_gap_set_scan_params(&m_scan_params); if (errRc != ESP_OK) { ESP_LOGE(LOG_TAG, "esp_ble_gap_set_scan_params: err: %d, text: %s", errRc, GeneralUtils::errorToString(errRc)); m_semaphoreScanEnd.give(); return m_scanResults; } errRc = ::esp_ble_gap_start_scanning(duration); if (errRc != ESP_OK) { ESP_LOGE(LOG_TAG, "esp_ble_gap_start_scanning: err: %d, text: %s", errRc, GeneralUtils::errorToString(errRc)); m_semaphoreScanEnd.give(); return m_scanResults; } m_stopped = false; m_semaphoreScanEnd.take("start"); m_semaphoreScanEnd.give(); ESP_LOGD(LOG_TAG, "<< start()"); return m_scanResults; } // start /** * @brief Stop an in progress scan. * @return N/A. */ void BLEScan::stop() { ESP_LOGD(LOG_TAG, ">> stop()"); esp_err_t errRc = ::esp_ble_gap_stop_scanning(); m_stopped = true; if (errRc != ESP_OK) { ESP_LOGE(LOG_TAG, "esp_ble_gap_stop_scanning: err: %d, text: %s", errRc, GeneralUtils::errorToString(errRc)); return; } m_semaphoreScanEnd.give(); ESP_LOGD(LOG_TAG, "<< stop()"); } // stop /** * @brief Return the count of devices found in the last scan. * @return The number of devices found in the last scan. */ int BLEScanResults::getCount() { return m_vectorAdvertisedDevices.size(); } // getCount /** * @brief Return the specified device at the given index. * The index should be between 0 and getCount()-1. * @param [in] i The index of the device. * @return The device at the specified index. */ BLEAdvertisedDevice BLEScanResults::getDevice(uint32_t i) { return m_vectorAdvertisedDevices.at(i); } #endif /* CONFIG_BT_ENABLED */