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?