Skip to content

Commit 350115e

Browse files
author
Tim Ebbeke
committed
Improved example code.
1 parent e91a641 commit 350115e

File tree

7 files changed

+34
-9
lines changed

7 files changed

+34
-9
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,8 @@ Everything will be contained within "bluetoot_project".
3636
- cd build
3737
- cmake ..
3838
- cmake --build . -j4
39+
40+
## Troubleshooting
41+
### Unit dbus-org.bluez.service not found
42+
Start the bluetooth service that exposes the dbus interfaces: `sudo systemctl start bluetooth`.
43+
### could not give myself a name: Permission denied

bluetooth/bluez/adapter.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,20 +148,28 @@ namespace Bluetooth
148148
return devices;
149149
}
150150
//---------------------------------------------------------------------------------------------------------------------
151-
Device Adapter::getPairedByName(std::string const& name)
151+
std::optional<Device> Adapter::getPairedByName(std::string const& name)
152152
{
153+
bool anyFound = false;
153154
DBusGlue::object_path foundDev;
154155
auto devices = pairedDevices();
155156
for (auto const& device : devices)
156157
{
157-
std::cout << device << "\n";
158158
Device dev{impl_->bus, device};
159159

160160
if (dev.api().Name == name)
161+
{
161162
foundDev = device;
163+
anyFound = true;
164+
break;
165+
}
166+
}
167+
if (!anyFound)
168+
{
169+
return std::nullopt;
162170
}
163171

164-
return {impl_->bus, foundDev};
172+
return Device{impl_->bus, foundDev};
165173
}
166174
//#####################################################################################################################
167175
}

bluetooth/bluez/adapter.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <memory>
77
#include <string>
8+
#include <optional>
89

910
namespace Bluetooth
1011
{
@@ -98,7 +99,7 @@ namespace Bluetooth
9899
* @param name
99100
* @return
100101
*/
101-
Device getPairedByName(std::string const& name);
102+
std::optional<Device> getPairedByName(std::string const& name);
102103

103104
private:
104105
std::unique_ptr <Implementation> impl_;

bluetooth/bluez/device.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ namespace Bluetooth
3636
}
3737
//---------------------------------------------------------------------------------------------------------------------
3838
Device::~Device() = default;
39+
//---------------------------------------------------------------------------------------------------------------------
40+
Device::Device(Device&&) = default;
41+
//---------------------------------------------------------------------------------------------------------------------
42+
Device& Device::operator=(Device&&) = default;
3943
//---------------------------------------------------------------------------------------------------------------------
4044
Device::device_type& Device::api()
4145
{

bluetooth/bluez/device.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ namespace Bluetooth
1515

1616
Device(DBusGlue::dbus* bus, DBusGlue::object_path const& path);
1717
~Device();
18+
Device(Device&&);
19+
Device(Device const&) = delete;
20+
Device& operator=(Device&&);
21+
Device& operator=(Device const&) = delete;
1822

1923
device_type& api();
2024

main.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ int main()
1818
1,
1919
"/bla/bluetooth",
2020
"SerialServer",
21-
true,
22-
"de.blabla.cpp"s
21+
true
22+
// requires modified rights as documented in README under 3)
23+
// "de.blabla.cpp"s
2324
};
2425

2526
Server server{&bus, info};
@@ -36,8 +37,10 @@ int main()
3637
*/
3738

3839
auto phone = driver.getPairedByName("HUAWEI P20");
39-
40-
phone.api().Trusted = true;
40+
if (phone)
41+
{
42+
phone->api().Trusted = true;
43+
}
4144

4245
server.start(
4346
// on connect:

0 commit comments

Comments
 (0)