From bee91cabe5c3f53f351d129542229334c321100a Mon Sep 17 00:00:00 2001 From: Rohit Grover Date: Mon, 18 May 2015 09:10:22 +0100 Subject: [PATCH] enable context for startScan. --- public/BLEDevice.h | 18 ++++++++++++++++++ public/Gap.h | 26 +++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/public/BLEDevice.h b/public/BLEDevice.h index ffcdd5e..c441f1f 100644 --- a/public/BLEDevice.h +++ b/public/BLEDevice.h @@ -271,6 +271,18 @@ public: */ ble_error_t startScan(void (*callback)(const Gap::AdvertisementCallbackParams_t *params)); + /** + * Start scanning (Observer Procedure) based on the scan-params currently + * in effect. + * + * @param[in] object + * @param[in] callbackMember + * The above pair of parameters define the callback object + * and member function to receive the advertisement params. + */ + template + ble_error_t startScan(T *object, void (T::*memberCallback)(const Gap::AdvertisementCallbackParams_t *params)); + /** * Stop scanning. The current scanning parameters remain in effect. * @@ -787,6 +799,12 @@ BLEDevice::startScan(void (*callback)(const Gap::AdvertisementCallbackParams_t * return transport->getGap().startScan(scanningParams, callback); } +template +inline ble_error_t +BLEDevice::startScan(T *object, void (T::*memberCallback)(const Gap::AdvertisementCallbackParams_t *params)) { + return transport->getGap().startScan(scanningParams, object, memberCallback); +} + inline ble_error_t BLEDevice::stopScan(void) { return transport->getGap().stopScan(); diff --git a/public/Gap.h b/public/Gap.h index 6bc19e3..e56e20f 100644 --- a/public/Gap.h +++ b/public/Gap.h @@ -172,7 +172,6 @@ private: virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &) = 0; virtual ble_error_t startAdvertising(const GapAdvertisingParams &) = 0; virtual ble_error_t stopAdvertising(void) = 0; - virtual ble_error_t startScan(const GapScanningParams &scanningParams, void (*callback)(const AdvertisementCallbackParams_t *params)) = 0; virtual ble_error_t stopScan() = 0; virtual uint16_t getMinAdvertisingInterval(void) const = 0; virtual uint16_t getMinNonConnectableAdvertisingInterval(void) const = 0; @@ -190,7 +189,32 @@ private: virtual ble_error_t setAppearance(uint16_t appearance) = 0; virtual ble_error_t getAppearance(uint16_t *appearanceP) = 0; + ble_error_t startScan(const GapScanningParams &scanningParams, void (*callback)(const AdvertisementCallbackParams_t *params)) { + ble_error_t err = BLE_ERROR_NONE; + if (callback) { + if ((err = startRadioScan(scanningParams)) == BLE_ERROR_NONE) { + onAdvertisementReport.attach(callback); + } + } + + return err; + } + + template + ble_error_t startScan(const GapScanningParams &scanningParams, T *object, void (T::*callbackMember)(const AdvertisementCallbackParams_t *params)) { + ble_error_t err = BLE_ERROR_NONE; + if (object && callbackMember) { + if ((err = startRadioScan(scanningParams)) == BLE_ERROR_NONE) { + onAdvertisementReport.attach(object, callbackMember); + } + } + + return err; + } + protected: + virtual ble_error_t startRadioScan(const GapScanningParams &scanningParams) = 0; + /* Event callback handlers */ void setOnTimeout(EventCallback_t callback) {onTimeout = callback;} void setOnConnection(ConnectionEventCallback_t callback) {onConnection = callback;}