Bluecove Bluetooth remote device service discovery example

BlueCove + Bluetooth + 700x300.png

BlueCove
BlueCove is Java library for bluetooth. Its used to interact with Bluetooth devices using Java. BlueCove currently supports Mac OS X, WIDCOMM, BlueSoleil and Microsoft Bluetooth stack. Current version is 2.1.0. Read more on official website http://bluecove.org

Today I'm going to demonstrate "How to use BlueCove to search near by Bluetooth devices?" and "How to find Bluetooth device services using BlueCove".

RemoteDeviceDiscovery
RemoteDeviceDiscovery class is used to find paired and near by devices.
import java.util.Enumeration;
import java.util.Vector;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;

public class RemoteDeviceDiscovery {

    public Vector getDevices() {
        /* Create Vector variable */
        final Vector devicesDiscovered = new Vector();
        try {
            final Object inquiryCompletedEvent = new Object();
            /* Clear Vector variable */
            devicesDiscovered.clear();

            /* Create an object of DiscoveryListener */
            DiscoveryListener listener = new DiscoveryListener() {

                public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
                    /* Get devices paired with system or in range(Without Pair) */
                    devicesDiscovered.addElement(btDevice);
                }

                public void inquiryCompleted(int discType) {
                    /* Notify thread when inquiry completed */
                    synchronized (inquiryCompletedEvent) {
                        inquiryCompletedEvent.notifyAll();
                    }
                }

                /* To find service on bluetooth */
                public void serviceSearchCompleted(int transID, int respCode) {
                }

                /* To find service on bluetooth */
                public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
                }
            };

            synchronized (inquiryCompletedEvent) {
                /* Start device discovery */
                boolean started = LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC, listener);
                if (started) {
                    System.out.println("wait for device inquiry to complete...");
                    inquiryCompletedEvent.wait();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        /* Return list of devices */
        return devicesDiscovered;
    }
}

ServicesSearch
ServicesSearch class used to find specific service on Bluetooth device. Each service identified using UUID (Universally unique identifier). All UUID listed on https://www.bluetooth.org/en-us/specification/assigned-numbers/service-discovery . Replace your required UUID in following code.
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.bluetooth.*;

public class ServicesSearch {

    /** 
     * UUID used to find specific service supported by bluetooth device
     * https://www.bluetooth.org/en-us/specification/assigned-numbers/service-discovery
     * Find UUIDs for all types of bluetooth services.
     */
    /* To find push object service */
    private UUID OBEX_OBJECT_PUSH_PROFILE = new UUID(0x1105);
    /* To find file transfer service */
    private UUID OBEX_FILE_TRANSFER_PROFILE = new UUID(0x1106);
    /* To find hands free service */
    private UUID HANDS_FREE = new UUID(0x111E);
    /* Get URL attribute from bluetooth service */
    private int URL_ATTRIBUTE = 0X0100;
    
    public Map<String, List<String>> getBluetoothDevices() {        
        /**
         * Find service on bluetooth device 
         * Note: In following line you can use one service at a time. I'm new to bluetooth programming it might me wrong perception.
         * UUID[] searchUuidSet = new UUID[]{OBEX_FILE_TRANSGER_PROFILE};
         * 
         * CORRECT: UUID[] searchUuidSet = new UUID[]{OBEX_FILE_TRANSGER_PROFILE};
         * WRONG: UUID[] searchUuidSet = new UUID[]{OBEX_FILE_TRANSGER_PROFILE, OBEX_OBJECT_PUSH_PROFILE};
         */
        /* Initialize UUID Array */
        UUID[] searchUuidSet = new UUID[]{HANDS_FREE};
        final Object serviceSearchCompletedEvent = new Object();
        int[] attrIDs = new int[]{URL_ATTRIBUTE};
        
        /* Create an object to get list of devices in range or paired */
        RemoteDeviceDiscovery remoteDeviceDiscovery = new RemoteDeviceDiscovery();
        /* Create map to return Bluetooth device address, name and URL */
        final Map<String, List<String>> mapReturnResult = new HashMap<String, List<String>>(); 

        try {
            /* Create an object of DiscoveryListener */
            DiscoveryListener listener = new DiscoveryListener() {

                /* To find bluetooth devices */
                public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
                }

                /* To find bluetooth devices */
                public void inquiryCompleted(int discType) {
                }

                /* Find service URL of bluetooth device */
                public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
                    for (int i = 0; i < servRecord.length; i++) {
                        /* Find URL of bluetooth device */
                        String url = servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
                        if (url == null) {
                            continue;
                        }
                        String temporaryString = "";
                        /* Get object of bluetooth device */
                        RemoteDevice rd = servRecord[i].getHostDevice();
                        /* Get attribute from ServiceRecord */
                        DataElement serviceName = servRecord[i].getAttributeValue(URL_ATTRIBUTE);
                        if (serviceName != null) {         
                            temporaryString = serviceName.getValue() + "\n" + url;
                            /* Put it in map */
                            mapReturnResult.get(rd.getBluetoothAddress()).add(temporaryString);
                        } else {
                            temporaryString = "Uknown service \n" + url;
                            /* Put it in map */
                            mapReturnResult.get(rd.getBluetoothAddress()).add(temporaryString);
                        }
                    }
                }

                public void serviceSearchCompleted(int transID, int respCode) {
                    /* Notify thread when search completed */
                    synchronized (serviceSearchCompletedEvent) {
                        serviceSearchCompletedEvent.notifyAll();
                    }
                }
            };

            /* Get list of bluetooth device from class RemoteDeviceDiscovery */
            for (Enumeration en = remoteDeviceDiscovery.getDevices().elements(); en.hasMoreElements();) {
                /* Get RemoteDevice object */
                RemoteDevice btDevice = (RemoteDevice) en.nextElement();
                /* Create list to return details */
                List<String> listDeviceDetails = new ArrayList<String>();
                
                try {
                    /* Add bluetooth device name and address in list */
                    listDeviceDetails.add(btDevice.getFriendlyName(false));
                    listDeviceDetails.add(btDevice.getBluetoothAddress());
                } catch (Exception e) {
                }
                
                /* Put bluetooth device details in map */
                mapReturnResult.put(btDevice.getBluetoothAddress(), listDeviceDetails);
                synchronized (serviceSearchCompletedEvent) {
                    LocalDevice.getLocalDevice().getDiscoveryAgent().searchServices(attrIDs, searchUuidSet, btDevice, listener);
                    serviceSearchCompletedEvent.wait();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        /* Return bluetooth devices detail */
        return mapReturnResult;
    }
}

BluetoothDevices
BluetoothDevices is simple JFrame used to give Uter Interface for program. Its created in NetBeans IDE, You may find some code unexplained which is generated by IDE.
import com.javaquery.bluetooth.ServicesSearch;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.UIManager;

public class BluetoothDevices extends javax.swing.JFrame {

    /* DefaultListModel to attach it with JList */
    private DefaultListModel defaultModel;
    /* Map to get device details list */
    private Map<String, List<String>> mapReturnResult = new HashMap<String, List<String>>();
    /* Map to identify device on user click of JList */
    private Map<Integer, List<String>> mapDevicePosition = new HashMap<Integer, List<String>> ();

