1
#include <com/ubuntu/location/connectivity/manager.h>
3
#include <core/posix/signal.h>
9
namespace location = com::ubuntu::location;
13
// Just convenience to safe some typing.
14
typedef std::vector<location::connectivity::RadioCell> RadioCells;
15
typedef std::vector<location::connectivity::WirelessNetwork::Ptr> WirelessNetworks;
18
// This example illustrates the usage of the location-service-specific connectivity API.
19
// The setup for obtaining measurements of wifis and radio cells is as follows:
21
// (1.) Obtain an instance of location::connectivity::Manager.
22
// (2.) Connect to the changed signals of the wifi and cell properties and react according to your component's requirements.
23
// (3.) Bootstrap your own setup by explicitly getting all visible wifis and connected cells.
24
int main(int argc, char** argv)
26
// We catch sig-term to exit cleanly.
27
auto trap = core::posix::trap_signals_for_all_subsequent_threads({core::posix::Signal::sig_term});
28
trap->signal_raised().connect([trap](core::posix::Signal)
33
// Trying to acquire a default implementation.
34
auto cm = location::connectivity::platform_default_manager();
38
std::cerr << "Could not get hold of a connectivity::Manager implementation, aborting..." << std::endl;
42
// Subscribe to wifi added/removed signals.
43
cm->wireless_network_added().connect([](const location::connectivity::WirelessNetwork::Ptr& wifi)
45
std::cout << "Visible wireless network was added: " << *wifi << std::endl;
47
// We don't want to keep the object alive
48
std::weak_ptr<location::connectivity::WirelessNetwork> wp
55
// Subscribe to signal strength and last_seen updates. Please note that this is not considering
56
// the case of subscribing to already known wifis. We leave this up
57
// to consumers of the api.
58
wifi->last_seen().changed().connect([wp](const std::chrono::system_clock::time_point& tp)
62
std::cout << "Signal strength changed for wifi " << sp->ssid().get() << ": " << tp.time_since_epoch().count() << std::endl;
65
wifi->signal_strength().changed().connect([wp](const location::connectivity::WirelessNetwork::SignalStrength& s)
69
std::cout << "Signal strength changed for wifi " << sp->ssid().get() << ": " << s << std::endl;
73
cm->wireless_network_removed().connect([](const location::connectivity::WirelessNetwork::Ptr& wifi)
75
std::cout << "Visible wireless network was removed: " << wifi->ssid().get() << std::endl;
78
// Iterate over all radio cells that the device is connected with.
79
cm->enumerate_connected_radio_cells([](const location::connectivity::RadioCell::Ptr& cell)
81
std::cout << *cell << std::endl;
83
std::weak_ptr<location::connectivity::RadioCell> wp{cell};
85
// Subscribe to changes on the cell
86
cell->changed().connect([wp]()
91
std::cout << "Something changed on a radio cell: " << *sp << std::endl;
95
cm->connected_cell_added().connect([](const location::connectivity::RadioCell::Ptr& cell)
97
std::cout << *cell << std::endl;
99
// Subscribe to changes on the cell
100
cell->changed().connect([]()
102
std::cout << "Something changed on a radio cell." << std::endl;
106
cm->connected_cell_removed().connect([](const location::connectivity::RadioCell::Ptr& cell)
108
std::cout << *cell << std::endl;
111
// Iterate over all networks that are visible right now.
112
cm->enumerate_visible_wireless_networks([](const location::connectivity::WirelessNetwork::Ptr& wifi)
114
std::cout << *wifi << std::endl;
116
// We don't want to keep the object alive
117
std::weak_ptr<location::connectivity::WirelessNetwork> wp
122
// Subscribe to last-seen updates.
123
wifi->last_seen().changed().connect([wp](const std::chrono::system_clock::time_point& tp)
127
std::cout << "Last seen changed for wifi " << *sp << std::endl;
130
// Subscribe to signal strength updates. Please note that this is not considering
131
// the case of subscribing to already known wifis. We leave this up
132
// to consumers of the api.
133
wifi->signal_strength().changed().connect([wp](const location::connectivity::WirelessNetwork::SignalStrength& s)
137
std::cout << "Signal strength changed for wifi: " << *sp << std::endl;
141
// Subscribe to end-of-scan signals
142
cm->wireless_network_scan_finished().connect([]()
144
std::cout << "A wireless network scan finished." << std::endl;
147
// Request a scan for wireless networks.
150
cm->request_scan_for_wireless_networks();
151
} catch(const std::runtime_error& e)
153
std::cerr << e.what() << std::endl;
156
bool cancelled = false;
162
while (not cancelled)
164
cm->enumerate_visible_wireless_networks([](const location::connectivity::WirelessNetwork::Ptr&)
166
// We do nothing with the actual values and just keep the thread running
167
// to put some load on the infrastructure.
177
while (not cancelled)
179
cm->enumerate_connected_radio_cells([](const location::connectivity::RadioCell::Ptr&)
181
// We do nothing with the actual values and just keep the thread running
182
// to put some load on the infrastructure.