显示确认对话框以接受连接,bluecove 服务器 java
Show confirm dialog to accept the connection, bluecove server java
我按照本教程用 bluecove 做了一个蓝牙服务器:http://luugiathuy.com/2011/02/android-java-bluetooth/ 我想在连接前用 JOptionPane
添加一个确认弹出窗口,但我不知道如何。你能帮助我吗?这是代码:
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:抱歉我的英语不好,我来自 ARG。
Oracle 已经详细记录了这个名为 "How to Make Dialogs" 的主题,该文档展示了如何创建不同类型的对话框,这些对话框会提示用户一些期望用户输入的内容,并且有些只是用图标显示一些数据。
http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
根据您的要求,您可以这样做:
...
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;
}
}
...
我按照本教程用 bluecove 做了一个蓝牙服务器:http://luugiathuy.com/2011/02/android-java-bluetooth/ 我想在连接前用 JOptionPane
添加一个确认弹出窗口,但我不知道如何。你能帮助我吗?这是代码:
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:抱歉我的英语不好,我来自 ARG。
Oracle 已经详细记录了这个名为 "How to Make Dialogs" 的主题,该文档展示了如何创建不同类型的对话框,这些对话框会提示用户一些期望用户输入的内容,并且有些只是用图标显示一些数据。
http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
根据您的要求,您可以这样做:
...
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;
}
}
...