Фоновое отслеживание SSID WI-Fi в Unity Android приложении

Необходимо, чтобы приложение в фоне распознавало SSID Wi-Fi, к которому сейчас подключен телефон. Код работы при использовании приложения есть:

using UnityEngine;
using UnityEngine.Events;
public class SSID : MonoBehaviour
{
    public static SSID Instance { get; private set; }
    public UnityEvent<string> ChageWifi { get; private set; } = new();

    private const string JAVA_CLASS_IDENTIFIER = "com.unity3d.player.UnityPlayer";
    private string _lastSSID = string.Empty;

    private void Start() =>
        Instance = this;

    private void Update()
    {
        AndroidJavaObject activity = new AndroidJavaClass(JAVA_CLASS_IDENTIFIER).GetStatic<AndroidJavaObject>("currentActivity");
        var _wifiManager = activity.Call<AndroidJavaObject>("getSystemService", "wifi");
        using AndroidJavaObject wifiInfo = _wifiManager.Call<AndroidJavaObject>("getConnectionInfo");
        using AndroidJavaObject currentSsid = wifiInfo.Call<AndroidJavaObject>("getSSID");
        var ssid = currentSsid.Call<string>("toString");

        var isEquals = string.Equals(ssid, _lastSSID);
        if (isEquals)
            return;

        _lastSSID = ssid;
        ChageWifi.Invoke(string.Equals(ssid, "<unknown ssid>") ? null : ssid);
        Debug.Log(ssid);
    }
}

Андроид манифест:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.unity3d.player" >

    <uses-sdk
        android:minSdkVersion="27"
        android:targetSdkVersion="33" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    
    <uses-feature android:glEsVersion="0x00030000" />
    <uses-feature
        android:name="android.hardware.vulkan.version"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.touchscreen"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.touchscreen.multitouch"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.touchscreen.multitouch.distinct"
        android:required="false" />

    <application android:extractNativeLibs="true" >
        <meta-data
            android:name="unity.splash-mode"
            android:value="0" />
        <meta-data
            android:name="unity.splash-enable"
            android:value="True" />
        <meta-data
            android:name="unity.launch-fullscreen"
            android:value="True" />
        <meta-data
            android:name="notch.config"
            android:value="portrait|landscape" />
        <meta-data
            android:name="unity.auto-report-fully-drawn"
            android:value="true" />

        <activity
            android:name="com.unity3d.player.UnityPlayerActivity"
            android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density"
            android:exported="true"
            android:hardwareAccelerated="false"
            android:launchMode="singleTask"
            android:resizeableActivity="false"
            android:screenOrientation="portrait"
            android:theme="@style/UnityThemeSelector" >
            <intent-filter>
                <category android:name="android.intent.category.LAUNCHER" />

                <action android:name="android.intent.action.MAIN" />
            </intent-filter>

            <meta-data
                android:name="unityplayer.UnityActivity"
                android:value="true" />
            <meta-data
                android:name="notch_support"
                android:value="true" />
        </activity>
    </application>

</manifest>

Только это не работает в фоне.

Обратил внимание на GitHub https://github.com/nintendaii/unity-background-service , где показана работа в фоне, однако у в моём случае код не работает (А точнее push-сервис появляется, но не работает отображение SSID через другие push)

Код сервиса:

using UnityEngine;

public class BackgroundService : MonoBehaviour
{
    public static BackgroundService Instance { get; private set; }

    private AndroidJavaClass unityClass;
    private AndroidJavaObject unityActivity;
    private AndroidJavaClass customClass;
    private const string PackageName = "com.kdg.toast.plugin.Bridge";
    private const string UnityDefaultJavaClassName = "com.unity3d.player.UnityPlayer";
    private const string CustomClassReceiveActivityInstanceMethod = "ReceiveActivityInstance";
    private const string CustomClassStartServiceMethod = "StartService";
    private const string CustomClassStopServiceMethod = "StopService";


    private void Awake()
    {
        Instance = this;
        SendActivityReference(PackageName);
    }


    private void SendActivityReference(string packageName)
    {
        unityClass = new AndroidJavaClass(UnityDefaultJavaClassName);
        unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
        customClass = new AndroidJavaClass(packageName);
        customClass.CallStatic(CustomClassReceiveActivityInstanceMethod, unityActivity);
        StartService();
    }

    public void StartService()
    {
        customClass.CallStatic(CustomClassStartServiceMethod);
    }

    public void StopService()
    {
        customClass.CallStatic(CustomClassStopServiceMethod);
    }
}

Код уведомлений:

using System.Collections;
using UnityEngine;

public class PushCorutine : MonoBehaviour
{
    private void Start() =>
        Starting();

    private void Starting() =>
        StartCoroutine(CreatePush());

    private IEnumerator CreatePush()
    {
        yield return new WaitForSeconds(2);
        PushSSID.Instance.Push("asda");
        Starting();
    }
}

Код отправки уведомлений:

using NotificationSamples;
using System;
using UnityEngine;

public class PushSSID : MonoBehaviour
{
    public static PushSSID Instance { get; private set; }
    [SerializeField] private GameNotificationsManager _notifManager;

    private void Start()
    {
        Instance = this;

        SSID.Instance.ChageWifi.AddListener(Push);
        GameNotificationChannel channel = new("Isoveli", "Wifi notification", "notification");
        _notifManager.Initialize(channel);
    }

    public void Push(string nameWifi)
    {
        IGameNotification notification = _notifManager.CreateNotification();
        if (notification == null)
            return;
        notification.Title = nameWifi != null ? "Вы подключились к Wi-fi" : "Вы отключились от Wi-fi";
        notification.Body = nameWifi != null ? "Подлючены к " + nameWifi : "Вы были отключены от WiFi";
        notification.DeliveryTime = DateTime.Now;
        _notifManager.ScheduleNotification(notification);
    }
}

Ответы (0 шт):