Waiting for an Event
I'm trying to implement a Bluetooth functionality class for a Blackberry. But I got stuck in connecting the device..
I made a simple MainScreen that connect, disconnect (for now). The bluetooth functionality is implmemented in another class that implmements BluetoothSerialPortListener.
When i select the "connect" option, it runs this code:
connect(BluetoothSerialPortInfo info){
port = new BluetoothSerialPort(info, BluetoothSerialPort.BAUD_57600,....,this);
When the connection protocol finishes, the function deviceConnected(boolean success)
is executed by the Blackberry, with a boolean
that has the result of the connection (true if it is connected, and false if not). I wanted to check for this boolean value before return from the connect(BluetoothSerialPortInfo info)
method, so i put a wait(1000)
in the it, and a notify()
in deviceConnected(boolean success)
.
The problem is that both functions or methods are executed by the same thread, and when the wait(1000)
is made, the timeout finishes, and then the deviceConnected function is executed...
I tried to run the connect method in a separate Thread, and it worked, but then i couldn't access to the MainScreen objects to notify the success or not of the connection (and even if i could, i would prefer not doing it).
I would love to know how to run the Listener methods in a separate Thread, so they could be executed even if the main thread is busy.
Thanks in advance.
(I hope i explaned myself...)
EDIT More explanation, in case i didn't explained it well:
The thing is, i do connect(info)
, and if i try to do for example Thread.sleep(10000), the thread will sleep 10 seconds, and after that time, the deviceConnected method is called, and then can i see the answer. Watever i try to do in the method that runs connect(info)
just postpone the execution of deviceConnected
. That is why i would run the Listener methods (deviceConnected
as example) in another Thread, so it can be executed while i wait in the method connect(info)
for the answer....
EDIT: CODE:
The connect method:
public int BT_ConnectDevice(BluetoothSerialPortInfo info)
{
if (info==null) return(0x4F);
try
{
_port = new BluetoothSerialPort(info, BluetoothSerialPort.BAUD_57600, BluetoothSerialPort.DATA_FORMAT_PARITY_NONE | BluetoothSerialPort.DATA_FORMAT_STOP_BITS_1 | BluetoothSerialPort.DATA_FORMAT_DATA_BITS_8, BluetoothSerialPort.FLOW_CONTROL_NONE, 1024, 1024, this);
return(0);
}
catch(Exception e)
{
return(0x3F);
}
}
The method e开发者_高级运维xecuted by the Listener:
public void deviceConnected(boolean success)
{
this._bDeviceIsConnected=success;
}
I tried to add in the connect method something like:
synchronized(lock){
try{
lock.wait(10000);
}
catch(Exception e){}
if (_bDeviceIsConnected) return (0);
}
return(0x3F);
And of course add the lock.notify()
in the deviceConnected. But as long as i wait, the deviceConnected is executed after the return from BT_ConnectDevice.
Might I suggest a re-imagining of your system design? Why do need the connect() method to be blocking?
My suggestion would be to redesign such that the code that calls connect() understands that it is a non-blocking call and then have your listener kick-off the necessary next step(s) once the connection is established.
You could try something like this pseudo-code:
public void fieldChanged(Field f, int c) {
if (f == myConnectButton) {
connect(...);
}
}
private class MyBluetoothConnectionListener ... {
public void deviceConnected(boolean success) {
if (success) {
bluetoothConnectionEstablished();
} else {
bluetoothConnectionFailed();
}
}
}
private void bluetoothConnectionEstablished() {
// TODO - put your logic here for what to do when the connection succeeds
}
private void bluetoothConnectionFailed() {
// TODO - put your logic here for what to do when the connection fails
}
could you not just run the connection in a while loop?
while(!success){
// try to connect
}
hopefully, it'll keep trying before you time-out.
with regards to the multithreading here's a link that'll show you a tutorial on how its done server side. Remember the implements runnable on the class and to run() each thread to start it.
hope this helps.
精彩评论