    public BluetoothDevices() {
        initComponents();
        defaultModel = new DefaultListModel();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        JListBluetoothDevices = new javax.swing.JList();
        lblDeviceName = new javax.swing.JLabel();
        lblRuntimeDeviceName = new javax.swing.JLabel();
        lblDeviceAddress = new javax.swing.JLabel();
        lblRuntimeDeviceAddress = new javax.swing.JLabel();
        lblServiceDetails = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        JTextAreaServiceDetails = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Bluecove Bluetooth Discovery");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        JListBluetoothDevices.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                JListBluetoothDevicesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(JListBluetoothDevices);

        lblDeviceName.setText("Bluetooth Device Name");

        lblDeviceAddress.setText("Bluetooth Device Address");

        lblServiceDetails.setText("Service Details");

        JTextAreaServiceDetails.setColumns(20);
        JTextAreaServiceDetails.setRows(5);
        jScrollPane2.setViewportView(JTextAreaServiceDetails);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblDeviceName)
                            .addComponent(lblDeviceAddress))
                        .addGap(73, 73, 73)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblRuntimeDeviceAddress)
                            .addComponent(lblRuntimeDeviceName, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(lblServiceDetails)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblDeviceName)
                            .addComponent(lblRuntimeDeviceName))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblDeviceAddress)
                            .addComponent(lblRuntimeDeviceAddress))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblServiceDetails)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	/* Search for bluetooth device when window opened */
    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        int intDevicePosition = 0;
        JListBluetoothDevices.setModel(defaultModel);        

        /* Create an object of ServicesSearch */
        ServicesSearch ss = new ServicesSearch();
        /* Get bluetooth device details */
        mapReturnResult = ss.getBluetoothDevices();

        /* Add devices in JList */
        for (Map.Entry<String, List<String>> entry : mapReturnResult.entrySet()) {            
            defaultModel.addElement(entry.getValue().get(0));
            mapDevicePosition.put(intDevicePosition, entry.getValue());
            intDevicePosition++;
        }
    }//GEN-LAST:event_formWindowOpened

	/* On click of any item in List Box */
    private void JListBluetoothDevicesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_JListBluetoothDevicesMouseClicked
		/* Get bluetooth device details from temporary list */
        List<String> tmpDeviceDetails = mapDevicePosition.get(JListBluetoothDevices.getSelectedIndex());
		/* Set bluetooth device name */
        lblRuntimeDeviceName.setText(tmpDeviceDetails.get(0));
		/* Set bluetooth device Address */
        lblRuntimeDeviceAddress.setText(tmpDeviceDetails.get(1));       
        
        if(tmpDeviceDetails.size() > 2 && tmpDeviceDetails.get(2) != null){
			/* Set bluetooth device service name and URL */
            JTextAreaServiceDetails.setText(tmpDeviceDetails.get(2));
        }else{
            JTextAreaServiceDetails.setText("Service not found");
        }
    }//GEN-LAST:event_JListBluetoothDevicesMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                 /* To set new look and feel */
                JFrame.setDefaultLookAndFeelDecorated(true);
                try {
                    /**
                     * Change look and feel of JFrame to Nimbus 
                     * For other look and feel check
					 * http://www.javaquery.com/2013/06/how-to-applyset-up-swing-look-and-feel.html
                     */
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
                } catch (Exception ex) {
                   ex.printStackTrace();
                }
                /* Create an object of BluetoothDevices */
                BluetoothDevices bluetoothDevicesFrame = new BluetoothDevices();
                /* make BluetoothDevices visible */
                bluetoothDevicesFrame.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList JListBluetoothDevices;
    private javax.swing.JTextArea JTextAreaServiceDetails;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblDeviceAddress;
    private javax.swing.JLabel lblDeviceName;
    private javax.swing.JLabel lblRuntimeDeviceAddress;
    private javax.swing.JLabel lblRuntimeDeviceName;
    private javax.swing.JLabel lblServiceDetails;
    // End of variables declaration//GEN-END:variables
}

Downloads
NetBeans Project: http://goo.gl/q06HYL
Driver: http://goo.gl/LaR61U


Sample Client - Server Chat Application in Java

Client Server ChatApplication

Wikipedia: A network socket is an endpoint of an inter-process communication flow across a computer network.

Difference between Server and Client
Server Socket is one that accept connection on specific port and can respond. Where client can send message to server on specified port and can get response from server.

We will send messages over network only. In this demo application I'm sending and receiving messages on localhost. To create full-fledged chat application you need to modify many things like...

  • Every system must have Socket Server running on local system to accept messages from another system.
  • Get list of all system which is running your application. 
  • If you are planning to create rich chat application, provide advance settings like Save chat log, Play sound when receives message, Send encrypted messages, etc... can be lot more.

Note: There will be plenty of code which is not explained in all source code which was generated by NetBeans IDE. NetBeans project code attached at the end of article.

Socket Server
It will accept connection and listen for messages and pass it to desired frame/user chat window. I create Server in form JFrame just to give better look.
/*
 * mainFrame.java
 * This program demonstarte client server message application.
 * mainFrame.java will act as a server program aimed to receive messages and respond to client.
 */
package com.javaquery.frames;

import java.awt.Frame;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.UIManager;
import org.json.JSONObject;

public class mainFrame extends javax.swing.JFrame {

    /* Inner class to create socket server */
    private class mainServer extends Thread {

        /* Create ServerSocket variable */
        private ServerSocket serverSocket;

        /* Constructor to initialize serverSocket */
        public mainServer() throws IOException {
            serverSocket = new ServerSocket(6666);
        }

