All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.microlog.midp.bluetooth.BluetoothConnectionHandlerImpl Maven / Gradle / Ivy

There is a newer version: 2.3.5
Show newest version
package net.sf.microlog.midp.bluetooth;

import java.io.DataOutputStream;
import java.io.IOException;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;

import net.sf.microlog.core.MicrologConstants;

/**
 * Handles the Bluetooth communication. Opens, closes and sends messages with
 * Bluetooth.
 * 
 * @author Jarle Hansen ([email protected])
 * @since 2.2
 */
class BluetoothConnectionHandlerImpl implements BluetoothConnectionHandler {
	private String connectionString = null;

	private StreamConnection connection = null;
	private DataOutputStream dataOutputStream = null;

	private String bluetoothClientID = null;

	BluetoothConnectionHandlerImpl() {
	}

	private void establishConnectionSelectService() {
		System.err
				.println("No Server Bluetooth address or Server URL set, using the selectService. Please try to update your code to use a Server Bluetooth address instead!");

		try {
			DiscoveryAgent agent = LocalDevice.getLocalDevice()
					.getDiscoveryAgent();

			connectionString = agent.selectService(new UUID(
					MicrologConstants.DEFAULT_BT_UUID_STRING, false),
					ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
		} catch (BluetoothStateException bse) {
			System.err
					.println("Failed to connect to the Bluetooth log server. "
							+ bse);
		}
	}

	public void findAndSetConnectionString(
			final BluetoothRemoteDevice remoteDevice) {
		final BluetoothServiceSearch serviceSearch = BluetoothServiceSearch
				.newInstance();
		connectionString = serviceSearch.getLoggerServiceString(remoteDevice);
	}

	public void setConnectionString(final String connectionString) {
		this.connectionString = connectionString;
	}

	/**
	 * Opens a Bluetooth connection to the server application. If no connection
	 * String is available, it will use the selectService method. This is not
	 * recommended!
	 */
	public boolean openConnection() {
		boolean connectionOpen = false;

		if (connectionString == null) {
			establishConnectionSelectService();
		}

		try {
			openOutputStream();
			connectionOpen = true;
		} catch (IOException e) {
			System.err
					.println("Failed to connect to the Bluetooth log server with connection string "
							+ connectionString + ' ' + e);
		}

		return connectionOpen;
	}

	private void openOutputStream() throws IOException {
		if (connectionString != null) {
			connection = (StreamConnection) Connector.open(connectionString);
			dataOutputStream = connection.openDataOutputStream();
		}
	}

	/**
	 * Shuts down the logging service. On the server side this will close the
	 * desktop application.
	 */
	public void shutdownLoggingService() throws IOException {
		if (dataOutputStream != null) {
			try {
				dataOutputStream.writeUTF("[STOP]");
			} catch (IOException io) {
				System.err
						.println("Failed to send [STOP] to the Bluetooth server, "
								+ io);
			} finally {
				close();
			}
		}
	}

	/**
	 * Close the log.
	 */
	public void close() throws IOException {
		if (dataOutputStream != null) {
			try {
				dataOutputStream.close();
			} catch (IOException e) {
				System.err
						.println("Failed to terminate the dataOutputStream in a controlled way."
								+ e);
			}
		}

		if (connection != null) {
			try {
				connection.close();
			} catch (IOException e) {
				System.err.println("Failed to close the log " + e);
			}
		}
	}

	/**
	 * Writes the formatted log statement to the output stream.
	 */
	public void writeLogToStream(final String formattedLogStatement) {
		if (dataOutputStream != null) {
			try {
				dataOutputStream.writeUTF(formattedLogStatement);
				dataOutputStream.flush();
			} catch (IOException io) {
				System.err.println("Unable to log to the output stream. " + io);
			}
		}
	}

	/**
	 * Returns the clientID that will be used to identify the connected client.
	 * If the clientID is not set from the user it will try to use the Bluetooth
	 * friendly name instead.
	 * 
	 * @param clientID
	 *            the client id for the BluetoothAppender.
	 * 
	 * @return the id String
	 */
	public String getBluetoothClientID(final String clientID) {
		if (bluetoothClientID == null) {
			if (clientID == null || clientID.length() == 0) {
				bluetoothClientID = getBluetoothFriendlyName();
			}

			if (clientID != null || bluetoothClientID == null) {
				bluetoothClientID = clientID;
			}
		}

		return bluetoothClientID;
	}

	/**
	 * Tries to retrieve the Bluetooth friendly name. If this is successful the
	 * first time the same name will be returned each time.
	 * 
	 * @return the Bluetooth friendly name, that is the name specified by the
	 *         user.
	 */
	private String getBluetoothFriendlyName() {
		String bluetoothFriendlyName = "";

		try {
			bluetoothFriendlyName = LocalDevice.getLocalDevice()
					.getFriendlyName();
		} catch (BluetoothStateException bse) {
			System.err.println("Unable to get the Bluetooth friendly name, "
					+ bse);
		}

		return bluetoothFriendlyName;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy