From 7c9f266cdb37e215279208198dfe009dc66c5daa Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 21 Feb 2015 10:28:00 +0100 Subject: o Creating a class to access GATT. --- apps/ble-inspect-device.cpp | 56 +++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 35 deletions(-) (limited to 'apps/ble-inspect-device.cpp') diff --git a/apps/ble-inspect-device.cpp b/apps/ble-inspect-device.cpp index 361311c..e50f6ae 100644 --- a/apps/ble-inspect-device.cpp +++ b/apps/ble-inspect-device.cpp @@ -7,25 +7,16 @@ using namespace std; using namespace trygvis::bluetooth; -Mac *targetMac; +void scan_callback(BluetoothDevice & device) { + cout << "Inspecting device: " << device.getMac().str() << endl; -void scan_callback(BluetoothDevice &device) { - device.adapter().stopScan(); + auto &gatt = device.connectGatt(); - 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(); + gatt.discoverServices(); - device.discoverServices(); - - vector services = device.getServices(); + vector < BluetoothGattService * > services = gatt.getServices(); cout << "Device has " << services.size() << " services" << endl; - + for (auto &s: services) { const vector characteristics = s->getCharacteristics(); @@ -36,7 +27,7 @@ void scan_callback(BluetoothDevice &device) { } } - device.disconnect(); + gatt.disconnect(); } int main(int argc, char *argv[]) { @@ -45,28 +36,23 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } - int e; -// try { - Mac mac = Mac::parseMac(argv[1]); - targetMac = &mac; + BluetoothSystem bluetoothSystem; - BluetoothAdapter &adapter = getAdapter(0); + try { + Mac mac = Mac::parseMac(argv[1]); - BluetoothDevice &device = adapter.getDevice(mac); + BluetoothAdapter &adapter = getAdapter(0); - scan_callback(device); + BluetoothDevice &device = adapter.getDevice(mac); -// adapter->runScan(scan_callback); + 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; + return EXIT_SUCCESS; + } catch (std::runtime_error ex) { + W << "std::runtime_error: " << ex.what(); + return EXIT_FAILURE; + } catch (std::exception ex) { + W << "std::exception: " << ex.what(); + return EXIT_FAILURE; + } } -- cgit v1.2.3