I am currently creating a Java application using two threads:
The first stream concerns the user interface of the application, as well as the processing of the command received through the bluetooth stream.
The bluetooth stream is a bluetooth server that waits for the robot to connect and process communications.
At the moment, the user interface thread is in a waiting state () until the bluetooth thread receives a new message for processing.
The problem is that I can track the notify / notifyAll call from the bluetooth stream, but my user interface does not resume it.
Now Iโm sure that I misunderstood how to properly manage synchronized threads, but I canโt understand what is wrong in my software.
Here is the code for the user interface:
package mapper; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.util.ArrayList; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTable; public class MapperUI extends JFrame implements Runnable { private ArrayList<String> messageArray; public MapperUI(){ super(); build(); this.setVisible(true); new Thread(this).start(); } private void build(){ setTitle("SLAM Mapper"); setSize(600,500); setLocationRelativeTo(null); setResizable(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setContentPane(buildContentPane()); } private JPanel buildContentPane(){ JPanel main = new JPanel(); main.setLayout(new BorderLayout());
Here is bluetoothService:
package mapper; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import javax.bluetooth.DiscoveryAgent; import javax.bluetooth.LocalDevice; import javax.bluetooth.UUID; import javax.microedition.io.Connector; import javax.microedition.io.StreamConnection; import javax.microedition.io.StreamConnectionNotifier; public class MapperCom extends Thread { public final UUID uuid = new UUID( "27012f0c68af4fbf8dbe6bbaf7aa432a", false); public final String name = "Server"; public final String url = "btspp://localhost:" + uuid + ";name=" + name + ";authenticate=false;encrypt=false;"; private LocalDevice local ; private StreamConnectionNotifier server ; private StreamConnection conn ; private DataInputStream din ; private DataOutputStream dout ; private String command; private String message; public MapperCom(){ try { this.command = ""; this.message = ""; System.out.println("Setting device to be discoverable..."); local = LocalDevice.getLocalDevice(); local.setDiscoverable(DiscoveryAgent.GIAC); System.out.println("Start advertising service..."); server = (StreamConnectionNotifier)Connector.open(url); System.out.println("Waiting for incoming connection...\n"); conn = server.acceptAndOpen(); System.out.println("Client Connected..."); din = new DataInputStream(conn.openInputStream()); dout = new DataOutputStream(conn.openOutputStream()); new Thread(this).start(); } catch (Exception e) { System.out.println("Exception Occured: " + e.toString()); } } @Override public synchronized void run(){ System.out.println("Bluetooth Thread Started"); while(true){ try { String cmd = ""; char c; System.out.println("Waiting for message"); while (((c = din.readChar()) > 0) && (c!='\n') ){ System.out.println("Char received :"+c); cmd = cmd + c; } storeMessage(cmd); System.out.println("Bt Notify......"); notifyAll(); System.out.println("Bt is Waiting for a command from mapper......"); wait(); sendResponse(); } catch (IOException e) {
source share