18#ifndef NIMBLE_CPP_CLIENT_H_
19#define NIMBLE_CPP_CLIENT_H_
22#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
24# if defined(CONFIG_NIMBLE_CPP_IDF)
25# include "host/ble_gap.h"
27# include "nimble/nimble/host/include/host/ble_gap.h"
30# include "NimBLEAddress.h"
52 bool deleteAttributes =
true,
53 bool asyncConnect =
false,
56 bool connect(
bool deleteAttributes =
true,
bool asyncConnect =
false,
bool exchangeMTU =
true);
57 bool disconnect(uint8_t reason = BLE_ERR_REM_USER_CONN_TERM);
59 void setSelfDelete(
bool deleteOnDisconnect,
bool deleteOnConnectFail);
75 bool updateConnParams(uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout);
80 uint16_t scanInterval = 16,
81 uint16_t scanWindow = 16);
82 const std::vector<NimBLERemoteService*>&
getServices(
bool refresh =
false);
83 std::vector<NimBLERemoteService*>::iterator
begin();
84 std::vector<NimBLERemoteService*>::iterator
end();
94 bool response =
false);
96# if CONFIG_BT_NIMBLE_EXT_ADV
98 bool updatePhy(uint8_t txPhysMask, uint8_t rxPhysMask, uint16_t phyOptions = 0);
99 bool getPhy(uint8_t* txPhy, uint8_t* rxPhy);
103 uint8_t deleteCallbacks : 1;
104 uint8_t deleteOnDisconnect : 1;
105 uint8_t deleteOnConnectFail : 1;
106 uint8_t asyncConnect : 1;
107 uint8_t exchangeMTU : 1;
119 bool retrieveServices(
const NimBLEUUID* uuidFilter =
nullptr);
120 static int handleGapEvent(
struct ble_gap_event* event,
void* arg);
121 static int exchangeMTUCb(uint16_t conn_handle,
const ble_gatt_error* error, uint16_t mtu,
void* arg);
122 static int serviceDiscoveredCB(uint16_t connHandle,
123 const struct ble_gatt_error* error,
124 const struct ble_gatt_svc* service,
128 mutable int m_lastErr;
129 int32_t m_connectTimeout;
131 std::vector<NimBLERemoteService*> m_svcVec;
133 uint16_t m_connHandle;
134 uint8_t m_terminateFailCount;
135 mutable uint8_t m_asyncSecureAttempt;
138# if CONFIG_BT_NIMBLE_EXT_ADV
141 ble_gap_conn_params m_connParams;
216# if CONFIG_BT_NIMBLE_EXT_ADV
A BLE device address.
Definition NimBLEAddress.h:41
A representation of a BLE advertised device found by a scan.
Definition NimBLEAdvertisedDevice.h:45
A specialized container class to hold BLE attribute values.
Definition NimBLEAttValue.h:72
Callbacks associated with a BLE client.
Definition NimBLEClient.h:150
virtual void onPhyUpdate(NimBLEClient *pClient, uint8_t txPhy, uint8_t rxPhy)
Called when the PHY update procedure is complete.
Definition NimBLEClient.cpp:1302
virtual void onConfirmPasskey(NimBLEConnInfo &connInfo, uint32_t pin)
Called when using numeric comparision for pairing.
Definition NimBLEClient.cpp:1292
virtual bool onConnParamsUpdateRequest(NimBLEClient *pClient, const ble_gap_upd_params *params)
Called when server requests to update the connection parameters.
Definition NimBLEClient.cpp:1274
virtual void onMTUChange(NimBLEClient *pClient, uint16_t MTU)
Called when the connection MTU changes.
Definition NimBLEClient.cpp:1297
virtual void onConnect(NimBLEClient *pClient)
Called after client connects.
Definition NimBLEClient.cpp:1262
virtual void onPassKeyEntry(NimBLEConnInfo &connInfo)
Called when server requests a passkey for pairing.
Definition NimBLEClient.cpp:1279
virtual void onIdentity(NimBLEConnInfo &connInfo)
Called when the peer identity address is resolved.
Definition NimBLEClient.cpp:1288
virtual void onConnectFail(NimBLEClient *pClient, int reason)
Called when a connection attempt fails.
Definition NimBLEClient.cpp:1266
virtual void onDisconnect(NimBLEClient *pClient, int reason)
Called when disconnected from the server.
Definition NimBLEClient.cpp:1270
virtual void onAuthenticationComplete(NimBLEConnInfo &connInfo)
Called when the pairing procedure is complete.
Definition NimBLEClient.cpp:1284
A model of a BLE client.
Definition NimBLEClient.h:49
void setConnectPhy(uint8_t phyMask)
Set the PHY types to use when connecting to a server.
Definition NimBLEClient.cpp:408
std::vector< NimBLERemoteService * >::iterator end()
Get iterator to the end of the vector of remote service pointers.
Definition NimBLEClient.cpp:614
bool isConnected() const
Are we connected to a server?
Definition NimBLEClient.cpp:1218
size_t deleteService(const NimBLEUUID &uuid)
Delete a service by UUID from the local database to free resources.
Definition NimBLEClient.cpp:116
bool discoverAttributes()
Retrieves the full database of attributes that the peripheral has available.
Definition NimBLEClient.cpp:702
bool updatePhy(uint8_t txPhysMask, uint8_t rxPhysMask, uint16_t phyOptions=0)
Request a change to the PHY used for this peer connection.
Definition NimBLEClient.cpp:430
void setConnectionParams(uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout, uint16_t scanInterval=16, uint16_t scanWindow=16)
Set the connection parameters to use when connecting to a server.
Definition NimBLEClient.cpp:464
bool exchangeMTU()
Begin the MTU exchange process with the server.
Definition NimBLEClient.cpp:905
int getRssi() const
Ask the BLE server for the RSSI value.
Definition NimBLEClient.cpp:585
bool connect(const NimBLEAdvertisedDevice *device, bool deleteAttributes=true, bool asyncConnect=false, bool exchangeMTU=true)
Connect to an advertising device.
Definition NimBLEClient.cpp:155
bool secureConnection(bool async=false) const
Initiate a secure connection (pair/bond) with the server. Called automatically when a characteristic ...
Definition NimBLEClient.cpp:306
bool disconnect(uint8_t reason=BLE_ERR_REM_USER_CONN_TERM)
Disconnect from the peer.
Definition NimBLEClient.cpp:347
NimBLEAttValue getValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID)
Get the value of a specific characteristic associated with a specific service.
Definition NimBLEClient.cpp:805
NimBLEAddress getPeerAddress() const
Retrieve the address of the peer.
Definition NimBLEClient.cpp:562
bool setDataLen(uint16_t txOctets)
Request an update of the data packet length.
Definition NimBLEClient.cpp:514
NimBLEConnInfo getConnInfo() const
Get detailed information about the current peer connection.
Definition NimBLEClient.cpp:533
void setSelfDelete(bool deleteOnDisconnect, bool deleteOnConnectFail)
Set or unset a flag to delete this client when disconnected or connection failed.
Definition NimBLEClient.cpp:378
void deleteServices()
Delete all service objects created by this client and clear the vector.
Definition NimBLEClient.cpp:102
NimBLERemoteCharacteristic * getCharacteristic(uint16_t handle)
Get the remote characteristic with the specified handle.
Definition NimBLEClient.cpp:859
bool getPhy(uint8_t *txPhy, uint8_t *rxPhy)
Get the PHY used for this peer connection.
Definition NimBLEClient.cpp:445
uint16_t getMTU() const
Get the current mtu of this connection.
Definition NimBLEClient.cpp:877
bool setPeerAddress(const NimBLEAddress &address)
Set the peer address.
Definition NimBLEClient.cpp:571
std::string toString() const
Return a string representation of this client.
Definition NimBLEClient.cpp:1241
bool setValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID, const NimBLEAttValue &value, bool response=false)
Set the value of a specific characteristic associated with a specific service.
Definition NimBLEClient.cpp:832
std::vector< NimBLERemoteService * >::iterator begin()
Get iterator to the beginning of the vector of remote service pointers.
Definition NimBLEClient.cpp:606
Config getConfig() const
Get a copy of the clients configuration.
Definition NimBLEClient.cpp:387
uint16_t getConnHandle() const
Get the connection handle for this client.
Definition NimBLEClient.cpp:554
bool updateConnParams(uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout)
Update the connection parameters:
Definition NimBLEClient.cpp:488
void setConfig(Config config)
Set the client configuration options.
Definition NimBLEClient.cpp:395
const std::vector< NimBLERemoteService * > & getServices(bool refresh=false)
Get a pointer to the vector of found services.
Definition NimBLEClient.cpp:685
NimBLERemoteService * getService(const char *uuid)
Get the service BLE Remote Service instance corresponding to the uuid.
Definition NimBLEClient.cpp:623
int getLastError() const
Get the last error code reported by the NimBLE host.
Definition NimBLEClient.cpp:1258
void setConnectTimeout(uint32_t timeout)
Set the timeout to wait for connection attempt to complete.
Definition NimBLEClient.cpp:546
void setClientCallbacks(NimBLEClientCallbacks *pClientCallbacks, bool deleteCallbacks=true)
Set the callbacks that will be invoked when events are received.
Definition NimBLEClient.cpp:1227
bool cancelConnect() const
Cancel an ongoing connection attempt.
Definition NimBLEClient.cpp:362
Connection information.
Definition NimBLEConnInfo.h:32
A model of a BLE Device from which all the BLE roles are created.
Definition NimBLEDevice.h:109
A model of a remote BLE characteristic.
Definition NimBLERemoteCharacteristic.h:34
A model of a remote BLE service.
Definition NimBLERemoteService.h:34
The model of a BLE server.
Definition NimBLEServer.h:60
A model of a BLE UUID.
Definition NimBLEUUID.h:41
A structure to hold data for a task that is waiting for a response.
Definition NimBLEUtils.h:32