Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect class name of the Polar BLE SDK class you want to use #500

Open
2 of 10 tasks
PaoloSalvatico opened this issue Nov 8, 2024 · 0 comments
Open
2 of 10 tasks
Labels
question Further information is requested

Comments

@PaoloSalvatico
Copy link

PaoloSalvatico commented Nov 8, 2024

Platform your question concerns:

  • Android
  • iOS
  • Other
  • Platform is not relevant for this question

Device:

  • Polar OH1
  • Polar Verity Sense
  • Polar H10
  • Polar H9
  • Other
  • Device is not relevant for this question

Description:
I'm working on a Unity project that will use the poler sdk in the Meta Quest 3. I created an .aar file from the demo PolarSDK-ECG-HR-Demo and i put it in the Unity folder, then i updated manifest and gradle.build. I created a script to use the sdk function, but i keep receiving the same error "Java.lang.ClassNotFoundException to com.polar.sdk.api.PolarBleApiDefaultImpl" to the class name i put in the script.

My question is what should i put as class name to create an instance of the polar sdk and use its functions? Alse Is it possible to do it in the first place, can i create a file .aar and put in Unity then create a build that can use the polar functions?

Here's my script:

// Unity script

using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.Android;
using System;
using System.Collections;

public class PolarManager : MonoBehaviour
{
public TextMeshProUGUI statusText; // Reference to your UI Text element
public TextMeshProUGUI playerText;
public TextMeshProUGUI activityText;

private AndroidJavaObject polarBleSdk;
private string cacheDirectory;

void Start()
{
    StartCoroutine(RequestPermissionsCoroutine());
}

public void StartPermission()
{
    StartCoroutine(RequestPermissionsCoroutine());
}

IEnumerator RequestPermissionsCoroutine()
{
    // Request permissions
    Permission.RequestUserPermission(Permission.FineLocation);
    Permission.RequestUserPermission("android.permission.BLUETOOTH_CONNECT");
    Permission.RequestUserPermission("android.permission.BLUETOOTH_SCAN");

    // Wait until permissions are granted
    while (!Permission.HasUserAuthorizedPermission(Permission.FineLocation) ||
           !Permission.HasUserAuthorizedPermission("android.permission.BLUETOOTH_CONNECT") ||
           !Permission.HasUserAuthorizedPermission("android.permission.BLUETOOTH_SCAN"))
    {
        yield return null;
    }

    // Permissions granted, proceed to initialize
    CallNativePlugin();
    //StartScanning();
}

public void CallNativePlugin()
{
    // Retrieve the UnityPlayer class.
    AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    if (unityPlayerClass == null) return;
    UpdateStatus(playerText, "UnityPlayer found.");

    // Retrieve the UnityPlayerActivity object ( a.k.a. the current context )
    AndroidJavaObject unityActivity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
    if (unityActivity == null) return;
    UpdateStatus(activityText, "Activity ok.");

    // Retrieve the PolarBleApiDefaultImpl class from the Polar SDK.
    AndroidJavaClass polarBleApiDefaultImpl = new AndroidJavaClass("com.polar.sdk.api.PolarBleApiDefaultImpl");
    if (polarBleApiDefaultImpl == null) return;
    UpdateStatus(statusText, "polar api default instantiate");

    // Initialize the PolarBleApi with desired features, e.g., HR monitoring
    int features = (int)PolarBleSdkFeature.FEATURE_HR;
    polarBleSdk = polarBleApiDefaultImpl.CallStatic<AndroidJavaObject>("defaultImplementation", unityActivity, features);

    UpdateStatus(activityText, "PolarBleApi initialized.");

    // Set API callback
    SetApiCallback();
}

void SetApiCallback()
{
    // Create a callback object
    AndroidJavaProxy apiCallback = new PolarBleApiCallback(this);
    polarBleSdk.Call("setApiCallback", apiCallback);
}

public void StartScanning()
{
    polarBleSdk.Call("searchForDevice");
}

public void ConnectToDevice(string deviceId)
{
    polarBleSdk.Call("connectToDevice", deviceId);
}

public void DisconnectFromDevice(string deviceId)
{
    polarBleSdk.Call("disconnectFromDevice", deviceId);
}

// Callback methods
public void OnDeviceConnected(string identifier)
{
    UpdateStatus(statusText, "Device connected: " + identifier);
}

public void OnDeviceDisconnected(string identifier)
{
    UpdateStatus(statusText, "Device disconnected: " + identifier);
}

public void OnHeartRateReceived(string identifier, int heartRate)
{
    UpdateStatus(statusText, "Heart Rate: " + heartRate);
    // Use the heart rate data as needed
}

public void OnDeviceFound(string deviceId, string name)
{
    UpdateStatus(statusText, "Device found: " + name + " (" + deviceId + ")");
    // Optionally, automatically connect or display a list to the user
    ConnectToDevice(deviceId);
}

void UpdateStatus(TextMeshProUGUI text, string message)
{
    text.text += message + "\n";
}

// Enums for features
public enum PolarBleSdkFeature
{
    FEATURE_HR = 0,
}

// AndroidJavaProxy for PolarBleApiCallback
private class PolarBleApiCallback : AndroidJavaProxy
{
    private PolarManager manager;

    public PolarBleApiCallback(PolarManager manager) : base("com.polar.polarsdkecghrdemo")
    {
        this.manager = manager;
    }

    public void deviceConnected(AndroidJavaObject polarDeviceInfo)
    {
        string deviceId = polarDeviceInfo.Get<string>("deviceId");
        manager.OnDeviceConnected(deviceId);
    }

    public void deviceDisconnected(AndroidJavaObject polarDeviceInfo)
    {
        string deviceId = polarDeviceInfo.Get<string>("deviceId");
        manager.OnDeviceDisconnected(deviceId);
    }

    public void hrNotificationReceived(string identifier, AndroidJavaObject polarHrData)
    {
        int heartRate = polarHrData.Get<int>("hr");
        manager.OnHeartRateReceived(identifier, heartRate);
    }

    public void deviceFound(AndroidJavaObject polarDeviceInfo)
    {
        string deviceId = polarDeviceInfo.Get<string>("deviceId");
        string name = polarDeviceInfo.Get<string>("name");
        manager.OnDeviceFound(deviceId, name);
    }
}

}

@PaoloSalvatico PaoloSalvatico added the question Further information is requested label Nov 8, 2024
@PaoloSalvatico PaoloSalvatico changed the title incorrect class name of the Polar BLE SDK class you want to use Incorrect class name of the Polar BLE SDK class you want to use Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant