Android Open Source - Remote-Bluetooth-Android-master Wait Thread






From Project

Back to project page Remote-Bluetooth-Android-master.

License

The source code is released under:

MIT License

If you think the Android project Remote-Bluetooth-Android-master listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.luugiathuy.apps.remotebluetooth;
/*  www.ja v a 2  s  .  c o  m*/
import java.io.IOException;

import javax.bluetooth.BluetoothStateException;
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;

public class WaitThread implements Runnable{

  /** 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("04c6093b00001000800000805f9b34fb", false);
      System.out.println(uuid.toString());
      
            String url = "btspp://localhost:" + uuid.toString() + ";name=RemoteBluetooth";
            notifier = (StreamConnectionNotifier)Connector.open(url);
        } catch (BluetoothStateException e) {
          System.out.println("Bluetooth is not turned on.");
      e.printStackTrace();
      return;
    } catch (IOException e) {
      e.printStackTrace();
      return;
    }
    
    // waiting for connection
    while(true) {
      try {
        System.out.println("waiting for connection...");
              connection = notifier.acceptAndOpen();
              
              Thread processThread = new Thread(new ProcessConnectionThread(connection));
              processThread.start();
              
      } catch (Exception e) {
        e.printStackTrace();
        return;
      }
    }
  }
}




Java Source Code List

com.luugiathuy.apps.remotebluetooth.BluetoothCommandService.java
com.luugiathuy.apps.remotebluetooth.DeviceListActivity.java
com.luugiathuy.apps.remotebluetooth.ProcessConnectionThread.java
com.luugiathuy.apps.remotebluetooth.RemoteBluetoothServer.java
com.luugiathuy.apps.remotebluetooth.RemoteBluetooth.java
com.luugiathuy.apps.remotebluetooth.WaitThread.java