12-17-2011 10:20 AM - edited 12-17-2011 03:47 PM
Hiho,
I have a problem with the following Code:
public class MyApp extends UiApplication
{
public static void main(String[] args)
{
MyApp app = new MyApp();
app.enterEventDispatcher();
}
public MyApp()
{
if(System.getProperty("bluetooth.api.version") != null)
{
initializeMode();
}
else
{
Dialog.alert("JSR 82 Bluetooth APIs not present");
System.exit(0);
}
}
private void initializeMode()
{
System.out.println("INITIALIZE");
pushScreen(new ClientScreen());
}
}
public ClientScreen(int uuid)
{
super(Manager.NO_VERTICAL_SCROLL);
setTitle("...");
try {
_local = LocalDevice.getLocalDevice();
_discoveryAgent = _local.getDiscoveryAgent();
} catch (BluetoothStateException e) {
e.printStackTrace();
}
//startInquiry...
}
I start like in the BluetoothJSR82Demo with pushing a new Screen onto the stack. In the constructor I want to initiate a bluetooth device discovery.
Now the problem only appears when Bluetooth is disabled on the device at the beginning. So the getLocalDevice() command causes a Pop-Up that bluetooth has to be enabled. When the user chooses "yes" then it takes round about 1 minute to enable it. The debugger output is like this:
[66988.26] BT: HCI_INITIALIZED
[66988.26] BT: EIR set
[66996.805] VM:+GC(f)w=11
[66997.4] VM:-GCt=76,b=1,r=0,g=f,w=11
[66997.43] VM:QUOT t=4
[66997.43] VM:+CR
[66997.445] VM:-CR t=2
[67010.97] JVM: bklt @67010968: timer
[67010.97] JVM: bklt[1] @67010968: usrIdle 23, usrTime 30, usrAct 1
[67010.97] JVM: bklt[1] @67010968: chkIdle 29, currTime 30
[67010.97] JVM: bklt @67010968: setTimer 7
[67017.96] JVM: bklt @67017960: timer
[67017.96] JVM: bklt[1] @67017960: usrIdle 30, usrTime 30, usrAct 1
[67017.96] JVM: bklt[1] @67017960: chkIdle 36, currTime 30
[67017.96] JVM: bklt[1] @67017960: enableBacklight 0
[67017.96] JVM: bklt[1]: setTimeout 30
When I use
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run() {
pushScreen(new ClientScreen());
}
});
instead, the Pop-Up appears but the app crashes afterwards. So I can enable Bluetooth via the popup being in the homescreen.
I have to say I really have not understood when to use this invokeLater stuff so far.
Thanks in advance
njaa
12-21-2011 02:31 PM
You should not use code in a screen's constructor that triggers a dialog.
If you want to initiate discovery on startup, perform that operation in a new thread you create in the screen's constructor.
12-07-2012 01:28 PM
The sample does not work on BlackBerry 9900.
I modify the Github sample code, and then share to everyone.
https://sourceforge.net/projects/bluetoothjsr82d/
Cheers,