Android Open Source - roodroid Server Bluetooth






From Project

Back to project page roodroid.

License

The source code is released under:

Copyright (c) 2011, Jonathan Perichon & Lucas Gerbeaux Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"...

If you think the Android project roodroid 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 fr.utbm.roodroid.server;
/*from  w  w w . j a  va2  s.co  m*/
import java.io.IOException;
import java.util.UUID;

import fr.utbm.roodroid.ApplicationManager;
import fr.utbm.roodroid.Connection;
import fr.utbm.roodroid.ConnectionBluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.util.Log;

/**
 * ServerBluetooth
 * Extends Server in order to instantiate the ConnectionBluetooth.
 * 
 * @author Jonathan Perichon <jonathan.perichon@gmail.com>
 * @author Lucas Gerbeaux <lucas.gerbeaux@gmail.com>
 *
 */
public class ServerBluetooth extends Server {

  private static final String NAME = "BluetoothServer";
  private final BluetoothAdapter adapter;
  private BluetoothServerSocket serverSocket = null;

  public ServerBluetooth(AuthMethod authMethod, int maxNbMsgSent, int maxNbClients) throws Exception {
    super(authMethod, maxNbMsgSent, maxNbClients);
    adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter == null) {
      throw new Exception("The device does not support Bluetooth");
    }
    
    if (adapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
      throw new Exception("The device is not discoverable");
    }
  }

  @Override
  public void run() {
    BluetoothSocket socket = null;
    Connection con = null;
    Thread t = null;
    while (isRunning()) {
      try {
        for (UUID uuid : ApplicationManager.getInstance().getUUIDs()) {
          serverSocket = adapter.listenUsingRfcommWithServiceRecord(NAME, uuid);
          socket = serverSocket.accept();
          if (socket != null) {
            Log.d("okkkk", "client added");
            con = new ConnectionBluetooth(handler, socket);
            t = new Thread(con);
            t.start();
            awaitingConnections.add(con);
          }                      
        }
      } catch (IOException e) {
            Log.e("ServerBluetooth", e.getMessage());
        if (con != null) {
          this.awaitingConnections.remove(con);
          this.activeConnections.values().remove(con);
          con.disconnect();
        }
        if (t != null) {
          t.interrupt();
        }
      }
    }
  }
  
  public void closeServerSocket() {
    try {
      serverSocket.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}




Java Source Code List

fr.utbm.roodroid.ApplicationManager.java
fr.utbm.roodroid.ConnectionBluetooth.java
fr.utbm.roodroid.ConnectionWifi.java
fr.utbm.roodroid.Connection.java
fr.utbm.roodroid.Conversation.java
fr.utbm.roodroid.Message.java
fr.utbm.roodroid.PacketClient.java
fr.utbm.roodroid.Packet.java
fr.utbm.roodroid.TCPCommandType.java
fr.utbm.roodroid.TextMessage.java
fr.utbm.roodroid.activity.AuthorizedUsernamesAdapter.java
fr.utbm.roodroid.activity.BluetoothDiscovery.java
fr.utbm.roodroid.activity.ClientBluetoothSettings.java
fr.utbm.roodroid.activity.ClientWifiSettings.java
fr.utbm.roodroid.activity.ConversationsAdapter.java
fr.utbm.roodroid.activity.ConversationsList.java
fr.utbm.roodroid.activity.LogPage.java
fr.utbm.roodroid.activity.MessagesAdapter.java
fr.utbm.roodroid.activity.MessagesList.java
fr.utbm.roodroid.activity.ProfileTypeChooser.java
fr.utbm.roodroid.activity.ServerAdvancedSettings.java
fr.utbm.roodroid.activity.ServerBluetoothMain.java
fr.utbm.roodroid.activity.ServerBluetoothSettings.java
fr.utbm.roodroid.activity.ServerWifiMain.java
fr.utbm.roodroid.activity.ServerWifiSettings.java
fr.utbm.roodroid.client.ClientBluetooth.java
fr.utbm.roodroid.client.ClientWifi.java
fr.utbm.roodroid.client.Client.java
fr.utbm.roodroid.client.ConversationsDataSource.java
fr.utbm.roodroid.client.ConversationsHelper.java
fr.utbm.roodroid.server.AuthByID.java
fr.utbm.roodroid.server.AuthByPassword.java
fr.utbm.roodroid.server.AuthMethod.java
fr.utbm.roodroid.server.AuthNone.java
fr.utbm.roodroid.server.ServerBluetooth.java
fr.utbm.roodroid.server.ServerWifi.java
fr.utbm.roodroid.server.Server.java