04-28-2012 01:34 PM - edited 04-28-2012 11:22 PM
Hey all, trying to get a thread to start on a button click, causing a "searching" popupscreen to show, then when the bluetooth inquirycompleted method is called, it pops the searching popupscreen as shown:
the button that triggers it:
engage.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
active = true;
repeat = false;
UiApplication.getUiApplication().pushScreen(progre ssing);
new RefreshThread().start();
}
});
add(engage);
WaitDone.setText("on entry");
add(WaitDone);
This is the run method of the thread:
public void run() {
while (active == true) {
if (repeat == false) {
active = false;
}
try {
waiting = "waiting";
remoteDevices.removeAllElements();
devicesList.removeAllElements();
discoveryAgent.startInquiry(DiscoveryAgent.GIAC, Settings);
} catch (Exception e) {
e.printStackTrace();
}
if (repeat == true) {
try {
Thread.sleep(20000); // how long to wait before
// searching
// again.
} catch (InterruptedException e) {
}
}
}
the inquiry completed method:
public void inquiryCompleted(int discType) {
monitor = monitor + 10;
waiting = "done";
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
if (repeat == false)
UiApplication.getUiApplication().popScreen(progres sing);
// WaitDone.setText(waiting);
}
});
}
It is supposed to work so that when you click the button, repeat equals false, so that when the thread's run method is called, it only goes through once because afterwards since repeat=false, active=false and the while loop will end. This works and it pops the popup perfectly, the issue is when I click the button again, the popup appears but it is never popped as it should be by the inquirycompleted method. I am still working on my understanding of threading and if anyone could show me my errors that would be terrific.
04-28-2012 05:08 PM
I've put in a cancel button to pop the popup if it is taking too long and have noticed that when I cancel it after having taken too long, the variable waiting=waiting, therefore it is getting hung up following waiting, on the discoveryAgent.startInquiry(DiscoveryAgent.GIA