diff options
author | Petre Eftime <[email protected]> | 2016-02-19 18:28:13 +0200 |
---|---|---|
committer | Petre Eftime <[email protected]> | 2016-02-29 15:12:56 +0200 |
commit | 2b1e875411936e4e41a1bf71951afcdbff8119e7 (patch) | |
tree | 8325acfcd137bec03082021eaeafe3ed92138fe4 /src/BluetoothManager.cpp | |
parent | 66709784581b0b643e6824c83114178ac5a561f6 (diff) |
Better handling of default adapter and initialization
Signed-off-by: Petre Eftime <[email protected]>
Diffstat (limited to 'src/BluetoothManager.cpp')
-rw-r--r-- | src/BluetoothManager.cpp | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/src/BluetoothManager.cpp b/src/BluetoothManager.cpp index 2e540b19..b0338242 100644 --- a/src/BluetoothManager.cpp +++ b/src/BluetoothManager.cpp @@ -192,15 +192,20 @@ BluetoothManager::BluetoothManager() : event_list() objects = g_dbus_object_manager_get_objects(gdbus_manager); + default_adapter = nullptr; for (l = objects; l != NULL; l = l->next) { Object *object = (Object *) l->data; Adapter1 *adapter = object_get_adapter1(object); if (adapter != NULL) { - default_adapter = new BluetoothAdapter(adapter); + default_adapter = std::unique_ptr<BluetoothAdapter>(new BluetoothAdapter(adapter)); break; } } g_list_free_full(objects, g_object_unref); + + if (default_adapter == nullptr) { + throw std::runtime_error("No adapter installed or not recognized by system"); + } } BluetoothManager *BluetoothManager::get_bluetooth_manager() @@ -267,20 +272,20 @@ std::vector<std::unique_ptr<BluetoothGattService>> BluetoothManager::get_service return vector; } -bool BluetoothManager::set_default_adapter(BluetoothAdapter *adapter) +bool BluetoothManager::set_default_adapter(BluetoothAdapter &adapter) { - if (adapter != NULL) { - BluetoothAdapter *prev_adapter = default_adapter; - default_adapter = new BluetoothAdapter(*adapter); - delete prev_adapter; - return true; - } - return false; + default_adapter = std::unique_ptr<BluetoothAdapter>(adapter.clone()); + return true; +} + +std::unique_ptr<BluetoothAdapter> BluetoothManager::get_default_adapter() +{ + return std::unique_ptr<BluetoothAdapter>(default_adapter->clone()); } bool BluetoothManager::start_discovery() { - if (default_adapter != NULL) + if (default_adapter != nullptr) return default_adapter->start_discovery(); else return false; |