From 94ee66acd7897a4d756d87e328ca15498dab21fd Mon Sep 17 00:00:00 2001 From: Cole Brinsfield Date: Mon, 1 Feb 2021 19:10:38 -0800 Subject: [PATCH] try connect to last bluetooth device (#20) * Working on cleaning wifi * Reworked bluetooth handshake based around @presslab-us's work * Forgot to remove a line * QOL: try to reconnect to last phone BT on openauto start * bt autoconnect raspberry pi workaround * Try to connect to last successful bluetooth device * Bad merge fix * pointers * typo * Update SocketInfoResponse.proto * Added a config option to autoconnect to last bt device (defaults off) --- btservice/AndroidBluetoothServer.cpp | 7 +++++ btservice/btservice.cpp | 31 +++++++++++++++++++ btservice_proto/SocketInfoResponse.proto | 1 + include/btservice/btservice.hpp | 4 ++- .../openauto/Configuration/Configuration.hpp | 8 +++++ .../openauto/Configuration/IConfiguration.hpp | 4 +++ openauto/Configuration/Configuration.cpp | 26 ++++++++++++++++ 7 files changed, 80 insertions(+), 1 deletion(-) diff --git a/btservice/AndroidBluetoothServer.cpp b/btservice/AndroidBluetoothServer.cpp index c172815..5ee99a3 100644 --- a/btservice/AndroidBluetoothServer.cpp +++ b/btservice/AndroidBluetoothServer.cpp @@ -121,6 +121,13 @@ void AndroidBluetoothServer::handleSocketInfoRequestResponse(QByteArray data) btservice::proto::SocketInfoResponse socketInfoResponse; socketInfoResponse.ParseFromArray(data, data.size()); OPENAUTO_LOG(info) <<"[AndroidBluetoothServer] Received SocketInfoRequestResponse, status: "<setLastBluetoothPair(socket_->peerAddress().toString().toStdString()); + config_->save(); + } } diff --git a/btservice/btservice.cpp b/btservice/btservice.cpp index 6c14b28..b3e7123 100644 --- a/btservice/btservice.cpp +++ b/btservice/btservice.cpp @@ -37,7 +37,38 @@ btservice::btservice(openauto::configuration::IConfiguration::Pointer config) { OPENAUTO_LOG(info) << "[btservice] Service registered, port: " << cServicePortNumber; } + if(config->getAutoconnectBluetooth()) + connectToBluetooth(QBluetoothAddress(QString::fromStdString(config->getLastBluetoothPair())), address); } +void btservice::connectToBluetooth(QBluetoothAddress addr, QBluetoothAddress controller) +{ + // The raspberry pi has a really tough time using bluetoothctl (or really anything) to connect to an Android phone + // even though phone connecting to the pi is fine. + // I found a workaround where you can make the pi attempt an rfcomm connection to the phone, and it connects immediately + // This might require setting u+s on rfcomm though + // Other computers with more sane bluetooth shouldn't have an issue using bluetoothctl + +#ifdef RPI + // tries to open an rfcomm serial on channel 2 + // channel doesn't really matter here, 2 is just "somewhat standard" + QString program = QString::fromStdString("sudo stdbuf -oL rfcomm connect hci0 ")+addr.toString()+QString::fromStdString(" 2"); + btConnectProcess = new QProcess(); + OPENAUTO_LOG(info)<<"[btservice] Attempting to connect to last bluetooth device, "<start(program, QProcess::Unbuffered | QProcess::ReadWrite); +#else + btConnectProcess = new QProcess(); + btConnectProcess->setProcessChannelMode(QProcess::SeparateChannels); + OPENAUTO_LOG(info)<<"[btservice] Attempting to connect to last bluetooth device, "<start("bluetoothctl"); + btConnectProcess->waitForStarted(); + btConnectProcess->write(QString("select %1\n").arg(controller.toString()).toUtf8()); + btConnectProcess->write(QString("connect %1\n").arg(addr.toString()).toUtf8()); + btConnectProcess->closeWriteChannel(); + btConnectProcess->waitForFinished(); +#endif +} + + } } diff --git a/btservice_proto/SocketInfoResponse.proto b/btservice_proto/SocketInfoResponse.proto index 252d0fb..90b1391 100644 --- a/btservice_proto/SocketInfoResponse.proto +++ b/btservice_proto/SocketInfoResponse.proto @@ -15,6 +15,7 @@ enum Status { STATUS_NO_SUPPORTED_WIFI_CHANNELS = -8; STATUS_INSTRUCT_USER_TO_CHECK_THE_PHONE = -9; STATUS_PHONE_WIFI_DISABLED = -10; + STATUS_WIFI_NETWORK_UNAVAILABLE = -11; } message SocketInfoResponse diff --git a/include/btservice/btservice.hpp b/include/btservice/btservice.hpp index 7bc86e8..4fe91a2 100644 --- a/include/btservice/btservice.hpp +++ b/include/btservice/btservice.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include "OpenautoLog.hpp" #include "btservice/AndroidBluetoothService.hpp" @@ -18,9 +19,10 @@ public: private: const uint16_t cServicePortNumber = 22; - + void connectToBluetooth(QBluetoothAddress addr, QBluetoothAddress controller); openauto::btservice::AndroidBluetoothService androidBluetoothService_; openauto::btservice::AndroidBluetoothServer androidBluetoothServer_; + QProcess *btConnectProcess; }; } diff --git a/include/openauto/Configuration/Configuration.hpp b/include/openauto/Configuration/Configuration.hpp index 59b9299..6f92f73 100644 --- a/include/openauto/Configuration/Configuration.hpp +++ b/include/openauto/Configuration/Configuration.hpp @@ -74,6 +74,10 @@ public: void setWifiPassword(std::string value) override; std::string getWifiMAC() override; void setWifiMAC(std::string value) override; + bool getAutoconnectBluetooth() override; + void setAutoconnectBluetooth(bool value) override; + std::string getLastBluetoothPair() override; + void setLastBluetoothPair(std::string value) override; private: void readButtonCodes(boost::property_tree::ptree& iniConfig); @@ -97,6 +101,8 @@ private: std::string wifiSSID_; std::string wifiPassword_; std::string wifiMAC_; + bool autoconnectBluetooth_; + std::string lastBluetoothPair_; static const std::string cConfigFileName; @@ -138,6 +144,8 @@ private: static const std::string cWifiSSID; static const std::string cWifiPskey; static const std::string cWifiMAC; + static const std::string cAutoconnectBluetooth; + static const std::string cLastBluetoothPair; }; } diff --git a/include/openauto/Configuration/IConfiguration.hpp b/include/openauto/Configuration/IConfiguration.hpp index 413e9aa..68d9b0b 100644 --- a/include/openauto/Configuration/IConfiguration.hpp +++ b/include/openauto/Configuration/IConfiguration.hpp @@ -83,6 +83,10 @@ public: virtual void setWifiPassword(std::string value) = 0; virtual std::string getWifiMAC() = 0; virtual void setWifiMAC(std::string value) = 0; + virtual bool getAutoconnectBluetooth() = 0; + virtual void setAutoconnectBluetooth(bool value) = 0; + virtual std::string getLastBluetoothPair() = 0; + virtual void setLastBluetoothPair(std::string value) = 0; }; } diff --git a/openauto/Configuration/Configuration.cpp b/openauto/Configuration/Configuration.cpp index d34db28..bf53f44 100644 --- a/openauto/Configuration/Configuration.cpp +++ b/openauto/Configuration/Configuration.cpp @@ -64,6 +64,8 @@ const std::string Configuration::cInputEnterButtonKey = "Input.EnterButton"; const std::string Configuration::cWifiSSID = "WiFi.SSID"; const std::string Configuration::cWifiPskey = "WiFi.Password"; const std::string Configuration::cWifiMAC = "WiFi.AdapterMAC"; +const std::string Configuration::cAutoconnectBluetooth = "WiFi.AutoconnectLastBluetoothDevice"; +const std::string Configuration::cLastBluetoothPair = "WiFi.LastBluetoothPair"; Configuration::Configuration() { @@ -107,6 +109,8 @@ void Configuration::load() wifiSSID_ = iniConfig.get(cWifiSSID, ""); wifiPassword_ = iniConfig.get(cWifiPskey, ""); wifiMAC_ = iniConfig.get(cWifiMAC, ""); + autoconnectBluetooth_ = iniConfig.get(cAutoconnectBluetooth, false); + lastBluetoothPair_ = iniConfig.get(cLastBluetoothPair, ""); } catch(const boost::property_tree::ini_parser_error& e) { @@ -161,6 +165,8 @@ void Configuration::save() iniConfig.put(cWifiSSID, wifiSSID_); iniConfig.put(cWifiPskey, wifiPassword_); iniConfig.put(cWifiMAC, wifiMAC_); + iniConfig.put(cAutoconnectBluetooth, autoconnectBluetooth_); + iniConfig.put(cLastBluetoothPair, lastBluetoothPair_); boost::property_tree::ini_parser::write_ini(cConfigFileName, iniConfig); } @@ -334,6 +340,26 @@ void Configuration::setWifiMAC(std::string value) wifiMAC_ = value; } +bool Configuration::getAutoconnectBluetooth() +{ + return autoconnectBluetooth_; +} + +void Configuration::setAutoconnectBluetooth(bool value) +{ + autoconnectBluetooth_ = value; +} + +std::string Configuration::getLastBluetoothPair() +{ + return lastBluetoothPair_; +} + +void Configuration::setLastBluetoothPair(std::string value) +{ + lastBluetoothPair_ = value; +} + void Configuration::readButtonCodes(boost::property_tree::ptree& iniConfig) { this->insertButtonCode(iniConfig, cInputPlayButtonKey, aasdk::proto::enums::ButtonCode::PLAY);