gattlib
gattlib copied to clipboard
callback on gattlib_connect not invoke
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
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<Device> 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; }