Reputation: 377
Environment: C, Linux, Bluez 5.4
3rd gatt_connect gives an error. Seems there is some resource which is not closed or shutdown. No clue what.
Connect Device - gatt_connect
Disconnect Device - remove battery
Connect Device - gatt_connect
Disconnect Device - remove battery
Connect Device - gatt_connect
3rd Connect Device gives this error:
connect: Device or resource busy (16)
connect_cb connect error: Device or resource busy (16)
// Watcher called each time channel is disconnected.
static gboolean channel_watcher(GIOChannel *iochannel, GIOCondition cond, gpointer user_data)
{
int devid = (int) user_data;
g_io_channel_shutdown(iochannel, FALSE, NULL);
g_io_channel_unref(iochannel);
iochannel = NULL;
return FALSE;
}
Makes the GATT connection:
GIOChannel* GattConnect(int dev_id, char *hwid, int handle, char *value)
{
GIOChannel *chan;
opt_listen=TRUE;
opt_dst = hwid;
opt_handle = handle;
opt_value = value;
chan = gatt_connect(opt_src, opt_dst, opt_dst_type, opt_sec_level, opt_psm, opt_mtu, connect_cb);
if(chan == NULL) {
fprintf(logFile, "GattConnect - null channel\n");
}
else {
g_io_add_watch(chan, G_IO_HUP, channel_watcher, (void *)dev_id);
}
return chan;
}
Please provide ideas of what may may be causing the resource to be busy
Upvotes: 2
Views: 5763
Reputation: 3212
Bluez accesses GATT services through a (Unix Domain) socket. If the connection fails then there is a timeout of 20 seconds before to timeout the socket.
I had a similar issue when I was implementing the example ble_scan
in GattLib. To fix it I reduced the timeout on the send/receive of the socket:
if (setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0) {
fprintf(stderr, "l2cap_connect: Failed to setsockopt for receive timeout.\n");
return -1;
}
if (setsockopt (sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0) {
fprintf(stderr, "l2cap_connect: Failed to setsockopt for sending timeout.\n");
return -1;
}
I guess the GattLib ble_scan
example does what you are trying to achieve: https://github.com/labapart/gattlib/blob/master/examples/ble_scan/ble_scan.c
Upvotes: 1