Java Bluetooth Chat - help on detection please

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2008
Posts: 99
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 5
Clawsy Clawsy is offline Offline
Junior Poster in Training

Java Bluetooth Chat - help on detection please

 
0
  #1
Oct 22nd, 2009
Hello,
I'm working on a bluetooth chat application project called 'BlueChat'. I coded the server (BServer.java) and client (BClient.java) parts (2 separate threads). The visual part is called BlueChat.java and allowes the user to select to be the server or the client.

I wrote a lot of System.out.println("")'s to check WHY NO MESSAGE IS SEND (there are no errors or exceptions). So I detect that:
- On the server, when it reaches
  1. con = notifier.acceptAndOpen();
part, loopes infinitely as it doesn't receive any join from a 'Client'
- On the client when it passes the 'inquiry' but it's waiting infinitely on the service detection:
  1. serviceL.wait();
as it can never find a remote host device (the server)


Here are the classes:
The 'visual' class
  1. public class BlueChat extends MIDlet implements CommandListener{
  2.  
  3.  
  4. /*******************************************************************************/
  5.  
  6.  
  7. private final Command EXIT_CMD = new Command("Exit", Command.EXIT, 2);
  8.  
  9.  
  10.  
  11.  
  12. Command RECEIVE_CMD = new Command("Receive", Command.ITEM, 2);
  13. Command SEND_CMD = new Command("Send", Command.ITEM, 2);
  14.  
  15.  
  16.  
  17. /** value is true after creating the server/client */
  18. private boolean isInit = false;
  19.  
  20. /*******************************************************************************/
  21.  
  22.  
  23.  
  24. Display display;
  25. Form mainForm;
  26. boolean firstTime = true;
  27.  
  28. StringItem status;
  29.  
  30.  
  31. //Bluetooth members
  32.  
  33.  
  34. LocalDevice local = null;
  35. DiscoveryAgent discoveryAgent = null;
  36. ServiceRecord SR = null;
  37.  
  38. L2CAPConnectionNotifier notifier = null;
  39. L2CAPConnection con = null;
  40. String service_UUID = null;
  41.  
  42. InputStream input = null;
  43. OutputStream output = null;
  44.  
  45.  
  46.  
  47. String deviceName="",deviceAddress="";
  48. private boolean listening=true;
  49. private StringItem msg;
  50.  
  51.  
  52. //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">
  53. //</editor-fold>
  54.  
  55. /**
  56.   * The BlueChat constructor.
  57.   */
  58. public BlueChat() {
  59.  
  60. }
  61.  
  62.  
  63. protected void setAlert(String info) {
  64. Alert a = new Alert("INFO");
  65. a.setString(info);
  66. a.setTimeout(Alert.FOREVER);
  67. display.setCurrent(a);
  68. }
  69.  
  70. ...
  71. public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
  72. // write pre-switch user code here
  73. Display display = getDisplay();
  74. if (alert == null) {
  75. display.setCurrent(nextDisplayable);
  76. } else {
  77. display.setCurrent(alert, nextDisplayable);
  78. }
  79. // write post-switch user code here
  80. }
  81. //</editor-fold>
  82.  
  83. /**
  84.   * Returns a display instance.
  85.   * @return the display instance.
  86.   */
  87. public Display getDisplay () {
  88. return Display.getDisplay(this);
  89. }
  90.  
  91. /**
  92.   * Exits MIDlet.
  93.   */
  94. public void exitMIDlet() {
  95. switchDisplayable (null, null);
  96. destroyApp(true);
  97. notifyDestroyed();
  98. }
  99.  
  100. /**
  101.   * Called when MIDlet is started.
  102.   * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
  103.   */
  104. public void startApp() {
  105.  
  106. firstTime = true;
  107. mainForm = new Form("BlueChat");
  108.  
  109. if(firstTime)
  110. {
  111. display = Display.getDisplay(this);
  112. // vizual
  113. status = new StringItem("Status:", "...");
  114. mainForm.append(status);
  115. msg = new StringItem("Message:","...");
  116. mainForm.append(msg);
  117.  
  118. //comenzi
  119. mainForm.addCommand(EXIT_CMD);
  120. mainForm.addCommand(SEND_CMD);
  121. mainForm.addCommand(RECEIVE_CMD);
  122.  
  123.  
  124. mainForm.setCommandListener(this);
  125.  
  126. /////////////////
  127. firstTime = false;
  128. }
  129.  
  130. display.setCurrent(mainForm);
  131. /* try {
  132.   connect();
  133.   } catch (IOException ex) {
  134.   ex.printStackTrace();
  135.   }*/
  136. }
  137.  
  138. public void commandAction(Command c, Displayable d)
  139. {
  140. if(c == EXIT_CMD)
  141. {
  142. this.destroyApp(true);
  143. this.notifyDestroyed();
  144.  
  145. }
  146. else if(c == RECEIVE_CMD)
  147. {
  148. new BServer(this);
  149.  
  150. }
  151. else if(c == SEND_CMD)
  152. {
  153. new BClient(this);
  154. }
  155.  
  156.  
  157.  
  158. }
  159.  
  160. /**
  161.   * Called when MIDlet is paused.
  162.   */
  163. public void pauseApp() {
  164. midletPaused = true;
  165. }
  166.  
  167. /**
  168.   * Called to signal the MIDlet to terminate.
  169.   * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
  170.   */
  171. public void destroyApp(boolean unconditional) {
  172. }
  173.  
  174.  
  175.  
  176. }

