diff options
Diffstat (limited to 'apps/sm-get-value.cpp')
-rw-r--r-- | apps/sm-get-value.cpp | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/apps/sm-get-value.cpp b/apps/sm-get-value.cpp new file mode 100644 index 0000000..232c166 --- /dev/null +++ b/apps/sm-get-value.cpp @@ -0,0 +1,75 @@ +#include <exception> +#include <iostream> +#include <vector> +#include <boost/uuid/uuid_io.hpp> +#include <boost/optional.hpp> +#include "Bluetooth.h" +#include "soil-moisture.h" + +using namespace std; +using namespace trygvis::bluetooth; + +Mac *targetMac; + +void scan_callback(BluetoothDevice &device) { + device.adapter().stopScan(); + + if (device.mac() != *targetMac) { + cout << "found device: " << device.mac().str() << ", but not the one we want " << targetMac->str() << endl; + return; + } + + cout << "Connecting to device: " << device.mac().str() << endl; + + device.connect(); + + device.discoverServices(); + + vector<BluetoothGattService *> services = device.getServices(); + cout << "Device has " << services.size() << " services" << endl; + + for (auto &s: services) { + const vector<BluetoothGattCharacteristic *> characteristics = s->getCharacteristics(); + + cout << "Service: UUID: " << s->getUuid() << ", has " << characteristics.size() << " characteristics" << endl; + + for (auto &c: characteristics) { + cout << "Characteristic: UUID: " << c->getUuid() << ", properties: " << (int) c->getProperties() << endl; + } + } + + boost::uuids::uuid soil_moisture_service; + boost::optional<BluetoothGattService*> service = device.findService(soil_moisture_service); + + device.disconnect(); +} + +int main(int argc, char *argv[]) { + if (argc != 2) { + cerr << "usage: " << argv[0] << " [mac]" << endl; + return EXIT_FAILURE; + } + + int e; +// try { + Mac mac = Mac::parseMac(argv[1]); + targetMac = &mac; + + BluetoothAdapter &adapter = getAdapter(0); + + BluetoothDevice &device = adapter.getDevice(mac); + + scan_callback(device); + + e = EXIT_SUCCESS; +// } catch (std::runtime_error ex) { +// W << "std::runtime_error: " << ex.what(); +// e = EXIT_FAILURE; +// } catch (std::exception ex) { +// W << "std::exception: " << ex.what(); +// e = EXIT_FAILURE; +// } + + shutdown(); + return e; +} |