I am creating an Android app that uses AllJoyn to connect to a Lifx bulb and provides functionality to control this bulb. I used the tutorial app and the sample app from AllJoyn demonstrating their lightning sdk as a basis.
When the app is loaded it provides the user with a list of the current bulbs on the network. When the app is sent to the background (with the back key and not the home key) and then resumed, the LightingDirector returns an empty array as if there is no lights and I don't know why. I've noticed their Android lighting tutorial app also doesn't display my bulbs when resumed from the background either, so I suspect an SDK bug.
To fix this I am current using System.exit(0) in my onDestroy() method to solve the issue, but this is not a good solution. Does anyone know of a way to solve this?
public class MainActivity extends AppCompatActivity implements NextControllerConnectionListener {
private static final int CONTROLLER_CONNECTION_DELAY = 5000;
public LightingDirector lightingDirector;
private ListView lv;
private ItemListView lampAdapter;
private ArrayList<Lamp> lamps = new ArrayList<Lamp>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.initViews();
// STEP 1: Initialize a lighting controller with default configuration.
LightingController lightingController = LightingController.get();
lightingController.init(new LightingControllerConfigurationBase(getApplicationContext().getFileStreamPath("").getAbsolutePath()));
lightingController.start();
// STEP 2: Instantiate the director, add the custom listener, then start.
lightingDirector = LightingDirector.get();
lightingDirector.setNetworkConnectionStatus(true);
lightingDirector.postOnNextControllerConnection(this, CONTROLLER_CONNECTION_DELAY);
lightingDirector.start("TutorialApp");
}
private void initViews() {
lv = (ListView) findViewById(R.id.list);
lampAdapter = new ItemListView(this, this.lamps);
lv.setAdapter(lampAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
goToPage2(position);
//send which one I have clicked on
}
});
}
private void goToPage2(int position) {
Intent dbIntent2 = new Intent(this, BulbSettingActivity.class);
dbIntent2.putExtra("position", position);
startActivity(dbIntent2);
}
@Override
public void onControllerConnected() {
Lamp[] lightingDirectorLamps = lightingDirector.getLamps();
if (lightingDirectorLamps.length > 0) {
// clear out whatever is in there
this.lamps.clear();
// add the lamps back
for (Lamp lamp : lightingDirectorLamps) {
this.lamps.add(lamp);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
lampAdapter.notifyDataSetChanged();
}
});
}
}
@Override
protected void onDestroy() {
lightingDirector.stop();
System.exit(0);
super.onDestroy();
}
}
On the LFS Tutorial App, I get that exception:
02-19 10:02:44.746 16855-16885/org.allseen.lsf.tutorial I/System.out: AllJoynManager.stop(): succeeded
02-19 10:02:44.771 16855-16885/org.allseen.lsf.tutorial I/System.out: AllJoynManager.destroy()
02-19 10:02:50.496 16855-16855/org.allseen.lsf.tutorial E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.allseen.lsf.tutorial, PID: 16855
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.allseen.lsf.tutorial/org.allseen.lsf.tutorial.TutorialActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.lang.Thread.interrupt()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3231)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3327)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1771)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7144)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:731)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:621)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.lang.Thread.interrupt()' on a null object reference
at org.allseen.lsf.sdk.manager.DefaultLightingSystemQueue.stop(DefaultLightingSystemQueue.java:116)
at org.allseen.lsf.sdk.manager.LightingSystemManager.setQueue(LightingSystemManager.java:231)
at org.allseen.lsf.sdk.manager.LightingSystemManager.init(LightingSystemManager.java:156)
at org.allseen.lsf.sdk.LightingDirector.start(LightingDirector.java:444)
at org.allseen.lsf.sdk.LightingDirector.start(LightingDirector.java:400)
at org.allseen.lsf.tutorial.TutorialActivity.onCreate(TutorialActivity.java:58)
at android.app.Activity.performCreate(Activity.java:6840)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3327)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1771)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7144)