Direct-BT
2.3.1
Direct-BT - Direct Bluetooth Programming.
|
Go to the documentation of this file.
56 const AdapterSetting changedmask,
const uint64_t timestamp)
override {
57 fprintf(stderr,
"****** Native Adapter SETTINGS_CHANGED: %s -> %s, changed %s\n",
61 fprintf(stderr,
"Status BTAdapter:\n");
62 fprintf(stderr,
"%s\n", a.
toString().c_str());
66 void discoveringChanged(
BTAdapter &a,
const ScanType currentMeta,
const ScanType changedType,
const bool changedEnabled,
const bool keepAlive,
const uint64_t timestamp)
override {
67 fprintf(stderr,
"****** DISCOVERING: meta %s, changed[%s, enabled %d, keepAlive %d]: %s\n",
72 bool deviceFound(std::shared_ptr<BTDevice> device,
const uint64_t timestamp)
override {
73 fprintf(stderr,
"****** FOUND__: %s\n", device->
toString(
true).c_str());
74 fprintf(stderr,
"Status Adapter:\n");
84 void deviceUpdated(std::shared_ptr<BTDevice> device,
const EIRDataType updateMask,
const uint64_t timestamp)
override {
85 fprintf(stderr,
"****** UPDATED: %s of %s\n",
to_string(updateMask).c_str(), device->
toString(
true).c_str());
88 void deviceConnected(std::shared_ptr<BTDevice> device,
const uint16_t handle,
const uint64_t timestamp)
override {
89 fprintf(stderr,
"****** CONNECTED: %s\n", device->
toString(
true).c_str());
93 void devicePairingState(std::shared_ptr<BTDevice> device,
const SMPPairingState state,
const PairingMode mode,
const uint64_t timestamp)
override {
94 fprintf(stderr,
"****** PAIRING STATE: state %s, mode %s, %s\n",
98 void deviceReady(std::shared_ptr<BTDevice> device,
const uint64_t timestamp)
override {
99 fprintf(stderr,
"****** READY: %s\n", device->
toString().c_str());
102 void deviceDisconnected(std::shared_ptr<BTDevice> device,
const HCIStatusCode reason,
const uint16_t handle,
const uint64_t timestamp)
override {
103 fprintf(stderr,
"****** DISCONNECTED: Reason 0x%X (%s), old handle %s: %s\n",
104 static_cast<uint8_t
>(reason),
to_string(reason).c_str(),
110 std::string toString()
const override {
111 return "MyAdapterStatusListener[this "+
to_hexstring(
this)+
"]";
121 const std::shared_ptr<BTDevice> dev = charDecl->getDeviceChecked();
123 fprintf(stderr,
"****** GATT Notify (td %" PRIu64
" ms, dev-discovered %" PRIu64
" ms): From %s\n",
125 if(
nullptr != charDecl ) {
126 fprintf(stderr,
"****** decl %s\n", charDecl->toString().c_str());
128 fprintf(stderr,
"****** rawv %s\n",
charValue.toString().c_str());
133 const bool confirmationSent)
override
135 const std::shared_ptr<BTDevice> dev = charDecl->getDeviceChecked();
137 fprintf(stderr,
"****** GATT Indication (confirmed %d, td(msg %" PRIu64
" ms, dev-discovered %" PRIu64
" ms): From %s\n",
139 if(
nullptr != charDecl ) {
140 fprintf(stderr,
"****** decl %s\n", charDecl->toString().c_str());
143 if(
nullptr != temp ) {
144 fprintf(stderr,
"****** valu %s\n", temp->
toString().c_str());
148 fprintf(stderr,
"****** rawv %s\n",
charValue.toString().c_str());
154 int main(
int argc,
char *argv[])
156 bool ok =
true, foundDevice=
false;
158 bool waitForEnter=
false;
160 bool forever =
false;
171 bool doHCI_Connect =
true;
173 for(
int i=1; i<argc; i++) {
174 if( !strcmp(
"-wait", argv[i]) ) {
176 }
else if( !strcmp(
"-forever", argv[i]) ) {
178 }
else if( !strcmp(
"-dev_id", argv[i]) && argc > (i+1) ) {
179 dev_id = atoi(argv[++i]);
180 }
else if( !strcmp(
"-skipConnect", argv[i]) ) {
181 doHCI_Connect =
false;
182 }
else if( !strcmp(
"-mac", argv[i]) && argc > (i+1) ) {
183 std::string macstr = std::string(argv[++i]);
187 fprintf(stderr,
"dev_id %d\n", dev_id);
188 fprintf(stderr,
"doHCI_Connect %d\n", doHCI_Connect);
189 fprintf(stderr,
"waitForDevice: %s\n", waitForDevice.
toString().c_str());
192 fprintf(stderr,
"Press ENTER to continue\n");
197 std::shared_ptr<BTAdapter> adapter = mngr.
getAdapter(dev_id);
198 if(
nullptr == adapter ) {
199 fprintf(stderr,
"adapter dev_id %d not available.\n", dev_id);
203 fprintf(stderr,
"Adapter invalid: %s\n", adapter->
toString().c_str());
207 fprintf(stderr,
"Adapter not powered: %s\n", adapter->
toString().c_str());
210 fprintf(stderr,
"Using adapter: %s\n", adapter->
toString().c_str());
216 while( ok && ( forever || !foundDevice ) ) {
219 perror(
"Adapter start discovery failed");
223 std::shared_ptr<BTDevice> device =
nullptr;
226 while(
nullptr == device ) {
229 foundDevice =
deviceFound->getAddressAndType().matches(waitForDevice);
239 if( ok &&
nullptr != device ) {
246 if( doHCI_Connect ) {
249 fprintf(stderr,
"Connect: Failed res %s, %s\n",
to_string(res).c_str(), device->
toString().c_str());
252 fprintf(stderr,
"Connect: Success\n");
255 fprintf(stderr,
"Connect: Skipped %s\n", device->
toString().c_str());
258 const uint64_t td03 = t3 - t0;
259 const uint64_t td13 = t3 - t1;
260 const uint64_t td01 = t1 - t0;
261 fprintf(stderr,
" discovery-only %" PRIu64
" ms,\n"
262 " connect-only %" PRIu64
" ms,\n"
263 " discovered to hci-connected %" PRIu64
" ms,\n"
264 " total %" PRIu64
" ms,\n",
273 if(
nullptr != gatt ) {
281 const uint64_t td45 = t5 - t4;
282 const uint64_t td05 = t5 - t0;
283 fprintf(stderr,
"\n\n\n");
284 fprintf(stderr,
"GATT primary-services completed\n");
285 fprintf(stderr,
" gatt connect -> gatt complete %" PRIu64
" ms,\n"
286 " discovered to gatt complete %" PRIu64
" ms,\n"
287 " total %" PRIu64
" ms\n\n",
291 if(
nullptr != ga ) {
292 fprintf(stderr,
" GenericAccess: %s\n\n", ga->
toString().c_str());
296 if(
nullptr != di ) {
297 fprintf(stderr,
" DeviceInformation: %s\n\n", di->
toString().c_str());
303 fprintf(stderr,
" [%2.2d] Service %s\n", (
int)i, primService.
toString().c_str());
304 fprintf(stderr,
" [%2.2d] Service Characteristics\n", (
int)i);
306 for(
size_t j=0; j<serviceCharacteristics.
size() && gatt->
isConnected(); j++) {
307 BTGattChar & serviceChar = *serviceCharacteristics.
at(j);
308 fprintf(stderr,
" [%2.2d.%2.2d] Decla: %s\n", (
int)i, (
int)j, serviceChar.
toString().c_str());
312 fprintf(stderr,
" [%2.2d.%2.2d] Value: %s\n", (
int)i, (
int)j, value.
toString().c_str());
316 if(
nullptr != cccd ) {
319 if( enableNotification || enableIndication ) {
321 fprintf(stderr,
" [%2.2d.%2.2d] Config Notification(%d), Indication(%d): Result %d\n",
322 (
int)i, (
int)j, enableNotification, enableIndication, res);
330 fprintf(stderr,
"GATT connect failed: %s\n", gatt->
getStateString().c_str());
337 #ifdef SHOW_STATIC_SERVICE_CHARACTERISTIC_COMPOSITION
343 fprintf(stderr,
"GattServiceCharacteristic %d: %s\n", (
int)i, gsc->
toString().c_str());
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
void remove() noexcept
Disconnects this device via disconnect(..) if getConnected()==true and explicitly removes its shared ...
std::string toString() const noexcept
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.
std::string toString() const noexcept
EIRDataType
Bit mask of 'Extended Inquiry Response' (EIR) data fields, indicating a set of related data.
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...
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 readCharacteristicValue(const BTGattChar &c, POctets &res, int expectedLength=-1)
BT Core Spec v5.2: Vol 3, Part G GATT: 4.8.1 Read Characteristic Value.
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.
BTGattChar event listener for notification and indication events.
@ TEMPERATURE_MEASUREMENT
uint16_t getServerMTU() const noexcept
std::string toString() const noexcept override
int main(int argc, char *argv[])
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.
std::mutex mtxDeviceFound
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.
BTGattDescRef getClientCharConfig() noexcept
bool addStatusListener(std::shared_ptr< AdapterStatusListener > l)
Add the given listener to the list if not already present.
std::string toString() const noexcept override
bool addCharListener(std::shared_ptr< BTGattCharListener > l)
Add the given listener to the list if not already present.
uint16_t getUsedMTU() const noexcept
Transient read only octet data, i.e.
std::string to_string(const BDAddressType type) noexcept
BTAdapter represents one Bluetooth Controller.
std::string getStateString() const noexcept
const jau::darray< const GattServiceCharacteristic * > GATT_SERVICES
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
bool isConnected() const noexcept
std::shared_ptr< BTGattHandler > getGattHandler() noexcept
Returns the connected GATTHandler or nullptr, see connectGATT(), getGattService() and disconnect().
std::shared_ptr< BTDevice > deviceFound
This C++ direct_bt scanner example is a TinyB backward compatible and not fully event driven.
std::string toString() const
Persistent octet data, i.e.
bool configNotificationIndication(BTGattDesc &cd, const bool enableNotification, const bool enableIndication)
BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration.
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.
std::condition_variable cvDeviceFound
SMPPairingState
SMP Pairing Process state definition.
static constexpr int number(const Defaults d)
Unique Bluetooth EUI48 address and BDAddressType tuple.
std::shared_ptr< BTGattDesc > BTGattDescRef
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.
jau::darray< BTGattServiceRef > & discoverCompletePrimaryServices(std::shared_ptr< BTGattHandler > shared_this)
Discover all primary services and all its characteristics declarations including their client config.
std::string toString() const noexcept
@ BDADDR_UNDEFINED
Undefined.