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)
This commit is contained in:
Cole Brinsfield 2021-02-01 19:10:38 -08:00 committed by GitHub
parent 5554052d84
commit 94ee66acd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 80 additions and 1 deletions

View File

@ -121,6 +121,13 @@ void AndroidBluetoothServer::handleSocketInfoRequestResponse(QByteArray data)
btservice::proto::SocketInfoResponse socketInfoResponse; btservice::proto::SocketInfoResponse socketInfoResponse;
socketInfoResponse.ParseFromArray(data, data.size()); socketInfoResponse.ParseFromArray(data, data.size());
OPENAUTO_LOG(info) <<"[AndroidBluetoothServer] Received SocketInfoRequestResponse, status: "<<socketInfoResponse.status(); OPENAUTO_LOG(info) <<"[AndroidBluetoothServer] Received SocketInfoRequestResponse, status: "<<socketInfoResponse.status();
if(socketInfoResponse.status() == 0)
{
// A status of 0 should be successful handshake (unless phone later reports an error, aw well)
// save this phone so we can autoconnect to it next time
config_->setLastBluetoothPair(socket_->peerAddress().toString().toStdString());
config_->save();
}
} }

View File

@ -37,7 +37,38 @@ btservice::btservice(openauto::configuration::IConfiguration::Pointer config)
{ {
OPENAUTO_LOG(info) << "[btservice] Service registered, port: " << cServicePortNumber; 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, "<<addr.toString().toStdString()<<" with `"<<program.toStdString();
btConnectProcess->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, "<<addr.toString().toStdString()<<" with bluetoothctl";
btConnectProcess->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
}
} }
} }

View File

@ -15,6 +15,7 @@ enum Status {
STATUS_NO_SUPPORTED_WIFI_CHANNELS = -8; STATUS_NO_SUPPORTED_WIFI_CHANNELS = -8;
STATUS_INSTRUCT_USER_TO_CHECK_THE_PHONE = -9; STATUS_INSTRUCT_USER_TO_CHECK_THE_PHONE = -9;
STATUS_PHONE_WIFI_DISABLED = -10; STATUS_PHONE_WIFI_DISABLED = -10;
STATUS_WIFI_NETWORK_UNAVAILABLE = -11;
} }
message SocketInfoResponse message SocketInfoResponse

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <QProcess>
#include <QBluetoothAddress> #include <QBluetoothAddress>
#include "OpenautoLog.hpp" #include "OpenautoLog.hpp"
#include "btservice/AndroidBluetoothService.hpp" #include "btservice/AndroidBluetoothService.hpp"
@ -18,9 +19,10 @@ public:
private: private:
const uint16_t cServicePortNumber = 22; const uint16_t cServicePortNumber = 22;
void connectToBluetooth(QBluetoothAddress addr, QBluetoothAddress controller);
openauto::btservice::AndroidBluetoothService androidBluetoothService_; openauto::btservice::AndroidBluetoothService androidBluetoothService_;
openauto::btservice::AndroidBluetoothServer androidBluetoothServer_; openauto::btservice::AndroidBluetoothServer androidBluetoothServer_;
QProcess *btConnectProcess;
}; };
} }

View File

@ -74,6 +74,10 @@ public:
void setWifiPassword(std::string value) override; void setWifiPassword(std::string value) override;
std::string getWifiMAC() override; std::string getWifiMAC() override;
void setWifiMAC(std::string value) 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: private:
void readButtonCodes(boost::property_tree::ptree& iniConfig); void readButtonCodes(boost::property_tree::ptree& iniConfig);
@ -97,6 +101,8 @@ private:
std::string wifiSSID_; std::string wifiSSID_;
std::string wifiPassword_; std::string wifiPassword_;
std::string wifiMAC_; std::string wifiMAC_;
bool autoconnectBluetooth_;
std::string lastBluetoothPair_;
static const std::string cConfigFileName; static const std::string cConfigFileName;
@ -138,6 +144,8 @@ private:
static const std::string cWifiSSID; static const std::string cWifiSSID;
static const std::string cWifiPskey; static const std::string cWifiPskey;
static const std::string cWifiMAC; static const std::string cWifiMAC;
static const std::string cAutoconnectBluetooth;
static const std::string cLastBluetoothPair;
}; };
} }

View File

@ -83,6 +83,10 @@ public:
virtual void setWifiPassword(std::string value) = 0; virtual void setWifiPassword(std::string value) = 0;
virtual std::string getWifiMAC() = 0; virtual std::string getWifiMAC() = 0;
virtual void setWifiMAC(std::string value) = 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;
}; };
} }

View File

@ -64,6 +64,8 @@ const std::string Configuration::cInputEnterButtonKey = "Input.EnterButton";
const std::string Configuration::cWifiSSID = "WiFi.SSID"; const std::string Configuration::cWifiSSID = "WiFi.SSID";
const std::string Configuration::cWifiPskey = "WiFi.Password"; const std::string Configuration::cWifiPskey = "WiFi.Password";
const std::string Configuration::cWifiMAC = "WiFi.AdapterMAC"; const std::string Configuration::cWifiMAC = "WiFi.AdapterMAC";
const std::string Configuration::cAutoconnectBluetooth = "WiFi.AutoconnectLastBluetoothDevice";
const std::string Configuration::cLastBluetoothPair = "WiFi.LastBluetoothPair";
Configuration::Configuration() Configuration::Configuration()
{ {
@ -107,6 +109,8 @@ void Configuration::load()
wifiSSID_ = iniConfig.get<std::string>(cWifiSSID, ""); wifiSSID_ = iniConfig.get<std::string>(cWifiSSID, "");
wifiPassword_ = iniConfig.get<std::string>(cWifiPskey, ""); wifiPassword_ = iniConfig.get<std::string>(cWifiPskey, "");
wifiMAC_ = iniConfig.get<std::string>(cWifiMAC, ""); wifiMAC_ = iniConfig.get<std::string>(cWifiMAC, "");
autoconnectBluetooth_ = iniConfig.get<bool>(cAutoconnectBluetooth, false);
lastBluetoothPair_ = iniConfig.get<std::string>(cLastBluetoothPair, "");
} }
catch(const boost::property_tree::ini_parser_error& e) catch(const boost::property_tree::ini_parser_error& e)
{ {
@ -161,6 +165,8 @@ void Configuration::save()
iniConfig.put<std::string>(cWifiSSID, wifiSSID_); iniConfig.put<std::string>(cWifiSSID, wifiSSID_);
iniConfig.put<std::string>(cWifiPskey, wifiPassword_); iniConfig.put<std::string>(cWifiPskey, wifiPassword_);
iniConfig.put<std::string>(cWifiMAC, wifiMAC_); iniConfig.put<std::string>(cWifiMAC, wifiMAC_);
iniConfig.put<bool>(cAutoconnectBluetooth, autoconnectBluetooth_);
iniConfig.put<std::string>(cLastBluetoothPair, lastBluetoothPair_);
boost::property_tree::ini_parser::write_ini(cConfigFileName, iniConfig); boost::property_tree::ini_parser::write_ini(cConfigFileName, iniConfig);
} }
@ -334,6 +340,26 @@ void Configuration::setWifiMAC(std::string value)
wifiMAC_ = 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) void Configuration::readButtonCodes(boost::property_tree::ptree& iniConfig)
{ {
this->insertButtonCode(iniConfig, cInputPlayButtonKey, aasdk::proto::enums::ButtonCode::PLAY); this->insertButtonCode(iniConfig, cInputPlayButtonKey, aasdk::proto::enums::ButtonCode::PLAY);