        /* Implement run() for Thread */
        public void run() {
            /* Keep Thread running */
            while (true) {
                try {
                    /* Accept connection on server */
                    Socket server = serverSocket.accept();
                    /* DataInputStream to get message sent by client program */
                    DataInputStream in = new DataInputStream(server.getInputStream());
                    /* We are receiving message in JSON format from client. Parse String to JSONObject */
                    JSONObject clientMessage = new JSONObject(in.readUTF());
                    
                    /* Flag to check chat window is opened for user that sent message */
                    boolean flagChatWindowOpened = false;
                    /* Reading Message and Username from JSONObject */
                    String userName = clientMessage.get("Username").toString();
                    String message = clientMessage.getString("Message").toString();
                    
                    /* Get list of Frame/Windows opened by mainFrame.java */
                    for(Frame frame : Frame.getFrames()){
                        /* Check Frame/Window is opened for user */
                        if(frame.getTitle().equals(userName)){
                            /* Frame/ Window is already opened */
                            flagChatWindowOpened = true;
                            /* Get instance of ChatWindow */
                            ChatWindow chatWindow = (ChatWindow) frame;
                            /* Get previous messages from TextArea */
                            String previousMessage = chatWindow.getjTextArea1().getText();
                            /* Set message to TextArea with new message */
                            chatWindow.getjTextArea1().setText(previousMessage+"\n"+message);
                        }
                    }
                    
                    /* ChatWindow is not open for user sent message to server */
                    if(!flagChatWindowOpened){
                        /* Create an Object of ChatWindow */
                        ChatWindow chatWindow = new ChatWindow();
                        /**
                         * We are setting title of window to identify user for next message we gonna receive
                         * You can set hidden value in ChatWindow.java file.
                         */
                        chatWindow.setTitle(userName);
                        /* Set message to TextArea */
                        chatWindow.getjTextArea1().setText(message);
                        /* Make ChatWindow visible */
                        chatWindow.setVisible(true);
                    }
                    
                    /* Get DataOutputStream of client to repond */
                    DataOutputStream out = new DataOutputStream(server.getOutputStream());
                    /* Send response message to client */
                    out.writeUTF("Received from "+clientMessage.get("Username").toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * To start SocketServer when mainFrame.java loads
     */
    public void startServer() {
        try {
            /* Create thread of Inner class mainServer */
            Thread t = new mainServer();
            /* Start Thread */
            t.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** Creates new form mainFrame */
    public mainFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    //                           
    private void initComponents() {

        lblInformation = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Server");
        setAlwaysOnTop(true);

        lblInformation.setText("Client - Server Sample Application");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblInformation)
                .addContainerGap(229, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblInformation)
                .addContainerGap(275, Short.MAX_VALUE))
        );

        pack();
    }//                         

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                /* To set new look and feel */
                JFrame.setDefaultLookAndFeelDecorated(true);
                try {
                    /**
                     * Change look and feel of JFrame to Nimbus 
                     * For other look and feel check
                     * 
                     */
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
                } catch (Exception ex) {
                   ex.printStackTrace();
                }
                /* Create an Object of mainFrame */
                mainFrame mFrame = new mainFrame();
                /* make mainFrame visible */
                mFrame.setVisible(true);
                /* Call startServer method  */
                mFrame.startServer();
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JLabel lblInformation;
    // End of variables declaration                   
}

Chat Window Source Code
ChatWindow.java is common class which creates Chat window for user. We are identify particular user's chat window by its title. You can have your logic.
package com.javaquery.frames;

public class ChatWindow extends javax.swing.JFrame {

    /** Creates new form ChatWindow */
    public ChatWindow() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    //                           
    private void initComponents() {

        lblInfo = new javax.swing.JLabel();
        scrollPane = new javax.swing.JScrollPane();
        chatArea = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Message Box");

        lblInfo.setText("Messages");

        chatArea.setColumns(20);
        chatArea.setRows(5);
        scrollPane.setViewportView(chatArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(lblInfo))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblInfo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }//                         

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ChatWindow().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JTextArea chatArea;
    private javax.swing.JLabel lblInfo;
    private javax.swing.JScrollPane scrollPane;
    // End of variables declaration                   

    /**
     * @return the chatArea
     */
    public javax.swing.JTextArea getjTextArea1() {
        return chatArea;
    }

    /**
     * @param chatArea the chatArea to set
     */
    public void setjTextArea1(javax.swing.JTextArea jTextArea1) {
        this.chatArea = jTextArea1;
    }
}

Client Source Code
This will send message to sever and get response. We are sending 3 messages to server. Two messages from same username.
/*
 * client.java
 * This program demonstarte client server message application.
 * client.java will act as a client program aimed to send messages and get server response
 */

package com.javaquery.network;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import org.json.JSONObject;

public class client {

    public static void main(String[] args) {
        client c = new client();
        /* Create JSON variable */
        JSONObject transmitJSON = new JSONObject();

        /**
         * This is sample program so we are sending three messages from same system with different username.
         * In your case you'll receive message from multiple systems.
         */
        /* Fill up JSON variable with message and username */
        transmitJSON.put("Message", "Hello JavaQuery!");
        transmitJSON.put("Username", "Vicky.Thakor");
        /* Send message to server */
        c.sendMessage("localhost", transmitJSON.toString());
        
        transmitJSON.put("Message", "Hello Apple!");
        transmitJSON.put("Username", "Steve.Jobs");
        /* Send message to server */
        c.sendMessage("localhost", transmitJSON.toString());
        
        transmitJSON.put("Message", "How are you?");
        transmitJSON.put("Username", "Steve.Jobs");
        /* Send message to server */
        c.sendMessage("localhost", transmitJSON.toString());
    }

    /**
     * @author javaQuery
     * @param host
     * @param message 
     */
    public void sendMessage(String host, String message) {
        try {
            /* Create new socket connection with server host using port 6666 (port can be anything) */
            Socket client = new Socket(host, 6666);
            /* Get server's OutputStream */
            OutputStream outToServer = client.getOutputStream();
            /* Get server's DataOutputStream to write/send message */
            DataOutputStream out = new DataOutputStream(outToServer);
            /* Write message to DataOutputStream */
            out.writeUTF(message);
            /* Get InputStream to get message from server */
            InputStream inFromServer = client.getInputStream();
            /* Get DataInputStream to read message of server */
            DataInputStream in = new DataInputStream(inFromServer);
            /* Print message received from server */
            System.out.println("Server says..." + in.readUTF());
            /* Close connection of client socket */
            client.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


Download Project

How to get list of opened JFrame in Java Swing?


Swing
Swing is built on base of Abstract Window Toolkit. Swing is used to create desktop application. Swing is using AWT directly or indirectly to provide you richer user interface. Swing is placed under javax.swing

JFrame can be accessible by its parent.



Understanding the Diagram
As you can see I created boundary around Parent JFrame, Child JFrame 1 and Child JFrame 2. It means you can access list of JFrame initiated by ParentJFrame. You can't access Standalone JFrame in Parent JFrame.


Frame.getFrames()
This method will return list of all Frame Object created by its parent and parent it self.


Parent JFrame Source Code
import java.awt.Frame;

public class ParentJFrame extends javax.swing.JFrame {

    /* Creates new form ParentJFrame */
    public ParentJFrame() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // //GEN-BEGIN:initComponents
    private void initComponents() {

        lblInfo = new javax.swing.JLabel();
        btnOpenChildJFrame = new javax.swing.JButton();
        btnListJFrames = new javax.swing.JButton();
        scrollPane = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Parent JFrame");

        lblInfo.setText("Parent JFrame");

        btnOpenChildJFrame.setText("Open Child JFrame");
        btnOpenChildJFrame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOpenChildJFrameActionPerformed(evt);
            }
        });

        btnListJFrames.setText("List JFrame");
        btnListJFrames.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnListJFramesActionPerformed(evt);
            }
        });

        textArea.setColumns(20);
        textArea.setRows(5);
        scrollPane.setViewportView(textArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                    .addComponent(lblInfo)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnOpenChildJFrame)
                        .addGap(18, 18, 18)
                        .addComponent(btnListJFrames)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblInfo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOpenChildJFrame)
                    .addComponent(btnListJFrames))
                .addGap(18, 18, 18)
                .addComponent(scrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// //GEN-END:initComponents

    /**
     * Open Child JFrame button click event
     */
    private void btnOpenChildJFrameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOpenChildJFrameActionPerformed
        /* Create an object of childJFrame */
        childJFrame child = new childJFrame();
        /* Make childJFrame visible */
        child.setVisible(true);
    }//GEN-LAST:event_btnOpenChildJFrameActionPerformed

