Android app with C++ cannot use device Network

85 views Asked by At

I'm trying to study C++ and Android development by creating an App using the NDI SDK, it's somethin i'm interested on so I think it's a good way to learn.

I already learned how to implement the native scripts into the Java application, it is running.

But the problem is that this SDK reads the local network to find screen sources, I'm just trying to identify the available sources in the network, the SDK takes care of it.

But looks like the app is not connected to the network, because I compiled the same C++ file on windows and I could get the sources. But it do not find it in the app. I tested on both emulator and real device, same result.

!important

I'm running the NDI source in the same system (Windows) that I'm developing the app. But the SDK works perfectly when I run the C++ file on windows console, so probably this isn't the problem.

Yes, I inserted the permissions:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Material3.DayNight"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

The app takes the same time to search, but it can't find the sources. What am I doing wrong?

Here's the C++ code:

#include <iostream>
#include <chrono>
#include <string>
#include "jni.h"
#include "Processing.NDI.Lib.h"

extern "C"
JNIEXPORT jstring JNICALL
Java_com_tildend_ndimonitormobile_NDILibrary_getSources(JNIEnv *env, jobject thiz) {
    // Not required, but "correct" (see the SDK documentation).
    if (!NDIlib_initialize())
        return env->NewStringUTF("Not initialized");

    // We are going to create an NDI finder that locates sources on the network.
    NDIlib_find_instance_t pNDI_find = NDIlib_find_create_v2();
    if (!pNDI_find)
        return env->NewStringUTF("Finder error");

    using namespace std::chrono;
    uint32_t no_sources = 0;
    const NDIlib_source_t* p_sources = nullptr;
    for (const auto start = high_resolution_clock::now(); high_resolution_clock::now() - start < minutes(1);) {
        // Wait up till 5 seconds to check for new sources to be added or removed
        if (!NDIlib_find_wait_for_sources(pNDI_find, 5000 /* milliseconds */)) {
            std::cout << "No change to the sources found.\n";
            continue;
        }

        // Get the updated list of sources
        p_sources = NDIlib_find_get_current_sources(pNDI_find, &no_sources);

        // Display all the sources.
        printf("Network sources (%u found).\n", no_sources);
        for (uint32_t i = 0; i < no_sources; i++) {
            std::cout << i + 1 << p_sources[i].p_ndi_name << std::endl;
        }
    }

    // Success. We are done
    return p_sources != nullptr ? env->NewStringUTF(p_sources[0].p_ndi_name) : env->NewStringUTF("Nothing found");
}

I'm new to C++, so there's probably a lot of things to improve on it.

I tried on emulator and real device, nothing changed. I tried removing the permission requests... yes, nothing I tried running the SDK in a C++ file with the same code on Windows Console, and it worked.

What can I try?

0

There are 0 answers