Пример #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();
  }
 /**
  * Method to return the output stream of the connection
  *
  * @return OutputStream
  */
 public OutputStream openOutputStream() throws IOException {
   return connection.openOutputStream();
 }
  /** Implementation of Thread. */
  public void run() {
    StreamConnection connection = null;

    try {
      _screen.updateDisplay("Opening Connection...");
      String url =
          "socket://"
              + _screen.getHostFieldText()
              + ":44444;interface=wifi"
              + (_screen.isDirectTCP() ? ";deviceside=true" : "");
      connection = (StreamConnection) Connector.open(url);
      _screen.updateDisplay("Connection with " + _screen.getHostFieldText() + " established.");

      _in = connection.openInputStream();
      _out = new OutputStreamWriter(connection.openOutputStream());

      // Send the HELLO string.
      // send("Hello from BlackBerry 9860.");
      // send("Hello from BlackBerry 9860.");
      send(AzrRP_Screen.getMsg());

      // Execute further data exchange here...

      // send("Bye");

      _screen.updateDisplay("Done!");
    } catch (IOException e) {
      System.err.println(e.toString());
    } finally {
      _screen.setThreadRunning(false);

      try {
        _in.close();
      } catch (IOException ioe) {
      }
      try {
        _out.close();
        AzrRP_Screen.setMsg(null);

      } catch (IOException ioe) {
      }
      try {
        connection.close();

      } catch (IOException ioe) {
      }
    }
  }