J2ME Bluetooth - more then one host prolem
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   mat_novak
Posted On:   Tuesday, June 3, 2008 02:18 AM

Hello, I have not so simple question concerning J2ME and Bluetooth in terms of examples that can be found on the net and situation with several hosts and one client. Most of examples that I was able to find on the net works fine on simulator (I am using NetBeans 6.1) but not on the real device. I think the reason is simple - in most cases in "servicesDiscovered()" method I have an entry like shown below (I am posting only the most relevant parts of the code): public void servicesDiscovered(int transID, ServiceRecord[] records) { for (int i = 0; i < records.length; i++) { m_strUrl = records[i].getConnectionURL(ServiceRecord.AUTHENTICATE_ENCRYPT, false);    More>>

Hello,

I have not so simple question concerning J2ME and Bluetooth in terms of examples that can be found on the net and situation with several hosts and one client.

Most of examples that I was able to find on the net works fine on simulator (I am using NetBeans 6.1) but not on the real device. I think the reason is simple - in most cases in "servicesDiscovered()" method I have an entry like shown below (I am posting only the most relevant parts of the code):



public void servicesDiscovered(int transID, ServiceRecord[] records) {



for (int i = 0; i < records.length; i++) {

m_strUrl = records[i].getConnectionURL(ServiceRecord.AUTHENTICATE_ENCRYPT, false);



if (m_strUrl.startsWith("btspp")) //we have found our service protocol

{

m_bServerFound = true;

break;

}

}

}



and then in serviceSearchCompleted() method we are trying to establish a connection like this:



public void serviceSearchCompleted(int arg0, int arg1) {

if (m_bServerFound) {

try { //lets the communication start by setting the url and send client reponse



m_StrmConn = (StreamConnection) Connector.open(m_strUrl);



m_Output = m_StrmConn.openOutputStream();

m_Input = m_StrmConn.openInputStream();



m_Output.write(CLIENT_RESPONSE.length());

m_Output.write(CLIENT_RESPONSE.getBytes());



System.out.println("serviceSearchCompleted");

} catch (IOException ex) {

ex.printStackTrace();

}

}

}







Like I said before, such a solution works fine on simulator, but not on real life. On real life during device/service discovery, a number of them can be found. And proposed servicesDiscovered() method takes the first one that satisfies the protocol requirenment. Of course I could discover a services by UUID but that won't resolve my problem neither, because I can imagine that there will be several apps with the same UUID started on more then one mobile.



Let's imagine the follwing situation - I have a J2ME Bluetooth chat application and three mobiles - mA, mB and mC.

On mA and mB I start my chat midlet and I set it to work as hosts.

On mC I start my chat midlet and set it to work as a client (I want them to connect to a host).And I would like my client's part of a chat midlet to tell me that two hosts were found (one on mA and one on mB) - it can be shown as choiceList or something similar. And then, after choosing the proper host, the connection will be established.



Or maybe simpler use case - a J2ME Bluetooth Echo application.

Once again - I set up mA and mB to work as hosts and then I want mC to connect to one of the hosts. And I have to be able to differentiate mA host and mB host.



How should I do this?



I'm not sure if I managed to describe my problem clearly.



Any fragment of source code would be more then welcome.

   <<Less
About | Sitemap | Contact