예제 #1
0
  // start server
  public void startServer() throws IOException {

    // Create a UUID for SPP
    UUID uuid = new UUID(Config.uuid, true);
    System.out.println(uuid.toString());
    // Create the servicve url
    String connectionString = "btspp://localhost:" + uuid + ";name=Bluetooth SPP Server";

    // open server url
    StreamConnectionNotifier streamConnNotifier =
        (StreamConnectionNotifier) Connector.open(connectionString);

    // Wait for client connection
    System.out.println("\nServer Started. Waiting for clients to connect...");
    while (isRunning) {
      connection = streamConnNotifier.acceptAndOpen();
      System.out.println("Connection opened");
      RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
      System.out.println("Remote device address: " + dev.getBluetoothAddress());
      System.out.println("Remote device name: " + dev.getFriendlyName(true));

      inStream = connection.openInputStream();
      outStream = connection.openOutputStream();

      if ((dev.getFriendlyName(true) == Config.pairedDevice) || true) {
        new Receiver().start();
        new Sender().start();
      }
    }
    streamConnNotifier.close();
  }
예제 #2
0
  public static void main(String[] args) throws IOException {
    System.out.println("Main.main");

    LocalDevice localDevice = LocalDevice.getLocalDevice();

    localDevice.setDiscoverable(DiscoveryAgent.GIAC); // Advertising the service

    String address = "742F6896BDD2"; // A500
    UUID device_UUID = new UUID(address, false);
    String url = "btspp://localhost:" + device_UUID + ";name=A500";
    StreamConnectionNotifier server = (StreamConnectionNotifier) Connector.open(url);

    System.out.println("wait until client connects");
    StreamConnection connection = server.acceptAndOpen(); // Wait until client connects
    // === At this point, two devices should be connected ===//
    System.out.println("connected");
    DataInputStream dis = connection.openDataInputStream();

    char c;
    while (true) {
      c = dis.readChar();
      if (c == 'x') break;
    }

    connection.close();

    System.out.println("Main.end");
  }
 void closeAll() {
   try {
     notif.close();
   } catch (IOException e) {
     e.printStackTrace();
   }
   super.closeAll();
 }
예제 #4
0
  private void startServer() throws IOException {
    UUID uuid = new UUID("1101", true);

    String connectionString = "btspp://localhost:" + uuid + ";name=BT Test Server";

    StreamConnectionNotifier streamConnNotifier =
        (StreamConnectionNotifier) Connector.open(connectionString);

    writeMessage("Bluetooth Server started. Waiting for Bluetooth test clients... \n");

    while (this.run) {
      connection = streamConnNotifier.acceptAndOpen();
      BTClientHandler btc = new BTClientHandler(connection);
      btc.start();
    }

    streamConnNotifier.close();
  }
예제 #5
0
 public void dispose() {
   log("Dispose");
   if (server != null)
     try {
       server.close();
     } catch (Exception e) {
       /*ignore*/
     }
 }
예제 #6
0
  protected BTGPSLocationProvider() throws LocationException {

    // TODO: Move this to searchConnect method?
    // TODO: The problem here is that it searches every time. Slow. Need to try Properties?
    // TODO: BIG ONE: Should only connect to GPS that isPaired() (from menu). Will
    // allow some degree of control over which GPS is connects to in classroom.
    try {
      da = LocalDevice.getLocalDevice().getDiscoveryAgent();
      da.startInquiry(DiscoveryAgent.GIAC, this);
    } catch (BluetoothStateException e) {
      throw new LocationException(e.getMessage());
    }

    while (!doneInq) {
      Thread.yield();
    }

    if (btDevice == null) throw new LocationException("No device found");

    String address = btDevice.getBluetoothAddress();
    String btaddy = "btspp://" + address;

    try {
      StreamConnectionNotifier scn = (StreamConnectionNotifier) Connector.open(btaddy);

      if (scn == null) throw new LocationException("Bad BT address");
      StreamConnection c = scn.acceptAndOpen();

      /* This problem below occurred one time for my Holux GPS. The solution was to
       * remove the device from the Bluetooth menu, then find and pair again.
       */
      if (c == null) throw new LocationException("Failed. Try pairing at menu again");
      InputStream in = c.openInputStream();

      if (in != null) {
        gps = new SimpleGPS(in);
        // c.close(); // TODO: Clean up when done. HOW TO HANDLE IN LOCATION?
      }
    } catch (IOException e) {
      throw new LocationException(e.getMessage());
    }
    // Add itself to SimpleGPS as listener
    SimpleGPS.addListener(this);
  }
예제 #7
0
 protected synchronized void stop() {
   _stop = true;
   try {
     // Close the connection so the thread will return.
     _notify.close();
   } catch (IOException e) {
     System.err.println(e.toString());
   } catch (NullPointerException e) {
     // The notify object likely failed to open, due to an IOException.
   }
 }
예제 #8
0
  public void run() {

    StreamConnection stream = null;
    InputStream input = null;
    MDSPushInputStream pushInputStream = null;

    while (!_stop) {
      try {

        // Synchronize here so that we don't end up creating a connection that is never closed.
        synchronized (this) {
          // Open the connection once (or re-open after an IOException),  so we don't end up
          // in a race condition, where a push is lost if it comes in before the connection
          // is open again. We open the url with a parameter that indicates that we should
          // always use MDS when attempting to connect.
          int port = RhoConf.getInstance().getInt("push_port");
          if (port == 0) port = 100;
          _notify = (StreamConnectionNotifier) Connector.open(URL + port + ";deviceside=false");
        }

        while (!_stop) {

          // NOTE: the following will block until data is received.
          LOG.TRACE("Block push thread until data is recieved");
          stream = _notify.acceptAndOpen();
          LOG.TRACE("Recieved push data");

          try {
            input = stream.openInputStream();
            pushInputStream = new MDSPushInputStream((HttpServerConnection) stream, input);

            // Extract the data from the input stream.

            DataBuffer db = new DataBuffer();
            byte[] data = new byte[CHUNK_SIZE];
            int chunk = 0;

            while (-1 != (chunk = input.read(data))) {
              db.write(data, 0, chunk);
            }

            processPushMessage(data);

            // This method is called to accept the push.
            pushInputStream.accept();

            input.close();
            stream.close();

            data = db.getArray();

          } catch (IOException e1) {
            // A problem occurred with the input stream , however, the original
            // StreamConnectionNotifier is still valid.
            System.err.println(e1.toString());

            if (input != null) {
              try {
                input.close();
              } catch (IOException e2) {
              }
            }

            if (stream != null) {
              try {
                stream.close();
              } catch (IOException e2) {
              }
            }
          }
        }

        _notify.close();
        _notify = null;

      } catch (IOException ioe) {
        LOG.TRACE("Exception thrown by _notify.acceptAndOpen() - exiting push thread");

        // Likely the stream was closed. Catches the exception thrown by
        // _notify.acceptAndOpen() when this program exits.

        _stop = true;

        if (_notify != null) {
          try {
            _notify.close();
            _notify = null;
          } catch (IOException e) {
          }
        }
      }
    }
  }
 StreamConnection connect() throws IOException {
   notif = (StreamConnectionNotifier) Connector.open("socket://:" + port);
   return notif.acceptAndOpen();
 }
예제 #10
0
 /**
  * クライアントからの接続待ち。
  *
  * @return 接続されたたセッションを返す。
  */
 public Session accept() throws IOException {
   log("Accept");
   StreamConnection channel = server.acceptAndOpen();
   log("Connect");
   return new Session(channel);
 }