examples: Demonstrate gattlib notification API

pull/21/head
Olivier Martin 2017-03-15 16:00:41 +01:00
parent 4dde0c3698
commit d0b18d5409
4 changed files with 122 additions and 1 deletions

View File

@ -54,6 +54,7 @@ set(ENV{PKG_CONFIG_PATH} "${PROJECT_BINARY_DIR}:$ENV{PKG_CONFIG_PATH}")
add_subdirectory(examples/ble_scan)
add_subdirectory(examples/discover)
add_subdirectory(examples/read_write)
add_subdirectory(examples/notification)
add_subdirectory(examples/nordic_uart)
# Some examples require Bluez code and other DBus support

View File

@ -69,7 +69,11 @@ Examples
* [Demonstrate BLE scanning and connection](/examples/ble_scan/ble_scan.c):
sudo ./examples/ble_scan/ble_scan
./examples/ble_scan/ble_scan
* [Demonstrate GATT notification using GATT Battery service](/examples/notification/notification.c):
./examples/notification/notification
**Note:** `examples/gatttool` has been partially ported to gattlib. There are two reasons: the laziness
(some of the GATT functions could be replaced by their gattlib equivalent) and the completeness (there

View File

@ -0,0 +1,34 @@
#
#
# GattLib - GATT Library
#
# Copyright (C) 2016-2017 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)
pkg_search_module(GLIB REQUIRED glib-2.0)
include_directories(${GLIB_INCLUDE_DIRS})
set(notification_SRCS notification.c)
add_executable(notification ${notification_SRCS})
target_link_libraries(notification ${GATTLIB_LIBRARIES})

View File

@ -0,0 +1,82 @@
/*
*
* GattLib - GATT Library
*
* Copyright (C) 2016-2017 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
*
*/
#include <assert.h>
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include "gattlib.h"
void notification_handler(const uuid_t* uuid, const uint8_t* data, size_t data_length, void* user_data) {
int i;
printf("Notification Handler: ");
for (i = 0; i < data_length; i++) {
printf("%02x ", data[i]);
}
printf("\n");
}
static void usage(char *argv[]) {
printf("%s <device_address>\n", argv[0]);
}
int main(int argc, char *argv[]) {
int ret;
gatt_connection_t* connection;
if (argc != 2) {
usage(argv);
return 1;
}
connection = gattlib_connect(NULL, argv[1], BDADDR_LE_PUBLIC, BT_SEC_LOW, 0, 0);
if (connection == NULL) {
fprintf(stderr, "Fail to connect to the bluetooth device.\n");
return 1;
}
gattlib_register_notification(connection, notification_handler, NULL);
// Battery Level UUID
uuid_t battery_level_uuid = {
.type= SDP_UUID16,
.value.uuid16= 0x2A19
};
ret = gattlib_notification_start(connection, &battery_level_uuid);
if (ret) {
fprintf(stderr, "Fail to start notification\n.");
return 1;
}
GMainLoop *loop = g_main_loop_new(NULL, 0);
g_main_loop_run(loop);
g_main_loop_unref(loop);
gattlib_disconnect(connection);
puts("Done");
return 0;
}