04-21-2010 11:49 AM
I am developing an J2ME application on blackberry to enable a bluetooth connection between it and another bluetooth module.
My BB: TMobile 8320
OS: V126.96.36.199(Platform 188.8.131.52)
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:
then, when I come to next step:
conn = (StreamConnection)Connector.open(URL,Connector.REA
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.
I have written a post before where I thought this issue might be related to the code signing. But after I signed my application, nothing changed. And I found the event log entry"no sig from 0x33" is actually not an signature issue.
I have found this post the most close to my problem, but the solution, setting false in getFriendlyName() to avoid pre-connection, does not work on my problem.
I hope anyone who has met this problem and got solution or just has valuable ideas could post here to help me as well as those who will face it and be frustrated in google searching.
Thank you all in advance!
04-21-2010 12:12 PM
A little supplement:
1. I have tested these URLs:
The first two throw IOException after around 20 seconds' pseudo-connection. The last two throw IOException immediately.
2. Here is my debug log:
d CC - Ret, "false" d CC - IsActive d CC - Ret, "false" d CC - IsActive d CC - Ret, "false" d CC - IsActive d CC - Ret, "false" d CC - IsActive a net.rim.bluetooth - DD16 i net.rim.link - Wait - idle i net.rim.link - Actn - lost i net.rim.link - Evnt - lost i net.rim.scan - Wait - idle a net.rim.link - NKSt a net.rim.scan - NKLt W net.rim.scan - CRAf i net.rim.scan - Actn - user actvty i net.rim.link - Wait - idle i net.rim.scan - Evnt - user actvty i net.rim.link - Actn - none i net.rim.link - Evnt - user d CC - Ret, "false" d CC IsActive a System - CMM:WirelessPaySys(506) a net.rim.bluetooth - SDC a net.rim.bluetooth - SDS a net.rim.bluetooth - SDPR a net.rim.bluetooth - SDS a net.rim.bluetooth - DC00 a System - CMM:WirelessPaySys(506) a System - CMM:WirelessPaySys(506) a System - CMM:WirelessPaySys(506) a System - CMM:WirelessPaySys(506) d CC - Ret, "false" d CC IsActive
WirelessPaySys is my appliation's name.
3. I have setup the BB's blackberry as this:
security level: High
Connection on Powerup: YES
Also, I have set the permission for my application:
User Data: ALLOW
I have turned off the BB's firewall.
After that, I have set the device properties of my bluetooth module:
(The services listed in "Device Properties" page is a bunch of repeated "SPP" for my bluetooth module)
4. My bluetooth module should work fine. Because I have connected it with my labtop, and successfully transfer data through the connection.(I use hyperterminal on laptop to receive the data transmitted from the module)
04-30-2010 02:29 PM - edited 04-30-2010 02:57 PM
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,
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:
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:
03-04-2011 05:22 AM
from your code :
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 );
i want to ask,
what "myPort" is use for in j2me bluetooth connection?
is it always called by j2me bluetooth code?
and after that, we should close() or disconnect() right?
04-26-2011 02:23 PM
On a slightly non-related topic; Could anyone tell me how I would be able to tell when a bluetooth connection is lost? like is there a connection status instruction in the bluetooth API? I am creating a bluetooth based proximity sensor, and would therefore like to trigger an audio alert when the devices go out of range from each other (and hence lose the connection)
Thanks in advance
05-04-2011 12:43 PM
I am actually trying to acheive something similar.
What i need to do is to have an app monitor a specific blutooth connection. I need the app to start running some code as soon as that specific bluthoot connection is made and i need it to run some other code when the connection is lost. That is the only BT interaction my app would have with that BT connection. No transmitting at all.
Have you found a solution to your problem?
05-08-2011 10:19 AM
I still have not been able to find an instruction in the API which is able to deternine whether a connection is open or not, though I have created a hack which temporarily solves the problem. After the connection is setup, The client or the server (depending on which end needs to know when the connection is lost), begins transmitting data over the connection. In the transmission code, you will have to use a try catch(IO exception) statement. In the IO exception handler you include a call to the function which you need to run when the connection is lost.
Here is a copy of the transmission routine:
I know this probably not the best way of solving this problem, but it works If you do find a more elegant solution, please let me know
07-13-2011 09:12 PM
my 2 pence here.
I tried the new BluetoothSerialPort as pointed before as a BT stack init but still didn't do it, it would still bail out when I hit the
ClientSession cs = (ClientSession)Connector.open(_btConnectionURL
The discovery agent and stuff I had it on a popup and would return the Address of the chosen BT dev that one work
without a hitch following the JSR--82.
I read a bit more the JSR-82 and since the issue seemed to be the BT stack was not init'ed properly here I just
tried the basic
localDev = LocalDevice.getLocalDevice();
and then the call to the connector.open doesnot fail no more ......Allelluya my code flows after that line .... this had me hitting my head to the wall real hard,so that one did it for me ... FYI
07-13-2011 10:17 PM
I am still refreshing my java knowledge and learning the bluetooth stuff, but is what you want possibly to imlement a bluetooth listener? That *might* let you detect when you're in range and out of range... not sure at all though. I'm not sure that BBs automatically connect to known devices when back in range?