diff --git a/library.properties b/library.properties index cacec6e6..dcdb63b7 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ -name=ArduinoBLE -version=1.4.1 +name=ArduinoBLE-CodedPHY +version=1.4.2-custom author=Arduino maintainer=Arduino sentence=Enables Bluetooth® Low Energy connectivity on the Arduino MKR WiFi 1010, Arduino UNO WiFi Rev2, Arduino Nano 33 IoT, Arduino Nano 33 BLE, Nicla Sense ME and UNO R4 WiFi. diff --git a/src/BLEDevice.cpp b/src/BLEDevice.cpp index e9ca9b99..810402e4 100644 --- a/src/BLEDevice.cpp +++ b/src/BLEDevice.cpp @@ -25,6 +25,10 @@ #include "BLEDevice.h" +#include "ble/AdvertisingParameters.h" +#include "ble/Gap.h" + + extern "C" int strcasecmp(char const *a, char const *b); BLEDevice::BLEDevice() : @@ -576,3 +580,25 @@ bool BLEDevice::discovered() return (_advertisementTypeMask & 0x18) != 0; } +void BLEDevice::setTxPower(int8_t dbm) { + if (!_ble_interface.hasInitialized()) return; + _ble_interface.gap().setTxPower(dbm); +} + +void BLEDevice::enableCodedPHYAdvertising(bool enabled) { + if (!_ble_interface.hasInitialized()) return; + + ble::AdvertisingParameters advParams( + ble::advertising_type_t::NON_CONNECTABLE_UNDIRECTED, + ble::adv_interval_t(ble::millisecond_t(200)) + ); + if (enabled) { + advParams.setPhy(ble::phy_t::LE_1M, ble::phy_t::LE_CODED); + } + auto& gap = _ble_interface.gap(); + gap.setAdvertisingParameters(_adv_handle, advParams); + + // Set TX power here if not using setTxPower separately + gap.setTxPower(4); +} + diff --git a/src/BLEDevice.h b/src/BLEDevice.h index bf710744..d3f9597c 100644 --- a/src/BLEDevice.h +++ b/src/BLEDevice.h @@ -93,6 +93,17 @@ class BLEDevice { BLECharacteristic characteristic(const char * uuid) const; BLECharacteristic characteristic(const char * uuid, int index) const; + /** + * Enable advertising on Coded PHY (long range). + * Must be called after BLE.begin() and before BLE.advertise(). + */ + void enableCodedPHYAdvertising(bool enabled = true); + + /** + * Set advertise and connection transmit power (in dBm, max +4 for Nano 33 BLE Rev2). + */ + void setTxPower(int8_t dbm); + protected: friend class ATTClass; friend class GAPClass;