esp-nimble-cpp / NimBLE-Arduino  1.3.1
NimBLEServer Class Reference

The model of a BLE server. More...

Public Member Functions

size_t getConnectedCount ()
 Return the number of connected clients. More...
 
NimBLEServicecreateService (const char *uuid)
 Create a BLE Service. More...
 
NimBLEServicecreateService (const NimBLEUUID &uuid, uint32_t numHandles=15, uint8_t inst_id=0)
 Create a BLE Service. More...
 
void removeService (NimBLEService *service, bool deleteSvc=false)
 Remove a service from the server. More...
 
void addService (NimBLEService *service)
 Adds a service which was either already created but removed from availability,
or created and later added to services list. More...
 
NimBLEAdvertisinggetAdvertising ()
 Retrieve the advertising object that can be used to advertise the existence of the server. More...
 
void setCallbacks (NimBLEServerCallbacks *pCallbacks, bool deleteCallbacks=true)
 Set the server callbacks. More...
 
void startAdvertising ()
 Start advertising. More...
 
void stopAdvertising ()
 Stop advertising.
 
void start ()
 Start the GATT server. Required to be called after setup of all services and characteristics / descriptors for the NimBLE host to register them.
 
NimBLEServicegetServiceByUUID (const char *uuid, uint16_t instanceId=0)
 Get a BLE Service by its UUID. More...
 
NimBLEServicegetServiceByUUID (const NimBLEUUID &uuid, uint16_t instanceId=0)
 Get a BLE Service by its UUID. More...
 
NimBLEServicegetServiceByHandle (uint16_t handle)
 Get a BLE Service by its handle. More...
 
int disconnect (uint16_t connID, uint8_t reason=BLE_ERR_REM_USER_CONN_TERM)
 Disconnect the specified client with optional reason. More...
 
void updateConnParams (uint16_t conn_handle, uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout)
 Request an Update the connection parameters: More...
 
uint16_t getPeerMTU (uint16_t conn_id)
 Get the MTU of the client. More...
 
std::vector< uint16_t > getPeerDevices ()
 Get the vector of the connected client ID's.
 
NimBLEConnInfo getPeerInfo (size_t index)
 Get the connection information of a connected peer by vector index. More...
 
NimBLEConnInfo getPeerInfo (const NimBLEAddress &address)
 Get the connection information of a connected peer by address. More...
 
NimBLEConnInfo getPeerIDInfo (uint16_t id)
 Get the connection information of a connected peer by connection ID. More...
 
void advertiseOnDisconnect (bool)
 Set the server to automatically start advertising when a client disconnects. More...
 

Detailed Description

The model of a BLE server.

Member Function Documentation

◆ addService()

void NimBLEServer::addService ( NimBLEService service)

Adds a service which was either already created but removed from availability,
or created and later added to services list.

Parameters
[in]serviceThe service object to add.
Note
If it is desired to advertise the service it must be added by calling NimBLEAdvertising::addServiceUUID.

◆ advertiseOnDisconnect()

void NimBLEServer::advertiseOnDisconnect ( bool  aod)

Set the server to automatically start advertising when a client disconnects.

Parameters
[in]aodtrue == advertise, false == don't advertise.

◆ createService() [1/2]

NimBLEService * NimBLEServer::createService ( const char *  uuid)

Create a BLE Service.

Parameters
[in]uuidThe UUID of the new service.
Returns
A reference to the new service object.

◆ createService() [2/2]

NimBLEService * NimBLEServer::createService ( const NimBLEUUID uuid,
uint32_t  numHandles = 15,
uint8_t  inst_id = 0 
)

Create a BLE Service.

Parameters
[in]uuidThe UUID of the new service.
[in]numHandlesThe maximum number of handles associated with this service.
[in]inst_idif we have multiple services with the same UUID we need to provide inst_id value different for each service.
Returns
A reference to the new service object.

◆ disconnect()

int NimBLEServer::disconnect ( uint16_t  connId,
uint8_t  reason = BLE_ERR_REM_USER_CONN_TERM 
)

Disconnect the specified client with optional reason.

Parameters
[in]connIdConnection Id of the client to disconnect.
[in]reasoncode for disconnecting.
Returns
NimBLE host return code.

◆ getAdvertising()

NimBLEAdvertising * NimBLEServer::getAdvertising ( )