    /**
     * List JFrame button click event
     */
    private void btnListJFramesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnListJFramesActionPerformed
        /* Get previous text of JFrame */
        String previousText = textArea.getText();
        String strNewFrames = "";
        /* It will give you list of JFrame created/Initiated by ParentJFrame */
        for(Frame frame: Frame.getFrames()){
            strNewFrames += frame.getTitle() + "\n";
        }
        /* Set text of JTextArea */
        textArea.setText(previousText+strNewFrames);
    }//GEN-LAST:event_btnListJFramesActionPerformed

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ParentJFrame().setVisible(true);
            }
        });
    }

    private javax.swing.JButton btnListJFrames;
    private javax.swing.JButton btnOpenChildJFrame;
    private javax.swing.JLabel lblInfo;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JTextArea textArea;
}

Child JFrame Source Code
public class childJFrame extends javax.swing.JFrame {

    /** Creates new form childJFrame */
    public childJFrame() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // //GEN-BEGIN:initComponents
    private void initComponents() {

        lblInfo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Child JFrame");

        lblInfo.setText("Child JFrame");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblInfo)
                .addContainerGap(188, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblInfo)
                .addContainerGap(91, Short.MAX_VALUE))
        );

        pack();
    }// //GEN-END:initComponents

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new childJFrame().setVisible(true);
            }
        });
    }
    private javax.swing.JLabel lblInfo;
}
I used NetBeans to create sample code. I explained code that relate the title of article. Major code done only in Parent JFrame. Child JFrame only supports example.

