Show Service.java syntax highlighted
/*
* Copyright (c) 2005
* Helsinki Institute of Physics
* see LICENSE file for details
*
* BTService.java
* Created on Jan 22, 2005
*/
package fi.hip.gb.bluetooth;
import java.io.IOException;
import java.util.Enumeration;
import javax.bluetooth.DataElement;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
/**
* Basic service implementation for Bluetooth devices.
*
* @author Juho Karppinen
* @version $Id: Service.java 270 2005-05-18 22:19:16Z jkarppin $
*/
public class Service {
/** target device */
protected EndPoint endpt;
/** connection to remote service */
protected StreamConnection con;
/** url for this service */
protected String url;
/** service record */
protected ServiceRecord svcRec;
/** thread to read from the BT-device */
protected Runnable reader;
/** thread to write to the BT-device */
protected Runnable sender;
/** true when processing should be terminated */
protected boolean done = false;
/**
* Constructor for the service
* @param endpt endpoint device
* @param url connection string to the service
*/
public Service(EndPoint endpt, String url) {
this.endpt = endpt;
this.url = url;
}
/**
* Constructor for the service
* @param endpt endpoint device
* @param sr record of the service
*/
public Service(EndPoint endpt, ServiceRecord sr) {
this.endpt = endpt;
this.url =
sr.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT,
false);
this.svcRec = sr;
}
/**
* Add already opened service.
* @param endpt endpoint device
* @param c connection to the device
*/
public Service(EndPoint endpt, StreamConnection c) {
this.endpt = endpt;
this.con = c;
}
/**
* Open the connection to the remote device, create reader
* and sender threads and triggers a callback event.
*
* @throws IOException if failed to open the connection
*/
public void openConnection() throws IOException {
BTService.getInstance().log("opening connection to " + this.url);
this.con = (StreamConnection) Connector.open(this.url);
if(this.sender != null)
new Thread(this.sender).start();
if(this.reader != null)
new Thread(this.reader).start();
}
/**
* Closes all the remote connection to the Bluetooth device.
* An event to callback is triggered.
*/
public void closeConnection() {
BTService.getInstance().log("closing connection with " + this.endpt.getName());
this.done = true;
BTService.getInstance().fireEvent(BTListener.EVENT_LEAVE, this.endpt, null);
// send TERMINATE signal to other connected BlueChat peers
//endpt.putString(BTService.SIGNAL_TERMINATE, "end");
this.con = null;
}
/**
* Gets the connection if exists.
* @return connection object or null if not found
*/
public StreamConnection getConnection() {
return this.con;
}
/**
* Gets the URL of the Bluetooth service.
* @return URL of the service.
*/
public String getURL() {
return this.url;
}
/**
* Sets the URL of the Bluetooth service.
* @param url URL of the service.
*/
public void setURL(String url) {
this.url = url;
}
public String toString() {
if(svcRec != null) {
// Get the 1st element of ServiceClassIDList
// and translate it to human friendly name
DataElement de = this.svcRec.getAttributeValue( 0x0001 );
Enumeration e = (Enumeration)de.getValue();
DataElement de2 = (DataElement)e.nextElement();
Object v = de2.getValue();
// this should return something like "SerialPort"
return Util.uuidToName( (UUID) v );
} else {
return url;
}
}
}
See more files for this project here