Retrieve the advertising object that can be used to advertise the existence of the server.

Returns
An advertising object.

◆ getConnectedCount()

size_t NimBLEServer::getConnectedCount ( )

Return the number of connected clients.

Returns
The number of connected clients.

◆ getPeerIDInfo()

NimBLEConnInfo NimBLEServer::getPeerIDInfo ( uint16_t  id)

Get the connection information of a connected peer by connection ID.

Parameters
[in]idThe connection id of the peer.

◆ getPeerInfo() [1/2]

NimBLEConnInfo NimBLEServer::getPeerInfo ( const NimBLEAddress address)

Get the connection information of a connected peer by address.

Parameters
[in]addressThe address of the peer.

◆ getPeerInfo() [2/2]

NimBLEConnInfo NimBLEServer::getPeerInfo ( size_t  index)

Get the connection information of a connected peer by vector index.

Parameters
[in]indexThe vector index of the peer.

◆ getPeerMTU()

uint16_t NimBLEServer::getPeerMTU ( uint16_t  conn_id)

Get the MTU of the client.

Returns
The client MTU or 0 if not found/connected.

◆ getServiceByHandle()

NimBLEService * NimBLEServer::getServiceByHandle ( uint16_t  handle)

Get a BLE Service by its handle.

Parameters
handleThe handle of the service.
Returns
A pointer to the service object or nullptr if not found.

◆ getServiceByUUID() [1/2]

NimBLEService * NimBLEServer::getServiceByUUID ( const char *  uuid,
uint16_t  instanceId = 0 
)

Get a BLE Service by its UUID.

Parameters
[in]uuidThe UUID of the service.
instanceIdThe index of the service to return (used when multiple services have the same UUID).
Returns
A pointer to the service object or nullptr if not found.

◆ getServiceByUUID() [2/2]

NimBLEService * NimBLEServer::getServiceByUUID ( const NimBLEUUID uuid,
uint16_t  instanceId = 0 
)

Get a BLE Service by its UUID.

Parameters
[in]uuidThe UUID of the service.
instanceIdThe index of the service to return (used when multiple services have the same UUID).
Returns
A pointer to the service object or nullptr if not found.

◆ removeService()

void NimBLEServer::removeService ( NimBLEService service,
bool  deleteSvc = false 
)

Remove a service from the server.

Immediately removes access to the service by clients, sends a service changed indication, and removes the service (if applicable) from the advertisments. The service is not deleted unless the deleteSvc parameter is true, otherwise the service remains available and can be re-added in the future. If desired a removed but not deleted service can be deleted later by calling this method with deleteSvc set to true.

Note
The service will not be removed from the database until all open connections are closed as it requires resetting the GATT server. In the interim the service will have it's visibility disabled.
Advertising will need to be restarted by the user after calling this as we must stop advertising in order to remove the service.
Parameters
[in]serviceThe service object to remove.
[in]deleteSvctrue if the service should be deleted.

◆ setCallbacks()

void NimBLEServer::setCallbacks ( NimBLEServerCallbacks pCallbacks,
bool  deleteCallbacks = true 
)

Set the server callbacks.

As a BLE server operates, it will generate server level events such as a new client connecting or a previous client disconnecting. This function can be called to register a callback handler that will be invoked when these events are detected.

Parameters
[in]pCallbacksThe callbacks to be invoked.
[in]deleteCallbacksif true callback class will be deleted when server is destructed.

◆ startAdvertising()

void NimBLEServer::startAdvertising ( )

Start advertising.

Start the server advertising its existence. This is a convenience function and is equivalent to retrieving the advertising object and invoking start upon it.

◆ updateConnParams()

void NimBLEServer::updateConnParams ( uint16_t  conn_handle,
uint16_t  minInterval,
uint16_t  maxInterval,
uint16_t  latency,
uint16_t  timeout 
)

Request an Update the connection parameters:

  • Can only be used after a connection has been established.
    Parameters
    [in]conn_handleThe connection handle of the peer to send the request to.
    [in]minIntervalThe minimum connection interval in 1.25ms units.
    [in]maxIntervalThe maximum connection interval in 1.25ms units.
    [in]latencyThe number of packets allowed to skip (extends max interval).
    [in]timeoutThe timeout time in 10ms units before disconnecting.