The SERVER class
  1. public class BServer implements Runnable{
  2. private LocalDevice local;
  3. private DiscoveryAgent discoveryAgent;
  4. private String deviceName;
  5. private String deviceAddress;
  6. // final private StringItem status,msg;
  7. private String service_UUID="00000000000010008000006057028A06";
  8. private L2CAPConnectionNotifier notifier;
  9. private boolean listening;
  10. private L2CAPConnection con;
  11.  
  12. private BlueChat chat;
  13.  
  14. BServer(BlueChat chat)
  15. {
  16.  
  17. this.chat = chat;
  18.  
  19. Thread t = new Thread(this);
  20. t.start();
  21. }
  22.  
  23. public void run() {
  24. try {
  25. System.out.println("Server thread started");
  26. local = LocalDevice.getLocalDevice();//initializare pt gazda
  27. local.setDiscoverable(DiscoveryAgent.LIAC);
  28. discoveryAgent = local.getDiscoveryAgent();
  29. deviceName = local.getFriendlyName();
  30. deviceAddress = local.getBluetoothAddress();
  31.  
  32. /* status.setText(deviceName+"\nAddress="+deviceAddress+
  33.   "\nDiscovery agent="+discoveryAgent.toString());
  34. */
  35.  
  36.  
  37.  
  38. String url = "btl2cap://localhost:"+service_UUID+";name="+deviceName;
  39.  
  40. notifier = (L2CAPConnectionNotifier) Connector.open(url);
  41. // con = (L2CAPConnection)Connector.open(url);
  42.  
  43. System.out.println("advertising the connection");
  44. /**************************************************************
  45.   WAITING INFINETELY HERE
  46. *******************************************************/
  47. con = notifier.acceptAndOpen();///////////////////////////////
  48. System.out.println("advertising finished");
  49. while(listening){
  50.  
  51. if(con.ready()){
  52. System.out.println("server READY to receive data");
  53. // primim in bytes data de la client apoi o trecem intr-un String
  54. byte[] buff = new byte[1000];
  55. con.receive(buff);
  56.  
  57. String s = new String(buff,0, buff.length);
  58. // msg.setText("a");
  59. //daca am primit mesaj alertam interfata vizuala
  60. System.out.println("Recieved from client: " + s.trim());
  61. chat.setAlert(s.trim());
  62.  
  63. //asa trimitem
  64. sendMessage("Hello client, my name is: " + local.getFriendlyName());
  65.  
  66. listening = false;
  67. }
  68. }
  69.  
  70.  
  71. } catch (IOException ex) {
  72. System.out.println("Eroare la SERVER\n");
  73. ex.printStackTrace();
  74. }
  75.  
  76.  
  77.  
  78. }
  79.  
  80.  
  81. private void sendMessage(String msg)
  82. {
  83. byte[] buff = msg.getBytes();
  84. try {
  85. con.send(buff);
  86. } catch(IOException e){
  87. System.out.println(e);
  88. }
  89. }
  90.  
  91. }