java.net.ConnectException: Connection timed out: connect

internet disconnected

This exception caused by several issues. Issue may be solved by ensuring following checklist.

- Connected to Internet / Local Area Network (LAN)
Make sure you are connected to internet or local area network.

- Behind Firewall
Firewall is common issue faced by many developer. We were facing same issue.
java.net.ConnectException: Connection timed out: connect
 at java.net.PlainSocketImpl.socketConnect(Native Method)
 at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
 at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
 at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
 at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
 at java.net.Socket.connect(Socket.java:529)
 at java.net.Socket.connect(Socket.java:478)
 at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
 at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
 at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
 at sun.net.www.http.HttpClient.(HttpClient.java:233)
 at sun.net.www.http.HttpClient.New(HttpClient.java:306)
 at sun.net.www.http.HttpClient.New(HttpClient.java:323)
 at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970)
 at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
 at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
 at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)
 at ReadWebpage.main(ReadWebpage.java:24)

Scenario
We were writing an applet program in Java to read webpage from internet. We can't even make request to well know search engine www.google.com even if its allowed in firewall rule. We called upon our network admin he said you must pass your request through proxy server. We set proxy in our Java code for HttpURLConnection and all went good.

Code
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.0.202", 8080));
HttpURLConnection con = (HttpURLConnection) data.openConnection(proxy);
Use of Proxy
You need to take care for proxy connection and it depends upon whether you are write program for applet or desktop application. If you are writing code for applet then in production applet, you don't need proxy connection as server will have all access and will work without proxy. If you are working on desktop application that run within the Local Area Network then you must set proxy in case of firewall.

Sign Applet using code signing certificate and verify jar


Applet
An applet is Java program designed to be delivered over Internet/Network. The Java program executed automatically by Java compatible browser.

Code Signing Certificate
Like in real world we sign document, Code Signing Certificate does the same with code. Code signing certificate is used to preserve authenticity of code. You can buy code signing certificate through different vendor like verisign, Thawte, etc...

Note: I am using .jks (Java KeyStore) file to sign my applet. I haven't tried with other certificate as its not available for free. You can also contact your vendor for the same.


Step 1: Start command prompt.

Step 2: Execute below command by changing instructed values.
Note: I placed certificate and applet in same folder and pointing jarsigner.exe.
/**
 * Change jarsigner.exe path 
 * Change certificate name and path
 * Change jar file name and path
 */
cmd> "C:\Program Files\Java\jdk1.6.0_25\bin\jarsigner.exe" -keystore javaQueryThawte.jks applet.jar server

Step 3: Verifying jar you just signed.
/**
 * Change jarsigner.exe path 
 * Change jar file name and path
 */
cmd> "C:\Program Files\Java\jdk1.6.0_25\bin\jarsigner.exe" -verify -verbose -certs applet.jar

Lets say you required to sign same applet again and again, we'll act smart. Create executable batch file for windows system.
/**
 * Open notepad
 * Change jarsigner.exe location
 * Change certificate name
 * Change jar file name
 * Save below code with file extension .bat i.e: signApplet.bat
 */
