3
3
#include " common.h"
4
4
#include " bluetoothHandler.h"
5
5
#include " bluetoothProfiles.h"
6
+ #include " bluetoothAdvertisement.h"
6
7
7
8
static constexpr const char * ADAPTER_ALIAS = " AA Wireless Dongle" ;
9
+ static constexpr const char * ADAPTER_ALIAS_DONGLE = " AndroidAuto-Dongle" ;
8
10
9
11
static constexpr const char * BLUEZ_BUS_NAME = " org.bluez" ;
10
12
static constexpr const char * BLUEZ_ROOT_OBJECT_PATH = " /" ;
11
13
static constexpr const char * BLUEZ_OBJECT_PATH = " /org/bluez" ;
12
14
13
15
static constexpr const char * INTERFACE_BLUEZ_ADAPTER = " org.bluez.Adapter1" ;
16
+ static constexpr const char * INTERFACE_BLUEZ_LE_ADVERTISING_MANAGER = " org.bluez.LEAdvertisingManager1" ;
17
+
14
18
static constexpr const char * INTERFACE_BLUEZ_DEVICE = " org.bluez.Device1" ;
15
19
static constexpr const char * INTERFACE_BLUEZ_PROFILE_MANAGER = " org.bluez.ProfileManager1" ;
16
20
21
+ static constexpr const char * LE_ADVERTISEMENT_OBJECT_PATH = " /com/aawgd/bluetooth/advertisement" ;
22
+
17
23
static constexpr const char * AAWG_PROFILE_OBJECT_PATH = " /com/aawgd/bluetooth/aawg" ;
18
24
static constexpr const char * AAWG_PROFILE_UUID = " 4de17a00-52cb-11e6-bdf4-0800200c9a66" ;
19
25
@@ -28,6 +34,9 @@ class BluezAdapterProxy: private DBus::ObjectProxy {
28
34
powered = this ->create_property <bool >(INTERFACE_BLUEZ_ADAPTER, " Powered" );
29
35
discoverable = this ->create_property <bool >(INTERFACE_BLUEZ_ADAPTER, " Discoverable" );
30
36
pairable = this ->create_property <bool >(INTERFACE_BLUEZ_ADAPTER, " Pairable" );
37
+
38
+ registerAdvertisement = this ->create_method <void (DBus::Path, DBus::Properties)>(INTERFACE_BLUEZ_LE_ADVERTISING_MANAGER, " RegisterAdvertisement" );
39
+ unregisterAdvertisement = this ->create_method <void (DBus::Path)>(INTERFACE_BLUEZ_LE_ADVERTISING_MANAGER, " UnregisterAdvertisement" );
31
40
}
32
41
33
42
public:
@@ -40,6 +49,9 @@ class BluezAdapterProxy: private DBus::ObjectProxy {
40
49
std::shared_ptr<DBus::PropertyProxy<bool >> powered;
41
50
std::shared_ptr<DBus::PropertyProxy<bool >> discoverable;
42
51
std::shared_ptr<DBus::PropertyProxy<bool >> pairable;
52
+
53
+ std::shared_ptr<DBus::MethodProxy<void (DBus::Path, DBus::Properties)>> registerAdvertisement;
54
+ std::shared_ptr<DBus::MethodProxy<void (DBus::Path)>> unregisterAdvertisement;
43
55
};
44
56
45
57
@@ -77,7 +89,7 @@ void BluetoothHandler::initAdapter() {
77
89
}
78
90
else {
79
91
m_adapter = BluezAdapterProxy::create (m_connection, adapter_path);
80
- m_adapter->alias ->set_value (ADAPTER_ALIAS );
92
+ m_adapter->alias ->set_value (m_adapterAlias );
81
93
}
82
94
}
83
95
@@ -117,15 +129,46 @@ void BluetoothHandler::exportProfiles() {
117
129
});
118
130
Logger::instance ()->info (" Bluetooth AA Wireless profile active\n " );
119
131
120
- // Register HSP Handset profile
121
- m_hspProfile = HSPHSProfile::create (HSP_HS_PROFILE_OBJECT_PATH);
122
- if (m_connection->register_object (m_hspProfile, DBus::ThreadForCalling::DispatcherThread) != DBus::RegistrationStatus::Success) {
123
- Logger::instance ()->info (" Failed to register HSP Handset profile\n " );
132
+ if (Config::instance ()->getConnectionStrategy () != ConnectionStrategy::DONGLE_MODE) {
133
+ // Register HSP Handset profile
134
+ m_hspProfile = HSPHSProfile::create (HSP_HS_PROFILE_OBJECT_PATH);
135
+ if (m_connection->register_object (m_hspProfile, DBus::ThreadForCalling::DispatcherThread) != DBus::RegistrationStatus::Success) {
136
+ Logger::instance ()->info (" Failed to register HSP Handset profile\n " );
137
+ }
138
+ registerProfile (HSP_HS_PROFILE_OBJECT_PATH, HSP_HS_UUID, {
139
+ {" Name" , DBus::Variant (" HSP HS" )},
140
+ });
141
+ Logger::instance ()->info (" HSP Handset profile active\n " );
124
142
}
125
- registerProfile (HSP_HS_PROFILE_OBJECT_PATH, HSP_HS_UUID, {
126
- {" Name" , DBus::Variant (" HSP HS" )},
127
- });
128
- Logger::instance ()->info (" HSP Handset profile active\n " );
143
+ }
144
+
145
+ void BluetoothHandler::startAdvertising () {
146
+ if (!m_adapter) {
147
+ return ;
148
+ }
149
+
150
+ // Register Advertisement Object
151
+ m_leAdvertisement = BLEAdvertisement::create (LE_ADVERTISEMENT_OBJECT_PATH);
152
+
153
+ m_leAdvertisement->type ->set_value (" peripheral" );
154
+ m_leAdvertisement->serviceUUIDs ->set_value (std::vector<std::string>{AAWG_PROFILE_UUID});
155
+ m_leAdvertisement->localName ->set_value (m_adapterAlias);
156
+
157
+ if (m_connection->register_object (m_leAdvertisement, DBus::ThreadForCalling::DispatcherThread) != DBus::RegistrationStatus::Success) {
158
+ Logger::instance ()->info (" Failed to register BLE Advertisement\n " );
159
+ }
160
+
161
+ (*m_adapter->registerAdvertisement )(LE_ADVERTISEMENT_OBJECT_PATH, {});
162
+ Logger::instance ()->info (" BLE Advertisement started\n " );
163
+ }
164
+
165
+ void BluetoothHandler::stopAdvertising () {
166
+ if (!m_adapter) {
167
+ return ;
168
+ }
169
+
170
+ (*m_adapter->unregisterAdvertisement )(LE_ADVERTISEMENT_OBJECT_PATH);
171
+ Logger::instance ()->info (" BLE Advertisement stopped\n " );
129
172
}
130
173
131
174
void BluetoothHandler::connectDevice () {
@@ -145,6 +188,8 @@ void BluetoothHandler::connectDevice() {
145
188
return ;
146
189
}
147
190
191
+ const bool isDongleMode = (Config::instance ()->getConnectionStrategy () == ConnectionStrategy::DONGLE_MODE);
192
+
148
193
Logger::instance ()->info (" Found %d bluetooth devices\n " , device_paths.size ());
149
194
150
195
for (const std::string &device_path: device_paths) {
@@ -161,16 +206,21 @@ void BluetoothHandler::connectDevice() {
161
206
Logger::instance ()->info (" Bluetooth device already connected, disconnecting\n " );
162
207
disconnect ();
163
208
}
164
- connectProfile (HSP_AG_UUID);
209
+ connectProfile (isDongleMode ? " " : HSP_AG_UUID);
165
210
Logger::instance ()->info (" Bluetooth connected to the device\n " );
166
- return ;
211
+ if (!isDongleMode) {
212
+ return ;
213
+ }
167
214
} catch (DBus::Error& e) {
168
- Logger::instance ()->info (" Failed to connect device at path: %s\n " , device_path.c_str ());
215
+ if (!isDongleMode) {
216
+ Logger::instance ()->info (" Failed to connect device at path: %s\n " , device_path.c_str ());
217
+ }
169
218
}
170
219
}
171
220
172
- Logger::instance ()->info (" Failed to connect to any known bluetooth device\n " );
173
-
221
+ if (!isDongleMode) {
222
+ Logger::instance ()->info (" Failed to connect to any known bluetooth device\n " );
223
+ }
174
224
}
175
225
176
226
void BluetoothHandler::retryConnectLoop () {
@@ -186,7 +236,9 @@ void BluetoothHandler::retryConnectLoop() {
186
236
}
187
237
}
188
238
189
- BluetoothHandler::instance ().powerOff ();
239
+ if (Config::instance ()->getConnectionStrategy () != ConnectionStrategy::DONGLE_MODE) {
240
+ BluetoothHandler::instance ().powerOff ();
241
+ }
190
242
}
191
243
192
244
void BluetoothHandler::init () {
@@ -196,7 +248,7 @@ void BluetoothHandler::init() {
196
248
m_dispatcher = DBus::StandaloneDispatcher::create ();
197
249
m_connection = m_dispatcher->create_connection ( DBus::BusType::SYSTEM );
198
250
199
- Logger ::instance ()->info ( " Unique Name: %s \n " , m_connection-> unique_name (). c_str ()) ;
251
+ m_adapterAlias = ( Config ::instance ()->getConnectionStrategy () == ConnectionStrategy::DONGLE_MODE) ? ADAPTER_ALIAS_DONGLE : ADAPTER_ALIAS ;
200
252
201
253
initAdapter ();
202
254
exportProfiles ();
@@ -209,6 +261,10 @@ void BluetoothHandler::powerOn() {
209
261
210
262
setPower (true );
211
263
setPairable (true );
264
+
265
+ if (Config::instance ()->getConnectionStrategy () == ConnectionStrategy::DONGLE_MODE) {
266
+ startAdvertising ();
267
+ }
212
268
}
213
269
214
270
std::optional<std::thread> BluetoothHandler::connectWithRetry () {
@@ -231,5 +287,8 @@ void BluetoothHandler::powerOff() {
231
287
return ;
232
288
}
233
289
290
+ if (Config::instance ()->getConnectionStrategy () == ConnectionStrategy::DONGLE_MODE) {
291
+ stopAdvertising ();
292
+ }
234
293
setPower (false );
235
294
}
0 commit comments