The client class
  1. public class BClient implements Runnable{
  2.  
  3. String s;
  4. private LocalDevice local;
  5. private DiscoveryAgent discoveryAgent;
  6. private String deviceName;
  7. private String deviceAddress;
  8.  
  9. private String service_UUID="00000000000010008000006057028A06";
  10. private L2CAPConnectionNotifier notifier;
  11. private L2CAPConnection con;
  12.  
  13. private BlueChat chat;
  14. private InquiryListener inquiryL;
  15. private ServiceListener serviceL;
  16. private boolean listening=true;
  17.  
  18. BClient(BlueChat chat)
  19. {
  20. this.chat = chat;
  21.  
  22. Thread t = new Thread(this);
  23. t.start();
  24. }
  25.  
  26. public void run() {
  27. try{
  28. byte[] buff = "SAALUT".getBytes();
  29.  
  30. System.out.println("Client thread started");
  31. //date dispozitiv
  32. local = LocalDevice.getLocalDevice();//initializare pt gazda
  33. local.setDiscoverable(DiscoveryAgent.LIAC);
  34. discoveryAgent = local.getDiscoveryAgent();
  35. deviceName = local.getFriendlyName();
  36. deviceAddress = local.getBluetoothAddress();
  37.  
  38. //cautare dispozitive bluetooth apoi serviciile fiecaruia
  39. inquiryL = new InquiryListener();
  40.  
  41. synchronized(inquiryL){
  42. System.out.println("inainte de cautare inquiry");
  43. discoveryAgent.startInquiry(DiscoveryAgent.LIAC, inquiryL);
  44. try{
  45. System.out.println("in asteptare");
  46. inquiryL.wait();
  47. }catch(InterruptedException e){ }
  48.  
  49.  
  50. System.out.println("asteptare inquiry terminata");
  51. }
  52.  
  53. //dispozitive gasite - in obiectul de cautare de mai sus
  54. Enumeration devices = inquiryL.cached_devices.elements();
  55.  
  56. UUID[] u = new UUID[]{ new UUID("00000000000010008000006057028A06", false) };
  57. int attributes[]= { 0x0100 };
  58.  
  59. //acum serviciile aferente FIECARUI ELEMENT... deic traversam vectorul de elemente
  60. serviceL = new ServiceListener();
  61. while(devices.hasMoreElements()){
  62. synchronized(serviceL){
  63. System.out.println("inainte de cautare servicii");
  64. discoveryAgent.searchServices(attributes, u, (RemoteDevice)devices.nextElement(), inquiryL);
  65. try{
  66. System.out.println("in asteptare");
  67. /**************************************************************
  68.   WAITING INFINETELY HERE
  69. *******************************************************/
  70. serviceL.wait();///////////
  71. }catch(InterruptedException e){}
  72.  
  73. System.out.println("asteptare servicii terminata");
  74. }
  75.  
  76.  
  77. if (serviceL.service!=null){
  78. try {
  79. String url;
  80. url = serviceL.service.getConnectionURL(0, false);
  81. deviceName = LocalDevice.getLocalDevice().getFriendlyName();
  82. con = (L2CAPConnection) Connector.open( url );
  83. sendMessage("Hello server, my name is: " + deviceName);
  84.  
  85. byte[] b = new byte[1000];
  86.  
  87. System.out.println("IN SERVICIU");
  88.  
  89. while (listening) {
  90. if (con.ready()){
  91. System.out.println("GATA DE PRIMIRE");
  92.  
  93. con.receive(b);
  94. String str = new String(b, 0, b.length);
  95. System.out.println("Received from server: " + str.trim());
  96. chat.setAlert(str.trim());
  97. listening = false;
  98. }
  99. }
  100. } catch (IOException g) {System.out.println(g);}
  101. }
  102.  
  103.  
  104. }
  105.  
  106.  
  107. } catch (IOException ex) {
  108. System.out.println("Eroare la CLIENT\n");
  109. ex.printStackTrace();
  110. }
  111. }
  112.  
  113.  
  114.  
  115.  
  116. /********* TRIMITE *****************/
  117.  
  118. private void sendMessage(String msg)
  119. {
  120. byte[] buff = msg.getBytes();
  121. try {
  122. con.send(buff);
  123. } catch(IOException e){
  124. System.out.println(e);
  125. }
  126. }
  127.  
  128.  
  129.  
  130. /*********** CLASA PENTRU CAUTARE DISPOZITIVE*******/
  131. class InquiryListener implements DiscoveryListener{
  132.  
  133. public Vector cached_devices;
  134.  
  135. InquiryListener(){
  136. cached_devices = new Vector();
  137. }
  138.  
  139. public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
  140. if( ! cached_devices.contains( btDevice ) ) {
  141. cached_devices.addElement( btDevice );
  142. }
  143. }
  144.  
  145. public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
  146. }
  147.  
  148. public void serviceSearchCompleted(int transID, int respCode) {
  149. }
  150.  
  151. public void inquiryCompleted(int discType) {
  152. synchronized(this){ this.notify();System.out.println("inquiry terminata"); }
  153. }
  154.  
  155. }
  156.  
  157.  
  158.  
  159. /*********** CLASA PENTRU CAUTARE SERVICII ale dispozitivelor*******/
  160. class ServiceListener implements DiscoveryListener{
  161.  
  162. public ServiceRecord service;
  163.  
  164. public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
  165. service = servRecord[0];
  166. System.out.println("foundService");
  167. }
  168.  
  169. public void serviceSearchCompleted(int transID, int respCode) {
  170. synchronized( this ){ this.notify();}
  171. }
  172.  
  173.  
  174. public void inquiryCompleted(int discType) {
  175. }
  176.  
  177. public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
  178. }
  179.  
  180. }
  181.  
  182.  
  183. }

So please help me on detection between devices if you can. I just don't know why the don't detect each other. I use NetBeans with Mobile Simulator and I even tested it on two real mobile phones - nothing happened
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,208
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer
 
-1
  #2
Oct 22nd, 2009
Sorry, I cannot help right know (little busy at the moment), but you can have look at Beginning J2ME: From Novice to Professional Chapter 12 that should help with device discovery
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 99
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 5
Clawsy Clawsy is offline Offline
Junior Poster in Training
 
0
  #3
Oct 22nd, 2009
Nice book, I read something.... it's just it uses other type of connection and things that doesn't match my case (L2CAPConnection is more flexible than StreamConnection).
However thanks for interest and I will wait your and others help... while continously trying to find 'the bug'....
Last edited by Clawsy; Oct 22nd, 2009 at 4:09 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: susman is an unknown quantity at this point 
Solved Threads: 1
susman susman is offline Offline
Newbie Poster

I am trying to develop the same aplication

 
0
  #4
10 Days Ago
Hello,
I'm a novice in J2ME but i started developing a bluetooth chat aplication. I managed to find some samples of btooth chat aplications but i'm not satisfied with it. Can you provide some help(i mean source code )? if i can help you with your problem i'll send you a solution.
Thank you
Originally Posted by Clawsy View Post
Hello,
I'm working on a bluetooth chat application project called 'BlueChat'. I coded the server (BServer.java) and client (BClient.java) parts (2 separate threads). The visual part is called BlueChat.java and allowes the user to select to be the server or the client.

I wrote a lot of System.out.println("")'s to check WHY NO MESSAGE IS SEND (there are no errors or exceptions). So I detect that:
- On the server, when it reaches
  1. con = notifier.acceptAndOpen();
part, loopes infinitely as it doesn't receive any join from a 'Client'
- On the client when it passes the 'inquiry' but it's waiting infinitely on the service detection:
  1. serviceL.wait();
as it can never find a remote host device (the server)


Here are the classes:
The 'visual' class
  1. public class BlueChat extends MIDlet implements CommandListener{
  2.  
  3.  
  4. /*******************************************************************************/
  5.  
  6.  
  7. private final Command EXIT_CMD = new Command("Exit", Command.EXIT, 2);
  8.  
  9.  
  10.  
  11.  
  12. Command RECEIVE_CMD = new Command("Receive", Command.ITEM, 2);
  13. Command SEND_CMD = new Command("Send", Command.ITEM, 2);
  14.  
  15.  
  16.  
  17. /** value is true after creating the server/client */
  18. private boolean isInit = false;
  19.  
  20. /*******************************************************************************/
  21.  
  22.  
  23.  
  24. Display display;
  25. Form mainForm;
  26. boolean firstTime = true;
  27.  
  28. StringItem status;
  29.  
  30.  
  31. //Bluetooth members
  32.  
  33.  
  34. LocalDevice local = null;
  35. DiscoveryAgent discoveryAgent = null;
  36. ServiceRecord SR = null;
  37.  
  38. L2CAPConnectionNotifier notifier = null;
  39. L2CAPConnection con = null;
  40. String service_UUID = null;
  41.  
  42. InputStream input = null;
  43. OutputStream output = null;
  44.  
  45.  
  46.  
  47. String deviceName="",deviceAddress="";
  48. private boolean listening=true;
  49. private StringItem msg;
  50.  
  51.  
  52. //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">
  53. //</editor-fold>
  54.  
  55. /**
  56.   * The BlueChat constructor.
  57.   */
  58. public BlueChat() {
  59.  
  60. }
  61.  
  62.  
  63. protected void setAlert(String info) {
  64. Alert a = new Alert("INFO");
  65. a.setString(info);
  66. a.setTimeout(Alert.FOREVER);
  67. display.setCurrent(a);
  68. }
  69.  
  70. ...
  71. public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
  72. // write pre-switch user code here
  73. Display display = getDisplay();
  74. if (alert == null) {
  75. display.setCurrent(nextDisplayable);
  76. } else {
  77. display.setCurrent(alert, nextDisplayable);
  78. }
  79. // write post-switch user code here
  80. }
  81. //</editor-fold>
  82.  
  83. /**
  84.   * Returns a display instance.
  85.   * @return the display instance.
  86.   */
  87. public Display getDisplay () {
  88. return Display.getDisplay(this);
  89. }
  90.  
  91. /**
  92.   * Exits MIDlet.
  93.   */
  94. public void exitMIDlet() {
  95. switchDisplayable (null, null);
  96. destroyApp(true);
  97. notifyDestroyed();
  98. }
  99.  
  100. /**
  101.   * Called when MIDlet is started.
  102.   * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
  103.   */
  104. public void startApp() {
  105.  
  106. firstTime = true;
  107. mainForm = new Form("BlueChat");
  108.  
  109. if(firstTime)
  110. {
  111. display = Display.getDisplay(this);
  112. // vizual
  113. status = new StringItem("Status:", "...");
  114. mainForm.append(status);
  115. msg = new StringItem("Message:","...");
  116. mainForm.append(msg);
  117.  
  118. //comenzi
  119. mainForm.addCommand(EXIT_CMD);
  120. mainForm.addCommand(SEND_CMD);
  121. mainForm.addCommand(RECEIVE_CMD);
  122.  
  123.  
  124. mainForm.setCommandListener(this);
  125.  
  126. /////////////////
  127. firstTime = false;
  128. }
  129.  
  130. display.setCurrent(mainForm);
  131. /* try {
  132.   connect();
  133.   } catch (IOException ex) {
  134.   ex.printStackTrace();
  135.   }*/
  136. }
  137.  
  138. public void commandAction(Command c, Displayable d)
  139. {
  140. if(c == EXIT_CMD)
  141. {
  142. this.destroyApp(true);
  143. this.notifyDestroyed();
  144.  
  145. }
  146. else if(c == RECEIVE_CMD)
  147. {
  148. new BServer(this);
  149.  
  150. }
  151. else if(c == SEND_CMD)
  152. {
  153. new BClient(this);
  154. }
  155.  
  156.  
  157.  
  158. }
  159.  
  160. /**
  161.   * Called when MIDlet is paused.
  162.   */
  163. public void pauseApp() {
  164. midletPaused = true;
  165. }
  166.  
  167. /**
  168.   * Called to signal the MIDlet to terminate.
  169.   * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
  170.   */
  171. public void destroyApp(boolean unconditional) {
  172. }
  173.  
  174.  
  175.  
  176. }

The SERVER class
  1. public class BServer implements Runnable{
  2. private LocalDevice local;
  3. private DiscoveryAgent discoveryAgent;
  4. private String deviceName;
  5. private String deviceAddress;
  6. // final private StringItem status,msg;
  7. private String service_UUID="00000000000010008000006057028A06";
  8. private L2CAPConnectionNotifier notifier;
  9. private boolean listening;
  10. private L2CAPConnection con;
  11.  
  12. private BlueChat chat;
  13.  
  14. BServer(BlueChat chat)
  15. {
  16.  
  17. this.chat = chat;
  18.  
  19. Thread t = new Thread(this);
  20. t.start();
  21. }
  22.  
  23. public void run() {
  24. try {
  25. System.out.println("Server thread started");
  26. local = LocalDevice.getLocalDevice();//initializare pt gazda
  27. local.setDiscoverable(DiscoveryAgent.LIAC);
  28. discoveryAgent = local.getDiscoveryAgent();
  29. deviceName = local.getFriendlyName();
  30. deviceAddress = local.getBluetoothAddress();
  31.  
  32. /* status.setText(deviceName+"\nAddress="+deviceAddress+
  33.   "\nDiscovery agent="+discoveryAgent.toString());
  34. */
  35.  
  36.  
  37.  
  38. String url = "btl2cap://localhost:"+service_UUID+";name="+deviceName;
  39.  
  40. notifier = (L2CAPConnectionNotifier) Connector.open(url);
  41. // con = (L2CAPConnection)Connector.open(url);
  42.  
  43. System.out.println("advertising the connection");
  44. /**************************************************************
  45.   WAITING INFINETELY HERE
  46. *******************************************************/
  47. con = notifier.acceptAndOpen();///////////////////////////////
  48. System.out.println("advertising finished");
  49. while(listening){
  50.  
  51. if(con.ready()){
  52. System.out.println("server READY to receive data");
  53. // primim in bytes data de la client apoi o trecem intr-un String
  54. byte[] buff = new byte[1000];
  55. con.receive(buff);
  56.  
  57. String s = new String(buff,0, buff.length);
  58. // msg.setText("a");
  59. //daca am primit mesaj alertam interfata vizuala
  60. System.out.println("Recieved from client: " + s.trim());
  61. chat.setAlert(s.trim());
  62.  
  63. //asa trimitem
  64. sendMessage("Hello client, my name is: " + local.getFriendlyName());
  65.  
  66. listening = false;
  67. }
  68. }
  69.  
  70.  
  71. } catch (IOException ex) {
  72. System.out.println("Eroare la SERVER\n");
  73. ex.printStackTrace();
  74. }
  75.  
  76.  
  77.  
  78. }
  79.  
  80.  
  81. private void sendMessage(String msg)
  82. {
  83. byte[] buff = msg.getBytes();
  84. try {
  85. con.send(buff);
  86. } catch(IOException e){
  87. System.out.println(e);
  88. }
  89. }
  90.  
  91. }

The client class
  1. public class BClient implements Runnable{
  2.  
  3. String s;
  4. private LocalDevice local;
  5. private DiscoveryAgent discoveryAgent;
  6. private String deviceName;
  7. private String deviceAddress;
  8.  
  9. private String service_UUID="00000000000010008000006057028A06";
  10. private L2CAPConnectionNotifier notifier;
  11. private L2CAPConnection con;
  12.  
  13. private BlueChat chat;
  14. private InquiryListener inquiryL;
  15. private ServiceListener serviceL;
  16. private boolean listening=true;
  17.  
  18. BClient(BlueChat chat)
  19. {
  20. this.chat = chat;
  21.  
  22. Thread t = new Thread(this);
  23. t.start();
  24. }
  25.  
  26. public void run() {
  27. try{
  28. byte[] buff = "SAALUT".getBytes();
  29.  
  30. System.out.println("Client thread started");
  31. //date dispozitiv
  32. local = LocalDevice.getLocalDevice();//initializare pt gazda
  33. local.setDiscoverable(DiscoveryAgent.LIAC);
  34. discoveryAgent = local.getDiscoveryAgent();
  35. deviceName = local.getFriendlyName();
  36. deviceAddress = local.getBluetoothAddress();
  37.  
  38. //cautare dispozitive bluetooth apoi serviciile fiecaruia
  39. inquiryL = new InquiryListener();
  40.  
  41. synchronized(inquiryL){
  42. System.out.println("inainte de cautare inquiry");
  43. discoveryAgent.startInquiry(DiscoveryAgent.LIAC, inquiryL);
  44. try{
  45. System.out.println("in asteptare");
  46. inquiryL.wait();
  47. }catch(InterruptedException e){ }
  48.  
  49.  
  50. System.out.println("asteptare inquiry terminata");
  51. }
  52.  
  53. //dispozitive gasite - in obiectul de cautare de mai sus
  54. Enumeration devices = inquiryL.cached_devices.elements();
  55.  
  56. UUID[] u = new UUID[]{ new UUID("00000000000010008000006057028A06", false) };
  57. int attributes[]= { 0x0100 };
  58.  
  59. //acum serviciile aferente FIECARUI ELEMENT... deic traversam vectorul de elemente
  60. serviceL = new ServiceListener();
  61. while(devices.hasMoreElements()){
  62. synchronized(serviceL){
  63. System.out.println("inainte de cautare servicii");
  64. discoveryAgent.searchServices(attributes, u, (RemoteDevice)devices.nextElement(), inquiryL);
  65. try{
  66. System.out.println("in asteptare");
  67. /**************************************************************
  68.   WAITING INFINETELY HERE
  69. *******************************************************/
  70. serviceL.wait();///////////
  71. }catch(InterruptedException e){}
  72.  
  73. System.out.println("asteptare servicii terminata");
  74. }
  75.  
  76.  
  77. if (serviceL.service!=null){
  78. try {
  79. String url;
  80. url = serviceL.service.getConnectionURL(0, false);
  81. deviceName = LocalDevice.getLocalDevice().getFriendlyName();
  82. con = (L2CAPConnection) Connector.open( url );
  83. sendMessage("Hello server, my name is: " + deviceName);
  84.  
  85. byte[] b = new byte[1000];
  86.  
  87. System.out.println("IN SERVICIU");
  88.  
  89. while (listening) {
  90. if (con.ready()){
  91. System.out.println("GATA DE PRIMIRE");
  92.  
  93. con.receive(b);
  94. String str = new String(b, 0, b.length);
  95. System.out.println("Received from server: " + str.trim());
  96. chat.setAlert(str.trim());
  97. listening = false;
  98. }
  99. }
  100. } catch (IOException g) {System.out.println(g);}
  101. }
  102.  
  103.  
  104. }
  105.  
  106.  
  107. } catch (IOException ex) {
  108. System.out.println("Eroare la CLIENT\n");
  109. ex.printStackTrace();
  110. }
  111. }
  112.  
  113.  
  114.  
  115.  
  116. /********* TRIMITE *****************/
  117.  
  118. private void sendMessage(String msg)
  119. {
  120. byte[] buff = msg.getBytes();
  121. try {
  122. con.send(buff);
  123. } catch(IOException e){
  124. System.out.println(e);
  125. }
  126. }
  127.  
  128.  
  129.  
  130. /*********** CLASA PENTRU CAUTARE DISPOZITIVE*******/
  131. class InquiryListener implements DiscoveryListener{
  132.  
  133. public Vector cached_devices;
  134.  
  135. InquiryListener(){
  136. cached_devices = new Vector();
  137. }
  138.  
  139. public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
  140. if( ! cached_devices.contains( btDevice ) ) {
  141. cached_devices.addElement( btDevice );
  142. }
  143. }
  144.  
  145. public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
  146. }
  147.  
  148. public void serviceSearchCompleted(int transID, int respCode) {
  149. }
  150.  
  151. public void inquiryCompleted(int discType) {
  152. synchronized(this){ this.notify();System.out.println("inquiry terminata"); }
  153. }
  154.  
  155. }
  156.  
  157.  
  158.  
  159. /*********** CLASA PENTRU CAUTARE SERVICII ale dispozitivelor*******/
  160. class ServiceListener implements DiscoveryListener{
  161.  
  162. public ServiceRecord service;
  163.  
  164. public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
  165. service = servRecord[0];
  166. System.out.println("foundService");
  167. }
  168.  
  169. public void serviceSearchCompleted(int transID, int respCode) {
  170. synchronized( this ){ this.notify();}
  171. }
  172.  
  173.  
  174. public void inquiryCompleted(int discType) {
  175. }
  176.  
  177. public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
  178. }
  179.  
  180. }
  181.  
  182.  
  183. }