@echo off
echo Applet Code Signing Console
"C:\Program Files\Java\jdk1.6.0_25\bin\jarsigner.exe" -keystore NextendersIndiaPvtLtd.jks POCDemo.jar server
"C:\Program Files\Java\jdk1.6.0_25\bin\jarsigner.exe" -verify -verbose -certs POCDemo.jar
pause

Place signApplet.bat, certificate and jar file in same folder. Now execute signApplet.bat and provide password of certificate. You are done with it.

Error Code: 1093. You can't specify target table 'user_master' for update in FROM clause

mysql_logo

You can't specify target table 'user_master' for update in FROM clause error comes up when you try to perform an update or delete operation while selecting data from table it self. This is something like How to update/delete self table in MySQL? We will produce same error and then will give you solution for the same.

Problem
/* UPDATING RECORDS */
UPDATE user_master
SET first_name = 'javaQuery'
WHERE id IN (SELECT id FROM user_master WHERE last_name = 'Jobs');

/* DELETING RECORDS */
DELETE 
FROM user_master
WHERE id IN (SELECT id FROM user_master WHERE user_name = 'javaQuery');
We are fetching all users whose Lastname is Jobs and replacing its Firstname in 1st query and deleting records in second query. This won't allow you to update or delete records.


Solution
/* UPDATING RECORDS */
UPDATE user_master
SET first_name = 'javaQuery'
WHERE id IN (SELECT * FROM (SELECT id FROM user_master WHERE user_name = 'javaQuery') AS userIDs);

/* DELETING RECORDS */
DELETE
FROM user_master
WHERE id IN (SELECT * FROM (SELECT id FROM user_master WHERE user_name = 'javaQuery') AS userIDs);

What we did?
  • We are selecting data from query SELECT id FROM user_master WHERE user_name = 'javaQuery' to temporary table called userIDs.
  • Select data from temporary table and use it in WHERE condition.

How to capture computer screen using Java?

screen capture macbook and desktop

Abstract Window Toolkit (AWT)
           AWT used to create and manage windows. It is used to create windows that run in a GUI environment. Swing is built on base of AWT. Swing is using AWT directly or indirectly to provide you richer user interface. AWT is placed under java.awt package.

We will use Abstract Window Toolkit to create screen capture program. For the sake of convenience, I’m executing example from main method. Following example will capture whole screen of your system.
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 * @author javaQuery
 */
public class ImageCapture {
    public static void main(String[] args) throws AWTException, IOException {
        /* File location is C:\Users\computerName\screenshot.png */
        File saveImage = new File(System.getProperty("user.home")+"\\screenshot.png");
        /* Define how much area you want to capture */
        Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); 
        /* Capture image */
        BufferedImage imageCapture = new Robot().createScreenCapture(rectangle);
        /* Write BufferedImage to file */
        ImageIO.write(imageCapture, "png", saveImage);
    }
}

Variations

Rectangle(int width, int height);
Rectangle rectangle = new Rectangle(100,100);
It will capture an image of 100 x 100 (WxH) from top left corner of screen.

Rectangle(Dimension(int width, int height));
Rectangle rectangle = new Rectangle(new Dimension(100, 100));
It will capture an image of 100 x 100 (WxH) as specified its dimension. It’s same as above specification. To use dimension you need to import java.awt.Dimension class in your code.

Rectangle(Point(int x, int y));
Rectangle rectangle = new Rectangle(new Point(20, 20));
/* setSize(int width, int height) */
rectangle.setSize(100, 100);
Point is used to specify PointX and PointY of screen. It capture image block of given size from PointX and PointY. In this case it will capture an image of 100 x 100 (WxH) from point x = 20 and point y = 20. To use Point you need to import java.awt.Point class in your code.

Rectangle(Point(int x, int y), Dimension(int width, int height))
Rectangle rectangle = new Rectangle(new Point(10,10), new Dimension(100, 100));
Using Point and Dimension at the same time. Capture 100 x 100 (WxH) image from point x = 10 and point y = 10. You need to import both class as follow java.awt.Point and java.awt.Dimension.

Rectangle(int x, int y, int width, int height)
Rectangle rectangle = new Rectangle(20, 20, 100, 100);
It’s same as specifing PointX and PointY of screen and its width - height. It will capture an image of 100 x 100 (WxH) from point x = 20 and point y = 20.

You can use this program for different purposes. One of them is Key Logger in Java.