diff options
author | Jacob Keeler <jacob.keeler@livioradio.com> | 2017-01-05 16:31:38 -0500 |
---|---|---|
committer | Jacob Keeler <jacob.keeler@livioradio.com> | 2017-03-03 10:48:09 -0500 |
commit | 8845f2d4b58f8d39e6055f3391ed065c4f250c54 (patch) | |
tree | 3a6fb4e959a8889cc55c597fefb4e4b9b065ee53 | |
parent | 77a9b3c7a3d8ccc9654930b9008f586ea3159a77 (diff) | |
download | sdl_core-feature/bluez5_support.tar.gz |
Modified device scanner to use bluetoothctl instead of bt-device, added automatic detection of BlueZ5feature/bluez5_support
Some systems, such as AGL, do not currrently have bluez-utils available. With the bluetoothctl shell introduced in BlueZ5, we can get a list of paired devices without this extra package.
-rw-r--r-- | CMakeLists.txt | 9 | ||||
-rw-r--r-- | src/components/transport_manager/src/bluetooth/bluetooth_device_scanner.cc | 40 |
2 files changed, 48 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 07dd4e8bea..6dd67e03a7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -273,6 +273,15 @@ endif() if (BUILD_BT_SUPPORT) add_definitions(-DBLUETOOTH_SUPPORT) message(STATUS "Bluetooth support enabled") + # Don't check for BlueZ version if we are cross-compiling, let that be specified in the toolchain + if (NOT CROSS_HOST) + exec_program("which bluetoothctl" + OUTPUT_VARIABLE BLUETOOTHCTL_DIR) + if (BLUETOOTHCTL_DIR) + add_definitions(-DBLUEZ5) + message(STATUS "BlueZ 5 detected") + endif () + endif () endif() if (BUILD_BACKTRACE_SUPPORT) diff --git a/src/components/transport_manager/src/bluetooth/bluetooth_device_scanner.cc b/src/components/transport_manager/src/bluetooth/bluetooth_device_scanner.cc index 85e6dc3f96..8bd7ea5c15 100644 --- a/src/components/transport_manager/src/bluetooth/bluetooth_device_scanner.cc +++ b/src/components/transport_manager/src/bluetooth/bluetooth_device_scanner.cc @@ -60,6 +60,23 @@ namespace transport_adapter { CREATE_LOGGERPTR_GLOBAL(logger_, "TransportManager") namespace { +#ifdef BLUEZ5 +int ValidateAddr(char* dev_list_entry) { + const int BT_ADDRESS_LENGTH = 17; + if (strlen(dev_list_entry) == BT_ADDRESS_LENGTH) { + for (int i = 0; i < BT_ADDRESS_LENGTH; i++) { + if (i % 3 < 2 && !isxdigit(dev_list_entry[i])) { + return false; + } else if (i % 3 == 2 && dev_list_entry[i] != ':') { + return false; + } + } + return true; + } else { + return false; + } +} +#else char* SplitToAddr(char* dev_list_entry) { char* bl_address = strtok(dev_list_entry, "()"); if (bl_address != NULL) { @@ -69,12 +86,19 @@ char* SplitToAddr(char* dev_list_entry) { return NULL; } } +#endif int FindPairedDevs(std::vector<bdaddr_t>* result) { LOG4CXX_TRACE(logger_, "enter. result adress: " << result); DCHECK(result != NULL); +#ifdef BLUEZ5 + const char* cmd = + "echo $\'paired-devices\\nquit\' | bluetoothctl | grep ^Device"; +#else const char* cmd = "bt-device -l"; + size_t counter = 0; +#endif FILE* pipe = popen(cmd, "r"); if (!pipe) { @@ -82,8 +106,20 @@ int FindPairedDevs(std::vector<bdaddr_t>* result) { return -1; } char* buffer = new char[1028]; - size_t counter = 0; + while (fgets(buffer, 1028, pipe) != NULL) { +#ifdef BLUEZ5 + // Ignore the first token (should be just "Device") + strtok(buffer, " "); + // Second token is the bluetooth address + char* bt_address = strtok(NULL, " "); + if (ValidateAddr(bt_address)) { + LOG4CXX_DEBUG(logger_, "Found paired device: " << bt_address); + bdaddr_t address; + str2ba(bt_address, &address); + result->push_back(address); + } +#else if (0 < counter++) { // skip first line char* bt_address = SplitToAddr(buffer); if (bt_address) { @@ -92,9 +128,11 @@ int FindPairedDevs(std::vector<bdaddr_t>* result) { result->push_back(address); } } +#endif delete[] buffer; buffer = new char[1028]; } + pclose(pipe); LOG4CXX_TRACE(logger_, "exit with 0"); delete[] buffer; |