Bluez/Linux-连接:设备或资源在gatt_connect上忙

Bluez/Linux-连接:设备或资源在gatt_connect上忙,linux,bluetooth,bluez,Linux,Bluetooth,Bluez,环境:C、Linux、Bluez5.4 第三个gatt_connect出现错误。似乎有一些资源未关闭或关闭。不知道是什么 Connect Device - gatt_connect Disconnect Device - remove battery Connect Device - gatt_connect Disconnect Device - remove battery Connect Device - gatt_connect 第三连接设备出现此错误: connect: Device

环境:C、Linux、Bluez5.4

第三个gatt_connect出现错误。似乎有一些资源未关闭或关闭。不知道是什么

Connect Device - gatt_connect
Disconnect Device - remove battery
Connect Device - gatt_connect
Disconnect Device - remove battery
Connect Device - gatt_connect
第三连接设备出现此错误:

connect: Device or resource busy (16)
connect_cb connect error: Device or resource busy (16)
//每次通道断开时,都会调用观察程序

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;
}
与关贸总协定建立联系:

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;
}

请提供可能导致资源繁忙的原因的想法

Bluez通过(Unix域)套接字访问GATT服务。如果连接失败,则在套接字超时之前有20秒的超时时间

在中实现示例
ble_scan
时,我遇到了类似的问题。为了解决这个问题,我减少了套接字发送/接收的超时时间:

    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;
    }
if(setsockopt(sock,SOL_SOCKET,SO_RCVTIMEO,(char*)和timeout,sizeof(timeout))<0){
fprintf(标准,“l2cap_连接:无法设置接收超时的锁定选项。\n”);
返回-1;
}
if(setsockopt(sock,SOL_SOCKET,SO_SNDTIMEO,(char*)和timeout,sizeof(timeout))<0){
fprintf(stderr,“l2cap_connect:无法设置用于发送超时的锁定选项。\n”);
返回-1;
}
我猜GattLib
ble_scan
示例实现了您想要实现的目标: