public class Client implements DiscoveryListener {
private UUID serverUUID = new UUID("F0E0D0C0B0A000908070605040302010",
false);
private DiscoveryAgent agent;
private boolean init = false;
private boolean serverFound = false;
private String url;
private final String CLIENT_RESPONSE = "Client is ready";
private OutputStream output;
private InputStream input;
private boolean isActive = false;
private boolean complete=false;
public boolean isActive(){
return isActive;
}
public boolean isCompleted(){
return complete;
}
public Client() {
}
public void searchDevices() {
try {
agent = LocalDevice.getLocalDevice().getDiscoveryAgent();
agent.startInquiry(DiscoveryAgent.GIAC, this);
} catch (BluetoothStateException e) {
System.out.println("Problem in searching the blue tooth devices");
System.out.println(e.getMessage());
}
}
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
try {
// Get Device Info
System.out.println("Device Discovered");
System.out.println("Major Device Class: "
+ cod.getMajorDeviceClass() + " Minor Device Class: "
+ cod.getMinorDeviceClass());
System.out.println("Bluetooth Address: "
+ btDevice.getBluetoothAddress());
System.out.println("Bluetooth Friendly Name: "
+ btDevice.getFriendlyName(true));
// Search for Services
UUID uuidSet[] = new UUID[1];
uuidSet[0] = serverUUID;
agent.searchServices(null, uuidSet, btDevice, this);
} catch (Exception e) {
System.out.println("Device Discovered Error: " + e);
}
}
public void inquiryCompleted(int discType) {
System.out.println("InquiryCompleted");
complete=true;
}
public void serviceSearchCompleted(int transId, int respCode) {
StreamConnection conn;
if (serverFound) {
try { // lets the communication start by setting the url and send
// client reponse
conn = (StreamConnection) Connector.open(url);
output = conn.openOutputStream();
input = conn.openInputStream();
System.out.println("*******Client*******");
output.write(CLIENT_RESPONSE.length());
output.write(CLIENT_RESPONSE.getBytes());
isActive = true;
System.out.println("serviceSearchCompleted");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public void servicesDiscovered(int transId, ServiceRecord[] records) {
for (int i = 0; i < records.length; i++) {
url = records[i].getConnectionURL(
ServiceRecord.AUTHENTICATE_ENCRYPT, false);
if (url.startsWith("btspp")) {
init = true;
serverFound = true;
break;
}
}
}