This example does not represent the recommended utilization of Direct-BT.
#include <cinttypes>
extern "C" {
#include <unistd.h>
}
const AdapterSetting changedmask,
const uint64_t timestamp)
override {
fprintf(stderr, "****** Native Adapter SETTINGS_CHANGED: %s -> %s, changed %s\n",
fprintf(stderr, "Status BTAdapter:\n");
fprintf(stderr,
"%s\n", a.
toString().c_str());
(void)timestamp;
}
void discoveringChanged(
BTAdapter &a,
const ScanType currentMeta,
const ScanType changedType,
const bool changedEnabled,
const bool keepAlive,
const uint64_t timestamp)
override {
fprintf(stderr, "****** DISCOVERING: meta %s, changed[%s, enabled %d, keepAlive %d]: %s\n",
(void)timestamp;
}
bool deviceFound(std::shared_ptr<BTDevice> device,
const uint64_t timestamp)
override {
fprintf(stderr,
"****** FOUND__: %s\n", device->
toString(
true).c_str());
fprintf(stderr, "Status Adapter:\n");
{
return true;
}
(void)timestamp;
}
void deviceUpdated(std::shared_ptr<BTDevice> device,
const EIRDataType updateMask,
const uint64_t timestamp)
override {
fprintf(stderr,
"****** UPDATED: %s of %s\n",
to_string(updateMask).c_str(), device->
toString(
true).c_str());
(void)timestamp;
}
void deviceConnected(std::shared_ptr<BTDevice> device, const uint16_t handle, const uint64_t timestamp) override {
fprintf(stderr,
"****** CONNECTED: %s\n", device->
toString(
true).c_str());
(void)handle;
(void)timestamp;
}
void devicePairingState(std::shared_ptr<BTDevice> device,
const SMPPairingState state,
const PairingMode mode,
const uint64_t timestamp)
override {
fprintf(stderr, "****** PAIRING STATE: state %s, mode %s, %s\n",
(void)timestamp;
}
void deviceReady(std::shared_ptr<BTDevice> device, const uint64_t timestamp) override {
fprintf(stderr,
"****** READY: %s\n", device->
toString().c_str());
(void)timestamp;
}
void deviceDisconnected(std::shared_ptr<BTDevice> device,
const HCIStatusCode reason,
const uint16_t handle,
const uint64_t timestamp)
override {
fprintf(stderr, "****** DISCONNECTED: Reason 0x%X (%s), old handle %s: %s\n",
static_cast<uint8_t
>(reason),
to_string(reason).c_str(),
(void)handle;
(void)timestamp;
}
std::string toString() const override {
return "MyAdapterStatusListener[this "+
to_hexstring(
this)+
"]";
}
};
const std::shared_ptr<BTDevice> dev = charDecl->getDeviceChecked();
fprintf(stderr, "****** GATT Notify (td %" PRIu64 " ms, dev-discovered %" PRIu64 " ms): From %s\n",
if( nullptr != charDecl ) {
fprintf(stderr, "****** decl %s\n", charDecl->toString().c_str());
}
fprintf(stderr,
"****** rawv %s\n",
charValue.toString().c_str());
}
const bool confirmationSent) override
{
const std::shared_ptr<BTDevice> dev = charDecl->getDeviceChecked();
fprintf(stderr, "****** GATT Indication (confirmed %d, td(msg %" PRIu64 " ms, dev-discovered %" PRIu64 " ms): From %s\n",
if( nullptr != charDecl ) {
fprintf(stderr, "****** decl %s\n", charDecl->toString().c_str());
if( nullptr != temp ) {
fprintf(stderr,
"****** valu %s\n", temp->
toString().c_str());
}
}
}
fprintf(stderr,
"****** rawv %s\n",
charValue.toString().c_str());
}
};
int main(
int argc,
char *argv[])
{
bool ok = true, foundDevice=false;
int dev_id = 0;
bool waitForEnter=false;
bool forever = false;
bool doHCI_Connect = true;
for(int i=1; i<argc; i++) {
if( !strcmp("-wait", argv[i]) ) {
waitForEnter = true;
} else if( !strcmp("-forever", argv[i]) ) {
forever = true;
} else if( !strcmp("-dev_id", argv[i]) && argc > (i+1) ) {
dev_id = atoi(argv[++i]);
} else if( !strcmp("-skipConnect", argv[i]) ) {
doHCI_Connect = false;
} else if( !strcmp("-mac", argv[i]) && argc > (i+1) ) {
std::string macstr = std::string(argv[++i]);
}
}
fprintf(stderr, "dev_id %d\n", dev_id);
fprintf(stderr, "doHCI_Connect %d\n", doHCI_Connect);
fprintf(stderr,
"waitForDevice: %s\n", waitForDevice.
toString().c_str());
if( waitForEnter ) {
fprintf(stderr, "Press ENTER to continue\n");
getchar();
}
std::shared_ptr<BTAdapter> adapter = mngr.
getAdapter(dev_id);
if( nullptr == adapter ) {
fprintf(stderr, "adapter dev_id %d not available.\n", dev_id);
exit(1);
}
fprintf(stderr,
"Adapter invalid: %s\n", adapter->
toString().c_str());
exit(1);
}
fprintf(stderr,
"Adapter not powered: %s\n", adapter->
toString().c_str());
exit(1);
}
fprintf(stderr,
"Using adapter: %s\n", adapter->
toString().c_str());
while( ok && ( forever || !foundDevice ) ) {
if( !ok) {
perror("Adapter start discovery failed");
goto out;
}
std::shared_ptr<BTDevice> device = nullptr;
{
while( nullptr == device ) {
foundDevice =
deviceFound->getAddressAndType().matches(waitForDevice);
}
}
}
}
if( ok && nullptr != device ) {
if( doHCI_Connect ) {
fprintf(stderr,
"Connect: Failed res %s, %s\n",
to_string(res).c_str(), device->
toString().c_str());
} else {
fprintf(stderr, "Connect: Success\n");
}
} else {
fprintf(stderr,
"Connect: Skipped %s\n", device->
toString().c_str());
}
const uint64_t td03 = t3 - t0;
const uint64_t td13 = t3 - t1;
const uint64_t td01 = t1 - t0;
fprintf(stderr, " discovery-only %" PRIu64 " ms,\n"
" connect-only %" PRIu64 " ms,\n"
" discovered to hci-connected %" PRIu64 " ms,\n"
" total %" PRIu64 " ms,\n",
if( nullptr != gatt ) {
{
const uint64_t td45 = t5 - t4;
const uint64_t td05 = t5 - t0;
fprintf(stderr, "\n\n\n");
fprintf(stderr, "GATT primary-services completed\n");
fprintf(stderr, " gatt connect -> gatt complete %" PRIu64 " ms,\n"
" discovered to gatt complete %" PRIu64 " ms,\n"
" total %" PRIu64 " ms\n\n",
}
if( nullptr != ga ) {
fprintf(stderr,
" GenericAccess: %s\n\n", ga->
toString().c_str());
}
if( nullptr != di ) {
fprintf(stderr,
" DeviceInformation: %s\n\n", di->
toString().c_str());
}
}
fprintf(stderr,
" [%2.2d] Service %s\n", (
int)i, primService.
toString().c_str());
fprintf(stderr, " [%2.2d] Service Characteristics\n", (int)i);
for(
size_t j=0; j<serviceCharacteristics.
size() && gatt->
isConnected(); j++) {
fprintf(stderr,
" [%2.2d.%2.2d] Decla: %s\n", (
int)i, (
int)j, serviceChar.
toString().c_str());
fprintf(stderr, " [%2.2d.%2.2d] Value: %s\n", (int)i, (int)j, value.toString().c_str());
}
}
if( nullptr != cccd ) {
if( enableNotification || enableIndication ) {
fprintf(stderr, " [%2.2d.%2.2d] Config Notification(%d), Indication(%d): Result %d\n",
(int)i, (int)j, enableNotification, enableIndication, res);
}
}
}
}
sleep(1);
} else {
fprintf(stderr,
"GATT connect failed: %s\n", gatt->
getStateString().c_str());
}
}
}
#ifdef SHOW_STATIC_SERVICE_CHARACTERISTIC_COMPOSITION
fprintf(stderr,
"GattServiceCharacteristic %d: %s\n", (
int)i, gsc->
toString().c_str());
}
#endif
out:
return 0;
}