diff --git a/CMakeLists.txt b/CMakeLists.txt index 0bfef34..866f1ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,6 +102,7 @@ set(ENV{PKG_CONFIG_PATH} "${PROJECT_BINARY_DIR}:$ENV{PKG_CONFIG_PATH}") if(GATTLIB_BUILD_EXAMPLES) # Examples + add_subdirectory(examples/advertisement_data) add_subdirectory(examples/ble_scan) add_subdirectory(examples/discover) add_subdirectory(examples/find_eddystone) diff --git a/examples/advertisement_data/CMakeLists.txt b/examples/advertisement_data/CMakeLists.txt new file mode 100644 index 0000000..4377283 --- /dev/null +++ b/examples/advertisement_data/CMakeLists.txt @@ -0,0 +1,31 @@ +# +# GattLib - GATT Library +# +# Copyright (C) 2016-2020 Olivier Martin +# +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# + +cmake_minimum_required(VERSION 2.6) + +find_package(PkgConfig REQUIRED) + +pkg_search_module(GATTLIB REQUIRED gattlib) + +set(advertisement_data_SRCS advertisement_data.c) + +add_executable(advertisement_data ${advertisement_data_SRCS}) +target_link_libraries(advertisement_data ${GATTLIB_LIBRARIES} ${GATTLIB_LDFLAGS}) diff --git a/examples/advertisement_data/advertisement_data.c b/examples/advertisement_data/advertisement_data.c new file mode 100644 index 0000000..7157829 --- /dev/null +++ b/examples/advertisement_data/advertisement_data.c @@ -0,0 +1,75 @@ +#include +#include +#include + +#include "gattlib.h" + +#define BLE_SCAN_TIMEOUT 60 + +static void ble_advertising_device(void *adapter, const char* addr, const char* name, void *user_data) { + gattlib_advertisement_data_t *advertisement_data; + size_t advertisement_data_count; + uint16_t manufacturer_id; + uint8_t *manufacturer_data; + size_t manufacturer_data_size; + int ret; + + ret = gattlib_get_advertisement_data_from_mac(adapter, addr, + &advertisement_data, &advertisement_data_count, + &manufacturer_id, &manufacturer_data, &manufacturer_data_size); + if (ret != 0) { + return; + } + + if (name) { + printf("Device %s - '%s': ", addr, name); + } else { + printf("Device %s: ", addr); + } + + for (size_t i = 0; i < manufacturer_data_size; i++) { + printf("%02x ", manufacturer_data[i]); + } + printf("\n"); +} + +int main(int argc, const char *argv[]) { + const char* adapter_name; + void* adapter; + int ret; + + if (argc == 1) { + adapter_name = NULL; + } else if (argc == 2) { + adapter_name = argv[1]; + } else { + fprintf(stderr, "%s []\n", argv[0]); + return 1; + } + + ret = gattlib_adapter_open(adapter_name, &adapter); + if (ret) { + fprintf(stderr, "ERROR: Failed to open adapter.\n"); + return 1; + } + + ret = gattlib_adapter_scan_enable_with_filter(adapter, + NULL, /* Do not filter on any specific Service UUID */ + 0 /* RSSI Threshold */, + GATTLIB_DISCOVER_FILTER_NOTIFY_CHANGE, /* Notify change of advertising data/RSSI */ + ble_advertising_device, + 0, /* timeout=0 means infinite loop */ + NULL /* user_data */); + if (ret) { + fprintf(stderr, "ERROR: Failed to scan.\n"); + goto EXIT; + } + + gattlib_adapter_scan_disable(adapter); + + puts("Scan completed"); + +EXIT: + gattlib_adapter_close(adapter); + return ret; +}