Back to project page roodroid.
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.
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(); } } }