|
| enum class | EventId : uint8_t {
kSecurityLevel
, kDedicatedBondingCompleted
, kAdvertisingReport
, kExtendedAdvertisingReport
,
kInquiryResult
, kInquiryComplete
, kRssiMeasurement
, kLocalOobData
,
kPairingStarted
, kPairingComplete
, kDisconnectionComplete
, kCommandComplete
,
kLeScanRequestReceived
, kLeScanTimeout
, kLePeriodicAdvertisingSyncEstablished
, kLePeriodicAdvertisingReport
,
kLePeriodicAdvertisingSyncLost
, kLeConnectionComplete
, kLeEnhancedConnectionComplete
, kLeRemoteConnectionParameterRequest
,
kLeConnectionUpdateComplete
, kLePhyUpdateComplete
, kLeDataLengthChange
, kLeAdvertisingSetTerminated
,
kL2capConnectionParameterUpdateRequest
, kPrivacyEnabled
} |
| | Event IDs used by Gap::EventHandler. More...
|
| |
| enum class | AdvertisingEventType : uint16_t {
kConnectable = 0x0001
, kScannable = 0x0002
, kDirected = 0x0004
, kHighDutyCycle = 0x0008
,
kLegacy = 0x0010
, kAnonymous = 0x0020
, kIncludeTxPower = 0x0040
} |
| | Extended advertising event properties (bitfield). More...
|
| |
| enum class | Phy : uint8_t { kNone = 0x00
, kLe1M = 0x01
, kLe2M = 0x02
, kLeCoded = 0x03
} |
| | LE PHY values reported in extended advertising reports. More...
|
| |
| enum class | AdvertisingType : uint8_t { kAdvInd = 0x00
, kAdvDirectInd = 0x01
, kAdvScanInd = 0x02
, kAdvNonConnInd = 0x03
} |
| | Legacy advertising types for LE advertising parameters. More...
|
| |
| enum class | DirectAddressType : uint8_t { kPublic = 0x00
, kRandom = 0x01
} |
| | Direct address type for directed advertising. More...
|
| |
| enum class | AdvertisingChannelMap : uint8_t { kChannel37 = 0x01
, kChannel38 = 0x02
, kChannel39 = 0x04
, kAll = 0x07
} |
| | Advertising channel map bitfield. More...
|
| |
| enum class | AdvertisingFilterPolicy : uint8_t { kScanAnyConnectAny = 0x00
, kScanWhitelistConnectAny = 0x01
, kScanAnyConnectWhitelist = 0x02
, kScanWhitelistConnectWhitelist = 0x03
} |
| | Advertising filter policy. More...
|
| |
Manages Generic Access Profile (GAP) functionality for BLE.
This class provides a high-level, object-oriented interface for managing the BLE GAP layer, acting as a C++ wrapper around the underlying C-based BTstack API. It simplifies common GAP operations such as advertising and connection management by maintaining state and handling HCI event dispatching. Scanning is currently exposed only via events, not via public start/stop APIs.
The Gap class is implemented as a singleton, accessible via GetInstance(), ensuring a single point of control for the device's GAP layer.
Design Logic
The class abstracts away the low-level details of the BTstack. It works by:
- Configuration Caching: Storing advertising parameters, data, and other settings within the class.
- State Management: Tracking the advertising state (
IsAdvertisingEnabled()) and connection status (IsConnected()).
- Event-Driven Callbacks: Using the
EventHandler interface, which users can implement to react to BLE events (e.g., connection, disconnection, advertising reports) in a clean, C++ idiomatic way.
- HCI Event Dispatching: The
DispatchBleHciPacket method is the entry point for raw HCI events from the BTstack, which are then parsed and forwarded to the appropriate EventHandler methods. Your application must call this for events to reach the handlers.
Advertising Configuration (Legacy)
To start advertising, you must configure three main components:
- Advertising Parameters: Define the "how" of advertising.
- Type: Connectable, scannable, non-connectable, etc.
- Interval: The frequency of advertising packets.
- Channels: Which BLE channels to advertise on. This is configured using
SetAdvertisingParameters().
- Advertising Data: The main payload (up to 31 bytes) that is broadcast to all listening devices. This typically includes flags and the device name. This is configured using
SetAdvertisingData().
- Scan Response Data (Optional): An additional 31-byte payload that a central device can request after seeing the initial advertisement. This is often used for supplementary information that doesn't fit in the main payload. This is configured using
SetScanResponseData().
Applying Configurations and Utilities
- **
SetAdvertisingParameters(const AdvertisementParameters& params):** Takes a struct that defines the advertising behavior. A default-constructed AdvertisementParameters provides standard connectable, undirected advertising settings.
- **
SetAdvertisingData(...) / SetScanResponseData(...):** These methods accept a raw pointer and length. For convenience, SetAdvertisingData is overloaded to accept a std::vector<uint8_t> or an AdvertisementDataBuilder instance, which is the recommended utility for safely constructing valid advertising payloads.
- **
StartAdvertising() / StopAdvertising():** Simple methods to enable or disable advertising.
Dynamic Data Updates
You can update advertising or scan response data at any time, even while advertising is active. The SetAdvertisingData and SetScanResponseData methods will automatically handle the underlying requirements of the RPi Pico BLE stack: they temporarily stop advertising, apply the new data, and restart advertising if it was previously enabled. This ensures a seamless update without manual intervention.
Limitations / Future Work
- Extended advertising: not implemented in this wrapper yet. The API only supports legacy advertising (31-byte payloads, ADV_* types).
- Scanning control: scan start/stop configuration is not exposed yet. Only scan-related events are surfaced via
EventHandler.
Complete Code Example (Peripheral / Advertising)
Below is a complete example of setting up and starting a connectable BLE advertisement with a device name. It also shows a minimal event dispatch hook that forwards BTstack HCI events into the Gap instance.
#include <iostream>
public:
void OnAdvertisingStart(uint8_t status) const override {
if (status == 0) {
std::cout << "Advertising started successfully." << std::endl;
} else {
std::cout << "Failed to start advertising, status: " << (int)status << std::endl;
}
}
uint16_t, uint16_t, uint16_t) const override {
if (status == 0) {
std::cout << "Device connected: " << address << std::endl;
}
}
uint8_t reason) const override {
std::cout << "Device disconnected, reason: " << (int)reason << std::endl;
std::cout << "Advertising restarted." << std::endl;
}
};
void setup_ble_advertising() {
static MyGapEventHandler my_handler;
gap.AddEventHandler(my_handler);
gap.SetAdvertisingParameters(params);
gap.SetAdvertisingData(ad_builder);
gap.StartAdvertising();
}
void on_btstack_event(uint8_t packet_type, const uint8_t* packet, uint16_t size) {
}
BLE GAP advertisement data builder.
Builder for assembling a complete advertising payload.
Definition advertisement_data.hpp:304
@ kLeGeneralDiscoverableMode
BLE address container with an associated address type.
Definition ble_address.hpp:43
void StartAdvertising()
Convenience helpers for starting/stopping advertising.
virtual BleError DispatchBleHciPacket(uint8_t packet_type, const uint8_t *packet_data, uint16_t packet_data_size)
Dispatch a raw HCI packet into the GAP event pipeline.
static Gap * GetInstance()
Get the singleton instance.
Provides a C++ wrapper for the BTstack Generic Access Profile (GAP).
uint16_t ConnectionHandle
Definition gap.hpp:23
uint16_t min_interval
Minimum advertising interval (unit: 0.625 ms).
Definition gap.hpp:969
uint16_t max_interval
Maximum advertising interval (unit: 0.625 ms).
Definition gap.hpp:973
Minimal Event-Only Example (e.g., central role)
If another component configures scanning or connections, you can still use Gap for event handling by registering a handler and dispatching HCI events.
public:
override { if (status == 0) { (void)handle;
}
}
};
void init_gap_events() {
static MyGapEventHandler handler;
}
void AddEventHandler(const EventHandler &handler)
Register an event handler.