Package gui

Source Code of gui.ServerConection$WaitAcceptOpen

package gui;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import main.BTMIDlet;
import util.BTConnection;

/**
*
* @author TeXteR
*/
public class ServerConection extends TimerTask {

    private final String serviceUUID = "C7E0AF6060F111DFA08BC4732EF25ACD";
    private final String connectionParameters = ";authorize=false;authenticate=false;encrypt=false";
    private BTMIDlet bTMIDlet;
    private Form form;
    private Gauge gauge;
    private Timer timer;
    private LocalDevice localDevice;
    private StreamConnectionNotifier streamConnectionNotifier;
    private WaitAcceptOpen waitAcceptOpen;
    private String localName;
    private int counter = 0;

    public ServerConection(BTMIDlet bTMIDlet) {
        this.bTMIDlet = bTMIDlet;
        form = new Form("Please wait");
        gauge = new Gauge("Waiting for players...", false, 100, 0);

        timer = new Timer();
        timer.schedule(this, 0, 10);

        form.append(gauge);

        initBT();
        bTMIDlet.getDisplay().setCurrent(form);

        // Tworymy watek oczekujaczy na polaczenie
        waitAcceptOpen = new WaitAcceptOpen();
        waitAcceptOpen.start();
    }

    /**
     * Inicjuje pola potrzebne do polaczenia
     */
    private void initBT() {
        try {
            // Uzyskanie localDevice
            localDevice = LocalDevice.getLocalDevice();
            // Pobieramy nazwe urzadzenia
            localName = localDevice.getFriendlyName();
            // ustawiamy parametr polaczenia na GIAC. Trzeba sie dowiedziec czy warto. Moze lepsze bedzie LIAC
            localDevice.setDiscoverable(DiscoveryAgent.GIAC);
        } catch (BluetoothStateException ex) {
            System.out.println(ex.getMessage());
        }
    }

    /**
     * @see TimerTask
     */
    public void run() {
        counter++;
        if (counter > gauge.getMaxValue()) {
            counter = 0;
        }
        gauge.setValue(counter);
    }

    private class WaitAcceptOpen extends Thread {

        public void run() {
            try {
                // Tworzymy obiekt ktory bedzie czekal na polaczenie
                streamConnectionNotifier = (StreamConnectionNotifier) Connector.open("btspp://localhost:" + serviceUUID + ";name=" + localName + connectionParameters);
                form.append("\nOtworzono Connector");
                // Ta linia uruchamia metode synchronizowana. Dopoki polaczenie nie zostanie nawiazane, watek stoi tutaj.
                // nawiazanie polaczenia nastepuje w momencie otworzenia URL u klienta
                StreamConnection streamConnection = streamConnectionNotifier.acceptAndOpen();
                form.append("\nacceptAndOpen()");
                // Obowiazkowo zamykamy obiekt czekajacy na polaczenie
                streamConnectionNotifier.close();
                form.append("\nnotifier.close()");
                // Konczymy zadanie Timera
                timer.cancel();
                cancel();
                form.append("\nZaakceptowano");
                new MessagingForm(bTMIDlet, new BTConnection(streamConnection));
            } catch (IOException ex) {
                form.append(ex.getMessage());
            }
        }
    }
}
TOP

Related Classes of gui.ServerConection$WaitAcceptOpen

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.