如何在swing应用程序检测到设备后进行蓝牙连接

时间:2013-08-01 11:35:09

标签: java bluetooth

我想从基于窗口的java swing应用程序将JSON数据发送到移动设备。我的应用程序能够成功搜索设备,但现在我需要建立与移动和应用程序的连接。我如何建立连接?

public class BluetoothFinder {

public static final Vector<RemoteDevice> devicesDiscovered = new Vector<RemoteDevice>();
Object bluetoothName;

     public void BluetoothDeviceSearch() {
    final Object inquiryCompletedEvent = new Object();

    devicesDiscovered.clear();
    DiscoveryListener listener = new DiscoveryListener() {

        @Override
        public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
            System.out.println("Device " + btDevice.getBluetoothAddress() + " found");
            devicesDiscovered.addElement(btDevice);
            try {
                System.out.println("     name " + btDevice.getFriendlyName(false));
                bluetoothName = btDevice.getFriendlyName(false);
            } catch (IOException cantGetDeviceName) {
            }
        }
}

void printResult() {
    System.out.println("PrintResult method Taget Hit ..." + devicesDiscovered.size() + " device(s) found " + "New " + devicesDiscovered);
    System.out.println("Printing ...");

    Enumeration bluetoothDevicesList = devicesDiscovered.elements();
    while (bluetoothDevicesList.hasMoreElements()) {
        Object result = (Object) bluetoothDevicesList.nextElement();
        System.out.println("Bluetooth Device " + result);

        if (result.toString().equals("CC051B9C88DF")) {

            JOptionPane.showMessageDialog(null, "Bluetooth Device Name : '" + bluetoothName + "' Bluetooth Device No : '" + result + "'",
                    "Bluetooth Devices Search Result", 1);
        }

    }



    public static void main(String[] args) {
    BluetoothFinder bluetoothFinder = new BluetoothFinder();
    bluetoothFinder.BluetoothDeviceSearch();
    bluetoothFinder.printResult();
    }
}

以上代码能够搜索设备,但我不知道如何与窗口操作系统和Android移动设备建立连接。