Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

callback on gattlib_connect not invoke #284

Open
oranelbaz2000 opened this issue Sep 22, 2024 · 0 comments
Open

callback on gattlib_connect not invoke #284

oranelbaz2000 opened this issue Sep 22, 2024 · 0 comments

Comments

@oranelbaz2000
Copy link

oranelbaz2000 commented Sep 22, 2024

I'm new to BLE development, and we're using Linux (ubuntu) with the latest version of GattLib (0.7.2).
When we call the gattlib_connect function, the callback (on_device_connect) is only invoked when the function does not succeed, meaning the return value is non-zero.
Why does this happen? How can I interact with the "connection" object if the callback isn't triggered on a successful connection? Any guidance would be appreciated! (start scan and stop scan working fine)

#include
#include <pthread.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/queue.h>
#include "gattlib.h"
#include
#include
#include
#include

typedef void (*ble_discovered_device_t)(const char *addr, const char *name);

gattlib_adapter_t *adapter;
gattlib_connection_t *current_connection;
bool isConnected;

// represent a Bluetooth device
struct Device
{
std::string name;
std::string macID;
};

// list of devices
std::vector deviceList;

void on_device_connect(gattlib_adapter_t *adapter, const char *dst, gattlib_connection_t *connection, int error, void *user_data)
{
current_connection = connection;
}

void ble_discovered_device(gattlib_adapter_t *adapter, const char *addr, const char *name, void *user_data)
{
int ret;

if (name)
{
    printf("Device %s - '%s'\n", addr, name);
}
else
{
    printf("Device %s\n", addr);
}

// insert device info to list
Device newDevice;
newDevice.name = (name != nullptr) ? std::string(name) : "";
newDevice.macID = (addr != nullptr) ? std::string(addr) : "";   
deviceList.push_back(newDevice);

}

void BLEInterface::Init()
{
isConnected = false;
deviceList.clear();

int ret = gattlib_adapter_open(NULL, &adapter);
if (ret)
{
    printf("Failed to open adapter. Error code: %d\n", ret);
}

}

void BLEInterface::StartScaning(BoardSide side, PairType pairingType, uint8_t roomId)
{

std::thread([](){
     
    gattlib_mainloop([](void *arg) -> void * {

        // start scanning
        printf("Start scanning\n");
        int ret = gattlib_adapter_scan_enable(adapter, ble_discovered_device, 0, NULL /* user_data */);
        if (ret)
        {
            printf("Failed to start scan. Error code: %d\n", ret);
        }       
        
        return NULL;
    
    }, NULL);

}).detach();   

}

void BLEInterface::StopScaning()
{
printf("Stop scanning...\n");

int ret = gattlib_adapter_scan_disable(adapter);

if (ret)
{
    printf("Failed to stop scan. Error code: %d\n", ret);
}
else
{
    printf("scanning stopped\n");
}

}

void BLEInterface::Connect(uint8_t *address, uint8_t len)
{

if (IsConnected())
{
    printf("already connected\n");
}
else
{
    printf("connecting to device...\n");

    int ret = gattlib_connect(adapter, (char*)address, GATTLIB_CONNECTION_OPTIONS_NONE, on_device_connect, NULL);
        
    if (ret)
    {
        printf("Failed to connect. Error code: %d\n", ret);
        current_connection = NULL;
    }
    else
    {
        printf("connected\n");
        isConnected = true;
         // in this state the on_device_connect callback is not invoke!!!
    } 
           
}

}

void BLEInterface::Disconnect()
{
if (!IsConnected())
{
printf("not connected\n");
}
else
{
printf("disconnecting from device...\n");

    int ret = gattlib_disconnect(current_connection, true);
    
    if (ret)
    {
        printf("Failed to disconnect Error code: %d\n", ret);
    }
    else
    {
        printf("disconnected\n");
        isConnected = false;
    }
}

}

bool BLEInterface::IsConnected()
{
return isConnected;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant