Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First of all there are few things in you code need to be reviewed/fixed</p> <p>1- the <code>ArrayList&lt;Contact&gt; contacts</code> should be defined in your class, so the thread can have access to it and populate it <strong>not</strong> as a local variable within <code>getContacts()</code> method</p> <pre><code>public ArrayList&lt;Contact&gt; getContacts() { //ArrayList&lt;Contact&gt; contacts = new ArrayList&lt;&gt;(); return contacts; } </code></pre> <p>2- You should avoid using <em>infinite</em> loop within the run method to be able to stop the thread when you want to.</p> <pre><code>//while(true) while(isRunning) { // a flag that is set to true by default } </code></pre> <p>3- Checking that the connection equal <em>without</em> setting it to null after disconnecting means that the connection will be accepted <strong>only</strong> from the first client (assuming that connection is originally set to null) and afterwards your will just have an infinite loop but the code <code>this.connection = this.server.acceptAndOpen();</code> will not be reachable anymore </p> <pre><code>if(this.connection == null) { while((line = reader.readLine()) != null) { if(line.equals("--#do:disconnect")) { // You have to set it to null if you want to continue listening after disconnecting this.connection = null break; } } } </code></pre> <p>Or simply <strong>remove this check altogether</strong>, I see it is useless.</p> <p>Now back to your question:</p> <p>You can define your contact list as a class member to be accessible by both <code>run()</code> and <code>getContacts()</code> methods. You can make it final if needed. Then populate this list within the <code>run()</code> method; that's all.</p> <p>e.g.</p> <pre><code> class MyServerThread implements Runnable { private boolean isRunning = true; ArrayList&lt;Contact&gt; contacts = new ArrayList&lt;&gt;(); public ArrayList&lt;Contact&gt; getContacts(){ // Make sure that your not currently updating the contacts when this method is called // you can define a boolean flag and/or use synchronization return contacts; } public void run() { ... while(isRunning ) { this.connection = this.server.acceptAndOpen(); System.out.println("INFO: Bluetooth client connected"); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.openInputStream())); this.writer = new BufferedWriter(new OutputStreamWriter(connection.openOutputStream())); // You need to remove previously received contacts before issuing a new --#do:getcontacts command contacts.clear(); String line; while((line = reader.readLine()) != null) { if(line.equals("--#do:disconnect")) { break; } // Here you can parse the contact information String contactName = ... String contactPhone = ... contacts.add(new Contact(contactName,contactPhone)); } System.out.println("INFO: Client disconnected"); } } catch(BluetoothStateException ex) { ex.printStackTrace(); } catch(IOException ex) { ex.printStackTrace(); } } } </code></pre> <p>You do not have to use Object serialization, you can construct a simple protocol to send contacts from the phone to the PC, something similar to the commands you send e.g. <code>--#C:name$phone</code></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload