NimBLE-Arduino 1.4.2
Loading...
Searching...
No Matches
NimBLEServer Class Reference

The model of a BLE server. More...

#include <NimBLEServer.h>

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)
 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...
 
void setCallbacks (NimBLEServerCallbacks *pCallbacks, bool deleteCallbacks=true)
 Set the server callbacks. More...
 
NimBLEExtAdvertisinggetAdvertising ()
 Retrieve the advertising object that can be used to advertise the existence of the server. More...
 
bool startAdvertising (uint8_t inst_id, int duration=0, int max_events=0)
 Start advertising. More...
 
bool stopAdvertising (uint8_t inst_id)
 Convenience function to stop advertising a data set. More...
 
bool startAdvertising ()
 Start advertising. More...
 
bool stopAdvertising ()
 Stop advertising. More...
 
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...
 
void setDataLen (uint16_t conn_handle, uint16_t tx_octets)
 Request an update of the data packet length. 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)

Create a BLE Service.

Parameters
[in]uuidThe UUID of the new 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 advertisements. 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.

◆ setDataLen()

void NimBLEServer::setDataLen ( uint16_t  conn_handle,
uint16_t  tx_octets 
)

Request an update of the data packet length.

  • Can only be used after a connection has been established.

    Sends a data length update request to the peer. The Data Length Extension (DLE) allows to increase the Data Channel Payload from 27 bytes to up to 251 bytes. The peer needs to support the Bluetooth 4.2 specifications, to be capable of DLE.

    Parameters
    [in]conn_handleThe connection handle of the peer to send the request to.
    [in]tx_octetsThe preferred number of payload octets to use (Range 0x001B-0x00FB).

◆ startAdvertising() [1/2]

bool NimBLEServer::startAdvertising ( )

Start advertising.

Returns
True if advertising started successfully.

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

◆ startAdvertising() [2/2]

bool NimBLEServer::startAdvertising ( uint8_t  inst_id,
int  duration = 0,
int  max_events = 0 
)

Start advertising.

Parameters
[in]inst_idThe extended advertisement instance ID to start.
[in]durationHow long to advertise for in milliseconds, 0 = forever (default).
[in]max_eventsMaximum number of advertisement events to send, 0 = no limit (default).
Returns
True if advertising started successfully.

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

◆ stopAdvertising() [1/2]

bool NimBLEServer::stopAdvertising ( )

Stop advertising.

Returns
True if advertising stopped successfully.

◆ stopAdvertising() [2/2]

bool NimBLEServer::stopAdvertising ( uint8_t  inst_id)

Convenience function to stop advertising a data set.

Parameters
[in]inst_idThe extended advertisement instance ID to stop advertising.
Returns
True if advertising stopped successfully.

◆ 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.