From f1cf87fec4b33baae6b396e2a7701d6b2eb75914 Mon Sep 17 00:00:00 2001 From: Mark Liversedge Date: Mon, 28 Jan 2013 19:43:00 +0000 Subject: [PATCH] Kickr BTLE connection fixup No need to scan first, connect directly to the previouslt paired device uuid. By no longer scanning we can have multiple threads connecting at once without conflicting with each other. --- src/Kickr.cpp | 27 +-------------------------- src/WFApi.h | 2 +- src/WFApi.mm | 18 ++++++++++++++---- 3 files changed, 16 insertions(+), 31 deletions(-) diff --git a/src/Kickr.cpp b/src/Kickr.cpp index a6c5a757b..f18b6f99e 100644 --- a/src/Kickr.cpp +++ b/src/Kickr.cpp @@ -213,32 +213,7 @@ Kickr::connectKickr() { // get a pool for this thread pool = WFApi::getInstance()->getPool(); - - // do we even have BTLE hardware? - if (WFApi::getInstance()->isBTLEEnabled() == false) { - WFApi::getInstance()->freePool(pool); - return (-1); - } - - // discover first... - if (scanned == false) find(); - - // connect - bool found = false; - WFApi *w = WFApi::getInstance(); - int i; - for (i=0; ideviceCount(); i++) { - if (w->deviceUUID(i) == devConf->portSpec) { - found = true; - break; - } - } - if (found == false) { - WFApi::getInstance()->freePool(pool); - return (-1); - } - - w->connectDevice(i); + WFApi::getInstance()->connectDevice(devConf->portSpec); return 0; } diff --git a/src/WFApi.h b/src/WFApi.h index af1d09457..a369baa2f 100644 --- a/src/WFApi.h +++ b/src/WFApi.h @@ -87,7 +87,7 @@ public: QString deviceUUID(int); // return the UUID for device n // connect and disconnect - bool connectDevice(int n); // connect the device n + bool connectDevice(QString uuid); // connect the device n bool disconnectDevice(); // disconnect // has data? diff --git a/src/WFApi.mm b/src/WFApi.mm index bc87ec378..62d207159 100644 --- a/src/WFApi.mm +++ b/src/WFApi.mm @@ -43,6 +43,13 @@ static QString toQString(const NSString *nsstr) return result; } +static inline NSString* fromQString(const QString &string) +{ + const QByteArray utf8 = string.toUtf8(); + const char* cString = utf8.constData(); + return [[NSString alloc] initWithUTF8String:cString]; +} + // Thi source file contains the private objc interface (WFBridge) that // sits atop the Wahoo Fitness APIs at the top of the source file. // @@ -116,7 +123,7 @@ static QString toQString(const NSString *nsstr) //============================================================================ // connect and disconnect a device --(BOOL)connectDevice: (int)n +-(BOOL)connectDevice: (NSString *)uuid { // it takes far too long! [[WFHardwareConnector sharedConnector] disableFirmwareCheck]; @@ -124,12 +131,15 @@ static QString toQString(const NSString *nsstr) // just in case there is a discovery in action, lets cancel it... [[WFHardwareConnector sharedConnector] cancelDiscoveryOnNetwork:WF_NETWORKTYPE_BTLE]; - WFDeviceParams* dev = (WFDeviceParams*)[discoveredSensors objectAtIndex:n]; + WFDeviceParams* dev = [[WFDeviceParams alloc] init]; + dev.deviceUUIDString = uuid; + dev.networkType = WF_NETWORKTYPE_BTLE; + WFConnectionParams* params = [[WFConnectionParams alloc] init]; params.sensorType = WF_SENSORTYPE_BIKE_POWER; params.networkType = WF_NETWORKTYPE_BTLE; params.sensorSubType = WF_SENSOR_SUBTYPE_BIKE_POWER_KICKR; - params.searchTimeout = 15; + params.searchTimeout = 5; params.device1 = dev; // request the sensor connection. @@ -265,7 +275,7 @@ QString WFApi::deviceUUID(int n) int WFApi::connectionStatus() { return [wf connectionStatus]; } bool WFApi::isConnected() { return [wf isConnected]; } bool WFApi::hasData() { return [wf hasData]; } -bool WFApi::connectDevice(int n) { return [wf connectDevice:n]; } +bool WFApi::connectDevice(QString uuid) { return [wf connectDevice: fromQString(uuid)]; } bool WFApi::disconnectDevice() { return [wf disconnectDevice]; } int WFApi::deviceCount() { return [wf deviceCount]; }