Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 10
Registered: ‎04-16-2010
My Device: Blackberry 8320
My Carrier: Tmobile

Connector.open() cannot get through for Bluetooth connection

Hi ALL,

 

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

 

then, when I come to next step:

 

conn = (StreamConnection)Connector.open(URL,Connector.READ_WRITE);

 

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.

 

Here are 3 posts with the same problem but never solved: one, two ,and three.

 

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!

Contributor
Posts: 10
Registered: ‎04-16-2010
My Device: Blackberry 8320
My Carrier: Tmobile

Re: Connector.open() cannot get through for Bluetooth connection

A little supplement:

 

1. I have tested these URLs:

 

btspp://000666019C30:1;authenticate=false;encrypt=false;master=false

btspp://000666019C30:1

btspp://000666019C30:0

btspp://000666019C30

 

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:

 

discoverable: YES

security level: High

Connection on Powerup: YES

 

Also, I have set the permission for my application:

 

Connection:  ALLOW

Interactions: ALLOW

User Data: ALLOW

 

I have turned off the BB's firewall.

 

After that, I have set the device properties of my bluetooth module:

 

Truested: Yes

Encryption: Disable

 

(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)

Contributor
Posts: 10
Registered: ‎04-16-2010
My Device: Blackberry 8320
My Carrier: Tmobile

Re: Connector.open() cannot get through for Bluetooth connection

Any idea on this tricky problem?  It is more than welcomed!

Contributor
Posts: 10
Registered: ‎04-16-2010
My Device: Blackberry 8320
My Carrier: Tmobile

Re: Connector.open() cannot get through for Bluetooth connection

[ Edited ]

Hi all,

 

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.
  •  

     

    Regards,

    Josh

     

    Contributor
    Posts: 43
    Registered: ‎05-21-2010
    My Device: curve 8520
    My Carrier: staff

    Re: Connector.open() cannot get through for Bluetooth connection

    Dear Josh,

     

    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?

     

    thank u.

    Highlighted
    New Contributor
    Posts: 2
    Registered: ‎04-26-2011
    My Device: Blackberry 9800 torch
    My Carrier: MTN

    Re: Connector.open() cannot get through for Bluetooth connection

    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)

     

    Please advise

    Thanks in advance

    New Member
    Posts: 1
    Registered: ‎05-04-2011
    My Device: curve 8330
    My Carrier: Bell

    Re: Connector.open() cannot get through for Bluetooth connection

    Trishaulr,

     

    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?

     

    Thanks

     

    Fredbb.

    New Contributor
    Posts: 2
    Registered: ‎04-26-2011
    My Device: Blackberry 9800 torch
    My Carrier: MTN

    Re: Connector.open() cannot get through for Bluetooth connection

    Fredbb

     

    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:

     

     

    public void SendMessages(String v_strData)
      {
                try
                {
                    m_Output.write(v_strData.length());
                    m_Output.write(v_strData.getBytes());
                    
                }
                catch (IOException ex) 
                {
                    ConnectionLostRoutine();    //function which runs when connection is lost
      CloseAll(); //connection must then be closed so that it may be re-opened at a later stage
                    InitServer(); //server or client must then be re-initialized as the connections have been closed
                }
                       
          } 

     

     

    I know this probably not the best way of solving this problem, but it works Smiley Happy If you do find a more elegant solution, please let me know

    New Developer
    Posts: 169
    Registered: ‎04-12-2010
    My Device: Storm
    My Carrier: Verizon

    Re: Connector.open() cannot get through for Bluetooth connection

      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

     

          try
            {
                    localDev = LocalDevice.getLocalDevice();
            }
            catch(....

     

     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

     

       Cheers

     

     

     

    New Contributor
    Posts: 7
    Registered: ‎07-11-2011
    My Device: Blackberry Tour 9630
    My Carrier: Verizon

    Re: Connector.open() cannot get through for Bluetooth connection

    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?