01-17-2012 06:23 PM
I've been fighting with bluetooth for a week now with limited success. I'm trying to write an app to talk to a bluetooth character device that responds to ascii AT commands much like a modem (though not a modem).
I have tried a slight modification to the BluetoothSample app. Sometimes it works. Sometimes it doesn't. Very frustrating. No ammount of rebooting the phone will make it predictable. (9800 OS6.0).
I do have a 3rd party app that talks to my BT device just fine and consistently. So I know it's not a hardware problem.
I've read all the docs and exhausted the forum posts but I think I am still missing the big picture. Can someone share a usage pattern on how it's supposed to work? I think part of my problem has to do with which thread is communicating.
Here is my latest broken code. As usual... it bombs on the Connector.open.
Any help will be appreciated...
package mypackage; import java.io.DataOutputStream; import java.io.DataInputStream; import java.io.IOException; import javax.microedition.io.Connector; import javax.microedition.io.StreamConnection; import net.rim.device.api.bluetooth.BluetoothSerialPort; import net.rim.device.api.bluetooth.BluetoothSerialPortInfo; import net.rim.device.api.ui.component.LabelField; import net.rim.device.api.ui.container.MainScreen; public final class MyScreen extends MainScreen { private BluetoothIO btcon; public MyScreen(){ setTitle("BT Test 2"); btcon = new BluetoothIO(); btcon.start(); } public void onDisplay(){ add(new LabelField("Connected: "+btcon.isConnected())); btcon.open(); add(new LabelField("Connected: "+btcon.isConnected())); btcon.close(); add(new LabelField("Connected: "+btcon.isConnected())); } public class BluetoothIO extends Thread{ private StreamConnection _btCon; private DataOutputStream _dout; private DataInputStream _din; private boolean connected = false; public BluetoothIO(){} public boolean isConnected(){ return connected; } public int receive(){ int contents = 0; try { contents = _din.readInt(); } catch (IOException e) { e.printStackTrace(); } return contents; }//receive public void send(int i){ try { _dout.writeInt(13); } catch (IOException e) { e.printStackTrace(); } }//send public void close(){ connected = false; if (_btCon != null) { try { _btCon.close(); } catch(IOException ioe) { } } if (_din != null) { try { _din.close(); } catch(IOException ioe) { } } if (_dout != null) { try { _dout.close(); } catch(IOException ioe) { } } _btCon = null; _din = null; _dout = null; }//close public void open(){ BluetoothSerialPortInfo[] _portInfo = BluetoothSerialPort.getSerialPortInfo(); try { _btCon = (StreamConnection)Connector.open( _portInfo[0].toString(), Connector.READ_WRITE ); _dout = _btCon.openDataOutputStream(); _din = _btCon.openDataInputStream(); connected = true; } catch (IOException e) { e.printStackTrace(); } }//open }//BluetoothIO }
01-17-2012 10:31 PM
01-17-2012 11:40 PM
I love this forum. I got it running just as your response arrived...
I did give up and went back to the all blackberry approach.
I think the crutucal factor was creating a thread (and how it was created) and doing all communicaitons on a separate thread...
I had to do it with:
public class BluetoothClient implements Runnable, BluetoothSerialPortListener{
public void run() {
//open here
}
// read and write in here too - based on the RIM bluetooth sample
}
and
btcon = new BluetoothClient();
new Thread(btcon).start();
For some reason coding it as extending Thread implementing BluetoothSerialPortListener prevented the listerer from working. I still don;t understand why. But the Runnable in a Thread version works...
Thanks again
Peter