example: Add example to find Eddystone in C

pull/160/head
Olivier Martin 2020-04-07 10:36:32 +02:00 committed by Olivier Martin
parent 392d9d5eb3
commit 51bbc0242b
3 changed files with 128 additions and 1 deletions

View File

@ -1,7 +1,7 @@
#
# GattLib - GATT Library
#
# Copyright (C) 2016-2019 Olivier Martin <olivier@labapart.org>
# Copyright (C) 2016-2020 Olivier Martin <olivier@labapart.org>
#
#
# This program is free software; you can redistribute it and/or modify
@ -104,6 +104,7 @@ if(GATTLIB_BUILD_EXAMPLES)
# Examples
add_subdirectory(examples/ble_scan)
add_subdirectory(examples/discover)
add_subdirectory(examples/find_eddystone)
add_subdirectory(examples/read_write)
add_subdirectory(examples/read_write_memory)
add_subdirectory(examples/notification)

View File

@ -0,0 +1,31 @@
#
# GattLib - GATT Library
#
# Copyright (C) 2016-2020 Olivier Martin <olivier@labapart.org>
#
#
# 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(find_eddystone_SRCS find_eddystone.c)
add_executable(find_eddystone ${find_eddystone_SRCS})
target_link_libraries(find_eddystone ${GATTLIB_LIBRARIES} ${GATTLIB_LDFLAGS} ${PCRE_LIBRARIES} pthread)

View File

@ -0,0 +1,95 @@
//#include <pthread.h>
//#include <stdio.h>
//#include <stdint.h>
//#include <stdlib.h>
//#include <sys/queue.h>
#include "gattlib.h"
#define BLE_SCAN_EDDYSTONE_TIMEOUT 20
/**
* @brief Handler called on new discovered BLE device
*
* @param adapter is the adapter that has found the BLE device
* @param addr is the MAC address of the BLE device
* @param name is the name of BLE device if advertised
* @param advertisement_data is an array of Service UUID and their respective data
* @param advertisement_data_count is the number of elements in the advertisement_data array
* @param manufacturer_id is the ID of the Manufacturer ID
* @param manufacturer_data is the data following Manufacturer ID
* @param manufacturer_data_size is the size of manufacturer_data
* @param user_data Data defined when calling `gattlib_register_on_disconnect()`
*/
void on_eddystone_found(void *adapter, const char* addr, const char* name,
gattlib_advertisement_data_t *advertisement_data, size_t advertisement_data_count,
uint16_t manufacturer_id, uint8_t *manufacturer_data, size_t manufacturer_data_size,
void *user_data)
{
puts("Found Eddystone device");
// Look for Eddystone Common Data
for (size_t i = 0; i < advertisement_data_count; i++) {
gattlib_advertisement_data_t *advertisement_data_ptr = &advertisement_data[i];
if (gattlib_uuid_cmp(&advertisement_data_ptr->uuid, &gattlib_eddystone_common_data_uuid) == GATTLIB_SUCCESS) {
switch (advertisement_data_ptr->data[0]) {
case EDDYSTONE_TYPE_UID:
puts("\tEddystone UID");
break;
case EDDYSTONE_TYPE_URL:
printf("\tEddystone URL %s%s (TX Power:%d)\n",
gattlib_eddystone_url_scheme_prefix[advertisement_data_ptr->data[2]],
advertisement_data_ptr->data + 3,
advertisement_data_ptr->data[1]);
break;
case EDDYSTONE_TYPE_TLM:
puts("\tEddystone TLM");
break;
case EDDYSTONE_TYPE_EID:
puts("\tEddystone EID");
break;
default:
fprintf(stderr, "\tEddystone ID %d not supported\n", advertisement_data_ptr->data[0]);
}
}
}
// We stop advertising on the first device
gattlib_adapter_scan_disable(adapter);
}
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 [<bluetooth-adapter>]\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_eddystone(adapter,
0, /* rssi_threshold. The value is not relevant as we do not pass GATTLIB_EDDYSTONE_LIMIT_RSSI */
GATTLIB_EDDYSTONE_TYPE_URL,
on_eddystone_found, BLE_SCAN_EDDYSTONE_TIMEOUT, NULL);
if (ret) {
fprintf(stderr, "ERROR: Failed to scan.\n");
goto EXIT;
}
puts("Scan completed");
EXIT:
gattlib_adapter_close(adapter);
return ret;
}