This is my first project with bluecove and I am having problems before I even start!
Here is my code:
import java.io.IOException;
import java.util.ArrayList;
import bluecove;
/**
* Class that discovers all bluetooth devices in the neighbourhood and
displays their name and bluetooth address.
*/
public class BluetoothDeviceDiscovery implements DiscoveryListener {
// object used for waiting
private static Object lock = new Object();
// vector containing the devices discovered
public static ArrayList<RemoteDevice> devices;
public BluetoothDeviceDiscovery() {
devices = new ArrayList<RemoteDevice>();
}
// main method of the application
public static void main(String[] args) throws IOException {
//create an instance of this class
BluetoothDeviceDiscovery bluetoothDeviceDiscovery=new
BluetoothDeviceDiscovery();
//display local device address and name
LocalDevice localDevice = LocalDevice.getLocalDevice();
System.out.println("Address: " + localDevice.getBluetoothAddress());
System.out.println("Name: " + localDevice.getFriendlyName());
//find devices
DiscoveryAgent agent = localDevice.getDiscoveryAgent();
System.out.println("Starting device inquiry…");
agent.startInquiry(DiscoveryAgent.GIAC, bluetoothDeviceDiscovery);
try {
synchronized(lock) {
lock.wait();
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Device Inquiry Completed. ");
//print all devices in devices
int deviceCount=devices.size();
System.out.println(deviceCount);
if(deviceCount <= 0) {
System.out.println("No Devices Found .");
} else {
//print bluetooth device addresses and names in the format [ No. address (name) ]
System.out.println("Bluetooth Devices:" );
for (int i = 0; i < deviceCount; i++) {
RemoteDevice remoteDevice=(RemoteDevice)devices.get(i);
System.out.println(i +". "+remoteDevice.getBluetoothAddress() );
//("+remoteDevice.getFriendlyName(true)+")");
}
}
}
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod)
{
System.out.println("Device discovered: "+btDevice.getBluetoothAddress());
//add the device to the vector
if(!devices.contains(btDevice))
{
devices.add(btDevice);
}
}
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
}
public void serviceSearchCompleted(int transID, int respCode) {
}
public void inquiryCompleted(int discType)
{
synchronized(lock){
lock.notify();
}
switch (discType)
{
case DiscoveryListener.INQUIRY_COMPLETED :
System.out.println("INQUIRY_COMPLETED");
break;
case DiscoveryListener.INQUIRY_TERMINATED :
System.out.println("INQUIRY_TERMINATED");
break;
case DiscoveryListener.INQUIRY_ERROR :
System.out.println("INQUIRY_ERROR");
break;
default :
System.out.println("Unknown Response Code");
break;
}
}
}
I am getting an error on line 3 (surprise, surprise) telling me that a ';' was expected.
I know the real reason I am getting an error is that I did not import bluecove correctly.
I downloaded the bluecove jar, renamed it and added the file to my classpath thinking that I could now import and use bluecove in my java projects.
Did I setup/import bluecove the wrong way? How do I actually import bluecove so I can use bluetooth in my project?
The first two
importstatements inform Java about the full package paths for theIOExceptionandArrayListclasses that you are referencing in your code.Once you start using bluecove classes in your code, you will need to add one or more import statements telling Java about the full package paths of those classes.