Direct-BT
2.3.1
Direct-BT - Direct Bluetooth Programming.
|
Go to the documentation of this file.
57 const AdapterSetting changedmask,
const uint64_t timestamp)
override {
58 fprintf(stderr,
"****** Native Adapter SETTINGS_CHANGED: %s -> %s, changed %s\n",
62 fprintf(stderr,
"Status BTAdapter:\n");
63 fprintf(stderr,
"%s\n", a.
toString().c_str());
67 void discoveringChanged(
BTAdapter &a,
const ScanType currentMeta,
const ScanType changedType,
const bool changedEnabled,
const bool keepAlive,
const uint64_t timestamp)
override {
68 fprintf(stderr,
"****** DISCOVERING: meta %s, changed[%s, enabled %d, keepAlive %d]: %s\n",
73 bool deviceFound(std::shared_ptr<BTDevice> device,
const uint64_t timestamp)
override {
74 fprintf(stderr,
"****** FOUND__: %s\n", device->
toString(
true).c_str());
75 fprintf(stderr,
"Status Adapter:\n");
85 void deviceUpdated(std::shared_ptr<BTDevice> device,
const EIRDataType updateMask,
const uint64_t timestamp)
override {
86 fprintf(stderr,
"****** UPDATED: %s of %s\n",
to_string(updateMask).c_str(), device->
toString(
true).c_str());
89 void deviceConnected(std::shared_ptr<BTDevice> device,
const uint16_t handle,
const uint64_t timestamp)
override {
90 fprintf(stderr,
"****** CONNECTED: %s\n", device->
toString(
true).c_str());
94 void devicePairingState(std::shared_ptr<BTDevice> device,
const SMPPairingState state,
const PairingMode mode,
const uint64_t timestamp)
override {
95 fprintf(stderr,
"****** PAIRING STATE: state %s, mode %s, %s\n",
99 void deviceReady(std::shared_ptr<BTDevice> device,
const uint64_t timestamp)
override {
100 fprintf(stderr,
"****** READY: %s\n", device->
toString().c_str());
103 void deviceDisconnected(std::shared_ptr<BTDevice> device,
const HCIStatusCode reason,
const uint16_t handle,
const uint64_t timestamp)
override {
104 fprintf(stderr,
"****** DISCONNECTED: Reason 0x%X (%s), old handle %s: %s\n",
105 static_cast<uint8_t
>(reason),
to_string(reason).c_str(),
111 std::string toString()
const override {
112 return "MyAdapterStatusListener[this "+
to_hexstring(
this)+
"]";
124 const std::shared_ptr<BTDevice> dev = charDecl->getDeviceChecked();
126 fprintf(stderr,
"****** GATT Notify (td %" PRIu64
" ms, dev-discovered %" PRIu64
" ms): From %s\n",
128 if(
nullptr != charDecl ) {
129 fprintf(stderr,
"****** decl %s\n", charDecl->toString().c_str());
131 fprintf(stderr,
"****** rawv %s\n",
charValue.toString().c_str());
136 const bool confirmationSent)
override
138 const std::shared_ptr<BTDevice> dev = charDecl->getDeviceChecked();
140 fprintf(stderr,
"****** GATT Indication (confirmed %d, td(msg %" PRIu64
" ms, dev-discovered %" PRIu64
" ms): From %s\n",
142 if(
nullptr != charDecl ) {
143 fprintf(stderr,
"****** decl %s\n", charDecl->toString().c_str());
146 if(
nullptr != temp ) {
147 fprintf(stderr,
"****** valu %s\n", temp->
toString().c_str());
151 fprintf(stderr,
"****** rawv %s\n",
charValue.toString().c_str());
155 int main(
int argc,
char *argv[])
157 bool ok =
true, foundDevice=
false;
159 bool waitForEnter=
false;
161 bool forever =
false;
172 bool doHCI_Connect =
true;
174 for(
int i=1; i<argc; i++) {
175 if( !strcmp(
"-wait", argv[i]) ) {
177 }
else if( !strcmp(
"-forever", argv[i]) ) {
179 }
else if( !strcmp(
"-dev_id", argv[i]) && argc > (i+1) ) {
180 dev_id = atoi(argv[++i]);
181 }
else if( !strcmp(
"-skipConnect", argv[i]) ) {
182 doHCI_Connect =
false;
183 }
else if( !strcmp(
"-mac", argv[i]) && argc > (i+1) ) {
184 std::string macstr = std::string(argv[++i]);
188 fprintf(stderr,
"dev_id %d\n", dev_id);
189 fprintf(stderr,
"doHCI_Connect %d\n", doHCI_Connect);
190 fprintf(stderr,
"waitForDevice: %s\n", waitForDevice.
toString().c_str());
193 fprintf(stderr,
"Press ENTER to continue\n");
198 std::shared_ptr<BTAdapter> adapter = mngr.
getAdapter(dev_id);
199 if(
nullptr == adapter ) {
200 fprintf(stderr,
"adapter dev_id %d not available.\n", dev_id);
204 fprintf(stderr,
"Adapter invalid: %s\n", adapter->
toString().c_str());
208 fprintf(stderr,
"Adapter not powered: %s\n", adapter->
toString().c_str());
211 fprintf(stderr,
"Using adapter: %s\n", adapter->
toString().c_str());
217 while( ok && ( forever || !foundDevice ) ) {
220 perror(
"Adapter start discovery failed");
224 std::shared_ptr<BTDevice> device =
nullptr;
227 while(
nullptr == device ) {
230 foundDevice =
deviceFound->getAddressAndType().matches(waitForDevice);
240 if( ok &&
nullptr != device ) {
247 if( doHCI_Connect ) {
250 fprintf(stderr,
"Connect: Failed res %s, %s\n",
to_string(res).c_str(), device->
toString().c_str());
253 fprintf(stderr,
"Connect: Success\n");
256 fprintf(stderr,
"Connect: Skipped %s\n", device->
toString().c_str());
263 if( primServices.
size() > 0 ) {
266 const uint64_t td15 = t5 - t1;
267 const uint64_t td05 = t5 - t0;
268 fprintf(stderr,
"\n\n\n");
269 fprintf(stderr,
"GATT primary-services completed\n");
270 fprintf(stderr,
" discovery-done to gatt complete %" PRIu64
" ms,\n"
271 " discovered to gatt complete %" PRIu64
" ms,\n"
272 " total %" PRIu64
" ms\n\n",
276 if(
nullptr != ga ) {
277 fprintf(stderr,
" GenericAccess: %s\n\n", ga->
toString().c_str());
283 if(
nullptr != di ) {
284 fprintf(stderr,
" DeviceInformation: %s\n\n", di->
toString().c_str());
289 for(
size_t i=0; i<primServices.
size(); i++) {
291 fprintf(stderr,
" [%2.2d] Service %s\n", (
int)i, primService.
toString().c_str());
292 fprintf(stderr,
" [%2.2d] Service Characteristics\n", (
int)i);
294 for(
size_t j=0; j<serviceCharacteristics.
size(); j++) {
295 BTGattChar & serviceChar = *serviceCharacteristics.
at(j);
296 fprintf(stderr,
" [%2.2d.%2.2d] Decla: %s\n", (
int)i, (
int)j, serviceChar.
toString().c_str());
301 fprintf(stderr,
" [%2.2d.%2.2d] Value: %s ('%s')\n", (
int)i, (
int)j, value.
toString().c_str(), sval.c_str());
304 bool cccdEnableResult[2];
306 fprintf(stderr,
" [%2.2d.%2.2d] Config Notification(%d), Indication(%d): Result %d\n",
307 (
int)i, (
int)j, cccdEnableResult[0], cccdEnableResult[1], cccdRet);
jau::darray< BTGattCharRef > characteristicList
List of Characteristic Declarations as shared reference.
HCIStatusCode stopDiscovery() noexcept
Closes the discovery session.
HCIStatusCode connectDefault() noexcept
Establish a default HCI connection to this device, using certain default parameter.
const_reference at(size_type i) const
Like std::vector::at(size_type), immutable reference.
AdapterSetting
Adapter Setting Bits.
std::shared_ptr< GattDeviceInformationSvc > getDeviceInformation(jau::darray< BTGattServiceRef > &primServices)
bool hasProperties(const PropertyBitVal v) const noexcept
std::string toString() const noexcept
constexpr jau::nsize_t getSize() const noexcept
Returns the used memory size for read and write operations, may be zero.
static std::shared_ptr< GattTemperatureMeasurement > get(const TROOctets &source) noexcept
static const BDAddressAndType ANY_DEVICE
Using EUI48::ANY_DEVICE and BDAddressType::BDADDR_UNDEFINED to match any device.
void notificationReceived(BTGattCharRef charDecl, const TROOctets &charValue, const uint64_t timestamp) override
Called from native BLE stack, initiated by a received notification associated with the given BTGattCh...
EIRDataType
Bit mask of 'Extended Inquiry Response' (EIR) data fields, indicating a set of related data.
bool readValue(POctets &res, int expectedLength=-1)
BT Core Spec v5.2: Vol 3, Part G GATT: 4.8.1 Read Characteristic Value.
static const uuid16_t _TEMPERATURE_MEASUREMENT(GattCharacteristicType::TEMPERATURE_MEASUREMENT)
BTAdapter status listener for BTDevice discovery events: Added, updated and removed; as well as for c...
constexpr uint8_t const * get_ptr() const noexcept
std::string toString() const noexcept
uint64_t getCreationTimestamp() const noexcept
Returns the timestamp in monotonic milliseconds when this device instance has been created,...
Representing a complete [Primary] Service Declaration including its list of Characteristic Declaratio...
ScanType
Meta ScanType as derived from BTMode, with defined value mask consisting of BDAddressType bits.
std::string toString() const noexcept override
bool addCharListener(std::shared_ptr< Listener > l)
Add the given BTGattChar::Listener to the listener list if not already present.
bool isPowered() const noexcept
Returns whether the adapter is valid, plugged in and powered.
HCIStatusCode
BT Core Spec v5.2: Vol 1, Part F Controller Error Codes: 1.3 List of Error Codes.
std::shared_ptr< BTGattChar > BTGattCharRef
std::shared_ptr< GattGenericAccessSvc > getGattGenericAccess()
Returns the shared GenericAccess instance, retrieved by getGattService() or nullptr if not available.
@ TEMPERATURE_MEASUREMENT
std::string toString() const noexcept override
std::mutex mtxDeviceFound
static BTManager & get(const BTMode defaultBTMode=BTMode::NONE)
Retrieves the singleton instance.
HCIStatusCode disconnect(const HCIStatusCode reason=HCIStatusCode::REMOTE_USER_TERMINATED_CONNECTION) noexcept
Disconnect the LE or BREDR peer's GATT and HCI connection.
int main(int argc, char *argv[])
const uint64_t ts_creation
constexpr size_type size() const noexcept
Like std::vector::size().
PairingMode
Bluetooth secure pairing mode.
A thread safe singleton handler of the Linux Kernel's BlueZ manager control channel.
uint32_t dfa_utf8_decode(uint32_t &state, uint32_t &codep, const uint32_t byte_value)
bool addStatusListener(std::shared_ptr< AdapterStatusListener > l)
Add the given listener to the list if not already present.
std::string toString() const noexcept override
void indicationReceived(BTGattCharRef charDecl, const TROOctets &charValue, const uint64_t timestamp, const bool confirmationSent) override
Called from native BLE stack, initiated by a received indication associated with the given BTGattChar...
Transient read only octet data, i.e.
std::string to_string(const BDAddressType type) noexcept
BTAdapter represents one Bluetooth Controller.
uint64_t getCurrentMilliseconds() noexcept
Returns current monotonic time in milliseconds.
A packed 48 bit EUI-48 identifier, formerly known as MAC-48 or simply network device MAC address (Med...
std::string to_hexstring(value_type const &v) noexcept
Produce a lower-case hexadecimal string representation of the given pointer.
bool isValid() const noexcept
Returns whether the adapter is valid, i.e.
std::string toString() const noexcept override
jau::darray< std::shared_ptr< BTGattService > > getGattServices() noexcept
Returns a list of shared GATTService available on this device if successful, otherwise returns an emp...
bool isConnected() const noexcept
bool configNotificationIndication(const bool enableNotification, const bool enableIndication, bool enabledState[2])
BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration.
std::shared_ptr< BTGattHandler > getGattHandler() noexcept
Returns the connected GATTHandler or nullptr, see connectGATT(), getGattService() and disconnect().
std::string toString() const
std::shared_ptr< BTDevice > deviceFound
This C++ direct_bt scanner example is a TinyB backward compatible and not fully event driven.
std::condition_variable cvDeviceFound
Persistent octet data, i.e.
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.
SMPPairingState
SMP Pairing Process state definition.
static constexpr int number(const Defaults d)
Unique Bluetooth EUI48 address and BDAddressType tuple.
std::shared_ptr< BTAdapter > getAdapter(const uint16_t dev_id) const noexcept
Returns the DBTAdapter with the given dev_id, or nullptr if not found.
BTAdapter & getAdapter() const
Returns the managing adapter.
std::string toString() const noexcept
BTGattChar event listener for notification and indication events.
@ BDADDR_UNDEFINED
Undefined.