diff --git a/libraries/CurieBLE/src/BLEPeripheral.cpp b/libraries/CurieBLE/src/BLEPeripheral.cpp index 2bcee466..5bd0f1ec 100644 --- a/libraries/CurieBLE/src/BLEPeripheral.cpp +++ b/libraries/CurieBLE/src/BLEPeripheral.cpp @@ -146,8 +146,7 @@ BLECentral BLEPeripheral::central(void) bool BLEPeripheral::connected(void) { - BLEDevice centralBle = BLE.central(); - return centralBle.connected(); + return BLE.connected(); } void BLEPeripheral::init() diff --git a/libraries/CurieBLE/src/internal/BLEDeviceManager.cpp b/libraries/CurieBLE/src/internal/BLEDeviceManager.cpp index ed00e863..6afd9827 100644 --- a/libraries/CurieBLE/src/internal/BLEDeviceManager.cpp +++ b/libraries/CurieBLE/src/internal/BLEDeviceManager.cpp @@ -171,18 +171,43 @@ void BLEDeviceManager::end() bool BLEDeviceManager::connected(const BLEDevice *device) const { - bt_conn_t* conn = bt_conn_lookup_addr_le(device->bt_le_address()); bool retval = false; - //pr_debug(LOG_MODULE_BLE, "%s-%d: add-%s", __FUNCTION__, __LINE__, device->address().c_str()); - if (NULL != conn) + + if (BLEUtils::isLocalBLE(*device)) { - //pr_debug(LOG_MODULE_BLE, "%s-%d: state-%d", __FUNCTION__, __LINE__,conn->state); - if (conn->state == BT_CONN_CONNECTED) + // first check if in peripheral mode and a central is connected + if (BLEUtils::macAddressValid(_peer_central)) { retval = true; } - bt_conn_unref(conn); + else // next check if in central mode and connected to any peripherals + { + for (int i = 0; i < BLE_MAX_CONN_CFG; i++) + { + if (BLEUtils::macAddressValid(_peer_peripheral[i])) + { + retval = true; + break; + } + } + } + } + else + { + bt_conn_t* conn = bt_conn_lookup_addr_le(device->bt_le_address()); + + //pr_debug(LOG_MODULE_BLE, "%s-%d: add-%s", __FUNCTION__, __LINE__, device->address().c_str()); + if (NULL != conn) + { + //pr_debug(LOG_MODULE_BLE, "%s-%d: state-%d", __FUNCTION__, __LINE__,conn->state); + if (conn->state == BT_CONN_CONNECTED) + { + retval = true; + } + bt_conn_unref(conn); + } } + return retval; }