Thursday, June 24, 2010

BlackBerry Bluetooth

I am developing an J2ME application on blackberry to enable a bluetooth connection between it and another bluetooth module.
My BB: TMobile 8320
OS: V4.5.0.81(Platform 2.7.0.78)

Now the problem is I have done the device discovery and service discovery successfully, and I have got the bluetooth module SPP service's URL successfully which is this:
btspp://000666019C30:1;authenticate=false;encrypt=false;master=false
where the URL is just the string above, i.e. bstp://...., the J2ME application idles around 20 seconds and then throw a IOException indicating the connection cannot make.
While there is an quite interesting phenomenon that during the 20 seconds, the bluetooth module's LED was SOLID GREEN which means it is in connection status. And also the blackberry's LED blinking in blue which indicates it is communicating through bluetooth. But after the exception threw out, everything back to before, just like nothing happened.

The problem was solved! I am giving the solution in case the same problem may bother other developers in the future.

1. Actually, my code does nothing wrong. The connector.open() method is pretty straightforward and identical on the standard J2ME platform. My application suddenly worked and connected to the Bluetooth module after I ran the Bluetooth sample application from blackberry. I checked its source code, and found before the normal process: device discovery, service discovery, and get connected, there is another important step, which should be the first stage, called stack initialization. In this step, we will initialize the SPP parameters like Baud rate, data format and so on. Here is the statement right from the sample:

this.myPort = new BluetoothSerialPort("SPP",  BluetoothSerialPort.BAUD_9600, BluetoothSerialPort.DATA_FORMAT_PARITY_NONE |                BluetoothSerialPort.DATA_FORMAT_STOP_BITS_1 | BluetoothSerialPort.DATA_FORMAT_DATA_BITS_8,               BluetoothSerialPort.FLOW_CONTROL_NONE, BUFFER_SIZE, BUFFER_SIZE, this );

You can put it in the constructor of your Midlet. One thing needs to be mentioned is to use this function, we have to purchase a code signing key from blackberry or the application will not be allowed to install.

2. If you find your application is frozen during the connection, that is because if certain class was ran in the main thread, the application will be locked, and StreamConnection is one, see here:

http://www.blackberry.com/developers/docs/4.2api/javax/microedition/io/Connector.html

So just put the connection part into another thread, there will not any freeze.

3. Here is another reason that may lead to the problem, though it is not the case for me, I still would like to append it here in case it may help you:

acowart:



  • Requesting the device's friendlyname (with the boolean set to true) causes a connection to bluetooth, and running service discovery immediately afterwards can fail, since the previous connection may not have disconnected yet.



  • There are no APIs to programmatically enable or disable Bluetooth.



    1 comment:

    1. can you please share me the the code as i am facing the same issue

      ReplyDelete