From d945dee5b039e9e2d51b36852ef58a812acab302 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 10 Jul 2015 09:23:49 +0200 Subject: o Some iOS experiments. Very WiP. --- apps/CMakeLists.txt | 1 + ble/CMakeLists.txt | 37 ++++++++++++++++++++++++++++++++++--- ble/OsxBluetooth.mm | 41 ++++++++++++++++++++++++++++++++++------- ble/OsxBluetoothImpl.h | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+), 10 deletions(-) create mode 100644 ble/OsxBluetoothImpl.h diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index a7defa2..81cfaec 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -76,6 +76,7 @@ target_link_libraries(launcher ${BLUEZ_LIBRARIES}) target_link_libraries(launcher ${PQXX_LIBRARIES}) target_link_libraries(launcher ${LOG4CPLUS_LIBRARIES}) target_link_libraries(launcher ${CMAKE_THREAD_LIBS_INIT}) +#target_link_libraries(launcher PUBLIC Foundation) target_compile_options(launcher PRIVATE -Wno-deprecated-register ${COMPILE_OPTIONS}) foreach(app ${APPS}) diff --git a/ble/CMakeLists.txt b/ble/CMakeLists.txt index 5f20a21..fef06c5 100644 --- a/ble/CMakeLists.txt +++ b/ble/CMakeLists.txt @@ -10,12 +10,43 @@ if(IS_LINUX) list(APPEND SOURCE_FILES LinuxBluetooth.cpp LinuxBluetooth.h) list(APPEND COMPILE_OPTIONS -DIS_LINUX) elseif(IS_APPLE) - list(APPEND SOURCE_FILES OsxBluetooth.mm OsxBluetooth.h) + list(APPEND SOURCE_FILES OsxBluetooth.mm OsxBluetooth.h OsxBluetoothImpl.h) list(APPEND COMPILE_OPTIONS -DIS_APPLE) + + find_library(CORE_BLUETOOTH_LIBRARY CoreBluetooth) + if (NOT CORE_BLUETOOTH_LIBRARY) + message(FATAL_ERROR "CoreBluetooth not found") + endif() + message(STATUS "CORE_BLUETOOTH_LIBRARY=${CORE_BLUETOOTH_LIBRARY}") +# list(APPEND INCLUDE_DIRECTORIES ${CORE_BLUETOOTH_LIBRARY}/Headers) +# list(APPEND LIBRARIES_LIST ${CORE_BLUETOOTH_LIBRARY}/CoreBluetooth) + list(APPEND LIBRARIES_LIST ${CORE_BLUETOOTH_LIBRARY}) + + find_library(IOBLUETOOTH_LIBRARY IOBluetooth) + if (NOT IOBLUETOOTH_LIBRARY) + message(FATAL_ERROR "IOBluetooth not found") + endif() + message(STATUS "IOBLUETOOTH_LIBRARY=${IOBLUETOOTH_LIBRARY}") +# list(APPEND INCLUDE_DIRECTORIES ${IOBLUETOOTH_LIBRARY}/Headers) +# list(APPEND LIBRARIES_LIST ${IOBLUETOOTH_LIBRARY}/IOBluetooth) + list(APPEND LIBRARIES_LIST ${IOBLUETOOTH_LIBRARY}) + + find_library(FOUNDATION_LIBRARY Foundation) + if (NOT FOUNDATION_LIBRARY) + message(FATAL_ERROR "Foundation not found") + endif() + message(STATUS "FOUNDATION_LIBRARY=${FOUNDATION_LIBRARY}") +# list(APPEND INCLUDE_DIRECTORIES ${FOUNDATION_LIBRARY}/Headers) +# list(APPEND LIBRARIES_LIST ${FOUNDATION_LIBRARY}/Foundation) + list(APPEND LIBRARIES_LIST ${FOUNDATION_LIBRARY}) endif() +list(APPEND INCLUDE_DIRECTORIES "${PROJECT_SOURCE_DIR}/include") + find_package(Boost COMPONENTS regex system program_options REQUIRED) +list(APPEND INCLUDE_DIRECTORIES "${Boost_INCLUDE_DIRS}") + add_library(ble ${SOURCE_FILES}) -target_include_directories(ble PUBLIC "${PROJECT_SOURCE_DIR}/include") -target_include_directories(ble PUBLIC "${Boost_INCLUDE_DIRS}") +target_include_directories(ble PUBLIC "${Boost_INCLUDE_DIRS}" ${INCLUDE_DIRECTORIES}) target_compile_options(ble PRIVATE -Wno-deprecated-register ${COMPILE_OPTIONS}) +target_link_libraries(ble PUBLIC ${LIBRARIES_LIST}) diff --git a/ble/OsxBluetooth.mm b/ble/OsxBluetooth.mm index e94edaf..32645d5 100644 --- a/ble/OsxBluetooth.mm +++ b/ble/OsxBluetooth.mm @@ -1,14 +1,32 @@ -#import -#import -#import -#import +#include -#import "BluetoothImpl.h" -#import "OsxBluetooth.h" +#include "BluetoothImpl.h" +#include "OsxBluetooth.h" +#include "OsxBluetoothImpl.h" #include + using namespace std; +@implementation InquiryDelegateWrapper + +- (id) initWithCPPInstance:(trygvis::bluetooth::osx::InquiryDelegate*)_adapter +{ + self = [super init]; + if (self) { + adapter = _adapter; + } + return self; +} + +- (void)deviceInquiryStarted:(IOBluetoothDeviceInquiry *)sender; +{ + if (adapter) + adapter->deviceInquiryStarted(); +} + +@end + namespace trygvis { namespace bluetooth { namespace osx { @@ -74,7 +92,16 @@ void OsxBluetoothAdapter::startScan() { void OsxBluetoothAdapter::stopScan() { } -void OsxBluetoothAdapter::runScan(std::function) { +void OsxBluetoothAdapter::runScan(std::function callback) { + auto y = new InquiryDelegate(*this, callback); + id x = [InquiryDelegateWrapper ::initWithCPPInstance:y]; + inq = [IOBluetoothDeviceInquiry inquiryWithDelegate:x]; + if(!inq) { + throw BluetoothException(&adapter, "Could not initialize inquery"); + } +} + +void InquiryDelegate::deviceInquiryStarted() { } BluetoothDevice &OsxBluetoothAdapter::getDevice(Mac &mac) { diff --git a/ble/OsxBluetoothImpl.h b/ble/OsxBluetoothImpl.h new file mode 100644 index 0000000..2abea7e --- /dev/null +++ b/ble/OsxBluetoothImpl.h @@ -0,0 +1,41 @@ +#ifndef OSX_BLUETOOTH_IMPL_H +#define OSX_BLUETOOTH_IMPL_H + +#import +#import +#import +#import +#import + +namespace trygvis { +namespace bluetooth { +namespace osx { + +class InquiryDelegate { +public: + InquiryDelegate(OsxBluetoothAdapter &adapter, std::function callback) : adapter(adapter), callback(callback) { + } + + void deviceInquiryStarted(); +private: + OsxBluetoothAdapter &adapter; + std::function callback; +}; + +} +} +} + +// TODO: can this be in a c++ namespace? + +@interface InquiryDelegateWrapper : NSObject { +// - (void)deviceInquiryStarted:(IOBluetoothDeviceInquiry *)sender +//trygvis::bluetooth::osx::OsxBluetoothAdapter *adapter; +trygvis::bluetooth::osx::InquiryDelegate* adapter; +} + +- (id) initWithCPPInstance:(trygvis::bluetooth::osx::InquiryDelegate*)adapter; +- (void)deviceInquiryStarted:(IOBluetoothDeviceInquiry *)sender; +@end + +#endif -- cgit v1.2.3