labapart / gattlib

Library to access GATT information from BLE (Bluetooth Low Energy) devices
http://labapart.com/
454 stars 160 forks source link

Failed to write DBus GATT characteristic: GDBus.Error:org.bluez.Error.Failed: Not connected #201

Open Snijd opened 3 years ago

Snijd commented 3 years ago

I have (sometimes) the error as above. Software is hanging then... in the part // Wait for completion of the event

Using the example notification. Calling: gattlib_write_char_by_uuid(connection, &g_write_uuid, input_ptr, length);

giving me error: Failed to write DBus GATT characteristic: GDBus.Error:org.bluez.Error.Failed: Not connected

How can i solve this (at least that the software is not hanging)

int gattlib_write_char_by_uuid(gatt_connection_t connection, uuid_t uuid, const 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, buffer_len);

}

int gattlib_write_char_by_handle(gatt_connection_t connection, uint16_t handle, const void buffer, size_t buffer_len) { gattlib_context_t* conn_context = connection->context; int write_completed = FALSE;

guint ret = gatt_write_char(conn_context->attrib, handle, (void*)buffer, buffer_len,
                gattlib_write_result_cb, &write_completed);
if (ret == 0) {
    return 1;
}

// Wait for completion of the event
while(write_completed == FALSE) {
    g_main_context_iteration(g_gattlib_thread.loop_context, FALSE);
}
return 0;

}