0

I did a Bluetooth server with bluecove following this tutorial:http://luugiathuy.com/2011/02/android-java-bluetooth/ I want to add a confirm popup with JOptionPane before the connection but I don't know how. Can you help me? Here is the code:

import com.intel.bluetooth.BluetoothStack;
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; 
import javax.swing.JOptionPane;

public class WaitThread implements Runnable { 
     private GestorPrincipal gestor = GestorPrincipal.getInstancia(); 

 /** * Constructor */ 
 public WaitThread() { }

 @Override 
 public void run() { 
     waitForConnection(); 
 } 

 /** * Waiting for connection from devices */ 
 private void waitForConnection() { 
     // retrieve the local Bluetooth device object 
     LocalDevice local = null;      
     StreamConnectionNotifier notifier; StreamConnection connection = null;   
     // setup the server to listen for connection 
     try { 
        local = LocalDevice.getLocalDevice();
        local.setDiscoverable(DiscoveryAgent.GIAC); 
        UUID uuid = new UUID(80087355); 
        // "04c6093b-0000-1000-8000-00805f9b34fb" 
        String url = "btspp://localhost:" + uuid.toString() + ";name=RemoteBluetooth"; 
        notifier = (StreamConnectionNotifier) Connector.open(url); 
     } 
     catch (Exception e) { 
        e.printStackTrace(); 
        return; 
      } 
      // waiting for connection 
      while (true) { 
        try { 
            System.out.println("Esperando la conexion...");
            gestor.agregarTexto("Esperando la conexion.."); 
            connection = notifier.acceptAndOpen(); 
            Thread processThread = new Thread(new ProcessConnectionThread(connection)); 
             processThread.start(); 
         } 
         catch (Exception e) { 
             e.printStackTrace(); return; 
          } 
     } 
   } 
}` 

PD: sorry for my English, I'm from ARG.

Martin
  • 68
  • 8

1 Answers1

0

Oracle has well-documented this topic named "How to Make Dialogs" this document shows how to create different kind of dialog boxes which will prompt user some with expecting input from user and some to just display some data with icon.

http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

For your requirement you could do something like this:

...
 while (true) { 
        try { 
            System.out.println("Esperando la conexion...");
            gestor.agregarTexto("Esperando la conexion..");
            connection = notifier.acceptAndOpen();
            //Ask user if he/she wants to get connected to this device
            int userChoice = JOptionPane.showConfirmDialog(
            null,
            "Would you like to connect to DEVICE_NAME?",
            "Connection Request",
            JOptionPane.YES_NO_OPTION);
            if(userChoice == JOptionPane.YES_OPTION) {
               // Create thread and come out of loop
               Thread processThread = new Thread(new ProcessConnectionThread(connection)); 
               processThread.start();
               break;
            }
            else {
              // user rejected the connection so close the connection here to allow other devices to connect
            }

         } 
         catch (Exception e) { 
             e.printStackTrace(); return; 
          } 
     }
...
  • Thanks, but it isn't my problem. I know how to make dialogs. My problem is that the `notifier.acceptAndOpen() ;` is in a while cycle and the dialog is shown all the time. – Martin Oct 09 '16 at 13:00
  • Do you want to display dialog box to a user saying what? When does the dialog box should appear? And if multiple Bluetooth devices are available should a user be prompt for each connection ? If the user accepts one connection should it come out of the loop? –  Oct 09 '16 at 23:51
  • I want to display a dialog box saying "The device xxxx is trying to connect with you, do you want?, yes, no". It should appear when a phone try to connect with the pc from my app, only if no one is connected. My code works, but when the phone want to connect the server accept it alone. – Martin Oct 10 '16 at 01:12
  • If the user say "yes" then it should come out of while loop and if the user say "no" then new dialog box should appearing(if another device is trying to connect) saying "The device xyz is trying to connect with you, do you want? , yes no" an so on. Am I right ? –  Oct 10 '16 at 03:40