Define device discovery, connection and link management procedures. More...
#include <Gap.h>
Data Structures | |
struct | EventHandler |
Definition of the general handler of Gap related events. More... | |
Public Member Functions | |
void | setEventHandler (EventHandler *handler) |
Assign the event handler implementation that will be used by the gap module to signal events back to the application. More... | |
bool | isFeatureSupported (controller_supported_features_t feature) |
Check controller support for a specific feature. More... | |
uint8_t | getMaxAdvertisingSetNumber () |
Return currently available number of supported advertising sets. More... | |
uint16_t | getMaxAdvertisingDataLength () |
Return maximum advertising data length supported. More... | |
uint16_t | getMaxConnectableAdvertisingDataLength () |
Return maximum advertising data length supported for connectable advertising. More... | |
uint16_t | getMaxActiveSetAdvertisingDataLength () |
Return maximum advertising data length you may set if advertising set is active. More... | |
ble_error_t | createAdvertisingSet (advertising_handle_t *handle, const AdvertisingParameters ¶meters) |
Create an advertising set and apply the passed in parameters. More... | |
ble_error_t | destroyAdvertisingSet (advertising_handle_t handle) |
Remove the advertising set (resets its set parameters). More... | |
ble_error_t | setAdvertisingParameters (advertising_handle_t handle, const AdvertisingParameters ¶ms) |
Set advertising parameters of an existing set. More... | |
ble_error_t | setAdvertisingPayload (advertising_handle_t handle, mbed::Span< const uint8_t > payload) |
Set new advertising payload for a given advertising set. More... | |
ble_error_t | setAdvertisingScanResponse (advertising_handle_t handle, mbed::Span< const uint8_t > response) |
Set new advertising scan response for a given advertising set. More... | |
ble_error_t | startAdvertising (advertising_handle_t handle, adv_duration_t maxDuration=adv_duration_t::forever(), uint8_t maxEvents=0) |
Start advertising using the given advertising set. More... | |
ble_error_t | stopAdvertising (advertising_handle_t handle) |
Stop advertising given advertising set. More... | |
bool | isAdvertisingActive (advertising_handle_t handle) |
Check if advertising is active for a given advertising set. More... | |
ble_error_t | setPeriodicAdvertisingParameters (advertising_handle_t handle, periodic_interval_t periodicAdvertisingIntervalMin, periodic_interval_t periodicAdvertisingIntervalMax, bool advertiseTxPower=true) |
Set periodic advertising parameters for a given advertising set. More... | |
ble_error_t | setPeriodicAdvertisingPayload (advertising_handle_t handle, mbed::Span< const uint8_t > payload) |
Set new periodic advertising payload for a given advertising set. More... | |
ble_error_t | startPeriodicAdvertising (advertising_handle_t handle) |
Start periodic advertising for a given set. More... | |
ble_error_t | stopPeriodicAdvertising (advertising_handle_t handle) |
Stop periodic advertising for a given set. More... | |
bool | isPeriodicAdvertisingActive (advertising_handle_t handle) |
Check if periodic advertising is active for a given advertising set. More... | |
ble_error_t | setScanParameters (const ScanParameters ¶ms) |
Set new scan parameters. More... | |
ble_error_t | startScan (scan_duration_t duration=scan_duration_t::forever(), duplicates_filter_t filtering=duplicates_filter_t::DISABLE, scan_period_t period=scan_period_t(0)) |
Start scanning. More... | |
ble_error_t | stopScan () |
Stop the ongoing scanning procedure. More... | |
ble_error_t | createSync (peer_address_type_t peerAddressType, const address_t &peerAddress, uint8_t sid, slave_latency_t maxPacketSkip, sync_timeout_t timeout) |
Synchronize with periodic advertising from an advertiser and begin receiving periodic advertising packets. More... | |
ble_error_t | createSync (slave_latency_t maxPacketSkip, sync_timeout_t timeout) |
Synchronize with periodic advertising from an advertiser and begin receiving periodic advertising packets. More... | |
ble_error_t | cancelCreateSync () |
Cancel sync attempt. More... | |
ble_error_t | terminateSync (periodic_sync_handle_t handle) |
Stop reception of the periodic advertising identified by the handle. More... | |
ble_error_t | addDeviceToPeriodicAdvertiserList (peer_address_type_t peerAddressType, const address_t &peerAddress, advertising_sid_t sid) |
Add device to the periodic advertiser list. More... | |
ble_error_t | removeDeviceFromPeriodicAdvertiserList (peer_address_type_t peerAddressType, const address_t &peerAddress, advertising_sid_t sid) |
Remove device from the periodic advertiser list. More... | |
ble_error_t | clearPeriodicAdvertiserList () |
Remove all devices from periodic advertiser list. More... | |
uint8_t | getMaxPeriodicAdvertiserListSize () |
Get number of devices that can be added to the periodic advertiser list. More... | |
ble_error_t | connect (peer_address_type_t peerAddressType, const address_t &peerAddress, const ConnectionParameters &connectionParams) |
Initiate a connection to a peer. More... | |
ble_error_t | cancelConnect () |
Cancel the connection attempt. More... | |
ble_error_t | readPhy (connection_handle_t connection) |
Read the PHY used by the transmitter and the receiver on a connection. More... | |
ble_error_t | setPreferredPhys (const phy_set_t *txPhys, const phy_set_t *rxPhys) |
Set the preferred PHYs to use in a connection. More... | |
ble_error_t | setPhy (connection_handle_t connection, const phy_set_t *txPhys, const phy_set_t *rxPhys, coded_symbol_per_bit_t codedSymbol) |
Update the PHY used by a connection. More... | |
ble_error_t | enablePrivacy (bool enable) |
Enable or disable privacy mode of the local device. More... | |
ble_error_t | setPeripheralPrivacyConfiguration (const peripheral_privacy_configuration_t *configuration) |
Set the privacy configuration used by the peripheral role. More... | |
ble_error_t | getPeripheralPrivacyConfiguration (peripheral_privacy_configuration_t *configuration) |
Get the privacy configuration used by the peripheral role. More... | |
ble_error_t | setCentralPrivacyConfiguration (const central_privay_configuration_t *configuration) |
Set the privacy configuration used by the central role. More... | |
ble_error_t | getCentralPrivacyConfiguration (central_privay_configuration_t *configuration) |
Get the privacy configuration used by the central role. More... | |
Static Public Attributes | |
static const peripheral_privacy_configuration_t | default_peripheral_privacy_configuration |
Default peripheral privacy configuration. More... | |
static const central_privay_configuration_t | default_central_privacy_configuration |
Default peripheral privacy configuration. More... | |
Define device discovery, connection and link management procedures.
Instance of a Gap class for a given BLE device should be accessed using BLE::gap(). The reference returned remains valid until the BLE instance shut down (see BLE::shutdown()).
Advertising consists of broadcasting at a regular interval a small amount of data containing valuable information about the device. These packets may be scanned by peer devices listening on BLE advertising channels.
Scanners may also request additional information from a device advertising by sending a scan request. If the broadcaster accepts scan requests, it can reply with a scan response packet containing additional information.
Advertising parameters are updated using setAdvertisingParams(). The main advertising payload is updated using setAdvertisingPayload(), and the scan response is updated using setAdvertisingScanResponse(). If the advertising is already updated, the data will take effect from the next advertising event.
To create a valid advertising payload and scan response, you may use AdvertisingDataBuilder. You must first allocate memory and create an mbed::Span and pass that into the AdvertisingDataBuilder, which will only be able to add as much data as fits in the provided buffer. The builder accepts any size of the buffer, but for the created data to be usable, it must be smaller than the maximum data length returned from getMaxAdvertisingDataLength().
Another option is to use AdvertisingDataSimpleBuilder, which allocates memory on the stack and offers a fluent interface at the expense of a reduced set of APIs and error management options.
Extended advertising allows for a wider choice of options than legacy advertising. You can send bigger payloads and use different PHYs. This allows for bigger throughput or longer range.
Extended advertising may be split across many packets and takes place on both the regular advertising channels and the rest of the 37 channels normally used by connected devices.
The 3 channels used in legacy advertising are called primary advertisement channels. The remaining 37 channels are used for secondary advertising. Unlike sending data during a connection, this allows the device to broadcast data to multiple devices.
The advertising starts on the primary channels (which you may select) and continues on the secondary channels as indicated in the packet sent on the primary channel. This way, the advertising can send large payloads without saturating the advertising channels. Primary channels are limited to 1M and coded PHYs, but secondary channels may use the increased throughput 2M PHY.
Similarly, you can use periodic advertising to transfer regular data to multiple devices.
The advertiser uses primary channels to advertise the information needed to listen to the periodic advertisements on secondary channels. This sync information will be used by the scanner who can now optimize for power consumption and only listen for the periodic advertisements at specified times.
Like extended advertising, periodic advertising offers extra PHY options of 2M and coded. The payload may be updated at any time and will be updated on the next advertisement event when the periodic advertising is active.
Advertisers may advertise multiple payloads at the same time. The configuration and identification of these is done through advertising sets. Use a handle obtained from createAvertisingSet() for advertising operations. After ending all advertising operations, remove the handle from the system using destroyAdvertisingHandle().
Extended advertising and periodic advertising is an optional feature, and not all devices support it. Some will only be able to see the now-called legacy advertising.
Legacy advertising is available through a special handle, LEGACY_ADVERTISING_HANDLE. This handle is always available, doesn't need to be created and can't be destroyed.
There is a limited number of advertising sets available because they require support from the controller. Their availability is dynamic and may be queried at any time using getMaxAdvertisingSetNumber(). Advertising sets take up resources even if they are not actively advertising right now, so it's important to destroy the set when you're done with it (or reuse it in the next advertisement).
Periodic advertising and extended advertising share the same set but not the same data. Extended advertising carries out periodic advertising synchronization information. Therefore, to let other devices be aware that your device exposes periodic advertising, you should start extended advertising of the set. Subsequently, you may disable extended advertising, and the periodic advertising will continue. If you start periodic advertising while extended advertising is inactive, periodic advertising won't start until you start extended advertising at a later time.
Privacy is a feature that allows a device to avoid being tracked by other (untrusted) devices. The device achieves it by periodically generating a new random address. The random address may be a resolvable random address, enabling trusted devices to recognize it as belonging to the same device. These trusted devices receive an Identity Resolution Key (IRK) during pairing. This is handled by the SecurityManager and relies on the other device accepting and storing the IRK.
You need to enable privacy by calling enablePrivacy() after having initialized the SecurityManager because privacy requires SecurityManager to handle IRKs. The behavior of privacy enabled devices is set by using setCentralPrivacyConfiguration(), which specifies what the device should be with devices using random addresses. Random addresses generated by privacy enabled devices can be of two types: resolvable (by devices who have the IRK) and unresolvable. Unresolvable addresses can't be used for connecting and connectable advertising. Therefore, a resolvable one will be used for these regardless of the privacy configuration.
Scanning consists of listening for peer advertising packets. From a scan, a device can identify devices available in its environment.
If the device scans actively, then it will send scan request to scannable advertisers and collect their scan responses.
Scanning is done by creating ScanParameters and applying them with setScanParameters(). One configured, you may call startScan().
When a scanning device receives an advertising packet, it will call onAdvertisingReport() in the registered event handler. A whitelist may be used to limit the advertising reports by setting the correct policy in the scan parameters.
A peer may connect device advertising connectable packets. The advertising procedure ends as soon as the device is connected. If an advertising timeout has been set in the advertising parameters then onAdvertisingEnd will be called in the registered eventHandler when it runs out.
A device accepting a connection request from a peer is named a peripheral, and the device initiating the connection is named a central.
Connection is initiated by central devices. A call to connect() will result in the device scanning on any PHYs set in ConectionParamters passed in.
Peripheral and central receive a connection event when the connection is effective. If successful will result in a call to onConnectionComplete in the EventHandler registered with the Gap.
It the connection attempt fails it will result in onConnectionComplete called on the central device with the event carrying the error flag.
Once a connection has been established, it is possible to change the physical transport used between the local and the distant device. Changing the transport can either increase the bandwidth or increase the communication range. An increased bandwidth equals a better power consumption but also a loss in sensibility and therefore a degraded range.
Symmetrically an increased range means a lowered bandwidth and a degraded power consumption.
Applications can change the PHY used by calling the function setPhy. Once the update has been made the result is forwarded to the application by calling the function onPhyUpdateComplete of the event handler registered.
The application code initiates a disconnection when it calls the disconnect(Handle_t, DisconnectionReason_t) function.
Disconnection may also be initiated by the remote peer or the local controller/stack. To catch all disconnection events, application code may set up an handler taking care of disconnection events by calling onDisconnection().
When supported by the host and controller you can select different modulation schemes (
You may set preferred PHYs (separately for RX and TX) using setPreferredPhys(). You may also set the currently used PHYs on a selected connection using setPhy(). Both of these settings are only advisory and the controller is allowed to make its own decision on the best PHY to use based on your request, the peer's supported features and the connection's physical conditions.
You may query the currently used PHY using readPhy() which will return the result through a call to the registered event handler. You may register the handler with setEventHandler(). The events inform about the currently used PHY and of any changes to PHYs which may be triggered autonomously by the controller or by the peer.
ble_error_t addDeviceToPeriodicAdvertiserList | ( | peer_address_type_t | peerAddressType, |
const address_t & | peerAddress, | ||
advertising_sid_t | sid | ||
) |
Add device to the periodic advertiser list.
Cannot be called when sync is ongoing.
peerAddressType | Peer address type. |
peerAddress | Peer address. |
sid | Advertiser set identifier. |
ble_error_t cancelConnect | ( | ) |
Cancel the connection attempt.
This is not guaranteed to succeed. As a result onConnectionComplete in the event handler will be called. Check the success parameter to see if the connection was created.
ble_error_t cancelCreateSync | ( | ) |
Cancel sync attempt.
ble_error_t clearPeriodicAdvertiserList | ( | ) |
Remove all devices from periodic advertiser list.
ble_error_t connect | ( | peer_address_type_t | peerAddressType, |
const address_t & | peerAddress, | ||
const ConnectionParameters & | connectionParams | ||
) |
Initiate a connection to a peer.
Once the connection is established an onConnectionComplete in the event handler will be called.
peerAddressType | |
peerAddress | |
connectionParams |
ble_error_t createAdvertisingSet | ( | advertising_handle_t * | handle, |
const AdvertisingParameters & | parameters | ||
) |
Create an advertising set and apply the passed in parameters.
The handle returned by this function must be used for all other calls that accept an advertising handle. When done with advertising, remove from the system using destroyAdvertisingSet().
[out] | handle | Advertising handle returned, valid only if function returned success. |
parameters | Advertising parameters for the newly created set. |
ble_error_t createSync | ( | peer_address_type_t | peerAddressType, |
const address_t & | peerAddress, | ||
uint8_t | sid, | ||
slave_latency_t | maxPacketSkip, | ||
sync_timeout_t | timeout | ||
) |
Synchronize with periodic advertising from an advertiser and begin receiving periodic advertising packets.
peerAddressType | Peer address type. |
peerAddress | Peer address. |
sid | Advertiser set identifier. |
maxPacketSkip | Number of consecutive periodic advertising packets that the receiver may skip after successfully receiving a periodic advertising packet. |
timeout | Maximum permitted time between successful receptions. If this time is exceeded, synchronisation is lost. |
ble_error_t createSync | ( | slave_latency_t | maxPacketSkip, |
sync_timeout_t | timeout | ||
) |
Synchronize with periodic advertising from an advertiser and begin receiving periodic advertising packets.
Use periodic advertising sync list to determine who to sync with.
maxPacketSkip | Number of consecutive periodic advertising packets that the receiver may skip after successfully receiving a periodic advertising packet. |
timeout | Maximum permitted time between successful receives. If this time is exceeded, synchronisation is lost. |
ble_error_t destroyAdvertisingSet | ( | advertising_handle_t | handle | ) |
Remove the advertising set (resets its set parameters).
The advertising set must not be active.
handle | Advertising set handle. |
ble_error_t enablePrivacy | ( | bool | enable | ) |
Enable or disable privacy mode of the local device.
When privacy is enabled, the system use private addresses while it scans, advertises or initiate a connection. The device private address is renewed every 15 minutes.
The privacy feature can be configured with the help of the functions setPeripheralPrivacyConfiguration and setCentralPrivacyConfiguration which respectively set the privacy configuration of the peripheral and central role.
By default private resolvable addresses are used for all procedures; including advertisement of nonconnectable packets. Connection request from an unknown initiator with a private resolvable address triggers the pairing procedure.
By default private resolvable addresses are used for all procedures; including active scanning. Addresses present in advertisement packet are resolved and advertisement packets are forwarded to the application even if the advertiser private address is unknown.
[in] | enable | Should be set to true to enable the privacy mode and false to disable it. |
ble_error_t getCentralPrivacyConfiguration | ( | central_privay_configuration_t * | configuration | ) |
Get the privacy configuration used by the central role.
[out] | configuration | The variable filled with the current configuration. |
uint16_t getMaxActiveSetAdvertisingDataLength | ( | ) |
Return maximum advertising data length you may set if advertising set is active.
uint16_t getMaxAdvertisingDataLength | ( | ) |
Return maximum advertising data length supported.
uint8_t getMaxAdvertisingSetNumber | ( | ) |
Return currently available number of supported advertising sets.
This may change at runtime.
uint16_t getMaxConnectableAdvertisingDataLength | ( | ) |
Return maximum advertising data length supported for connectable advertising.
uint8_t getMaxPeriodicAdvertiserListSize | ( | ) |
Get number of devices that can be added to the periodic advertiser list.
ble_error_t getPeripheralPrivacyConfiguration | ( | peripheral_privacy_configuration_t * | configuration | ) |
Get the privacy configuration used by the peripheral role.
[out] | configuration | The variable filled with the current configuration. |
bool isAdvertisingActive | ( | advertising_handle_t | handle | ) |
Check if advertising is active for a given advertising set.
handle | Advertising set handle. |
bool isFeatureSupported | ( | controller_supported_features_t | feature | ) |
Check controller support for a specific feature.
feature | Feature to check. |
bool isPeriodicAdvertisingActive | ( | advertising_handle_t | handle | ) |
Check if periodic advertising is active for a given advertising set.
handle | Advertising set handle. |
ble_error_t readPhy | ( | connection_handle_t | connection | ) |
Read the PHY used by the transmitter and the receiver on a connection.
Once the PHY has been read, it is reported back via the function onPhyRead of the event handler registered by the application.
connection | Handle of the connection for which the PHY being used is queried. |
ble_error_t removeDeviceFromPeriodicAdvertiserList | ( | peer_address_type_t | peerAddressType, |
const address_t & | peerAddress, | ||
advertising_sid_t | sid | ||
) |
Remove device from the periodic advertiser list.
Cannot be called when sync is ongoing.
peerAddressType | Peer address type. |
peerAddress | Peer address. |
sid | Advertiser set identifier. |
ble_error_t setAdvertisingParameters | ( | advertising_handle_t | handle, |
const AdvertisingParameters & | params | ||
) |
Set advertising parameters of an existing set.
handle | Advertising set handle. |
params | New advertising parameters. |
ble_error_t setAdvertisingPayload | ( | advertising_handle_t | handle, |
mbed::Span< const uint8_t > | payload | ||
) |
Set new advertising payload for a given advertising set.
handle | Advertising set handle. |
payload | Advertising payload. |
ble_error_t setAdvertisingScanResponse | ( | advertising_handle_t | handle, |
mbed::Span< const uint8_t > | response | ||
) |
Set new advertising scan response for a given advertising set.
This will be sent to device who perform active scanning.
handle | Advertising set handle. |
response | Advertising scan response. |
ble_error_t setCentralPrivacyConfiguration | ( | const central_privay_configuration_t * | configuration | ) |
Set the privacy configuration used by the central role.
[in] | configuration | The configuration to set. |
void setEventHandler | ( | EventHandler * | handler | ) |
Assign the event handler implementation that will be used by the gap module to signal events back to the application.
handler | Application implementation of an EventHandler. |
ble_error_t setPeriodicAdvertisingParameters | ( | advertising_handle_t | handle, |
periodic_interval_t | periodicAdvertisingIntervalMin, | ||
periodic_interval_t | periodicAdvertisingIntervalMax, | ||
bool | advertiseTxPower = true |
||
) |
Set periodic advertising parameters for a given advertising set.
handle | Advertising set handle. |
periodicAdvertisingIntervalMin | Minimum interval for periodic advertising. |
periodicAdvertisingIntervalMax | Maximum interval for periodic advertising. |
advertiseTxPower | Include transmission power in the advertisements. |
ble_error_t setPeriodicAdvertisingPayload | ( | advertising_handle_t | handle, |
mbed::Span< const uint8_t > | payload | ||
) |
Set new periodic advertising payload for a given advertising set.
handle | Advertising set handle. |
payload | Advertising payload. |
ble_error_t setPeripheralPrivacyConfiguration | ( | const peripheral_privacy_configuration_t * | configuration | ) |
Set the privacy configuration used by the peripheral role.
[in] | configuration | The configuration to set. |
ble_error_t setPhy | ( | connection_handle_t | connection, |
const phy_set_t * | txPhys, | ||
const phy_set_t * | rxPhys, | ||
coded_symbol_per_bit_t | codedSymbol | ||
) |
Update the PHY used by a connection.
Once the update process has been completed, it is reported back to the application via the function onPhyUpdateComplete of the event handler registered by the application.
connection | Handle of the connection to update. |
txPhys | Set of PHYs preferred for tx operations. If NULL then the choice is up to the Bluetooth subsystem. |
rxPhys | Set of PHYs preferred for rx operations. If NULL then the choice is up to the Bluetooth subsystem. |
codedSymbol | Number of symbols used to code a bit when le coded is used. If the value is UNDEFINED then the choice is up to the Bluetooth subsystem. |
ble_error_t setPreferredPhys | ( | const phy_set_t * | txPhys, |
const phy_set_t * | rxPhys | ||
) |
Set the preferred PHYs to use in a connection.
txPhys | Set of PHYs preferred for tx operations. If NULL then no preferred PHYs are set and the default value of the subsystem is used. |
rxPhys | Set of PHYs preferred for rx operations. If NULL then no preferred PHYs are set and the default value of the subsystem is used. |
ble_error_t setScanParameters | ( | const ScanParameters & | params | ) |
Set new scan parameters.
params | Scan parameters, |
ble_error_t startAdvertising | ( | advertising_handle_t | handle, |
adv_duration_t | maxDuration = adv_duration_t::forever() , |
||
uint8_t | maxEvents = 0 |
||
) |
Start advertising using the given advertising set.
handle | Advertising set handle. |
maxDuration | Max duration for advertising (in units of 10ms) - 0 means no limit. |
maxEvents | Max number of events produced during advertising - 0 means no limit. |
ble_error_t startPeriodicAdvertising | ( | advertising_handle_t | handle | ) |
Start periodic advertising for a given set.
Periodic advertising will not start until normal advertising is running but will continue to run after normal advertising has stopped.
handle | Advertising set handle. |
ble_error_t startScan | ( | scan_duration_t | duration = scan_duration_t::forever() , |
duplicates_filter_t | filtering = duplicates_filter_t::DISABLE , |
||
scan_period_t | period = scan_period_t(0) |
||
) |
Start scanning.
duration | How long to scan for. Special value 0 means scan forever. |
filtering | Filtering policy. |
period | How long to scan for in single period. If the period is 0 and duration is nonzero the scan will last for single duration. |
ble_error_t stopAdvertising | ( | advertising_handle_t | handle | ) |
Stop advertising given advertising set.
This is separate from periodic advertising which will not be affected.
handle | Advertising set handle. |
ble_error_t stopPeriodicAdvertising | ( | advertising_handle_t | handle | ) |
Stop periodic advertising for a given set.
handle | Advertising set handle. |
ble_error_t stopScan | ( | ) |
Stop the ongoing scanning procedure.
The current scanning parameters remain in effect.
BLE_ERROR_NONE | if successfully stopped scanning procedure. |
ble_error_t terminateSync | ( | periodic_sync_handle_t | handle | ) |
Stop reception of the periodic advertising identified by the handle.
handle | Periodic advertising synchronisation handle. |
|
static |
|
static |