Show EndPoint.java syntax highlighted
/*
* Copyright (c) 2005
* Helsinki Institute of Physics
* see LICENSE file for details
*
* EndPoint.java
* Created on Mar 7, 2004
*/
package fi.hip.gb.bluetooth;
import java.io.IOException;
import java.util.Vector;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.microedition.io.StreamConnection;
/**
* A EndPoint object stores connection objects <code>Service</code>
* to some device (endpoint).
*
* @author Juho Karppinen
* @version $Id: EndPoint.java 270 2005-05-18 22:19:16Z jkarppin $
*/
public class EndPoint {
/** remote device object */
private RemoteDevice remoteDev;
/** type of the device */
private DeviceClass deviceClass;
/** bluetooth discovery transId, obtainsed from searchServices */
protected int transId = -1; // -1 must be used for default
/** remote device name */
private String remoteName;
/** a list <code>Service</code> instances for this endpoint */
private Vector services = new Vector();
/**
* Creates an endpoint to the remote device.
*
* @param rdev remote device
* @param type device class
*/
public EndPoint(RemoteDevice rdev, DeviceClass type) {
this.remoteDev = rdev;
try {
// NOTE in 6600, this parameter must be false because
// according to some observation from other developer
// setting this to true mean the Bluetooth system need to make
// another connection to remote device, however, there is no
// available free connection, so it will give you exception
this.remoteName = rdev.getFriendlyName(false);
} catch (IOException ex) {
this.remoteName = "Unknown";
}
this.deviceClass = type;
}
/**
* Create an endpoint with limited information available. Device is
* initialized only with the device name.
*
* @param name name of the local device
*/
public EndPoint(String name) {
this.remoteName = name;
}
/**
* Adds a NMEA service based on the connection string.
*
* @param url connection string to the service
* @return just created <code>Service</code> object
*/
public Service addNMEAService(String url) {
Service svc = new NmeaService(this, url);
/*
* if((this.deviceClass != null && (this.deviceClass.getServiceClasses() &
* BTService.SERVICE_OBJECT_TRANSFER) != 0)) { svc = new
* AgentService(this, url); } else {
*/
this.services.addElement(svc);
return svc;
}
/**
* Adds a NMEA service based on the service record.
*
* @param svcRec record containing all information about the service
* @return just created <code>Service</code> object
*/
public Service addNMEAService(ServiceRecord svcRec) {
Service srv = new NmeaService(this, svcRec);
this.services.addElement(srv);
return srv;
}
/**
* Adds already opened service.
*
* @param c the connection to the service
* @return just created <code>Service</code> object
*/
public Service addService(StreamConnection c) {
Service srv = new AgentService(this, c);
this.services.addElement(srv);
return srv;
}
/**
* Gets all connections to the endpoint.
*
* @return an array of <code>Service</code> objects
*/
public Service[] getConnections() {
Service[] srvs = new Service[services.size()];
for (int i = 0; i < services.size(); i++) {
srvs[i] = (Service) services.elementAt(i);
}
return srvs;
}
/**
* Gets the name of the Bluetooth device.
*
* @return name
*/
public String getName() {
return this.remoteName;
}
/**
* Gets the remote device object.
*
* @return connection object or null if not found
*/
public RemoteDevice getRemoteDevice() {
return this.remoteDev;
}
/**
* Gets the type of the remote device.
*
* @return type of device
*/
public DeviceClass getDeviceClass() {
return this.deviceClass;
}
/**
* Returns additional information about the current remote device.
*
* @return information, or empty String if needed information not found
*/
public String printInfo() {
if (this.remoteDev != null && this.deviceClass != null) {
return Util.printRemoteDevice(this.remoteDev, this.deviceClass);
} else {
return "";
}
}
/**
* Returns the name of the remove device. Returns Unknown if the
* name is not known.
* @return name
*/
public String toString() {
if (this.remoteName != null) {
return this.remoteName;
} else {
return "Unknown";
}
}
}
See more files for this project here