Introduced 'gattlib_write_char_by_uuid'

pull/21/head
Olivier Martin 2017-03-14 22:51:06 +01:00
parent 74972ae011
commit ee2fabaf3f
3 changed files with 15 additions and 24 deletions

View File

@ -181,3 +181,16 @@ int gattlib_write_char_by_handle(gatt_connection_t* connection, uint16_t handle,
return 0;
}
int gattlib_write_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid, void* buffer, size_t buffer_len) {
uint16_t handle = 0;
int ret;
ret = get_handle_from_uuid(connection, uuid, &handle);
if (ret) {
fprintf(stderr, "Fail to find handle for UUID.\n");
return ret;
}
return gattlib_write_char_by_handle(connection, handle, buffer, sizeof(buffer));
}

View File

@ -82,30 +82,7 @@ int main(int argc, char *argv[]) {
printf("%02x ", buffer[i]);
printf("\n");
} else {
uint16_t handle = 0;
// Look for handle for the corresponding UUID
gattlib_characteristic_t* characteristics;
int characteristic_count;
ret = gattlib_discover_char(connection, &characteristics, &characteristic_count);
if (ret) {
fprintf(stderr, "Fail to discover characteristic.\n");
return 1;
}
for (i = 0; i < characteristic_count; i++) {
if (gattlib_uuid_cmp(&characteristics[i].uuid, &g_uuid) == 0) {
handle = characteristics[i].value_handle;
break;
}
}
if (handle == 0) {
fprintf(stderr, "Fail to find handle for UUID.\n");
return 1;
}
free(characteristics);
ret = gattlib_write_char_by_handle(connection, handle, buffer, sizeof(buffer));
ret = gattlib_write_char_by_uuid(connection, &g_uuid, buffer, sizeof(buffer));
assert(ret == 0);
}

View File

@ -139,6 +139,7 @@ int gattlib_discover_desc(gatt_connection_t* connection, gattlib_descriptor_t**
int gattlib_read_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid, void* buffer, size_t buffer_len);
int gattlib_read_char_by_uuid_async(gatt_connection_t* connection, uuid_t* uuid, gatt_read_cb_t gatt_read_cb);
int gattlib_write_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid, void* buffer, size_t buffer_len);
int gattlib_write_char_by_handle(gatt_connection_t* connection, uint16_t handle, void* buffer, size_t buffer_len);
void gattlib_register_notification(gatt_connection_t* connection, gattlib_event_handler_t notification_handler, void* user_data);