Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 21 additions & 28 deletions src/modules/BitChatBLEBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#include <NimBLEService.h>
#include <NimBLECharacteristic.h>

// Service and characteristic created in NimbleBluetooth.cpp during initial BLE setup
extern NimBLEService *bitchatBLEService;
extern NimBLECharacteristic *bitchatBLECharacteristic;

// Forward declaration for callback class
class BitChatBLECharacteristicCallbacks;

Expand Down Expand Up @@ -220,47 +224,36 @@ bool BitChatBLEBridge::setupBitChatService(NimBLEServer* server)
LOG_ERROR("BitChat BLE: No BLE server available");
return false;
}

std::lock_guard<std::mutex> lock(bleMutex);

try {
// Create BitChat service
bitchatService = server->createService(NimBLEUUID(BITCHAT_SERVICE_UUID));
if (!bitchatService) {
LOG_ERROR("BitChat BLE: Failed to create BitChat service");
return false;
}

// Create BitChat characteristic
bitchatCharacteristic = bitchatService->createCharacteristic(
NimBLEUUID(BITCHAT_CHARACTERISTIC_UUID),
NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_NR | NIMBLE_PROPERTY::NOTIFY
);

if (!bitchatCharacteristic) {
LOG_ERROR("BitChat BLE: Failed to create BitChat characteristic");
// Use the service and characteristic created in NimbleBluetooth::setupService()
// They must be created during initial BLE setup before advertising starts
if (!bitchatBLEService || !bitchatBLECharacteristic) {
LOG_ERROR("BitChat BLE: Service not created during BLE init - check NimbleBluetooth.cpp");
return false;
}

// Set initial empty value
bitchatCharacteristic->setValue((uint8_t*)nullptr, 0);
LOG_DEBUG("BitChat BLE: Created characteristic with READ, WRITE, WRITE_NR, NOTIFY (OPEN permissions)");


// Store references to the pre-created service and characteristic
bitchatService = bitchatBLEService;
bitchatCharacteristic = bitchatBLECharacteristic;

LOG_DEBUG("BitChat BLE: Using pre-created service and characteristic from NimbleBluetooth");

// Set up callbacks
if (!characteristicCallbacks) {
characteristicCallbacks = new BitChatBLECharacteristicCallbacks();
}

bitchatCharacteristic->setCallbacks(characteristicCallbacks);
activeBridge = this; // Set global pointer for callbacks

// Start the service
bitchatService->start();

serviceActive = true;

LOG_INFO("BitChat BLE: Service setup complete (characteristic callbacks registered)");
return true;

} catch (const std::exception& e) {
LOG_ERROR("BitChat BLE: Exception during service setup: %s", e.what());
return false;
Expand Down
11 changes: 5 additions & 6 deletions src/modules/BitChatBridgeModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,13 @@ int32_t BitChatBridgeModule::runOnce()
if (bleBridge.setupBitChatService(bleServer)) {
LOG_INFO("BitChat Bridge: BLE service setup successful (ESP32)");
bleServiceSetup = true;


// No need to restart advertising - service was created in NimbleBluetooth::setupService()
// before advertising started, so GATT table is already correct
LOG_INFO("BitChat Bridge: BitChat service available in GATT table");

LOG_INFO("BitChat Bridge: Creating initial announcement for BLE characteristic...");
sendPeerAnnouncement(); // This will create and broadcast the announcement immediately

// Don't restart advertising - the service is already registered with GATT
// and the BitChat UUID is already in the scan response from initial advertising
// NimBLE will automatically include all started services in GATT table
LOG_INFO("BitChat Bridge: BitChat service available in GATT table");
} else {
LOG_ERROR("BitChat Bridge: BLE service setup failed");
bleEnabled = false;
Expand Down
27 changes: 27 additions & 0 deletions src/nimble/NimbleBluetooth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ NimBLECharacteristic *BatteryCharacteristic;
NimBLECharacteristic *logRadioCharacteristic;
NimBLEServer *bleServer;

#if !MESHTASTIC_EXCLUDE_BITCHAT_BRIDGE
NimBLEService *bitchatBLEService = nullptr;
NimBLECharacteristic *bitchatBLECharacteristic = nullptr;
#endif

static bool passkeyShowing;

class BluetoothPhoneAPI : public PhoneAPI, public concurrency::OSThread
Expand Down Expand Up @@ -445,6 +450,28 @@ void NimbleBluetooth::setupService()
batteryLevelDescriptor->setUnit(0x27ad);

batteryService->start();

#if !MESHTASTIC_EXCLUDE_BITCHAT_BRIDGE
// Setup the BitChat service - must be created here before advertising starts
LOG_INFO("NimBLE: Setting up BitChat service");
bitchatBLEService = bleServer->createService(NimBLEUUID(BITCHAT_SERVICE_UUID));
if (bitchatBLEService) {
bitchatBLECharacteristic = bitchatBLEService->createCharacteristic(
NimBLEUUID(BITCHAT_CHARACTERISTIC_UUID),
NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_NR | NIMBLE_PROPERTY::NOTIFY
);
if (bitchatBLECharacteristic) {
bitchatBLECharacteristic->setValue((uint8_t*)nullptr, 0);
LOG_INFO("NimBLE: BitChat characteristic created with READ, WRITE, WRITE_NR, NOTIFY");
} else {
LOG_ERROR("NimBLE: Failed to create BitChat characteristic");
}
bitchatBLEService->start();
LOG_INFO("NimBLE: BitChat service started");
} else {
LOG_ERROR("NimBLE: Failed to create BitChat service");
}
#endif
}

void NimbleBluetooth::startAdvertising()
Expand Down