So please help me on detection between devices if you can. I just don't know why the don't detect each other. I use NetBeans with Mobile Simulator and I even tested it on two real mobile phones - nothing happened
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,208
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer
 
-1
  #5
10 Days Ago
Originally Posted by susman View Post
Hello,
I'm a novice in J2ME but i started developing a bluetooth chat aplication. I managed to find some samples of btooth chat aplications but i'm not satisfied with it. Can you provide some help(i mean source code )? if i can help you with your problem i'll send you a solution.
Thank you
You better post your issue in new thread instead of trying to bargaining in style "give me what you have, maybe I will give you what I have.."
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 99
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 5
Clawsy Clawsy is offline Offline
Junior Poster in Training
 
0
  #6
10 Days Ago
Just download the wirelesss development kit for java and, after you install it ouy'll find a folder with a lot of examples there. As for my problem, I abandoned L2CAP connection and used StreamConnection. Good Luck. In fact, this is an old unsolved thread.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,208
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer
 
-1
  #7
10 Days Ago
Originally Posted by Clawsy View Post
Just download the wirelesss development kit for java and, after you install it ouy'll find a folder with a lot of examples there. As for my problem, I abandoned L2CAP connection and used StreamConnection. Good Luck. In fact, this is an old unsolved thread.
So on the end you went with my original suggestion
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 99
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 5
Clawsy Clawsy is offline Offline
Junior Poster in Training
 
0
  #8
10 Days Ago
kind of... L2CAP connection is more reliable but I just can't make it work. Thanks
Reply With Quote Quick reply to this message  
Reply

Tags
bluetooth, detection, java, mobile

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC