Direct-BT
2.3.1
Direct-BT - Direct Bluetooth Programming.
|
BTAdapter represents one Bluetooth Controller. More...
#include <BTAdapter.hpp>
Public Member Functions | |
BTAdapter (const BTAdapter::ctor_cookie &cc, BTManager &mgmt_, const AdapterInfo &adapterInfo_) noexcept | |
Private ctor for private BTAdapter::make_shared() intended for friends. More... | |
BTAdapter (const BTAdapter &)=delete | |
void | operator= (const BTAdapter &)=delete |
~BTAdapter () noexcept | |
Releases this instance. More... | |
void | close () noexcept |
Closes this instance, usually being called by destructor or when this adapter is being removed as recognized and handled by BTManager. More... | |
std::string | get_java_class () const noexcept override |
bool | isPowered () const noexcept |
Returns whether the adapter is valid, plugged in and powered. More... | |
bool | isSuspended () const noexcept |
Returns whether the adapter is suspended, i.e. More... | |
bool | hasSecureConnections () const noexcept |
bool | hasSecureSimplePairing () const noexcept |
constexpr LE_Features | getLEFeatures () const noexcept |
Return LE_Features for this controller. More... | |
constexpr bool | hasHCIExtScan () const noexcept |
Returns true if HCI_LE_Set_Extended_Scan_Parameters and HCI_LE_Set_Extended_Scan_Enable is supported (Bluetooth 5.0). More... | |
constexpr bool | hasHCIExtConn () const noexcept |
Returns true if HCI_LE_Extended_Create_Connection is supported (Bluetooth 5.0). More... | |
bool | isValid () const noexcept |
Returns whether the adapter is valid, i.e. More... | |
BTMode | getBTMode () const noexcept |
Returns the current BTMode of this adapter. More... | |
BDAddressAndType const & | getAddressAndType () const noexcept |
Returns the adapter's public BDAddressAndType. More... | |
BDAddressAndType const & | getVisibleAddressAndType () const noexcept |
Returns the adapter's currently visible BDAddressAndType. More... | |
std::string | getName () const noexcept |
Returns the system name. More... | |
std::string | getShortName () const noexcept |
Returns the short system name. More... | |
const NameAndShortName & | getLocalName () const noexcept |
Returns the local friendly name and short_name. More... | |
std::shared_ptr< NameAndShortName > | setLocalName (const std::string &name, const std::string &short_name) noexcept |
Sets the local friendly name. More... | |
bool | setDiscoverable (bool value) noexcept |
Set the discoverable state of the adapter. More... | |
bool | setBondable (bool value) noexcept |
Set the bondable (aka pairable) state of the adapter. More... | |
bool | setPowered (bool value) noexcept |
Set the power state of the adapter. More... | |
HCIStatusCode | reset () noexcept |
Reset the adapter. More... | |
BTManager & | getManager () const noexcept |
Returns a reference to the used singleton BTManager instance, used to create this adapter. More... | |
HCIHandler & | getHCI () noexcept |
Returns a reference to the aggregated HCIHandler instance. More... | |
bool | isDeviceWhitelisted (const BDAddressAndType &addressAndType) noexcept |
Returns true, if the adapter's device is already whitelisted. More... | |
bool | addDeviceToWhitelist (const BDAddressAndType &addressAndType, const HCIWhitelistConnectType ctype, const uint16_t conn_interval_min=12, const uint16_t conn_interval_max=12, const uint16_t conn_latency=0, const uint16_t supervision_timeout=getHCIConnSupervisorTimeout(0, 15)) |
Add the given device to the adapter's autoconnect whitelist. More... | |
bool | removeDeviceFromWhitelist (const BDAddressAndType &addressAndType) |
Remove the given device from the adapter's autoconnect whitelist. More... | |
bool | addStatusListener (std::shared_ptr< AdapterStatusListener > l) |
Add the given listener to the list if not already present. More... | |
bool | addStatusListener (const BTDevice &d, std::shared_ptr< AdapterStatusListener > l) |
Please use BTDevice::addStatusListener() for clarity, merely existing here to allow JNI access. More... | |
bool | removeStatusListener (std::shared_ptr< AdapterStatusListener > l) |
Remove the given listener from the list. More... | |
bool | removeStatusListener (const AdapterStatusListener *l) |
Remove the given listener from the list. More... | |
int | removeAllStatusListener () |
Remove all status listener from the list. More... | |
HCIStatusCode | startDiscovery (const bool keepAlive=true, const bool le_scan_active=false, const uint16_t le_scan_interval=24, const uint16_t le_scan_window=24, const uint8_t filter_policy=0x00) |
Starts a new discovery session. More... | |
HCIStatusCode | stopDiscovery () noexcept |
Closes the discovery session. More... | |
ScanType | getCurrentScanType () const noexcept |
Returns the current meta discovering ScanType. More... | |
ScanType | getCurrentNativeScanType () const noexcept |
Returns the adapter's current native discovering ScanType. More... | |
bool | getDiscovering () const noexcept |
Returns the meta discovering state. More... | |
jau::darray< std::shared_ptr< BTDevice > > | getDiscoveredDevices () const noexcept |
Returns discovered devices from the last discovery. More... | |
int | removeDiscoveredDevices () noexcept |
Discards all discovered devices. More... | |
bool | removeDiscoveredDevice (const BDAddressAndType &addressAndType) noexcept |
Discards matching discovered devices. More... | |
std::shared_ptr< BTDevice > | findDiscoveredDevice (const EUI48 &address, const BDAddressType addressType) noexcept |
Returns shared BTDevice if found, otherwise nullptr. More... | |
std::shared_ptr< BTDevice > | findSharedDevice (const EUI48 &address, const BDAddressType addressType) noexcept |
Returns shared BTDevice if found, otherwise nullptr. More... | |
std::string | toString () const noexcept override |
std::string | toString (bool includeDiscoveredDevices) const noexcept |
void | printDeviceLists () noexcept |
Print the internally maintained BTDevice lists to stderr: More... | |
void | printStatusListenerList () noexcept |
Public Member Functions inherited from direct_bt::BTObject | |
virtual | ~BTObject () noexcept |
bool | isValid () const noexcept |
Returns whether the object's reference is valid and in a general operational state. More... | |
Public Member Functions inherited from jau::JavaUplink | |
std::string | javaObjectToString () const noexcept |
std::shared_ptr< JavaAnon > | getJavaObject () noexcept |
void | setJavaObject (std::shared_ptr< JavaAnon > objRef) noexcept |
Assigns a new shared JavaAnon reference, replaced item might be deleted via JNI from dtor. More... | |
void | setJavaObject () noexcept |
Resets the shared JavaAnon reference, the replaced item might be deleted via JNI from dtor. More... | |
void | clearJavaObject () noexcept |
Clears the java reference, i.e. More... | |
virtual void | checkValid () const |
Throws an IllegalStateException if isValid() == false. More... | |
virtual | ~JavaUplink () noexcept |
Static Public Member Functions | |
static std::string | java_class () noexcept |
Public Attributes | |
const uint16_t | dev_id |
Adapter's internal temporary device id. More... | |
Friends | |
std::shared_ptr< BTDevice > | BTDevice::getSharedInstance () const noexcept |
void | BTDevice::remove () noexcept |
std::shared_ptr< ConnectionInfo > | BTDevice::getConnectionInfo () noexcept |
void | BTDevice::sendMgmtEvDeviceDisconnected (std::unique_ptr< MgmtEvent > evt) noexcept |
HCIStatusCode | BTDevice::disconnect (const HCIStatusCode reason) noexcept |
HCIStatusCode | BTDevice::connectLE (uint16_t interval, uint16_t window, uint16_t min_interval, uint16_t max_interval, uint16_t latency, uint16_t supervision_timeout) noexcept |
HCIStatusCode | BTDevice::connectBREDR (const uint16_t pkt_type, const uint16_t clock_offset, const uint8_t role_switch) noexcept |
void | BTDevice::processL2CAPSetup (std::shared_ptr< BTDevice > sthis) |
bool | BTDevice::updatePairingState (std::shared_ptr< BTDevice > sthis, const MgmtEvent &evt, const HCIStatusCode evtStatus, SMPPairingState claimed_state) noexcept |
void | BTDevice::hciSMPMsgCallback (std::shared_ptr< BTDevice > sthis, const SMPPDUMsg &msg, const HCIACLData::l2cap_frame &source) noexcept |
void | BTDevice::processDeviceReady (std::shared_ptr< BTDevice > sthis, const uint64_t timestamp) |
jau::darray< std::shared_ptr< BTGattService > > | BTDevice::getGattServices () noexcept |
Additional Inherited Members | |
Protected Member Functions inherited from direct_bt::BTObject | |
BTObject () noexcept | |
Protected Attributes inherited from direct_bt::BTObject | |
std::atomic_bool | valid |
BTAdapter represents one Bluetooth Controller.
Controlling Environment variables:
Definition at line 249 of file BTAdapter.hpp.
|
noexcept |
Private ctor for private BTAdapter::make_shared() intended for friends.
Definition at line 224 of file BTAdapter.cpp.
|
delete |
|
noexcept |
Releases this instance.
Definition at line 238 of file BTAdapter.cpp.
bool BTAdapter::addDeviceToWhitelist | ( | const BDAddressAndType & | addressAndType, |
const HCIWhitelistConnectType | ctype, | ||
const uint16_t | conn_interval_min = 12 , |
||
const uint16_t | conn_interval_max = 12 , |
||
const uint16_t | conn_latency = 0 , |
||
const uint16_t | supervision_timeout = getHCIConnSupervisorTimeout(0, 15) |
||
) |
Add the given device to the adapter's autoconnect whitelist.
The given connection parameter will be uploaded to the kernel for the given device first.
Method will reject duplicate devices, in which case it should be removed first.
address | |
address_type | |
ctype | |
conn_interval_min | in units of 1.25ms, default value 12 for 15ms; Value range [6 .. 3200] for [7.5ms .. 4000ms] |
conn_interval_max | in units of 1.25ms, default value 12 for 15ms; Value range [6 .. 3200] for [7.5ms .. 4000ms] |
conn_latency | slave latency in units of connection events, default value 0; Value range [0 .. 0x01F3]. |
supervision_timeout | in units of 10ms, default value >= 10 x conn_interval_max, we use HCIConstInt::LE_CONN_MIN_TIMEOUT_MS minimum; Value range [0xA-0x0C80] for [100ms - 32s]. |
Definition at line 522 of file BTAdapter.cpp.
bool BTAdapter::addStatusListener | ( | const BTDevice & | d, |
std::shared_ptr< AdapterStatusListener > | l | ||
) |
Please use BTDevice::addStatusListener() for clarity, merely existing here to allow JNI access.
Definition at line 564 of file BTAdapter.cpp.
bool BTAdapter::addStatusListener | ( | std::shared_ptr< AdapterStatusListener > | l | ) |
Add the given listener to the list if not already present.
In case the AdapterStatusListener's lifecycle and event delivery shall be constrained to this device, please use BTDevice::addStatusListener().
Returns true if the given listener is not element of the list and has been newly added, otherwise false.
The newly added AdapterStatusListener will receive an initial AdapterStatusListener::adapterSettingsChanged(..) event, passing an empty AdapterSetting::NONE oldMask and changedMask, as well as current AdapterSetting newMask.
This allows the receiver to be aware of this adapter's current settings.
Definition at line 548 of file BTAdapter.cpp.
|
noexcept |
Closes this instance, usually being called by destructor or when this adapter is being removed as recognized and handled by BTManager.
Renders this adapter's BTAdapter::isValid() state to false.
Definition at line 252 of file BTAdapter.cpp.
|
noexcept |
Returns shared BTDevice if found, otherwise nullptr.
Definition at line 843 of file BTAdapter.cpp.
|
noexcept |
Returns shared BTDevice if found, otherwise nullptr.
Definition at line 934 of file BTAdapter.cpp.
|
inlineoverridevirtualnoexcept |
Implements jau::JavaUplink.
Definition at line 424 of file BTAdapter.hpp.
|
inlinenoexcept |
Returns the adapter's public BDAddressAndType.
The adapter's address as initially reported by the system is always its public address, i.e. BDAddressType::BDADDR_LE_PUBLIC.
Definition at line 497 of file BTAdapter.hpp.
|
inlinenoexcept |
Returns the current BTMode of this adapter.
Definition at line 487 of file BTAdapter.hpp.
|
inlinenoexcept |
Returns the adapter's current native discovering ScanType.
It can be modified through startDiscovery(..) and stopDiscovery().
Definition at line 747 of file BTAdapter.hpp.
|
inlinenoexcept |
Returns the current meta discovering ScanType.
It can be modified through startDiscovery(..) and stopDiscovery().
Note that if startDiscovery(..) has been issued with keepAlive==true, the meta ScanType will still keep the desired ScanType enabled even if it has been temporarily disabled.
Definition at line 738 of file BTAdapter.hpp.
|
noexcept |
Returns discovered devices from the last discovery.
Note that this list will be cleared when a new discovery is started over via startDiscovery().
Note that devices in this list might be no more available, use 'DeviceStatusListener::deviceFound(..)' callback.
Definition at line 901 of file BTAdapter.cpp.
|
inlinenoexcept |
Returns the meta discovering state.
It can be modified through startDiscovery(..) and stopDiscovery().
Definition at line 756 of file BTAdapter.hpp.
|
inlinenoexcept |
Returns a reference to the aggregated HCIHandler instance.
Definition at line 575 of file BTAdapter.hpp.
|
inlineconstexprnoexcept |
Return LE_Features for this controller.
BT Core Spec v5.2: Vol 6, Part B, 4.6 (LE LL) Feature Support
Definition at line 465 of file BTAdapter.hpp.
|
inlinenoexcept |
Returns the local friendly name and short_name.
Contains empty strings if not set.
The value is being updated via SET_LOCAL_NAME management event reply.
Definition at line 528 of file BTAdapter.hpp.
|
inlinenoexcept |
Returns a reference to the used singleton BTManager instance, used to create this adapter.
Definition at line 570 of file BTAdapter.hpp.
|
inlinenoexcept |
Returns the system name.
Definition at line 515 of file BTAdapter.hpp.
|
inlinenoexcept |
Returns the short system name.
Definition at line 520 of file BTAdapter.hpp.
|
inlinenoexcept |
Returns the adapter's currently visible BDAddressAndType.
The adapter's address as initially reported by the system is always its public address, i.e. BDAddressType::BDADDR_LE_PUBLIC.
The adapter's visible BDAddressAndType might be set to BDAddressType::BDADDR_LE_RANDOM before scanning / discovery mode (TODO).
Definition at line 510 of file BTAdapter.hpp.
|
inlineconstexprnoexcept |
Returns true if HCI_LE_Extended_Create_Connection is supported (Bluetooth 5.0).
Definition at line 471 of file BTAdapter.hpp.
|
inlineconstexprnoexcept |
Returns true if HCI_LE_Set_Extended_Scan_Parameters and HCI_LE_Set_Extended_Scan_Enable is supported (Bluetooth 5.0).
Definition at line 468 of file BTAdapter.hpp.
|
inlinenoexcept |
|
inlinenoexcept |
|
noexcept |
Returns true, if the adapter's device is already whitelisted.
Definition at line 518 of file BTAdapter.cpp.
|
inlinenoexcept |
Returns whether the adapter is valid, plugged in and powered.
Definition at line 437 of file BTAdapter.hpp.
|
inlinenoexcept |
Returns whether the adapter is suspended, i.e.
valid and plugged in, but not powered.
Definition at line 447 of file BTAdapter.hpp.
|
inlinenoexcept |
Returns whether the adapter is valid, i.e.
reference is valid, plugged in and generally operational, but not necessarily BTAdapter::isPowered() powered.
Definition at line 480 of file BTAdapter.hpp.
|
inlinestaticnoexcept |
|
delete |
|
noexcept |
Print the internally maintained BTDevice lists to stderr:
This is intended as a debug facility.
Definition at line 325 of file BTAdapter.cpp.
|
noexcept |
Definition at line 340 of file BTAdapter.cpp.
int BTAdapter::removeAllStatusListener | ( | ) |
Remove all status listener from the list.
Returns the number of removed status listener.
Definition at line 649 of file BTAdapter.cpp.
bool BTAdapter::removeDeviceFromWhitelist | ( | const BDAddressAndType & | addressAndType | ) |
Remove the given device from the adapter's autoconnect whitelist.
Definition at line 541 of file BTAdapter.cpp.
|
noexcept |
Discards matching discovered devices.
Returns true
if found and removed, otherwise false.
Definition at line 860 of file BTAdapter.cpp.
|
noexcept |
Discards all discovered devices.
Returns number of removed discovered devices.
Definition at line 877 of file BTAdapter.cpp.
bool BTAdapter::removeStatusListener | ( | const AdapterStatusListener * | l | ) |
Remove the given listener from the list.
Returns true if the given listener is an element of the list and has been removed, otherwise false.
Definition at line 599 of file BTAdapter.cpp.
bool BTAdapter::removeStatusListener | ( | std::shared_ptr< AdapterStatusListener > | l | ) |
Remove the given listener from the list.
Returns true if the given listener is an element of the list and has been removed, otherwise false.
Definition at line 585 of file BTAdapter.cpp.
|
noexcept |
Reset the adapter.
The semantics are specific to the HCI host implementation, however, it shall comply at least with the HCI Reset command and bring up the device from standby into a POWERED functional state afterwards.
BT Core Spec v5.2: Vol 4, Part E HCI: 7.3.2 Reset command
Definition at line 489 of file BTAdapter.cpp.
|
noexcept |
Set the bondable (aka pairable) state of the adapter.
Definition at line 357 of file BTAdapter.cpp.
|
noexcept |
Set the discoverable state of the adapter.
Definition at line 352 of file BTAdapter.cpp.
|
noexcept |
Sets the local friendly name.
Returns the immediate SET_LOCAL_NAME reply if successful, otherwise nullptr. The corresponding management event will be received separately.
Definition at line 348 of file BTAdapter.cpp.
|
noexcept |
Set the power state of the adapter.
Definition at line 362 of file BTAdapter.cpp.
HCIStatusCode BTAdapter::startDiscovery | ( | const bool | keepAlive = true , |
const bool | le_scan_active = false , |
||
const uint16_t | le_scan_interval = 24 , |
||
const uint16_t | le_scan_window = 24 , |
||
const uint8_t | filter_policy = 0x00 |
||
) |
Starts a new discovery session.
Returns HCIStatusCode::SUCCESS if successful, otherwise the HCIStatusCode error state;
if keepAlive
is true
, discovery state will be re-enabled in case the underlying Bluetooth implementation (BlueZ, ..) disabled it. Default is true
.
Using startDiscovery(keepAlive=true) and stopDiscovery() is the recommended workflow for a reliable discovery process.
+ --+-------+--------+-----------+----------------------------------------------------+ | # | meta | native | keepAlive | Note +---+-------+--------+-----------+----------------------------------------------------+ | 1 | true | true | false | - | 2 | false | false | false | - +---+-------+--------+-----------+----------------------------------------------------+ | 3 | true | true | true | - | 4 | true | false | true | temporarily disabled -> startDiscoveryBackground() | 5 | false | false | true | [4] -> [5] requires manual DISCOVERING event +---+-------+--------+-----------+----------------------------------------------------+
<p<blockquote>
Remaining default parameter values are chosen for using public address resolution and usual discovery intervals etc.
This adapter's HCIHandler instance is used to initiate scanning, see HCIHandler::le_set_scan_param() and HCIHandler::le_enable_scan().
Method will always clear previous discovered devices via removeDiscoveredDevices().
keepAlive | |
le_scan_active | true enables delivery of active scanning PDUs, otherwise no scanning PDUs shall be sent (default) |
le_scan_interval | in units of 0.625ms, default value 24 for 15ms; Value range [4 .. 0x4000] for [2.5ms .. 10.24s] |
le_scan_window | in units of 0.625ms, default value 24 for 15ms; Value range [4 .. 0x4000] for [2.5ms .. 10.24s]. Shall be <= le_scan_interval |
filter_policy | 0x00 accepts all PDUs (default), 0x01 only of whitelisted, ... |
Definition at line 679 of file BTAdapter.cpp.
|
noexcept |
Closes the discovery session.
This adapter's HCIHandler instance is used to stop scanning, see HCIHandler::le_enable_scan().
Definition at line 756 of file BTAdapter.cpp.
|
inlineoverridevirtualnoexcept |
Reimplemented from direct_bt::BTObject.
Definition at line 784 of file BTAdapter.hpp.
|
noexcept |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
const uint16_t direct_bt::BTAdapter::dev_id |
Adapter's internal temporary device id.
The internal device id is constant across the adapter lifecycle, but may change after its destruction.
Definition at line 273 of file BTAdapter.hpp.