| 51 | } |
| 52 | |
| 53 | int BrainAlive::prepare_session () |
| 54 | { |
| 55 | if (initialized) |
| 56 | { |
| 57 | safe_logger (spdlog::level::info, "Session is already prepared"); |
| 58 | return (int)BrainFlowExitCodes::STATUS_OK; |
| 59 | } |
| 60 | if (params.timeout < 1) |
| 61 | { |
| 62 | params.timeout = 5; |
| 63 | } |
| 64 | safe_logger (spdlog::level::info, "Use timeout for discovery: {}", params.timeout); |
| 65 | if (!init_dll_loader ()) |
| 66 | { |
| 67 | safe_logger (spdlog::level::err, "Failed to init dll_loader"); |
| 68 | return (int)BrainFlowExitCodes::GENERAL_ERROR; |
| 69 | } |
| 70 | size_t adapter_count = simpleble_adapter_get_count (); |
| 71 | if (adapter_count == 0) |
| 72 | { |
| 73 | safe_logger (spdlog::level::err, "No BLE adapters found"); |
| 74 | return (int)BrainFlowExitCodes::UNABLE_TO_OPEN_PORT_ERROR; |
| 75 | } |
| 76 | |
| 77 | brainalive_adapter = simpleble_adapter_get_handle (0); |
| 78 | if (brainalive_adapter == NULL) |
| 79 | { |
| 80 | safe_logger (spdlog::level::err, "Adapter is NULL"); |
| 81 | return (int)BrainFlowExitCodes::UNABLE_TO_OPEN_PORT_ERROR; |
| 82 | } |
| 83 | |
| 84 | simpleble_adapter_set_callback_on_scan_found ( |
| 85 | brainalive_adapter, ::brainalive_adapter_1_on_scan_found, (void *)this); |
| 86 | |
| 87 | if (!simpleble_adapter_is_bluetooth_enabled ()) |
| 88 | { |
| 89 | safe_logger (spdlog::level::warn, "Probably bluetooth is disabled."); |
| 90 | // dont throw an exception because of this |
| 91 | // https://github.com/OpenBluetoothToolbox/SimpleBLE/issues/115 |
| 92 | } |
| 93 | |
| 94 | simpleble_adapter_scan_start (brainalive_adapter); |
| 95 | int res = (int)BrainFlowExitCodes::STATUS_OK; |
| 96 | std::unique_lock<std::mutex> lk (m); |
| 97 | auto sec = std::chrono::seconds (1); |
| 98 | if (cv.wait_for ( |
| 99 | lk, params.timeout * sec, [this] { return this->brainalive_peripheral != NULL; })) |
| 100 | { |
| 101 | safe_logger (spdlog::level::info, "Found BrainAlive device"); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | safe_logger (spdlog::level::err, "Failed to find BrainAlive Device"); |
| 106 | res = (int)BrainFlowExitCodes::BOARD_NOT_READY_ERROR; |
| 107 | } |
| 108 | simpleble_adapter_scan_stop (brainalive_adapter); |
| 109 | if (res == (int)BrainFlowExitCodes::STATUS_OK) |
| 110 | { |
nothing calls this directly
no test coverage detected