Unity Plugin trouble

I have created an android library plugin for Unity (we use unity at work to create apps). The plugin seems to be working except for one thing, I can’t seem to get the setBackgroundScanPeriod or the setForegroundScanPeriod to work.

Here’s part of my Android code:

I declare the beaconManager of my class

private BeaconManager beaconManager;

I have a method to create the beaconManager in the class

public boolean setBeaconManager(){
	try{
		this.beaconManager = new BeaconManager(this.context);
	} catch (Exception e){
		Log.d("Unity", e.toString());
		return false;
	}
	return true;
}

I have another method to set the background scan period:

public String setBackgroundScanPeriod(long scanPeriod, long waitTime){
	try{
		beaconManager.setBackgroundScanPeriod(scanPeriod, waitTime);
	} catch (Exception e){
		return e.toString();
	}
	return "correcto";
}

Now, my Unity code looks like this (All this on my AwakeContent section):

try{
		activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
	} catch (Exception e){
		Debug.Log(e.ToString());
	}
	iniciarActivity();

	try{
		pluginClass = new AndroidJavaClass("com.consystec.beaconplugin.BeaconPlugin");
	} catch (Exception e){
		Debug.Log (e.ToString());
	}
	crearBeaconPlugin();

	setBackgroundScanPeriod(25000, 0);

My function to call for the android method is:

void setBackgroundScanPeriod(long scanPeriod, long waitMilis){
	using(pluginClass){
		activityContext.Call ("runOnUiThread", new AndroidJavaRunnable(() => {
			string estado = beaconPlugin.Call<string>("setBackgroundScanPeriod", scanPeriod, waitMilis);
			Debug.Log ("Background: " + estado);
		}));
	}
}

But I keep getting this Exception message:

I/Unity(31204): Background: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.estimote.sdk.BeaconManager.setBackgroundScanPeriod(long, long)' on a null object reference

Anyone has any idea what’s wrong?

Ok. I am so ashamed ot myself right now. I hadn’t noticed a “little” but very important thing. I had to call the setBeaconManager() function in Unity before the setBackgroundScanPeriod() function. I called the function to set the beacon manager inside my button instead of the AwakeContent.

I know, this was a very newbie mistake to make but I prefer to leave the topic along with this answer here than pretend it never happened since it could help someone else in the future.

Regards!

1 Like

Kevin Just started looking into this any chance you can sell the plugin or are you available to help
with an implementation?

Let me know

antonio(at)parsec3d.com
fb/parsec3d
@parsec3d

Hi Kevin. I am also very interested in getting/buying your plugin for bridging Estimote SDK with Unity. Please contact me via egil@theawe.dk :slight_smile: Big thanks!

Hiii…
Am also suggested this trouble…
/The comments describe what you would need to do if you were using raw JNI
AndroidJavaObject jo = new AndroidJavaObject(“java.lang.String”, “some_string”);
// jni.FindClass(“java.lang.String”);
// jni.GetMethodID(classID, “”, “(Ljava/lang/String;)V”);
// jni.NewStringUTF(“some_string”);
// jni.NewObject(classID, methodID, javaString);
int hash = jo.Call(“hashCode”);
// jni.GetMethodID(classID, “hashCode”, “()I”);
// jni.CallIntMethod(objectID, methodID);

Android Training